text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 06/07/17 at 2:09 PM

Last Modified on July 11 2017 19:26

@author: Neil Cook

Version 0.0.11
"""

import numpy as np
from astropy.io import fits
from astropy.table import Table
import matplotlib.pyplot as plt
import matplotlib.cm as cmaps
import os
import sys
from tqdm import tqdm
from skimage import measure
from skimage import filters

if sys.version_info[0] < 3:
    from collections import OrderedDict as dict
else:
    pass

# =============================================================================
# Define VARIABLES
# =============================================================================
# location of config file
configfile = '../config.txt'
# expected formats required for parameters
VARIABLES = dict(filepath=str, plotpath=str, savepath=str, files=list,
                 order_direction=str, xblur=float, yblur=float,
                 fitmin=int, fitmax=int, width=int, minpixelsinorder=int)
# descriptions for required parameters
DESCS = dict()
DESCS['filepath'] = ("string, location of the folder containing fits files" +
                    " to extract")
DESCS['plotpath'] = "string, location of place to save plots"
DESCS['savepath'] = "string, location to save extracted order fits files"
DESCS['poly_fits_file'] = ('string, file to save polynomials fits file to' +
                           ' will save in: (in $save path$/$input file$/ )')

DESCS['files'] = "list or string, names of image fits files to extract"
DESCS['order_direction'] = ("string, direction of the orders " +
                            "If 'horizontal' or 'H' or 'h' then orders are" +
                            "assumed to increase along the x axis" +
                            "If 'vertical' or 'V' or 'v' then orders are" +
                            "assumed to increase along the y axis")
DESCS['xblur'] = ("float, blur pixels in wavelength direction " +
                  "(to aid finding orders)")
DESCS['yblur'] = ("float, blur pixels in order direction " +
                  "(to aid finding orders)")
DESCS['pcut'] = ("float, percentile to remove background pixels at greats a " +
                 "mask of pixels to fit and not to fit")
DESCS['fitmin'] = "int, lowest order polynomial to fit"
DESCS['fitmax'] = "int, highest order polynomial to fit"
DESCS['width'] = "int, number of pixels to use for width of each order"
DESCS['minpixelsinorder'] = ("minimum number of pixels to use order" +
                             " (else it is discarded as noise/unusable)")
PORDER = ['filepath', 'plotpath', 'savepath', 'files', 'order_direction',
          'xblur', 'yblur', 'pcut', 'fitmin', 'fitmax', 'width',
          'minpixelsinorder']

# =============================================================================
# Define CMD line args and input/output
# =============================================================================
def cmdlineargs(p):
    """
    finds command lines inputs

    :return params:  dictionary, updated parameters to use in the program
    """
    # get args from command line (using sys.argv looking for '='
    cmdargs = sys.argv
    ckwargs = dict()
    for arg in cmdargs:
        if '=' in arg:
            key, value = arg.replace(' ', '').split('=')
            if key in VARIABLES:
                # try to convert to required type (using VARIABLES dict)
                try:
                    if VARIABLES[key] in [list, np.ndarray]:
                        ckwargs[key] = VARIABLES[key](value.split(','))
                    else:
                        ckwargs[key] = VARIABLES[key](value)
                except ValueError:
                    emsg = [key, str(VARIABLES[key])]
                    print('Command line input not understood for' +
                          'argument {0} must be {1}'.format(*emsg))
    # we need default values if not set by command line
    for key in p:
        if key not in ckwargs:
            ckwargs[key] = p[key]
    # return parameter dictionary
    return ckwargs

def textfileargs(p, cloc):
    """
    Imports parameters from text file located at cloc (by default this is
    set at the top of the program - if not changed below)

    :param p: dictionary, default parameter dictionary (the default values)
    :param cloc: string or None, location of config file

    :return:
    """

    emsg = 'Error in config file: '
    # load text file (remove all white spaces)
    data = np.genfromtxt(cloc, dtype=str, comments='#', delimiter='=')
    keys, values = data[:, 0], data[:, 1]
    for k in range(len(keys)):
        keys[k] = keys[k].replace(' ', '')
        values[k] = values[k].replace(' ', '')
    textdict = dict(zip(keys, values))
    # add root to paths
    paths = ['filepath', 'plotpath', 'savepath', 'polypath']
    for path in paths:
        p[path] = textdict['root'] + textdict[path]
        p[path] = p[path].replace('//', '/')
    # deal with poly fits files
    svar = 'poly_fits_file'
    if '.fit' not in textdict[svar]:
        emsg2 = '"{0}" (value of "{1}")'.format(svar, textdict[svar])
        raise Exception(emsg + emsg2 + ' must contain ".fit"')
    else:
        p[svar] = textdict[svar]
    # deal with file lists
    svar = 'files'
    p[svar] = textdict[svar].split(',')
    for lvar in p[svar]:
        if '.fit' not in lvar:
            emsg2 = '"{0}" (value of "{1}")'.format(svar, lvar)
            raise Exception(emsg + emsg2 + ' must contain ".fit"')
    # deal with order direction
    svar = 'order_direction'
    sargs = ['h', 'horizontal','v', 'vertical']
    sargs2 = 'H, h, Horizontal, V, v, or Vertical'
    if textdict[svar].lower() not in sargs:
        emsg2 = '"{0}" (value of "{1}")'.format(svar, textdict[svar])
        raise Exception(emsg + emsg2 + ' must contain: ' + sargs2)
    else:
        p[svar] = textdict[svar]
    # deal with floats
    svars = ['xblur', 'yblur', 'pcut']
    for svar in svars:
        emsg2 = '"{0}" (value of "{1}")'.format(svar, textdict[svar])
        try:
            p[svar] = float(textdict[svar])
        except ValueError:
            raise Exception(emsg + emsg2 + ' must be a float')
    # deal with integers
    svars = ['fitmin', 'fitmax','width', 'minpixelsinorder']
    for svar in svars:
        emsg2 = '"{0}" (value of "{1}")'.format(svar, textdict[svar])
        try:
            p[svar] = int(textdict[svar])
        except ValueError:
            raise Exception(emsg + emsg2 + ' must be an integer')
    # finally return parameters
    return p

def set_inputs(p):
    """
    Sets the inputs using user input

    :param p: dictionary, parameters to use in the program

    :return p: dictionary, updated parameters to use in the program
    """
    # loop through the default parameters
    print('\n\n' + '=' * 50)
    print('\tSetup inputs\n')
    for key in PORDER:
        if key not in p:
            continue
        cond2 = True
        while cond2:
            print('\n\n' + '='*50)
            description = ('\n' + DESCS[key] + '\n\n' + 'current value ' +
                           ' =\t{0}'.format(p[key]))
            q1args = [key, description]
            q1 = "\n\nModify {0}?\n\n\t{1}\n\n\tModify {0}? [Y]es or [N]o:\t"
            uinput0 = input(q1.format(*q1args))
            if 'Y' in uinput0.upper():
                uinput01 = input("\n\nEnter value for {0}\t:")
                try:
                    if VARIABLES[key] in [list, np.ndarray]:
                        p[key] = VARIABLES[key](uinput01.split(','))
                    else:
                        p[key] = VARIABLES[key](uinput01)
                    cond2 = False
                except ValueError:
                    emsg = [key, str(VARIABLES[key])]
                    print('Command line input not understood for' +
                          'argument {0} must be {1}'.format(*emsg))
                    cond2 = True
            elif 'N' in uinput0.upper():
                cond2 = False
            else:
                cond2 = True
    print('\n' + '=' * 50 + '\n\n')
    return p

def loaddata(filename, p):
    # make folder
    name = filename.split('.fit')[0]
    ppath = p['plotpath'] + '/' + name + '/'
    if name not in os.listdir(p['plotpath']):
        os.makedirs(ppath)
    fpath = p['savepath'] + '/' + name + '/'
    if name not in os.listdir(p['savepath']):
        os.makedirs(fpath)
    # --------------------------------------------------------------------
    # Load data
    ff = p['filepath'] + filename
    print('\n\t Loading CCD image for:\n\t {0}...'.format(ff))
    im = np.array(fits.getdata(ff))
    # --------------------------------------------------------------------
    # flip the image if orders are in the y direction
    # (default is x direction)
    if 'v' in p['order_direction'].lower():
        im = im.T
    # return the image and update params
    p['name'] = name
    p['ppath'] = ppath
    p['fpath'] = fpath
    return im, p

def save_fits(pfits, params, xls, xhs, spath):
    """
    save the fits to file, for use in extracting future spectra

    :param pfits: list, length same as number of orders, polynomial values
                      (output of np.polyval)

                      i.e. p where:

                      p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]

    """
    data = []
    cols = ['Order'] + list(range(params['fitmax'] + 1))[::-1]
    cols += ['low_x', 'high_x']

    # Loop around each order and plot fits
    for pp, pf in enumerate(pfits):
        row = [pp + 1] + list(np.zeros(params['fitmax'] + 1 - len(pf)))
        row += list(pf)
        row += [xls[pp], xhs[pp]]
        data.append(row)
    data = np.array(data)

    # convert to astropy table
    atable = Table()
    for c, col in enumerate(cols):
        atable[str(col)] = data[:, c]
    atable.write(spath + params['poly_fits_file'], overwrite=True)

def read_fits(filename, params):
    """
    read in the polynomial fit fits file (saved from Extract_Orders.py)
    and extract using polynomial fits

    :param filename: string, location and file name of the file containing the
                     polynomial fits (params['poly_fits_file'] by default)

    :param params: dict, parmaeter dictionary, must include the following
                         keywords:
                              - fitmax: int, highest order polynomial to fit
                                             the order to (default = 3)

    fits file should look like the following:

    Order          6                  5          ...       0        low_x   high_x
    float64      float64            float64       ...    float64    float64 float64
    ------- ------------------ ------------------ ... ------------- ------- -------
        1.0  7.80380819018e-20 -7.28455089523e-16 ... 1086.73647399     0.0  3028.0
        2.0  2.09138850722e-19 -2.01802656266e-15 ... 1123.38227429     0.0  3082.0
        3.0  1.56641316229e-19 -1.79748717147e-15 ... 1159.81599355     0.0  3203.0
        4.0  2.26957361856e-19 -2.31431373935e-15 ... 1196.07630084     0.0  3319.0

    where 6, 5, 4, 3, 2, 1, 0 are the polynomial powers in p

        i.e. p where:

            p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]

    :return:
    """
    # convert to astropy table
    atable = Table.read(filename)
    orders = np.array(atable['Order'], dtype=int)

    pfits1, xls, xhs = [], [], []
    for order in orders:
        pfit1 = []
        for p in range(params['fitmax']+1)[::-1]:
            pfit1.append(atable[str(p)][order-1])
        pfits1.append(pfit1)
        xls.append(int(atable['low_x'][order-1]))
        xhs.append(int(atable['high_x'][order-1]))

    return pfits1, xls, xhs

# =============================================================================
# Define Calculation functions
# =============================================================================
def min_max_clip(image, lowsigma, highsigma):
    """
    Performs a min max clip of the image

    :param image: numpy 2D array, containing the image to search

    :param lowsigma: float, multiplier of the std to take off the median
                     median - lowsigma * std becomes the minimum value to keep,
                     everything below gets set to this value

    :param highsigma: float, multiplier of the std to add to the median
                      median + highsigma * std maximum value to keep
                      everything below gets set to this value

    i.e. all values below (median - lowsigma * std) are set to
         (median - lowsigma * std)

         and

         all values above (median + highsigma * std) are set to
         (median + highsigma * std)

         returns an image
    """
    stats = ("\n Stats:")
    stats += ("\n   Median={0:.3f}, ".format(np.median(image)))
    stats += ("  1$\sigma$={0:.3f}".format(np.std(image)))
    # set all pixels above median + highsigma * sigma to 3 sigma
    image1 = np.array(image)
    lowv = np.median(image1) - lowsigma*np.std(image1)
    highv = np.median(image1) + highsigma*np.std(image1)
    highsig = image1 > highv
    lowsig = image1 < lowv
    image1[highsig] = highv
    image1[lowsig] = 0.0

    stats += ("\nLow is at {0}, ".format(lowv))
    stats += ("  High is at {0}".format(highv))
    # return altered image
    return image1, stats

def locate_orders(image, xb=4.0, yb=128.0, cutp=50.0, minpix=10000,
                  log=False):
    """
    Locate the orders using a blob finding algorithm

    :param image: numpy 2D array, containing the image to search

    :param xb: int, blur in the x direction (wavelength direction) use this
               to help identify orders by bluring out spectral information
               along the wavelength direction (default 128)

    :param yb: int, blur in the y direction (across orders), usually small,
               just used to remove any features across the order and make it
               easier to find the orders

    :param cutp: float, the percentile to cut at
                  i.e.
                    50.0 = median (50%)
                    15.9 = 1 sigma below (16%)
                    84.1 = 1 sigma above (84%)

    :param minpix: int, minimum number of pixels needed to deem order useful
                   (any orders with less than this number of pixels are
                    ignored) - useful to remove blobs not associated with orders
                    or orders with too few pixels to be usable

    :return dblobs: numpy 2D array equal in shape to the input image, contains
                    located "blobs" which should be the orders (if they were
                    detectable) format is 0 where no order is found and then
                    a different integer for each "blob" found

                    i.e.:

                        010203
                        010203
                        010203
                        010203

                        identifies three orders

    :return rs: numpy 1D array, the unique integers found in dblobs

                i.e. for case above

                       [0, 1, 2, 3]
    """
    # smooth out the data along the wavelength direction
    # (to remove faint regions and aid fitting along orders)
    if log:
        print("\n\t\tApplying blur...")
    if xb == 0 and yb == 0:
        image2 = np.array(image)
    else:
        image2 = filters.gaussian(image, sigma=[xb, yb])
    # mask out noise
    blobs = image2 > np.percentile(image2, cutp)

    # use skimage measure to differentitate orders
    if log:
        print("\n\t\tFinding order blobs...")
    dblobs = measure.label(blobs)
    # identify different regions
    rs = np.unique(dblobs)
    # require a minimum number of pixels in an order blob
    if log:
        print("\n\t\tRemoving blobs with under {0} pixels...".format(minpix))
    for r1 in list(rs):
        rmask = dblobs == r1
        if len(dblobs[rmask]) < minpix:
            dblobs[rmask] = 0.0
    # identify different regions (after cull)
    rs = np.unique(dblobs)
    # return orders (dblobs) and region list (rs)
    if log:
        print("\n\t\tOrder finding complete.")
    return dblobs, rs

def fit_orders(rs, dblobs, fmin=1, fmax=3):
    """
    Fits a a set of polynomails (of order fmin to fmax) to each order chooses
    lowest chisquared value as polynomial fit for that order

    :param rs: numpy 1D array, the unique integers found in dblobs

                i.e. for case above

                       [0, 1, 2, 3]

    :param dblobs: numpy 2D array equal in shape to the input image, contains
                    located "blobs" which should be the orders (if they were
                    detectable) format is 0 where no order is found and then
                    a different integer for each "blob" found

                    i.e.:

                        010203
                        010203
                        010203
                        010203

                        identifies three orders

    :param fmin: int, lowest order polynomial to fit the order to (default = 1)

    :param fmax: int, highest order polynomial to fit the order to (default = 3)

    :return pfits: list, length same as number of orders, polynomial values
                      (output of np.polyval)

                      i.e. p where:

                      p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]

    :return forders: list, length same as number of orders, contains choosen
                       polynomial order for each order (int)

    :return xls: list, length same as number of orders, the lowest x pixel
                   (wavelength direction) used in each order

    :return xhs: list, length same as number of orders, the highest x pixel
                   (wavelength direction) used in each order

    """
    pfits, forders, xls, xhs = [], [], [], []
    # loop around each order (blob regions)
    for region in rs:
        # ignore the 0 region (background region)
        if region == 0:
            continue
        # create a mask for this region
        mask = dblobs == region
        # Extract x and y positions of this mask
        px, py = np.where(mask)
        # save the min and max fit points
        xls.append(np.min(px))
        xhs.append(np.max(px))
        # fit a polynomial to this region
        pfs, ns = polynomialfit(px, py, nmin=fmin, nmax=fmax)
        pfits.append(pfs), forders.append(ns)
    return pfits, forders, xls, xhs

def remove_orders(polyfits, rm_orders, xlows, xhighs):
    if type(rm_orders) is not list:
        return polyfits, xlows, xhighs
    mask = np.repeat([True], len(polyfits))
    # Remove orders (if rm_orders is populated)
    for r in range(len(polyfits)):
        if r + 1 in rm_orders:
            mask[r] = False
    return mask

def polynomialfit(x, y, nmin=1, nmax=3):
    """
    Select the minimum chi squared polynomial fit between nmin and nmax

    :param x: x axis array
    :param y: y axis array
    :param nmin: minimum order to fit
    :param nmax: maximum order to fit

    Note could improve this to weight it by pixel uncertainties
    """
    chis, ps = [], []
    nrange = range(nmin, nmax+1)
    for n in nrange:
        p = np.polyfit(x, y, n)
        ps.append(p)
        chis.append(np.sum((y-np.polyval(p, x))**2))
    argmin = np.argmin(chis)
    return ps[argmin], nrange[argmin]

def extract_orders(image, pfits, xls, xhs, w=10):
    """
    Extract the spectra from each of the orders and return in a list of numpy
    arrays

    :param image: numpy 2D array, containing the CCD image

    :param pfits: list, length same as number of orders, polynomial values
                      (output of np.polyval)

                      i.e. p where:

                      p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]
    :param xls: list, length same as number of orders, the lowest x pixel
                   (wavelength direction) used in each order

    :param xhs: list, length same as number of orders, the highest x pixel
                   (wavelength direction) used in each order

    :param w: int, number of pixels either side of the polynomial to sum
              (extract) to create the spectrum pixels

    :return spec: list,  length same as number of orders, each containing
                  a 1D numpy array with the spectrum of each order in

    """
    spec = []
    xarr = range(0, len(image))
    for pp, pf in enumerate(pfits):
        print('\n\t\t Order {0}'.format(pp + 1))
        # find nearest y pixel
        xarr1 = range(xls[pp], xhs[pp])
        yarr = np.array(np.round(np.polyval(pf, xarr), 0), dtype=int)
        # loop around x pixels
        ospec = []
        for xrow in tqdm(xarr):
            # find the lower bound for extraction of y (i.e. need to deal with
            # going out of bounds
            if yarr[xrow] - w < 0:
                lower = 0
            else:
                lower = yarr[xrow] - w
            # find the upper bound for extraction of y
            if yarr[xrow] + w > len(image[0]):
                upper = len(image[0])
            else:
                upper = yarr[xrow] + w
            # extract and sum pixels to form this x pixel (wavelength) count
            if xrow in xarr1:
                ssum = np.sum(image[xrow, lower: upper])
                ospec.append(ssum)
            else:
                ospec.append(np.NaN)
        spec.append(ospec)
    return spec

def save_orders(spec, spath):
    """
    Save each order to file
    :param spec: list,  length same as number of orders, each containing
                  a 1D numpy array with the spectrum of each order in
    :param spath: path to save the fits file to
                  (columns = pixel number and counts)
    """
    for s in range(len(spec)):
        nt = Table()
        nt['pixel number'] = np.arange(0, len(spec[s]))
        nt['counts'] = spec[s]
        nt.write(spath + 'Order_{0}.fits'.format(s), overwrite=True)

def run_extraction(image, pfits, xls, xhs, params, name, ppath, fpath):
    # Extract fitted orders
    print('\n\t Extracting Orders...')
    spectra = extract_orders(image, pfits, xls, xhs, params['width'])
    # --------------------------------------------------------------------
    # Save fitted orders
    print('\n\t Saving Orders...')
    save_orders(spectra, fpath)
    # --------------------------------------------------------------------
    # Plot fitted orders
    print('\n\t Plotting Ordered Spectra...')
    plot_orders(pfits, spectra, name, ppath)

# =============================================================================
# Define plotting functions
# =============================================================================
def plot_image(image, name=None, spath=None, show=False, return_frame=False,
               frame=None):
    """
    Plots the raw CCD image to $spath$/$n$_Original[.png,.pdf]

    :param image: numpy 2D array, containing the CCD image

    :param n: string, name of the CCD image (extracted from file name) forms
              prefix of plot filename

    :param spath: path to save the plot to

    :param show: if True then plot is shown instead of saved

    :return:
    """
    if frame is None:
        fig, frame = plt.subplots(1, 1)
        fig.set_size_inches(32, 32)
    frame.imshow(image, cmap='gray')
    frame.set_xticklabels([])
    frame.set_yticklabels([])
    frame.xaxis.set_ticks_position('none')
    frame.yaxis.set_ticks_position('none')
    if return_frame:
        return frame
    elif show:
        print("\n\n Please close the graph (Figure 1) to continue (Alt F4).\n\n")
        plt.show()
    else:
        sname = spath + name + '_Original'
        plt.savefig(sname + '.png', bbox_inches='tight')
        # plt.savefig(sname + '.pdf', bbox_inches='tight')
        plt.close()

def plot_found_orders(dblobs, n=None, spath=None, show=False, return_frame=True,
                      frame=None):
    """
    Plots the found orders over the raw CCD image to
    $spath$/$n$_FoundOrders[.png,.pdf]

    :param dblobs: numpy 2D array equal in shape to the input image, contains
                    located "blobs" which should be the orders (if they were
                    detectable) format is 0 where no order is found and then
                    a different integer for each "blob" found

                    i.e.:

                        010203
                        010203
                        010203
                        010203

                        identifies three orders

    :param n: string, name of the CCD image (extracted from file name) forms
              prefix of plot filename

    :param spath: path to save the plot to

    :param show: if True then plot is shown instead of saved

    :return:
    """
    if frame is None:
        fig, frame = plt.subplots(1, 1)
        fig.set_size_inches(32, 32)
    frame.patch.set_facecolor('black')
    # frame.imshow(image, cmap='gray')
    # ddd = np.ma.masked_where(dblobs == 0, dblobs)
    uorders = np.unique(dblobs)
    ublobs = np.array(dblobs)
    for it in range(len(uorders)):
        oit = uorders[it]
        if oit == 0:
            continue
        mask = ublobs == oit
        if np.sum(mask) == 0:
            continue
        ublobs[mask] = it

    cmap = cmaps.jet
    cmap.set_bad('black', 0.0)
    frame.imshow(ublobs, cmap='jet')
    frame.set_xticklabels([])
    frame.set_yticklabels([])
    frame.xaxis.set_ticks_position('none')
    frame.yaxis.set_ticks_position('none')
    sname = str(spath) + str(n) + '_FoundOrders'

    if return_frame:
        return frame
    elif show:
        print("\n\n Please close the graph (Figure 1) to continue (Alt F4).\n\n")
        plt.show()
    else:
        plt.savefig(sname + '.png', bbox_inches='tight')
        # plt.savefig(sname + '.pdf', bbox_inches='tight')
        plt.close()

def plot_fitted_orders(image, pfits, xls, xhs, n=None, spath=None, show=False,
                       return_frame=True, frame=None, mask=None):
    """
    Plots the fitted orders over the raw CCD image to
    $spath$/$n$_FittedOrders[.png,.pdf]

    :param image: numpy 2D array, containing the CCD image

    :param pfits: list, length same as number of orders, polynomial values
                      (output of np.polyval)

                      i.e. p where:

                      p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]

    :param xls: list, length same as number of orders, the lowest x pixel
                   (wavelength direction) used in each order

    :param xhs: list, length same as number of orders, the highest x pixel
                   (wavelength direction) used in each order

    :param n: string, name of the CCD image (extracted from file name) forms
              prefix of plot filename

    :param spath: path to save the plot to

    :param show: if True then plot is shown instead of saved

    :return:
    """
    if mask is None:
        print('mask none')
        mask = np.repeat([True], len(pfits))
    if frame is None:
        fig, frame = plt.subplots(1, 1)
        fig.set_size_inches(32, 32)
    frame.imshow(image, cmap='gray')

    names = np.arange(1, len(pfits)+1)
    # Loop around each order and plot fits
    for pp, pf in enumerate(pfits):
        if mask[pp] == False:
            continue
        else:
            xarr = np.arange(xls[pp], xhs[pp], 1)
            yarr = np.polyval(pf, xarr)
            yarr[yarr < 0] = 0
            yarr[yarr > len(yarr)] = len(yarr)

            ymid = np.median(yarr)
            xmid = np.median(xarr)

            frame.plot(yarr, xarr, color='r', linewidth=2)
            frame.text(ymid, xmid, 'Order{0}'.format(names[pp]),
                       horizontalalignment='center', verticalalignment='center',
                       rotation=90, color='g', zorder=5)

    # finalise graph
    frame.set_xlim([0, len(image[0])*1.1])
    frame.set_ylim([0, len(image)])
    frame.set_xticklabels([])
    frame.set_yticklabels([])

    if return_frame:
        return frame
    elif show:
        print("\n\n Please close the graph (Figure 1) to continue (Alt F4).\n\n")
        plt.show()
    else:
        sname = spath + n + '_FittedOrders'
        plt.savefig(sname + '.png', bbox_inches='tight')
        # plt.savefig(sname + '.pdf', bbox_inches='tight')
        plt.close()

def plot_orders(pfits, spec, n, spath):
    """
    Plots the extracted order spectra counts vs pixel location
    $spath$/$n$_Spectrum_order_{0}[.png,.pdf]

    :param pfits: list, length same as number of orders, polynomial values
                      (output of np.polyval)

                      i.e. p where:

                      p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]

    :param spec: list,  length same as number of orders, each containing
                  a 1D numpy array with the spectrum of each order in

    :param n: string, name of the CCD image (extracted from file name) forms
              prefix of plot filename

    :param spath: path to save the plot to

    :return:
    """
    for pp, pf in enumerate(pfits):
        fig, frame = plt.subplots(1, 1)
        fig.set_size_inches(10, 10)
        frame.plot(spec[pp])
        frame.set_xlabel('Pixel value')
        frame.set_ylabel('Counts')
        frame.set_title('Order no. {0}'.format(pp + 1))
        sname = spath + n + '_Spectrum_order_{0}'.format(pp + 1)
        plt.savefig(sname + '.png', bbox_inches='tight')
        # plt.savefig(sname + '.pdf', bbox_inches='tight')
        plt.close()

reason: import numpy,from astropy


text:
# Copyright 2021 BlackRock, Inc.
# 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.
from __future__ import annotations

from typing import Sequence

import numpy as np
import numpy.typing as npt
import pytest

from hola.algorithm import HOLA
from hola.objective import FuncName, ObjectiveConfig, ObjectivesSpec
from hola.params import ParamConfig, ParamsSpec
from hola.tune import Tuner, tune

PARAMS_CONFIG: list[ParamsSpec] = [
    {"x_1": ParamConfig(min=-2.0, max=2.0), "x_2": ParamConfig(min=-1.0, max=3.0)},
    {"x_1": {"min": -2.0, "max": 2.0}, "x_2": {"min": -1.0, "max": 3.0}},
]
OBJECTIVE_CONFIG: list[ObjectivesSpec] = [
    {"f": ObjectiveConfig(target=10, limit=10)},
    {"f": {"target": 10.0, "limit": 10.0}},
]
SEED_0_SCORE = {"f": -332.84684342589117}

def rosenbrock(xs: Sequence[float] | npt.NDArray[np.floating]) -> float:
    x, y = xs
    a = 1.0 - x
    b = y - x * x
    return a * a + b * b * 100.0

def hyper_rose(x_1: float, x_2: float) -> dict[FuncName, float]:
    return {"f": -rosenbrock([x_1, x_2])}

@pytest.mark.parametrize("n_jobs", [1, 2])
@pytest.mark.parametrize("param_config", PARAMS_CONFIG)
@pytest.mark.parametrize("objective_config", OBJECTIVE_CONFIG)
def test_tuner(n_jobs: int, param_config: ParamsSpec, objective_config: ObjectivesSpec) -> None:
    np.random.seed(0)
    tuner = Tuner(
        HOLA(
            params_config=param_config,
            objectives_config=objective_config,
            min_samples=30,
            gmm_sampler="uniform",
            explore_sampler="uniform",
            top_frac=0.25,
        )
    )
    tuner.tune(hyper_rose, num_runs=100, n_jobs=n_jobs)
    assert tuner.get_best_scores() == SEED_0_SCORE

@pytest.mark.parametrize("param_config", PARAMS_CONFIG)
@pytest.mark.parametrize("objective_config", OBJECTIVE_CONFIG)
def test_hola(param_config: ParamsSpec, objective_config: ObjectivesSpec) -> None:
    np.random.seed(0)
    exp = HOLA(
        param_config, objective_config, min_samples=30, top_frac=0.25, gmm_sampler="uniform", explore_sampler="uniform"
    )
    assert exp.params_config == PARAMS_CONFIG[0]
    assert exp.objectives_config == OBJECTIVE_CONFIG[0]
    for _ in range(200):
        params = exp.sample()
        value = hyper_rose(**params)
        exp.add_run(value, params)
        # print(_, params, value)
    assert exp.get_best_scores() == SEED_0_SCORE

@pytest.mark.parametrize("param_config", PARAMS_CONFIG)
@pytest.mark.parametrize("objective_config", OBJECTIVE_CONFIG)
def test_tune(param_config: ParamsSpec, objective_config: ObjectivesSpec) -> None:
    tune(hyper_rose, param_config, objective_config)

ext: py

max_stars_repo_name: cristianmatache/HOLA

max_issues_repo_name: cristianmatache/HOLA

max_forks_repo_name: cristianmatache/HOLA

reason: import numpy


text:
#!/usr/bin/env python
#
# Let's the rover wander around without any goal.
#

from __future__ import print_function, division

from itertools import product
import numpy as np
import rospy
from std_msgs.msg import Header
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist, PointStamped
import time

# Assumed width of the rover. Used to calculate the
# minimum distance to a point in the rover's corridor
ROVER_WIDTH = 1

# Update time of the laser scanner
PERIOD = 0.1

# If the utility falls below this threshold, the fallback
# decisions are used
FALLBACK_DIST = 0.75

# Linear and angular velocities
LINVEL = [0.2, 0.3]
ANGVEL = [-0.3, -0.2, -0.1, 0.0, 0.1, 0.2, 0.3]
MAX_ANGVEL = 0.3

class Wanderer(object):
    def __init__(self):
        self.cmdPub = rospy.Publisher('/teleop/cmd_vel', Twist, queue_size=10)
        rospy.Subscriber('/scan', LaserScan, callback=self.scan_cb, queue_size=10)
        self.fallbackDirection = 0

    def get_utility(self, angles, ranges, linvel, angvel):
        """
        The utility is defined as the minimal distance in
        driving direction to the next obstacle.
        We calculate the state that we would get when the rover moves
        with the given velocities for one update interval.
        """
        # Integrate the estimated position after the action
        dt = 0.01
        dx = dy = 0
        for i in range(int(PERIOD / dt)):
            theta = i * dt * angvel
            dx = dx + linvel * np.cos(theta) * dt
            dy = dy + linvel * np.sin(theta) * dt

        # Transform to cartesian coordinates
        cartX = np.cos(angles) * ranges
        cartY = np.sin(angles) * ranges

        # Calculate the normal distance between the estimated direction
        # vector and the points. This is used to determine whether a point
        # lies in the corridor or not.
        x1 = dx
        y1 = dy
        x2 = dx + np.cos(theta)
        y2 = dy + np.sin(theta)
        d = (np.abs((y2 - y1) * cartX - (x2 - x1) * cartY + x2 * y1 - y2 * x1) /
                np.sqrt((y2 - y1)**2 + (x2 - x1)**2))

        # Get the minimum distance in the corridor.
        indices = np.abs(d) <= ROVER_WIDTH / 2
        drvPointsX = cartX[indices]
        drvPointsY = cartY[indices]

        try:
            m = np.min(np.sqrt((drvPointsX - dx)**2 + (drvPointsY - dy)**2))
        except ValueError:
            m = np.inf

        return m

    def scan_cb(self, msg):
        # convert to (angle, range) pairs
        inc = (msg.angle_max - msg.angle_min) / len(msg.ranges)
        ranges = np.array(msg.ranges)
        angles = np.array([msg.angle_min + inc * i for i in range(len(msg.ranges))])

        # Our actions are defined as pairs of linear and angular velocity,
        # out of an existing set of tuples. Here we calculate the maximum
        # expected utility for these actions.
        lv = None
        av = None
        umax = 0
        for linvel, angvel in product(LINVEL, ANGVEL):
            u = self.get_utility(angles, ranges, linvel, angvel)

            if u > umax:
                lv = linvel
                av = angvel
                umax = u

        # Fallback
        if umax < FALLBACK_DIST:
            lv = 0
            if np.nanmax(ranges) > FALLBACK_DIST:
                i = np.nanargmax(ranges)
                a = angles[i]
                print('Fallback to longest distance:', a)
                if self.fallbackDirection == 0:
                    if a < 0:
                        av = -MAX_ANGVEL
                        self.fallbackDirection = -1
                    else:
                        av = MAX_ANGVEL
                        self.fallbackDirection = 1
                else:
                    av = self.fallbackDirection * 0.3

            else:
                print('Fallback to constant angular velocity')
                # constant angular velocity
                av = MAX_ANGVEL
                self.fallbackDirection = 1
        else:
            self.fallbackDirection = 0

        cmd = Twist()
        cmd.linear.x = lv
        cmd.angular.z = av
        self.cmdPub.publish(cmd)
        print(lv, av)

def main():
    rospy.init_node('wanderer', anonymous=True)
    wanderer = Wanderer()
    rospy.spin()

if __name__ == '__main__':
    main()


text:
import numpy as np
import cv2
import torch
import random

from coord_utils import get_center_scale
from core.config import cfg

def transform_joint_to_other_db(src_joint, src_name, dst_name):
    src_joint_num = len(src_name)
    dst_joint_num = len(dst_name)

    new_joint = np.zeros(((dst_joint_num,) + src_joint.shape[1:]), dtype=np.float32)
    for src_idx in range(len(src_name)):
        name = src_name[src_idx]
        if name in dst_name:
            dst_idx = dst_name.index(name)
            new_joint[dst_idx] = src_joint[src_idx]

    return new_joint

def rotate_2d(pt_2d, rot_rad):
    x = pt_2d[0]
    y = pt_2d[1]
    sn, cs = np.sin(rot_rad), np.cos(rot_rad)
    xx = x * cs - y * sn
    yy = x * sn + y * cs
    return np.array([xx, yy], dtype=np.float32)

def flip_2d_joint(kp, width, flip_pairs):
    kp[:, 0] = width - kp[:, 0] - 1
    """Flip keypoints."""
    for lr in flip_pairs:
        kp[lr[0]], kp[lr[1]] = kp[lr[1]].copy(), kp[lr[0]].copy()

    return kp

def flip_3d_joint(kp, flip_pairs):
    """Flip keypoints."""
    for lr in flip_pairs:
        kp[lr[0]], kp[lr[1]] = kp[lr[1]].copy(), kp[lr[0]].copy()

    kp[:,0] = - kp[:,0]
    return kp

def j2d_processing(kp, res, bbox, rot, f, flip_pairs):
    """Process gt 2D keypoints and apply all augmentation transforms."""
    # flip the x coordinates
    center, scale = get_center_scale(bbox)
    trans = get_affine_transform(center, scale, rot, res)

    nparts = kp.shape[0]
    for i in range(nparts):
        kp[i, :2] = affine_transform(kp[i, :2].copy(), trans)

    if f:
        kp = flip_2d_joint(kp, res[0], flip_pairs)
    kp = kp.astype('float32')
    return kp, trans

def j3d_processing(S, r, f, flip_pairs):
    """Process gt 3D keypoints and apply all augmentation transforms."""
    # in-plane rotation
    rot_mat = np.eye(3)
    if not r == 0:
        rot_rad = -r * np.pi / 180
        sn, cs = np.sin(rot_rad), np.cos(rot_rad)
        rot_mat[0, :2] = [cs, -sn]
        rot_mat[1, :2] = [sn, cs]
    S = np.einsum('ij,kj->ki', rot_mat, S)

    # flip the x coordinates
    if f:
        S = flip_3d_joint(S, flip_pairs)
    S = S.astype('float32')

    return S

def my3d_processing(S, r):
    rot_mat = np.eye(3)
    if not r == 0:
        rot_rad = -r * np.pi / 180
        sn, cs = np.sin(rot_rad), np.cos(rot_rad)
        rot_mat[0, [0,2]] = [cs, sn]
        rot_mat[2, [0,2]] = [-sn, cs]
    S = np.einsum('ij,kj->ki', rot_mat, S)

    return S

def augm_params(is_train):
    if not is_train:
        return 0, 0

    """Get augmentation parameters."""
    flip = 0  # flipping
    rot = 0  # rotation
    # We flip with probability 1/2
    if cfg.AUG.flip and random.uniform(0, 1) <= 0.5:
        flip = 1

    rot_factor = cfg.AUG.rotate_factor
    # The rotation is a number in the area [-2*rotFactor, 2*rotFactor]
    rot = min(2 * rot_factor,
              max(-2 * rot_factor, np.random.randn() * rot_factor))

    if random.uniform(0, 1) <= 0.5:
        rot = 0

    return flip, rot

def transform_preds(coords, center, scale, output_size):
    target_coords = np.zeros(coords.shape)
    trans = get_affine_transform(center, scale, 0, output_size, inv=1)
    for p in range(coords.shape[0]):
        target_coords[p, 0:2] = affine_transform(coords[p, 0:2], trans)
    return target_coords

def flip_back(output_flipped, matched_pairs):

    output_flipped = output_flipped[:, :, :, ::-1]
    for pair in matched_pairs:
        tmp = output_flipped[:, pair[0], :, :].copy()
        output_flipped[:, pair[0], :, :] = output_flipped[:, pair[1], :, :]
        output_flipped[:, pair[1], :, :] = tmp

        return output_flipped

def get_affine_transform(center,
                         scale,
                         rot,
                         output_size,
                         shift=np.array([0, 0], dtype=np.float32),
                         inv=0):
    if not isinstance(scale, np.ndarray) and not isinstance(scale, list):
        print(scale)
        scale = np.array([scale, scale])

    src_w = scale[0]
    dst_w = output_size[0]
    dst_h = output_size[1]

    rot_rad = np.pi * rot / 180
    src_dir = get_dir([0, src_w * -0.5], rot_rad)
    dst_dir = np.array([0, dst_w * -0.5], np.float32)

    src = np.zeros((3, 2), dtype=np.float32)
    dst = np.zeros((3, 2), dtype=np.float32)
    src[0, :] = center + scale * shift
    src[1, :] = center + src_dir + scale * shift
    dst[0, :] = [dst_w * 0.5, dst_h * 0.5]
    dst[1, :] = np.array([dst_w * 0.5, dst_h * 0.5]) + dst_dir

    src[2:, :] = get_3rd_point(src[0, :], src[1, :])
    dst[2:, :] = get_3rd_point(dst[0, :], dst[1, :])

    if inv:
        trans = cv2.getAffineTransform(np.float32(dst), np.float32(src))
    else:
        trans = cv2.getAffineTransform(np.float32(src), np.float32(dst))

    return trans

def affine_transform(pt, t):
    new_pt = np.array([pt[0], pt[1], 1.]).T
    new_pt = np.dot(t, new_pt)
    return new_pt[:2]

def get_3rd_point(a, b):
    direct = a - b
    return b + np.array([-direct[1], direct[0]], dtype=np.float32)

# rotate src_point by rot_Rad
def get_dir(src_point, rot_rad):
    sn, cs = np.sin(rot_rad), np.cos(rot_rad)

    src_result = [0, 0]
    src_result[0] = src_point[0] * cs - src_point[1] * sn
    src_result[1] = src_point[0] * sn + src_point[1] * cs

    return src_result

def flip_joints(joints, joints_vis, width, matched_parts):
    # Flip horizontal
    joints[:, 0] = width - joints[:, 0] - 1

    # Change left-right parts
    for pair in matched_parts:
        joints[pair[0], :], joints[pair[1], :] = joints[pair[1], :], joints[pair[0], :].copy()
        joints_vis[pair[0], :], joints_vis[pair[1], :] = joints_vis[pair[1], :], joints_vis[pair[0], :].copy()

    return joints, joints_vis

def multi_meshgrid(*args):
    """
    Creates a meshgrid from possibly many
    elements (instead of only 2).
    Returns a nd tensor with as many dimensions
    as there are arguments
    """
    args = list(args)
    template = [1 for _ in args]
    for i in range(len(args)):
        n = args[i].shape[0]
        template_copy = template.copy()
        template_copy[i] = n
        args[i] = args[i].view(*template_copy)
        # there will be some broadcast magic going on
    return tuple(args)

def flip(tensor, dims):
    if not isinstance(dims, (tuple, list)):
        dims = [dims]
    indices = [torch.arange(tensor.shape[dim] - 1, -1, -1,
                            dtype=torch.int64) for dim in dims]
    multi_indices = multi_meshgrid(*indices)
    final_indices = [slice(i) for i in tensor.shape]
    for i, dim in enumerate(dims):
        final_indices[dim] = multi_indices[i]
    flipped = tensor[final_indices]
    assert flipped.device == tensor.device
    assert flipped.requires_grad == tensor.requires_grad
    return flipped


text:
# solutions.py
"""Introductory Labs: Intro to NumPy. Solutions file."""

import numpy as np

def prob1():
    """Define the matrices A and B as arrays. Return the matrix product AB."""
    A = np.array([[3,-1,4],[1,5,-9]])
    B = np.array([[2,6,-5,3],[5,-8,9,7],[9,-3,-2,-3]])
    return A.dot(B)

def prob2():
    """Define the matrix A as an array. Return the matrix -A^3 + 9A^2 - 15A."""
    A = np.array([[3,1,4],[1,5,9],[-5,3,1]])
    A2 = np.dot(A, A)
    return -np.dot(A, A2) + 9*A2 - 15*A

def prob3():
    """Define the matrices A and B as arrays. Calculate the matrix product ABA,
    change its data type to np.int64, and return it.
    """
    A = np.triu(np.ones((7,7)))
    B = np.full_like(A, 5) - np.tril(np.full_like(A, 6))
    return np.dot(np.dot(A, B), A).astype(np.int64)

def prob4(A):
    """Make a copy of 'A' and set all negative entries of the copy to 0.
    Return the copy.

    Example:
        >>> A = np.array([-3,-1,3])
        >>> prob4(A)
        array([0, 0, 3])
    """
    B = A.copy()
    B[A < 0] = 0
    return B

def prob5():
    """Define the matrices A, B, and C as arrays. Return the block matrix
                                | 0 A^T I |
                                | A  0  0 |,
                                | B  0  C |
    where I is the identity matrix of appropriate size and each 0 is a matrix
    of all zeros, also of appropriate sizes.
    """
    A = np.arange(6).reshape((3,2)).T
    m, n = A.shape
    B = np.tril(np.full((3,3), 3, dtype=np.float64))
    C = -2*np.eye(3)
    O = np.zeros_like(A)
    return np.vstack((  np.hstack((np.zeros((n,n)), A.T,     np.eye(n))),
                        np.hstack((A,               np.zeros((m,m)), O)),
                        np.hstack((B,               O.T,             C))  ))

def prob6(A):
    """Divide each row of 'A' by the row sum and return the resulting array.

    Example:
        >>> A = np.array([[1,1,0],[0,1,0],[1,1,1]])
        >>> prob6(A)
        array([[ 0.5       ,  0.5       ,  0.        ],
               [ 0.        ,  1.        ,  0.        ],
               [ 0.33333333,  0.33333333,  0.33333333]])
    """
    return A / A.sum(axis=1)[:,np.newaxis].astype(np.float64)
    # Or use np.vstack() or np.reshape().

def prob7():
    """Given the array stored in grid.npy, return the greatest product of four
    adjacent numbers in the same direction (up, down, left, right, or
    diagonally) in the grid.
    """
    grid = np.load("grid.npy")

    # Slicing method.
    return max(
        np.max(grid[:,:-3] * grid[:,1:-2] * grid[:,2:-1] * grid[:,3:]),
        np.max(grid[:-3,:] * grid[1:-2,:] * grid[2:-1,:] * grid[3:,:]),
        np.max(grid[:-3,:-3]* grid[1:-2,1:-2] * grid[2:-1,2:-1] *grid[3:,3:]),
        np.max(grid[:-3,3:] * grid[1:-2,2:-1] * grid[2:-1,1:-2] *grid[3:,:-3]))

    # Naive iterative method (much less efficient).
    high = 0
    m,n = grid.shape
    for i in xrange(m):
        for j in xrange(n):
            z, w = 0, 0
            x = np.prod(grid[i,j:j+4])          # Right
            y = np.prod(grid[i:i+4,j])          # Down
            if i < m-3 and j < n-3:             # Right diagonal
                z = np.prod([grid[i,j],
                             grid[i+1,j+1],
                             grid[i+2,j+2],
                             grid[i+3,j+3]])
            if i < m-3 and j > 3:               # Left diagonal
                w = np.prod([grid[i,j],
                             grid[i+1,j-1],
                             grid[i+2,j-2],
                             grid[i+3,j-3]])
            high = max(x, y, z, w, high)
    return high


text:
import matplotlib.pyplot as plt
import numpy as np
import time
import csv

plt.ion()

#Setting constants
min_x = 0
max_x = 400

figure, ax = plt.subplots()
lines, = ax.plot([],[], 'k')

ax.set_autoscaley_on(True)
ax.set_xlim(min_x, max_x)
ax.grid()

def on_running(xdata, ydata):
    lines.set_xdata(xdata)
    lines.set_ydata(ydata)

    ax.relim()
    ax.autoscale_view()

    figure.canvas.draw()
    figure.canvas.flush_events()

xdata = np.array([60, 120, 180, 240, 300, 360])
y = np.genfromtxt('LF1P.csv', delimiter=',',dtype=np.float_)
y2 = np.genfromtxt('TimeTicks1.csv', delimiter=',',dtype=np.int)

ydata = []
for i in range(330000,y.shape[0],100):
    ydata = y[i]
    print y[i][0], '  time= ',y2[i]
    #print ydata
    on_running(xdata, ydata)
    time.sleep(0.1)


text:
import pathlib

import pytest
# import numpy as np

# from gripy.legacy import py3grib2
from gripy import grib2

def get_test_gfs_grib():
    data_dir = pathlib.Path(__file__).parent / 'data'
    grib_file = data_dir / 'gfs.t12z.pgrb2.0p25.f000.grb'
    assert grib_file.exists()
    return grib_file

def get_test_gfs42_grib():
    data_dir = pathlib.Path(__file__).parent / 'data'
    grib_file = data_dir / 'gfs.t12z.pgrb2.0p25.f042'
    assert grib_file.exists()
    return grib_file

def get_test_nav_grib():
    data_dir = pathlib.Path(__file__).parent / 'data'
    grib_file = data_dir / 'navgem_2020020800f000.grib2'
    assert grib_file.exists()
    return grib_file

# def test_grib2decode_gfs_prod_def():
#     grib_file = get_test_gfs_grib()
#     msgs = py3grib2.Grib2Decode(str(grib_file))
#     assert len(msgs) == 2

#     expected_prod_def = [
#         [2, 2, 2, 0, 81, 0, 0, 1, 0, 103, 0,  10, 255, 0, 0],
#         [2, 3, 2, 0, 81, 0, 0, 1, 0, 103, 0,  10, 255, 0, 0],
#     ]
#     for expect, ret_msg in zip(expected_prod_def, msgs):
#         assert all(expect == ret_msg.product_definition_template)

def test_grib2_gfs_prod_def():
    grib_file = get_test_gfs_grib()
    with grib2.Grib2File(str(grib_file)) as g2:
        msgs = g2.grib_msgs
        assert len(msgs) == 2

        expected_prod_def = [
            [2, 2, 2, 0, 81, 0, 0, 1, 0, 103, 0,  10, 255, 0, 0],
            [2, 3, 2, 0, 81, 0, 0, 1, 0, 103, 0,  10, 255, 0, 0],
        ]
        for expect, ret_msg in zip(expected_prod_def, msgs):
            assert all(expect == ret_msg.section4.pds_template)
            assert ret_msg.discipline_int == 0
            assert ret_msg.discipline_name() == 'Meteorological products'

def test_grib2_gfs_pds_decode():
    grib_file = get_test_gfs_grib()
    with grib2.Grib2File(str(grib_file)) as g2:
        msgs = g2.grib_msgs
    assert len(msgs) == 2

    expected_shortname = ['UGRD', 'VGRD']
    for expect, ret_msg in zip(expected_shortname, msgs):
        print(ret_msg)
        assert expect == ret_msg.shortname

    expected_longname = ['U-component of wind', 'V-component of wind']
    for expect, ret_msg in zip(expected_longname, msgs):
        assert expect == ret_msg.longname

def test_grib2_gfs42_pds_decode():
    grib_file = get_test_gfs42_grib()
    with grib2.Grib2File(str(grib_file)) as g2:
        msgs = g2.grib_msgs
        assert len(msgs) == 1

        expected_shortname = ['CLWMR']
        for expect, ret_msg in zip(expected_shortname, msgs):
            print(ret_msg)
            print(ret_msg.section1)
            print(ret_msg.section4)
            assert expect == ret_msg.shortname

        expected_longname = ['Cloud mixing ratio']
        for expect, ret_msg in zip(expected_longname, msgs):
            assert expect == ret_msg.longname

def test_grib2_nav_pds_decode():
    grib_file = get_test_nav_grib()
    with grib2.Grib2File(str(grib_file)) as g2:
        msgs = g2.grib_msgs
    assert len(msgs) == 1

    expected_shortname = ['TMP', ]
    for expect, ret_msg in zip(expected_shortname, msgs):
        print(ret_msg)
        assert expect == ret_msg.shortname

    expected_longname = ['Temperature', ]
    for expect, ret_msg in zip(expected_longname, msgs):
        assert expect == ret_msg.longname

def test_grib2_nav_latlon():
    grib_file = get_test_nav_grib()
    with grib2.Grib2File(str(grib_file)) as g2:
        msgs = g2.grib_msgs
        lats, lons = msgs[0].section3.latlon()
    assert lats[0] > -90.1
    assert lats[-1] < 90.1

# def test_grib2decode_gfs_ident_sect():
    # grib_file = get_test_gfs_grib()
    # msgs = py3grib2.Grib2Decode(str(grib_file))

    # expected_ident_sect = [
    #     [7, 0, 2, 1, 1, 2020, 1, 17, 12, 0, 0, 0, 1],
    #     [7, 0, 2, 1, 1, 2020, 1, 17, 12, 0, 0, 0, 1],
    # ]
    # for expect, ret_msg in zip(expected_ident_sect, msgs):
    #     assert all(expect == ret_msg.identification_section)

# def test_grib2decode_gfs_values():
#     grib_file = get_test_gfs_grib()
#     msgs = py3grib2.Grib2Decode(str(grib_file))

#     expected_vals = [{'min': -25.66,
#                       'max': 29.63,
#                       'mean': 0.06,
#                       },
#                      {'min': -29.44,
#                       'max': 25.76,
#                       'mean': -0.04,
#                       }, ]
#     for expect, ret_msg in zip(expected_vals, msgs):
#         values = ret_msg.values
#         assert expect['min'] == pytest.approx(np.min(values), abs=0.1)
#         assert expect['max'] == pytest.approx(np.max(values), abs=0.1)
#         assert expect['mean'] == pytest.approx(np.mean(values), abs=0.1)

# def test_grib2decode_ngm_latlon():
#     grib_file = get_test_nav_grib()
#     msg = py3grib2.Grib2Decode(str(grib_file))
#     assert msg.latitude_first_gridpoint > -90.1
#     assert msg.latitude_last_gridpoint < 90.1

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

max_stars_repo_name: abrammer/gripy

max_issues_repo_name: abrammer/gripy

max_forks_repo_name: abrammer/gripy


text:
from __future__ import print_function
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import os
import glob
import skimage.io as io
import cv2
import time
from PIL import Image

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

from shutil import copyfile

from config import *

mask_dict = MASK_DICT

def convert_num_mask_to_color(dir_path_source, dir_path_target, mask_dict, is_gray = True):
    os.makedirs(dir_path_target, exist_ok = True)
    for count, filename in enumerate(sorted(os.listdir(dir_path_source))):
        img = io.imread(os.path.join(dir_path_source,filename),as_gray = is_gray)
        class_items = mask_dict.items()
        new_mask = np.zeros((img.shape[0], img.shape[1], 3))
        for key, value in class_items:
            class_name = key
            class_index = value[0]
            class_color = value[1]
            new_mask[img == class_index] = class_color

        cv2.imwrite(dir_path_target + '/' + filename, new_mask, [cv2.IMWRITE_PNG_COMPRESSION, 0])
        print(str(count) + ':' + os.path.join(dir_path_source,filename))

def dataset_preparation(dir_path_source_masks, dir_path_source_images, dir_path_target, mask_dict,
                        size_threshold = 20, ratio = 10, is_gray = True):
    os.makedirs(dir_path_target, exist_ok = True)
    os.makedirs(dir_path_target+'/train', exist_ok=True)
    os.makedirs(dir_path_target+'/test', exist_ok=True)
    os.makedirs(dir_path_target + '/too_small', exist_ok=True)
    os.makedirs(dir_path_target + '/train'+'/images', exist_ok=True)
    os.makedirs(dir_path_target + '/train' + '/masks', exist_ok=True)
    os.makedirs(dir_path_target + '/test'+'/images', exist_ok=True)
    os.makedirs(dir_path_target + '/test' + '/masks', exist_ok=True)
    os.makedirs(dir_path_target + '/too_small'+'/images', exist_ok=True)
    os.makedirs(dir_path_target + '/too_small'+ '/masks', exist_ok=True)

    full_sorted_list = sorted(os.listdir(dir_path_source_masks))
    full_count = len(full_sorted_list)
    no_file_count = 0
    bad_crop_count = 0

    for count, filename in enumerate(full_sorted_list):
        img = io.imread(os.path.join(dir_path_source_masks,filename),as_gray = is_gray)
        class_items = mask_dict.items()
        height = img.shape[0]
        width = img.shape[1]
        new_mask = np.zeros((height, width, 3))
        for key, value in class_items:
            class_name = key
            class_index = value[0]
            class_color = value[1]
            new_mask[img == class_index] = class_color

        target_path = dir_path_target
        if(width < size_threshold or height < size_threshold):
            target_path = target_path + '/too_small'
        else:
            #if(count < train_part*full_count):
            if (count % ratio != 0):
                target_path = target_path + '/train'
            else:
                target_path = target_path + '/test'

        image_file_name = filename.split('.')[0] + '.jpg'
        source_image_file_name = dir_path_source_images + '/' + image_file_name
        error_message = ''

        if (os.path.isfile(source_image_file_name)):
            (width_image, height_image) = Image.open(source_image_file_name).size
            if(width == width_image and height==height_image):
                cv2.imwrite(target_path + '/masks/' + filename, new_mask, [cv2.IMWRITE_PNG_COMPRESSION, 0])
                copyfile(source_image_file_name, target_path + '/images/' + image_file_name)
            else:
                bad_crop_count += 1
                error_message = 'bad_crop'
        else:
            no_file_count += 1
            error_message = 'no_file'

        print(error_message + str(count) + ':' + os.path.join(dir_path_source_masks,filename))
    print('no_file_count: ', no_file_count)
    print('bad_crop_count: ', bad_crop_count)

def integral_histogram(histogram):
    new_histogram = np.zeros((len(histogram), 1))
    for value in range(len(histogram)):
        if value == 0:
            continue
        new_histogram[value] = new_histogram[value-1]+histogram[value]
    return new_histogram

def get_mask_statistics(dir_path_source):
    width_histogram = np.zeros((4000, 1))
    height_histogram = np.zeros((4000, 1))
    max_size_histogram = np.zeros((4000, 1))
    x = np.arange(4000)
    for count, filename in enumerate(sorted(os.listdir(dir_path_source))):
        mask_size = Image.open(os.path.join(dir_path_source,filename)).size
        (width, height) = mask_size
        width_histogram[width] += 1
        height_histogram[height] += 1
        max_size_histogram[max(width,height)] += 1
    max_size = 500
    fig = plt.figure(num=None, figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')
    ax_1 = fig.add_subplot(211)
    ax_1.plot(x[:max_size], integral_histogram(width_histogram[:max_size]), color='red', linewidth=2)
    ax_1.xaxis.set_major_locator(ticker.MultipleLocator(20))
    ax_1.set(title='width_histogram')
    ax_1.grid(which='major',
            color='k')

    ax_2 = fig.add_subplot(212)
    ax_2.plot(x[:max_size], (max_size_histogram[:max_size]), color='red', linewidth=2)
    ax_2.xaxis.set_major_locator(ticker.MultipleLocator(20))
    ax_2.set(title='max_size_histogram')
    ax_2.grid(which='major',
              color='k')
    plt.show()

# get_mask_statistics(dir_path_source = '/media/cds-pc3/Data/pku_data_segmentation/pku_data_segmentation/car_segm_dataset/train/images')

# convert_num_mask_to_color(
#     dir_path_source = '/media/cds-pc3/Data/pku_data_segmentation/pku_data_segmentation/masks',
#     dir_path_target = '/media/cds-pc3/Data/pku_data_segmentation/pku_data_segmentation/color_masks',
#     mask_dict = mask_dict,
#     is_gray = True)

# dataset_preparation(
#     dir_path_source_masks = '/media/cds-pc3/Data/pku_data_segmentation/pku_data_segmentation/masks',
#     dir_path_source_images = '/media/cds-pc3/Data/pku_data_segmentation/pku_data_segmentation/images',
#     dir_path_target = '/media/cds-pc3/Data/pku_data_segmentation/pku_data_segmentation/car_segm_dataset',
#     mask_dict = mask_dict,
#     size_threshold = 20,
#     ratio = 10,
#     is_gray = True)

# dataset_preparation(
#     dir_path_source_masks = '/media/cds-pc3/Data/pku_data_segmentation/pku_data_segmentation/crop_train_masks',
#     dir_path_source_images = '/media/cds-pc3/Data/pku_data_segmentation/pku_data_segmentation/crop_train_images',
#     dir_path_target = '/media/cds-pc3/Data/pku_data_segmentation/pku_data_segmentation/car_segm_coco_dataset',
#     mask_dict = mask_dict,
#     size_threshold = 20,
#     ratio = 10,
#     is_gray = True)

def train_generator(batch_size,train_path,image_folder,mask_folder,aug_dict,image_color_mode = "color",
                    mask_color_mode = "color",image_save_prefix  = "images",mask_save_prefix  = "masks",
                    flag_multi_class = False, num_class = 2,save_to_dir = None,target_size = (256,256),seed = 1,
                    mask_dict = None):
    '''
    can generate image and mask at the same time
    use the same seed for image_datagen and mask_datagen to ensure the transformation for image and mask is the same
    if you want to visualize the results of generator, set save_to_dir = "your path"
    '''
    image_datagen = ImageDataGenerator(**aug_dict)
    mask_datagen = ImageDataGenerator(**aug_dict)
    image_generator = image_datagen.flow_from_directory(
        train_path,
        classes = [image_folder],
        class_mode = None,
        color_mode = image_color_mode,
        target_size = target_size,
        batch_size = batch_size,
        save_to_dir = save_to_dir,
        save_prefix  = image_save_prefix,
        seed = seed)
    mask_generator = mask_datagen.flow_from_directory(
        train_path,
        classes = [mask_folder],
        class_mode = None,
        color_mode = mask_color_mode,
        target_size = target_size,
        batch_size = batch_size,
        save_to_dir = save_to_dir,
        save_prefix  = mask_save_prefix,
        seed = seed)
    train_generator = zip(image_generator, mask_generator)
    for (img,mask) in train_generator:
        img,mask = adjustData(img, mask, mask_dict)
        yield (img,mask)

def adjustData(img, mask, mask_dict=None):
    img = img / 255
    class_items = mask_dict.items()
    num_class = len(class_items)
    batch_size = mask.shape[0]
    new_mask = np.zeros((batch_size, mask.shape[1], mask.shape[2], num_class))

    #в эту функцию приходит не одна маска, а батч масок
    for key, value in class_items:
        class_name = key
        class_index = value[0]

        label_color = value[1]

        min_color = (label_color[0] - 5, label_color[1] - 5, label_color[2] - 5)
        max_color = (label_color[0] + 5, label_color[1] + 5, label_color[2] + 5)
        for batch_id in range(batch_size):
            new_mask[batch_id, :, :, class_index] = cv2.inRange(mask[batch_id], min_color, max_color)

    mask = new_mask
    mask = mask / 255
    # elif(np.max(img) > 1):
    #     img = img / 255
    #     mask = mask /255
    #     mask[mask > 0.5] = 1
    #     mask[mask <= 0.5] = 0
    return (img,mask)

def test_generator(test_path, target_size = (256,256), as_gray = False):
    for filename in sorted(os.listdir(test_path)):
        start_time = time.time()
        img = io.imread(os.path.join(test_path,filename),as_gray = as_gray)
        img = img / 255
        img = cv2.resize(img,(target_size[1],target_size[0]))
        img = np.reshape(img,(1,)+img.shape)
        end_time = time.time()
        duration = end_time - start_time
        with open('logs/timeread.txt', "a") as log_file:
            log_file.write("Imread, s: " + str(duration) + "\n")
        yield img

def read_and_preprocess_image(test_path,filename,target_size = (256,256)):
    if test_path is None:
        raw = io.imread(filename)
    else:
        raw = io.imread(os.path.join(test_path, filename))
        #raw_ = cv2.imread(filename)
    #raw = cv2.cvtColor(raw, cv2.COLOR_BGR2RGB)
    img = raw / 255
    img = cv2.resize(img, (target_size[1], target_size[0]))
    img = np.reshape(img, (1,) + img.shape)
    scale_coef = (raw.shape[0]/target_size[0], raw.shape[1]/target_size[1])

    return img, raw, scale_coef

def label_visualize_smoothed(mask_dict, img, scale_coef = (1,1)):
    class_items = mask_dict.items()
    num_class = len(class_items)
    (out_h, out_w) = (int(img.shape[0] * scale_coef[0]), int(img.shape[1] * scale_coef[1]))
    img_out = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)

    for key, value in class_items:
        class_name = key
        class_index = value[0]
        label_color = value[1]
        threshold = value[2]

        mask = img[:,:,class_index]

        img_out[mask > threshold] = np.array(label_color)

    img_out_resized = cv2.resize(img_out, (out_w, out_h))

    return img_out_resized

def label_visualize_precise(mask_dict, img, scale_coef = (1,1)):
    class_items = mask_dict.items()
    num_class = len(class_items)
    (out_h, out_w) = (int(img.shape[0] * scale_coef[0]), int(img.shape[1] * scale_coef[1]))
    img_out = np.zeros((out_h, out_w, 3), dtype=np.uint8)
    mask = np.argmax(img, axis=-1)
    for key, value in class_items:
        class_name = key
        class_index = value[0]
        label_color = value[1]
        #threshold = value[2]

        #mask = img[:,:,class_index]
        #mask[mask > threshold] = 255
        #mask[mask <= threshold] = 0
        channel_mask = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8)
        channel_mask[mask == class_index] = 255
        mask_resized = cv2.resize(channel_mask, (out_w, out_h))

        img_out[mask_resized > 128] = np.array(label_color)

    return img_out

max_stars_repo_name: yuddim/DefectSoft

max_issues_repo_name: yuddim/DefectSoft

max_forks_repo_name: yuddim/DefectSoft


text:
# --------------
# Code starts here

import numpy as np

# Code starts here

# Adjacency matrix
adj_mat = np.array([[0,0,0,0,0,0,1/3,0],
                   [1/2,0,1/2,1/3,0,0,0,0],
                   [1/2,0,0,0,0,0,0,0],
                   [0,1,0,0,0,0,0,0],
                  [0,0,1/2,1/3,0,0,1/3,0],
                   [0,0,0,1/3,1/3,0,0,1/2],
                   [0,0,0,0,1/3,0,0,1/2],
                   [0,0,0,0,1/3,1,1/3,0]])

# Compute eigenvalues and eigencevectrs
eigenvalues, eigencevectors = np.linalg.eig(adj_mat)
print("eigenvalues: ", eigenvalues)
print("eigencevectors: ", eigencevectors)

# Eigen vector corresponding to 1
eigen_1 = abs(eigencevectors[:,0]) / np.linalg.norm(eigencevectors[:,0], 1)

# most important page
page = np.where(np.max(eigen_1) == eigen_1)[0][0] + 1

# Code ends here

# --------------
# Code starts here

# Initialize stationary vector I
init_I = np.array([1,0,0,0,0,0,0,0])

# Perform iterations for power method
for i in range(10):
     init_I = np.dot(adj_mat, init_I) / np.linalg.norm(init_I, 1)

power_page = np.where(np.max(init_I) == init_I)[0][0] + 1

# Code ends here

# --------------
# Code starts here

# New Adjancency matrix
# New Adjancency matrix
new_adj_mat = np.array([[0,0,0,0,0,0,0,0],
                   [1/2,0,1/2,1/3,0,0,0,0],
                  [1/2,0,0,0,0,0,0,0],
                   [0,1,0,0,0,0,0,0],
                   [0,0,1/2,1/3,0,0,1/2,0],
                   [0,0,0,1/3,1/3,0,0,1/2],
                   [0,0,0,0,1/3,0,0,1/2],
                   [0,0,0,0,1/3,1,1/2,0]])

# Initialize stationary vector I
new_init_I  = np.array([1,0,0,0,0,0,0,0])

# Perform iterations for power method
for i in range(10):
    new_init_I = np.dot(new_adj_mat, new_init_I) / np.linalg.norm(new_init_I, 1)

new_page_rank = np.where(np.max(new_adj_mat) == new_adj_mat) [0][0] + 1
print("new_init_I: ", new_init_I)
print("new_page_rank: ", new_page_rank)

# Code ends here

# --------------
# Alpha value
alpha = 0.85

# Code starts here

# Modified adjancency matrix
G = alpha * new_adj_mat + (np.ones(new_adj_mat.shape) - alpha) * (1/ len(new_adj_mat))

# Initialize stationary vector I
final_init_I = np.array([1,0,0,0,0,0,0,0])

# Perform iterations for power method
for i in range(10):
    final_init_I = np.dot(new_adj_mat, final_init_I) / np.linalg.norm(final_init_I, 1)

final_page_rank = np.where(np.max(final_init_I) == final_init_I)[0][0] + 1

print("final_init_I: ", final_init_I)
print("final_page_rank: ", final_page_rank)

# Code ends here

max_stars_repo_name: tripidhoble/nlp-dl-prework

max_issues_repo_name: tripidhoble/nlp-dl-prework

max_forks_repo_name: tripidhoble/nlp-dl-prework


text:
import numpy as np

from probatus.stat_tests import ks, es

def test_verbosity_true_(capsys):
    d1 = np.random.normal(size=1000)
    d2 = d1
    ks(d1, d2, verbose=True)
    captured = capsys.readouterr()
    assert captured.out == "\nKS: pvalue = 1.0\n\nKS: Null hypothesis cannot be rejected. Distributions not statistically different.\n"
    es(d1, d2, verbose=True)
    captured = capsys.readouterr()
    assert captured.out == "\nES: pvalue = 1.0\n\nES: Null hypothesis cannot be rejected. Distributions not statistically different.\n"

def test_verbosity_false(capsys):
    d1 = np.random.normal(size=1000)
    d2 = d1
    ks(d1, d2, verbose=False)
    captured = capsys.readouterr()
    assert captured.out == ""

max_stars_repo_name: anilkumarpanda/probatus

max_issues_repo_name: anilkumarpanda/probatus

max_forks_repo_name: anilkumarpanda/probatus


text:
import numpy as np
class Neural_Network(object):
	def __init__(self, tamInput, tamOutput, tamCamadaEsc, lambdaVal):
		# Definindo os metaparâmetros da rede
		self.tamInput = tamInput
		self.tamOutput = tamOutput
		self.tamCamadaEsc = tamCamadaEsc
		self.lambdaVal = lambdaVal

		# Inicializando os pesos da rede: W1 - Pesos da primeira camada (entrada) / W2 - Pesos da segunda camada (Saída)
		self.W1 = np.random.randn(self.tamInput, self.tamCamadaEsc)

		self.W2 = np.random.randn(self.tamCamadaEsc, self.tamOutput)

		self.bias = np.random.randn(self.tamCamadaEsc)
		self.a = -0.9

	def sigmoide(self, z):
		#Função de ativação - Sigmóide -
		return 1/(1+np.exp(-z))

	def derivadaSigmoide(self, z):
		return np.exp(-z)/(np.power(1+np.exp(-z), 2))

	def prelu(self, z):
		y = [self.a * z_i if z_i < 0 else z_i for z_i in z]
		y = np.matrix(np.ravel(y))
		return y

	def derivadaPrelu(self, z):
		derivadas = [self.a if z_i < 0 else 1 for z_i in z]
		derivadas = np.matrix(np.ravel(derivadas))
		return derivadas

	def funcaoCusto(self, x, y):
		# Calcula o erro para uma entrada X e o y real
		self.yEstimado = self.propaga(x)
		matrix_y = np.matrix(list(y.values()))
		matrix_x = np.matrix(list(x.values()))
		dif = matrix_y-self.yEstimado
		J = ((np.sum(np.power(dif, 2))) * 0.5)/matrix_x.shape[0] + (self.lambdaVal/2) * (np.sum(np.power(self.W1,2)) + np.sum(np.power(self.W2,2)))
		return J

	def derivadaCusto(self, x, y):
		# Calcula a derivada em função de W1 e W2
		self.yEstimado = self.propaga(x)
		matrix_x = np.matrix(list(x.values()))
		if(self.tamInput > 1):
			matrix_x = matrix_x.T
		matrix_y = np.matrix(list(y.values()))
		# erro a ser retropropagado
		ek = -np.subtract(matrix_y, self.yEstimado)
		'''
		print("Erro k:")
		print(ek.shape)
		print(ek)
		print("Derivada sigmoid YIN : ", self.derivadaSigmoide(self.yin).shape)
		print(self.derivadaSigmoide(self.yin))
		'''
		delta3 = np.multiply(ek, self.derivadaPrelu(self.yin))#self.derivadaSigmoide(self.yin))
		# Obtém o erro a ser retropropagado de cada camada, multiplicando pela derivada da função de ativação
		#adicionando o termo de regularização no gradiente (+lambda * pesos)
		dJdW2 = np.dot(delta3, self.zin) + self.lambdaVal*self.W2
		'''
		print("dJdW2 ------------ ", dJdW2.shape)
		print(dJdW2)
		print("Z shape:", self.z.shape)
		print(self.z)
		print("Derivada Z shape:", self.derivadaSigmoide(self.z).shape)
		print(self.derivadaSigmoide(self.z))
		print("W2 shape", self.W2.shape)
		print(self.W2)
		'''
		delta2 = np.multiply(np.dot(self.W2, delta3).T, self.derivadaSigmoide(self.z))
		dJdW1 = np.dot(matrix_x, delta2) + self.lambdaVal*self.W1
		return dJdW1, dJdW2

	# propaga as entradas através da estrutura da rede
	# entradas é um dicionário
	def propaga(self, entradas):
		X = np.matrix(list(entradas.values()))
		if(self.tamInput > 1):
			X = X.T
		# multiplica a matriz de entradas "x" pela de pesos "w1"
		self.z = np.dot(X.T, self.W1)
		# função de ativação
		self.zin = self.sigmoide(self.z)

		# multiplica a saída da camada do meio pelos pesos da ultima camada
		self.yin = np.dot(self.zin, self.W2)

		# aplica a função de ativação do neuronio de saida
		yEstimado = self.prelu(self.yin) ##self.sigmoide(self.yin)
		return yEstimado

	# funções auxiliares

	def getParams(self):
		# concatena os pesos das camadas em um vetor
		# params = np.concatenate((self.W1.ravel(),self.W2.ravel()))
		return self.W1.ravel(), self.W2.ravel()

	def getBias(self):
		return self.bias.ravel()
	def getError(self):
		return self.ek

	def setParams(self, params):
		# ajusta os pesos de acordo com o vetor de pesos passado
		W1_inicio = 0
		W1_fim = self.tamCamadaEsc * self.tamInput
		self.W1 = np.reshape(params[W1_inicio:W1_fim], (self.tamInput, self.tamCamadaEsc))
		W2_fim = W1_fim + self.tamCamadaEsc * self.tamOutput
		self.W2 = np.reshape(params[W1_fim:W2_fim], (self.tamCamadaEsc, self.tamOutput))

	def setBias(self, bias):
		self.bias = bias

	def computaGradientes(self, x, y):
		dJdW1, dJdW2 = self.derivadaCusto(x, y)
		return np.concatenate((dJdW1.ravel(), dJdW2.ravel()))

max_stars_repo_name: tuany/RNN

max_issues_repo_name: tuany/RNN

max_forks_repo_name: tuany/RNN


text:
import numpy as np
from thor_client import ThorClient
from hartmann import hartmann_6 as obj

# Create experiment.
tc = ThorClient()
# A boolean variable to determine whether or not we should use an integrated
# acquisition function.
integrated = False
# Create space.
dims = [
    {"name": "x1", "dim_type": "linear", "low": 0., "high": 1.},
    {"name": "x2", "dim_type": "linear", "low": 0., "high": 1.},
    {"name": "x3", "dim_type": "linear", "low": 0., "high": 1.},
    {"name": "x4", "dim_type": "linear", "low": 0., "high": 1.},
    {"name": "x5", "dim_type": "linear", "low": 0., "high": 1.},
    {"name": "x6", "dim_type": "linear", "low": 0., "high": 1.},
]
name = "Hartmann 6-D (Integrated - {})".format(integrated)
exp = tc.create_experiment(name, dims, overwrite=True)

# Main optimization loop.
for i in range(100):
    # Request new recommendation.
    if integrated:
        recs = [exp.create_recommendation() for _ in range(5)]
    else:
        recs = exp.create_recommendation(integrate_acq=False)
    for r in recs:
        x = r.config
        # Evaluate new recommendation.
        val = obj(np.array([
            x["x1"], x["x2"], x["x3"], x["x4"], x["x5"], x["x6"]
        ]))
        # Submit recommendation.
        r.submit_recommendation(val)

max_stars_repo_name: JamesBrofos/Thor-Python-Client

max_issues_repo_name: JamesBrofos/Thor-Python-Client

max_forks_repo_name: JamesBrofos/Thor-Python-Client


text:
from bs4 import BeautifulSoup
from urllib.request import urlopen
from pprint import pprint
from matplotlib import pyplot as plt
import numpy as np

site = "https://en.wikipedia.org/wiki/List_of_countries_by_carbon_dioxide_emissions"

html = urlopen(site)
soup = BeautifulSoup(html, 'html.parser')
# print(soup)

# tb = soup.find_all("tbody")
# print(tb)
# pprint(tb[1])

# tb1 = soup.find_all('table')[1].find_all('tr')
tb1 = soup.find_all('tbody')[1].find_all('tr')
# c = 0
emission_percentage = []
e_p = []
for row in tb1[5:]:
    # print(row)
    # print("!!!!!!!!!!")
    print(row.find_all('td')[0].text, row.find_all("td")[4].text)
    # print(row.find_all("td")[4].text)
    # print("#################")
    emission_percentage.append((row.find_all('td')[0].text.strip(), float(row.find_all("td")[4].text[:-1])))
    e_p.append(float(row.find_all("td")[4].text[:-1]))

print(len(emission_percentage))
emission_percentage.sort(key=lambda x: x[1], reverse=True)
print(emission_percentage[0:10])
print(e_p)

data = [i[1] for i in emission_percentage[0:10]]
lab = [i[0] for i in emission_percentage[0:10]]
print(data)
print(lab)
sizes = np.array(data)

def absolute_value(val):
    a = np.round(val/100.*sizes.sum(), 2)
    return a

fig = plt.figure(figsize =(10, 7))
# plt.pie(data, labels=lab, autopct='%1.2f%%')
# plt.pie(data, labels=lab, autopct=absolute_value, shadow=True)
plt.pie(data, labels=lab, autopct=absolute_value)
# plt.pie(data)
plt.show()

# print(sorted(emission_percentage, reverse=True)[0:10])

    # c += 1
    # if c > 5:
    #     break
# print(tb1)

# for tb in soup.find_all("tbody"):
#     print(tb)


text:
from __future__ import division

import numpy as np
import unittest

import chainer
from chainer import testing
from chainer.testing import attr

from chainercv.links.model.fpn import Head
from chainercv.links.model.fpn import head_loss_post
from chainercv.links.model.fpn import head_loss_pre

def _random_array(xp, shape):
    return xp.array(
        np.random.uniform(-1, 1, size=shape), dtype=np.float32)

@testing.parameterize(
    {'n_class': 1 + 1},
    {'n_class': 5 + 1},
    {'n_class': 20 + 1},
)
class TestHead(unittest.TestCase):

    def setUp(self):
        self.link = Head(n_class=self.n_class, scales=(1 / 2, 1 / 4, 1 / 8))

    def _check_call(self):
        hs = [
            chainer.Variable(_random_array(self.link.xp, (2, 64, 32, 32))),
            chainer.Variable(_random_array(self.link.xp, (2, 64, 16, 16))),
            chainer.Variable(_random_array(self.link.xp, (2, 64, 8, 8))),
        ]
        rois = [
            self.link.xp.array(((4, 1, 6, 3),), dtype=np.float32),
            self.link.xp.array(
                ((0, 1, 2, 3), (5, 4, 10, 6)), dtype=np.float32),
            self.link.xp.array(((10, 4, 12, 10),), dtype=np.float32),
        ]
        roi_indices = [
            self.link.xp.array((0,), dtype=np.int32),
            self.link.xp.array((1, 0), dtype=np.int32),
            self.link.xp.array((1,), dtype=np.int32),
        ]

        locs, confs = self.link(hs, rois, roi_indices)

        self.assertIsInstance(locs, chainer.Variable)
        self.assertIsInstance(locs.array, self.link.xp.ndarray)
        self.assertEqual(locs.shape, (4, self.n_class, 4))

        self.assertIsInstance(confs, chainer.Variable)
        self.assertIsInstance(confs.array, self.link.xp.ndarray)
        self.assertEqual(confs.shape, (4, self.n_class))

    def test_call_cpu(self):
        self._check_call()

    @attr.gpu
    def test_call_gpu(self):
        self.link.to_gpu()
        self._check_call()

    def _check_distribute(self):
        rois = self.link.xp.array((
            (0, 0, 10, 10),
            (0, 1000, 0, 1000),
            (0, 0, 224, 224),
            (100, 100, 224, 224),
        ), dtype=np.float32)
        roi_indices = self.link.xp.array((0, 1, 0, 0), dtype=np.int32)

        rois, roi_indices = self.link.distribute(rois, roi_indices)

        self.assertEqual(len(rois), 3)
        self.assertEqual(len(roi_indices), 3)
        for l in range(3):
            self.assertIsInstance(rois[l], self.link.xp.ndarray)
            self.assertIsInstance(roi_indices[l], self.link.xp.ndarray)

            self.assertEqual(rois[l].shape[0], roi_indices[l].shape[0])
            self.assertEqual(rois[l].shape[1:], (4,))
            self.assertEqual(roi_indices[l].shape[1:], ())

        self.assertEqual(sum(rois[l].shape[0] for l in range(3)), 4)

    def test_distribute_cpu(self):
        self._check_distribute()

    @attr.gpu
    def test_distribute_gpu(self):
        self.link.to_gpu()
        self._check_distribute()

    def _check_decode(self):
        rois = [
            self.link.xp.array(((4, 1, 6, 3),), dtype=np.float32),
            self.link.xp.array(
                ((0, 1, 2, 3), (5, 4, 10, 6)), dtype=np.float32),
            self.link.xp.array(((10, 4, 12, 10),), dtype=np.float32),
        ]
        roi_indices = [
            self.link.xp.array((0,), dtype=np.int32),
            self.link.xp.array((1, 0), dtype=np.int32),
            self.link.xp.array((1,), dtype=np.int32),
        ]
        locs = chainer.Variable(_random_array(
            self.link.xp, (4, self.n_class, 4)))
        confs = chainer.Variable(_random_array(
            self.link.xp, (4, self.n_class)))

        bboxes, labels, scores = self.link.decode(
            rois, roi_indices,
            locs, confs,
            (0.4, 0.2), ((100, 100), (200, 200)),
            0.5, 0.1)

        self.assertEqual(len(bboxes), 2)
        self.assertEqual(len(labels), 2)
        self.assertEqual(len(scores), 2)
        for n in range(2):
            self.assertIsInstance(bboxes[n], self.link.xp.ndarray)
            self.assertIsInstance(labels[n], self.link.xp.ndarray)
            self.assertIsInstance(scores[n], self.link.xp.ndarray)

            self.assertEqual(bboxes[n].shape[0], labels[n].shape[0])
            self.assertEqual(bboxes[n].shape[0], scores[n].shape[0])
            self.assertEqual(bboxes[n].shape[1:], (4,))
            self.assertEqual(labels[n].shape[1:], ())
            self.assertEqual(scores[n].shape[1:], ())

    def test_decode_cpu(self):
        self._check_decode()

    @attr.gpu
    def test_decode_gpu(self):
        self.link.to_gpu()
        self._check_decode()

class TestHeadLoss(unittest.TestCase):

    def _check_head_loss_pre(self, xp):
        rois = [
            xp.array(((4, 1, 6, 3),), dtype=np.float32),
            xp.array(
                ((0, 1, 2, 3), (5, 4, 10, 6)), dtype=np.float32),
            xp.array(((10, 4, 12, 10),), dtype=np.float32),
        ]
        roi_indices = [
            xp.array((0,), dtype=np.int32),
            xp.array((1, 0), dtype=np.int32),
            xp.array((1,), dtype=np.int32),
        ]

        bboxes = [
            xp.array(((2, 4, 6, 7), (1, 12, 3, 30)), dtype=np.float32),
            xp.array(((10, 2, 12, 12),), dtype=np.float32),
        ]
        labels = [
            xp.array((10, 4), dtype=np.float32),
            xp.array((1,), dtype=np.float32),
        ]
        rois, roi_indices, gt_locs, gt_labels = head_loss_pre(
            rois, roi_indices,  (0.1, 0.2), bboxes, labels)

        self.assertEqual(len(rois), 3)
        self.assertEqual(len(roi_indices), 3)
        self.assertEqual(len(gt_locs), 3)
        self.assertEqual(len(gt_labels), 3)
        for l in range(3):
            self.assertIsInstance(rois[l], xp.ndarray)
            self.assertIsInstance(roi_indices[l], xp.ndarray)
            self.assertIsInstance(gt_locs[l], xp.ndarray)
            self.assertIsInstance(gt_labels[l], xp.ndarray)

            self.assertEqual(rois[l].shape[0], roi_indices[l].shape[0])
            self.assertEqual(rois[l].shape[0], gt_locs[l].shape[0])
            self.assertEqual(rois[l].shape[0], gt_labels[l].shape[0])
            self.assertEqual(rois[l].shape[1:], (4,))
            self.assertEqual(roi_indices[l].shape[1:], ())
            self.assertEqual(gt_locs[l].shape[1:], (4,))
            self.assertEqual(gt_labels[l].shape[1:], ())

    def test_head_loss_pre_cpu(self):
        self._check_head_loss_pre(np)

    @attr.gpu
    def test_head_loss_pre_gpu(self):
        import cupy
        self._check_head_loss_pre(cupy)

    def _check_head_loss_post(self, xp):
        locs = chainer.Variable(_random_array(xp, (20, 81, 4)))
        confs = chainer.Variable(_random_array(xp, (20, 81)))
        roi_indices = [
            xp.random.randint(0, 2, size=5).astype(np.int32),
            xp.random.randint(0, 2, size=7).astype(np.int32),
            xp.random.randint(0, 2, size=8).astype(np.int32),
        ]
        gt_locs = [
            _random_array(xp, (5, 4)),
            _random_array(xp, (7, 4)),
            _random_array(xp, (8, 4)),
        ]
        gt_labels = [
            xp.random.randint(0, 80, size=5).astype(np.int32),
            xp.random.randint(0, 80, size=7).astype(np.int32),
            xp.random.randint(0, 80, size=8).astype(np.int32),
        ]

        loc_loss, conf_loss = head_loss_post(
            locs, confs, roi_indices, gt_locs, gt_labels, 2)

        self.assertIsInstance(loc_loss, chainer.Variable)
        self.assertIsInstance(loc_loss.array, xp.ndarray)
        self.assertEqual(loc_loss.shape, ())

        self.assertIsInstance(conf_loss, chainer.Variable)
        self.assertIsInstance(conf_loss.array, xp.ndarray)
        self.assertEqual(conf_loss.shape, ())

    def test_head_loss_post_cpu(self):
        self._check_head_loss_post(np)

    @attr.gpu
    def test_head_loss_post_gpu(self):
        import cupy
        self._check_head_loss_post(cupy)

testing.run_module(__name__, __file__)

max_stars_repo_name: souravsingh/chainercv

max_issues_repo_name: souravsingh/chainercv

max_forks_repo_name: souravsingh/chainercv

reason: import numpy,import cupy


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

import math

from chempy.chemistry import Equilibrium
from chempy.util._expr import Expr
from chempy.util.testing import requires
from chempy.units import (
    allclose,
    units_library,
    Backend,
    default_units as du,
    default_constants as dc,
)

from ..expressions import MassActionEq, GibbsEqConst

@requires("sympy")
def test_MassActionEq_symbolic():
    import sympy as sp

    K, A, B, C = sp.symbols("K A B C")
    mae = MassActionEq([K])
    eq = Equilibrium({"A"}, {"B", "C"})
    expr = mae.equilibrium_equation({"A": A, "B": B, "C": C}, equilibrium=eq)
    assert expr - K + B * C / A == 0

def test_GibbsEqConst():
    R, T = 8.314, 298.15
    dH, dS = -4e3, 16
    gee = GibbsEqConst([dH / R, dS / R])
    ref = math.exp(-(dH - T * dS) / (R * T))
    assert abs((gee({"temperature": T}) - ref) / ref) < 1e-14

def _gibbs(args, T, R, backend):
    H, S, Cp, Tref = args
    H2 = H + Cp * (T - Tref)
    S2 = S + Cp * backend.log(T / Tref)
    return backend.exp(-(H2 - T * S2) / (R * T))

def test_custom_gibbs():
    R, T = 8.314, 298.15
    dH, dS = -4e3, 16
    MyGibbs = MassActionEq.from_callback(
        _gibbs,
        parameter_keys=("temperature", "R"),
        argument_names=("H", "S", "Cp", "Tref"),
    )
    dCp = 123.45
    Tref = 242
    gee2 = MyGibbs([dH, dS, dCp, Tref])
    dH2 = dH + dCp * (T - Tref)
    dS2 = dS + dCp * math.log(T / Tref)
    ref2 = math.exp(-(dH2 - T * dS2) / (R * T))
    assert abs((gee2.eq_const({"temperature": T, "R": R}) - ref2) / ref2) < 1e-14

def test_GibbsEqConst__unique_keys():
    R, T = 8.314, 298.15
    dH, dS = -4e3, 16
    gee = GibbsEqConst(unique_keys=("dH1", "dS1"))
    ref = math.exp(-(dH - T * dS) / (R * T))
    assert (
        abs(
            (gee.eq_const({"temperature": T, "dH1": dH / R, "dS1": dS / R}) - ref) / ref
        )
        < 1e-14
    )

@requires("sympy")
def test_GibbsEqConst__latex():
    import sympy

    DH, DS, R, T = sympy.symbols(r"\Delta\ H \Delta\ S R T")
    gee = GibbsEqConst([DH / R, DS / R])
    res = gee.eq_const({"temperature": T}, backend=sympy)
    ref = sympy.exp(-(DH - T * DS) / (R * T))
    assert (res - ref).simplify() == 0

@requires(units_library)
def test_GibbsEqConst__units():
    R, T = dc.molar_gas_constant, 298.15 * du.K
    DH = -4e3 * du.J / du.mol
    DS = 16 * du.J / du.K / du.mol
    be = Backend()
    gee = GibbsEqConst([DH / R, DS / R])
    res = gee.eq_const({"temperature": T}, backend=be)
    ref = be.exp(-(DH - T * DS) / (R * T))
    assert allclose(res, ref)

@requires(units_library)
def test_GibbsEqConst__nested():
    class TExpr(Expr):
        argument_names = ("heat_capacity",)
        parameter_keys = ("energy",)

        def __call__(self, variables, backend=None):
            (heat_capacity,) = self.all_args(variables, backend=backend)
            (energy,) = self.all_params(variables, backend=backend)
            return energy / heat_capacity

    R = 8.314 * du.J / du.K / du.mol
    T = TExpr([10.0 * du.J / du.K])
    dH, dS = -4e3 * du.J / du.mol, 16 * du.J / du.K / du.mol
    gee = GibbsEqConst([dH / R, dS / R])
    be = Backend()
    Tref = 298.15 * du.K
    ref = be.exp(-(dH - Tref * dS) / (R * Tref))
    assert (
        be.abs(
            (
                gee.eq_const({"energy": 2981.5 * du.J, "temperature": T}, backend=be)
                - ref
            )
            / ref
        )
        < 1e-14
    )

max_stars_repo_name: bertiewooster/chempy

max_issues_repo_name: bertiewooster/chempy

max_forks_repo_name: bertiewooster/chempy

reason: import sympy


text:
"""
Split data to train, dev, test
"""
import sys
import os
import pandas as pd
import numpy as np
import argparse

from speech_augmentation import create_augment_wav_files

def create_files(args, df, directory, train=False):
    text_lines, scp_lines, utt2spk_lines = [], [], []
    human_annotation_lines = []

    for idx, row in df.iterrows():
        # skip audio files that are too long
        if row["長短"] > max_audio_length:
            continue

        if train and args.speech_aug:
            spk_id = row["講者"]
        else:
            # suisiann only has one speaker
            # we are not augmenting for dev and test set
            spk_id = "spk001"

        wav_file_path = os.path.join(data_dir, row["音檔"])
        utt_id = row["音檔"].split("/")[-1].split(".")[0]
        # add speaker-ids prefixes of utt-ids
        utt_id = spk_id+utt_id
        transcription = row[text_column_name]
        transcription = transcription.replace('"', '""')
        tai_han = row["漢字"]

        # skipping empty sentences
        if transcription:
            text_lines.append(f"{utt_id} {transcription}\n")

            scp_lines.append(f"{utt_id} {wav_file_path}\n")

            utt2spk_lines.append(f"{utt_id} {spk_id}\n")

            human_annotation_lines.append(f"{utt_id},{wav_file_path},\"{transcription}\",\"{tai_han}\",\n")

    # sort
    text_lines.sort()
    scp_lines.sort()
    utt2spk_lines.sort()

    # write to file
    with open(f"{directory}/text", "w+") as text_file:
        text_file.writelines(text_lines)

    with open(f"{directory}/wav.scp", "w+") as scp_file:
        scp_file.writelines(scp_lines)

    with open(f"{directory}/utt2spk", "w+") as utt2spk_file:
        utt2spk_file.writelines(utt2spk_lines)

    with open(f"{directory}/human_annotation.csv", "w+") as f:
        f.writelines(human_annotation_lines)

def combine_indices_to_same_line(pred_filepath, index_filepath, gold_length):
    with open(pred_filepath) as f:
        pred_lines = f.readlines()
    with open(index_filepath) as f:
        index_lines = f.readlines()

    outputs = [""] * gold_length
    for pred_line, idx in zip(pred_lines, index_lines):
        idx = int(idx)

        outputs[idx] += pred_line.strip()

    return outputs

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--data_dir', default='downloads/0.2.1',
                        help='path to SuiSiann data directory')
    parser.add_argument('--output_text', default='tailo',
                        choices = ["tailo", "cmn"],
                        help='ASR outout script')
    parser.add_argument('--speech_aug', action='store_true',
                        help="add data augmentation: pitch shift")

    # pseudo label related
    parser.add_argument('--pseudo_label', action='store_true',
                        help="read from MT output as label")

    args = parser.parse_args()

    train_size = 0.9
    dev_size = 0.05
    random_state = 11737

    max_audio_length = 20

    # data_dir = sys.argv[1]  # downloads/0.2.1
    # output_text = sys.argv[2] # tailo or cmn
    # speech_aug = bool(sys.argv[3])

    data_dir = args.data_dir
    output_text = args.output_text

    spk_id = "spk001"

    if output_text == "tailo":
        text_column_name = "羅馬字"
    elif output_text == "cmn":
        text_column_name = "漢字"
    else:
        raise NotImplementedError

    df = pd.read_csv(os.path.join(data_dir, "SuiSiann.csv"))

    if args.pseudo_label:
        print("Read pseudo labels from MT output")
        MT_dir = "/home/ubuntu/Taiwanese_ASR_MT/MT"
        suisiann_pred_filepath = os.path.join(MT_dir, "checkpoints/icorpus_nan_spm8000/nan_cmn/suisiann_b5.pred")
        suisiann_index_filepath = os.path.join(MT_dir, "data/suisiann_raw/nan_cmn/all.orig.nan.id")

        pseudo_lines = combine_indices_to_same_line(suisiann_pred_filepath, suisiann_index_filepath, len(df))

        # add pseudo_lines to dataframe
        assert len(df) == len(pseudo_lines), f"length of SuiSiann({len(df)}) doesn't match MT output({len(pseudo_lines)})"
        df["pseudo_cmn"] = pseudo_lines

        # changing the text column name
        text_column_name = "pseudo_cmn"

    train_df, dev_df, test_df = \
                np.split(df.sample(frac=1, random_state=random_state),
                        [int(train_size*len(df)),
                        int((train_size+dev_size)*len(df))])

    if args.speech_aug:
        aug_file = os.path.join(data_dir, "SuiSiann_aug.csv")

        if os.path.exists(aug_file):
            print("data augmentation files already exist, reading from csv file:", aug_file)
            aug_train_df = pd.read_csv(aug_file)

        else:
            pitch_factors = [-1, -2, -3]
            aug_wav_dir = os.path.join(data_dir, "augmented")

            if not os.path.isdir(aug_wav_dir):
                print(f"Create directory:{aug_wav_dir}")
                os.makedirs(aug_wav_dir)

            aug_train_df = create_augment_wav_files(train_df, pitch_factors, data_dir, aug_wav_dir, spk_id=spk_id)

            # save df to csv
            aug_train_df.to_csv(aug_file, index=False)
            print(f"Finished data augmentation, augmented wav files saved at {aug_wav_dir}")
            print("Data augmentation csv saved at", aug_file)

    print(
        f"# train: {len(train_df)}, # dev:{len(dev_df)}, # test:{len(test_df)}"
    )

    print("Creating files for train...", end="")
    if args.speech_aug:
        create_files(args, aug_train_df, "data/train", train=True)
    else:
        create_files(args, train_df, "data/train", train=True)
    print("Done.")

    print("Creating files for dev...", end="")
    create_files(args, dev_df, "data/dev")
    print("Done.")

    print("Creating files for test...", end="")
    create_files(args, test_df, "data/test")
    print("Done.")

max_stars_repo_name: yunhsuanchen/espnet

max_issues_repo_name: yunhsuanchen/espnet

max_forks_repo_name: yunhsuanchen/espnet


text:
import streamlit as st
import pandas as pd
import numpy as np
import seaborn as sns
import time
import matplotlib.pyplot as plt
import altair as alt

st.set_option("deprecation.showPyplotGlobalUse", False)

# Creating a simple registration form (basics)

st.title("Learning Streamlit")

st.subheader("Creating a simple Registraion Form")

first, last = st.columns(2)

first.text_input("First Name")
last.text_input("Last Name")

email, mobile = st.columns([3, 1])

email.text_input("Email")
mobile.text_input("Mobile")

username, password, retypePassword = st.columns([3, 2, 2])

username.text_input("Username")
password.text_input("Password", type="password")
retypePassword.text_input("Retype Password", type="password")

checkbox, blank, submit_button = st.columns(3)

checkbox.checkbox("I agree to the terms and conditions")
submit_button.button("Submit")


text:
import rospy
from sensor_msgs.msg import JointState
from roboy_middleware_msgs.msg import MotorCommand, JointStatus
from roboy_middleware_msgs.srv import ControlMode, ControlModeRequest

from math import tanh, sqrt, sin
import numpy as np

def softmax(x):
    return 1.0/(1+np.exp(-x))

rospy.init_node("musctest")

p = rospy.Publisher('/roboy/middleware/MotorCommand', MotorCommand, queue_size=1)
rospy.loginfo("Changing control mode to DISPLACEMENT")
change_control_mode = rospy.ServiceProxy('/roboy/shoulder_left/middleware/ControlMode', ControlMode)
req = ControlModeRequest()
req.motor_id = [0,1,2,3]
req.control_mode = 2 # DISPLACEMENT
change_control_mode(req)
rospy.loginfo("Changed to DISPLACEMENT control mode")
max_cmd = [40,10]

msg = MotorCommand()
msg.id = 3

def encoderticks2degrees(ticks):
    return ticks*(360.0/4096.0)

center = [2673.5, 2539.0]
motors_map = {0: [0,1], 1: [2,3]}

def cb2(data):
    angles = [] # in degrees [lower, upper]
    for i in [0,1]: #range(len(center)):
        encoderticks = data.absAngles[i] - center[i]
        angle = encoderticks2degrees(encoderticks)
        angles.append(angle)
        motors = motors_map[i]
        msg.motors.extend(motors)
        if angle<0:
            msg.set_points.extend([abs(sin(angle)*max_cmd[i]),0])
        else:
            msg.set_points.extend([0, abs(sin(angle)*max_cmd[i])])

    p.publish(msg)
    #rospy.loginfo("Current joint angle values: " + str(angles))
    #rospy.loginfo("Published MotorCommand: " + str(msg))
    msg.set_points = []
    msg.motors = []

def cb(data):
    msg.set_points = [0,0,0,0]

    if data.position[1]>0:
        msg.set_points[1] = sin(data.position[1])*max_cmd
    else:
        msg.set_points[0] = sin(abs(data.position[1]))*max_cmd

    if data.position[0]>0:
        msg.set_points[2] = sin(data.position[0])*5
    else:
        msg.set_points[3] = sin(abs(data.position[0]))*5
    p.publish(msg)

s_hw = rospy.Subscriber('/roboy/middleware/JointStatus', JointStatus, cb2, buff_size=1)
s = rospy.Subscriber('/joint_states', JointState, cb)
rospy.spin()


text:
import numpy as np

from MetaStruct.Objects.Shapes.Shape import Shape
from MetaStruct.Objects.Shapes.Sphere import Sphere

class HollowSphere(Shape):

    def __init__(self, design_space, x=0, y=0, z=0, r=1, t=0.3):

        self.designSpace = design_space

        super().__init__(self.designSpace, x, y, z)

        self.r = r
        self.t = t
        self.set_limits()

    def set_limits(self):

        self.x_limits = np.array(
            [self.x - self.r, self.x + self.r])
        self.y_limits = np.array(
            [self.y - self.r, self.y + self.r])
        self.z_limits = np.array(
            [self.z - self.r, self.z + self.r])

    def __repr__(self):

        return f'{self.__class__.__name__}({self.x}, {self.y}, {self.z}, {self.r}, {self.t})'

    def __str__(self):

        return super().__str__() + f'\nRadius: {self.r}\nWall Thickness: {self.t}'

    def evaluate_point(self, x, y, z):

        ball = Sphere(self.designSpace, self.x, self.y, self.z, self.r) - \
            Sphere(self.designSpace, self.x, self.y, self.z, self.r -
                   self.t)

        return ball.evaluate_point(x, y, z)


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

GRAY_SCALE_RANGE = 255

import pickle

data_filename = 'data_deskewed.pkl'
print('Loading data from file \'' + data_filename + '\' ...')
with open(data_filename, 'rb') as f:
    train_labels = pickle.load(f)
    train_images = pickle.load(f)
    test_labels = pickle.load(f)
    test_images = pickle.load(f)
    num_pixel = pickle.load(f)
print('Data loading complete.')

train_images = np.array(train_images)
train_images.resize(train_images.size // num_pixel, num_pixel)
test_images = np.array(test_images)
test_images.resize(test_images.size // num_pixel, num_pixel)
test_labels = np.array(test_labels)
train_labels = np.array(train_labels)

## normalization
train_images = train_images / GRAY_SCALE_RANGE
test_images = test_images / GRAY_SCALE_RANGE

from sklearn import svm, metrics

# clf = svm.SVC(gamma = 0.001)
clf = svm.SVC(kernel = 'linear')

clf.fit(train_images[:1000], train_labels[:1000])

prediction = clf.predict(test_images)

print("Classification report for classifier %s:\n%s\n"
	  % (clf, metrics.classification_report(test_labels, prediction)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(test_labels, prediction))

max_stars_repo_name: Evensgn/MNIST-learning

max_issues_repo_name: Evensgn/MNIST-learning

max_forks_repo_name: Evensgn/MNIST-learning