text:
#!/usr/bin/env python

""" test - Test NumericCholesky on random (positive definite) matrices."""

import numpy as np
from diff_edgelists import diff
import cholesky
import random
import subprocess
import sys, os.path, tempfile, time

# Generate a test matrix
def randentry():
    """Generate a random entry for the matrix."""
    #return random.randint(1, 10)
    return random.random() + 0.5

def generate_matrix(size=10, density=0.1):
    """Generate a square symmetric positive definite matrix of given
    size and density. Discards highly conditioned matrices"""
    discarded = 0
    while True:
        # Generate matrix factor L, a lower triangular (positive) matrix
        factor = np.zeros((size, size))
        for i in range(size):
            factor[i][i] = randentry()
            for j in range(i):
                if random.random() < density:
                    factor[i][j] = randentry()
        # A=L*L'
        matrix = np.dot(factor, factor.T)

        # Verify that A is well-conditioned
        cond = np.linalg.cond(matrix)
        print "%d x %d [%f]: Condition number is %g" % \
            (size, size, density, cond)
        if cond < 1e10:
            if discarded > 0:
                time.sleep(1)
            return matrix, factor
        else:
            discarded += 1
            print "  -> Discarding matrix %d" % discarded

def generate_matrices(count=100, size_range=(2, 500)):
    for _ in range(count):
        # Random parameters for the matrix
        size = random.randint(size_range[0], size_range[1])
        density = random.random()*(8.0 if size > 20 else 0.5)/size + 0.0001
        assert(density < 0.5)
        print "Using size=%d, density=%f" % (size, density)

        cholmatfn = "matrix.L.tmp"
        matrixfn = "matrix.tmp"

        # Generate a random matrix
        matrix, factor = generate_matrix(size, density)
        cholesky.write_matrix(factor, cholmatfn)
        cholesky.write_matrix(matrix, matrixfn)
        yield (matrixfn, size, size, density)

def runcmd(cmd):
    """Run an external command and check the exit status."""
    print cmd
    status = subprocess.call(cmd)
    assert(status == 0)
    return status

def verify(items, check=lambda x: x == 0):
    """Verify that all elements of items are equal to correct_answer."""
    return check(items[0]) and len(set(items)) == 1

def run_test(app, matrixfn, status=None, do_verify=True):
    """Run a test on the given copy of NumericCholesky."""
    if not status:
        status = StatusBar(enabled=False)

    edgesfn = matrixfn + cholesky.EDGES_SUFFIX
    filledfn = matrixfn + cholesky.FILLED_SUFFIX
    depfn = matrixfn + cholesky.DEP_SUFFIX
    choleskyfn = matrixfn + cholesky.CHOLESKYEDGES_SUFFIX

    threadings = [1, 2, 4, 8]
    results = []
    status.push('')
    # Repeat for each ordering supported by cholesky.py
    for orderingname, ordering in cholesky.ORDERINGS.items():
        print "Using ordering: %s" % orderingname
        status.update('<%s>' % orderingname)
        status.push('cholesky.py')
        # Do symbolic elimination
        cholesky.do_cholesky(matrixfn, ordering=ordering)
        #status.update('graph-convert')
        #runcmd([graphconv, '-floatedgelist2gr', filledfn, grfn])

        # Test NumericCholesky, with several different numbers of threads
        for thr in threadings:
            if '/NumericCholesky' in app:
                status.update('NumericCholesky -t=%d' % thr)
                runcmd([app, '-t=%d' % thr, filledfn + '.gr', depfn])
            elif '/Cholesky' in app:
                status.update('Cholesky -t=%d' % thr)
                runcmd([app, '-ordering=%s' % orderingname, '-t=%d' % thr,
                        edgesfn + '.gr'])
            else:
                raise NotImplemented
            # Compare result
            status.update('Verifying...')
            results.append(diff((choleskyfn, 'choleskyedges.txt'), quick=True))
        status.pop() # Pop: Task for the given ordering

        # Check for any errors
        if not do_verify or verify(results):
            print "OK"
        else:
            print "Got bad results with %s on %s" % (ordering, threadings)
            print results
            raise Exception
    status.pop()     # Pop: ordering

def _main(argv):
    """Main function (entry point for command-line use)."""
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('app', metavar='APP',
                        help='Path to NumericCholesky binary')
    parser.add_argument('--iterations', type=int, default=100,
                        help='Number of iterations')
    parser.add_argument('--temp-dir', default=None,
                        help='Directory to store temporary files')
    parser.add_argument('--min-size', type=int, default=2,
                        help='Minimum matrix size to test')
    parser.add_argument('--max-size', type=int, default=500,
                        help='Maximum matrix size to test')
    parser.add_argument('--matrix', action='append',
                        help='Specific matrix to test')
    parser.add_argument('--no-verify', action='count',
                        help='Do not verify the result')
    args = parser.parse_args(argv)

    app = os.path.abspath(args.app)
    status = StatusBar()

    if args.matrix:
        args.matrix = [os.path.abspath(i) for i in args.matrix]

    # Change to a temporary directory
    if args.temp_dir:
        temp_dir = os.path.abspath(args.temp_dir)
    else:
        temp_dir = tempfile.mkdtemp()
    print 'TEMP_DIR = %s' % temp_dir
    os.chdir(temp_dir)

    # Get an iterator of the matrices to test
    if args.matrix:
        matrices = ((i,) for i in args.matrix)
        nmatrices = len(args.matrix)
    else:
        assert(args.min_size > 1 and args.min_size <= args.max_size)
        nmatrices = args.iterations
        matrices = generate_matrices(count=nmatrices,
                                     size_range=(args.min_size, args.max_size))

    # Test each matrix
    for i, matrix in enumerate(matrices):
        name = '%d/%d' % (i+1, nmatrices)
        if args.matrix:
            name += ' '+os.path.basename(matrix[0])
        print "="*79
        print name
        print "="*79
        status.push('[%s]' % name)
        if len(matrix) > 1:     # Display matrix info, if available
            status.push('[%dx%d/%.3f]' % matrix[1:])
        run_test(app, matrix[0], status=status, do_verify=not args.no_verify)
        if len(matrix) > 1:
            status.pop()        # Pop: matrix size
        status.pop()            # Pop: matrix name
    print "%d matrices OK" % (nmatrices)
    status.paint()

class StatusBar(object):
    """Display a status bar in the xterm titlebar"""

    def __init__(self, enabled=True):
        self.items = []
        self.enabled = False
        if not enabled:
            return
        supported_terms = ['xterm', 'rxvt', 'screen', 'cygwin']
        if 'TERM' in os.environ:
            for i in supported_terms:
                if os.environ['TERM'].startswith(i):
                    self.enabled = True
                    break

    def push(self, item):
        """Push an item onto the end of statusbar. Repaints the
        statusbar."""
        self.items.append(item)
        self.paint()

    def pop(self):
        """Remove the last item from the statusbar. Does not
        repaint the statusbar."""
        self.items.pop()

    def update(self, item):
        """Replace the last item of the statusbar with item. Repaints
        the statusbar."""
        self.pop()
        self.push(item)

    def paint(self):
        """Update the statusbar. Has no effect if not enabled
        (unsupported terminal type)."""
        if self.enabled:
            sys.stdout.write("\033]0;%s\007" % ' '.join(self.items))

if __name__ == '__main__':
    _main(sys.argv[1:])


text:
from .utils import PyKEArgumentHelpFormatter
from . import kepio, kepmsg, kepkey
import numpy as np
from astropy.io import fits as pyfits
from matplotlib import pyplot as plt

# global variables

instr = ''; cadence = 1800.0; barytime0 = 0
nrm = 1; barytime = []; flux = []; xmin = 0; xmax = 1
ymin = 0; ymax = 1; xr = 1; yr = 1; xlab = ''; ylab = ''
mask = []; aid = None; bid = None; cid = None; did = None; eid = None; fid = None
clobb = True; outf = ''; verb = True; logf = ''; rinf = ''

__all__ = ['keprange']

def keprange(infile, outfile=None, datacol='SAP_FLUX', rinfile='',
             overwrite=False, verbose=False, logfile='keprange.log'):
    """
    keprange -- Define time ranges interactively for use with other PyKE tasks.

    A number of PyKE tasks, e.g. kepdetrend, kepoutlier, require the user to
    specify ranges in time over which to operate. keprange provides a visual
    and interactive tool with which to define time ranges and store them within
    an ASCII file. Choices are made using a GUI. Use the left-button of your
    mouse to select ranges. An existing ASCII file can be loaded, a new ASCII
    file can be written, the list of times can be cleared or printed using the
    buttons on the GUI.

    Parameters
    ----------
    infile : str
        The name of a MAST standard format FITS file containing a Kepler light
        curve within the first data extension.
    outfile : str
        The name of the output ASCII file storing time ranges for future use in
        other PyKE tools.
    rinfile : str
        An existing ASCII file containing time ranges in Barycentric Julian
        Date (BJD) can be uploaded into the task. This can be used as a basis
        for a new set of time ranges. This argument is optional and is not
        prompted for automatically. If no ascii file will be input then
        rinfile=None will clear the argument buffer after a previous use.
    datacol : str
        The datacol name containing data stored within extension 1 of infile.
        This data will be plotted against time so that the user can choose
        appropriate time ranges.
    overwrite : bool
        Overwrite the output file?
    verbose : bool
        Print informative messages and warnings to the shell and logfile?
    logfile : str
        Name of the logfile containing error and warning messages.

    Examples
    --------
    .. code-block:: bash

        $ keprange kplr002436324-2009259160929_llc.fits --verbose

    .. image:: ../_static/images/api/keprange.png
        :align: center
    """

    if outfile is None:
        outfile = infile.split('.')[0] + "-{}.txt".format(__all__[0])

    # startup parameters
    global instr, cadence, barytime0, nrm, barytime, flux
    global xmin, xmax, ymin, ymax, xr, yr, xlab, ylab
    global clobb, outf, verb, logf, rinf, col, bjdref, cade

    # log the call
    hashline = '--------------------------------------------------------------'
    kepmsg.log(logfile, hashline, verbose)
    call = ('KEPRANGE -- '
            + ' infile={}'.format(infile)
            + ' outfile={}'.format(outfile)
            + ' rinfile={}'.format(rinfile)
            + ' datacol={}'.format(datacol)
            + ' overwrite={}'.format(overwrite)
            + ' verbose={}'.format(verbose)
            + ' logfile={}'.format(logfile))
    kepmsg.log(logfile, call+'\n', verbose)
    clobb = overwrite
    outf = outfile
    verb = verbose
    logf = logfile
    rinf = rinfile

    # start time
    kepmsg.clock('KEPRANGE started at: ', logfile, verbose)

    # overwrite output file
    if overwrite:
        kepio.overwrite(outfile, logfile, verbose)
    if kepio.fileexists(outfile):
        errmsg = 'ERROR -- KEPRANGE: {} exists. Use --overwrite'.format(outfile)
        kepmsg.err(logfile, errmsg, verbose)

    ## open input file
    instr = pyfits.open(infile, 'readonly')
    tstart, tstop, bjdref, cadence = kepio.timekeys(instr, infile,
                                                    logfile, verbose)
    try:
        work = instr[0].header['FILEVER']
        cadenom = 1.0
    except:
        cadenom = cadence
    cade = cadenom

    # fudge non-compliant FITS keywords with no values
    instr = kepkey.emptykeys(instr, infile, logfile, verbose)
    # input data
    table = instr[1].data

    # filter out NaNs
    work1 = []; work2 = []
    col = datacol
    barytime = kepio.readtimecol(infile, table, logfile, verbose)
    try:
        flux = instr[1].data.field(col)
    except:
        errmsg = ('ERROR -- KEPRANGE: no datacol named {} in table {} [1]'
                  .format(col, infile))
        kepmsg.err(infile, errmsg, verbose)
    for i in range(len(barytime)):
        if (np.isfinite(barytime[i]) and np.isfinite(flux[i])
            and flux[i] != 0.0):
            work1.append(barytime[i] + bjdref)
            work2.append(flux[i])
    barytime = np.array(work1, dtype=np.float64)
    flux = np.array(work2, dtype=np.float32) / cadenom

    # clean up x-axis unit
    barytime0 = float(int(tstart / 100) * 100.0)
    barytime = barytime - barytime0
    xlab = 'BJD $-$ {}'.format(barytime0)

    # clean up y-axis units
    nrm = len(str(int(flux.max()))) - 1
    flux = flux / 10 ** nrm
    ylab = '10$^%d$ e$^-$ s$^{-1}$' % nrm

    # data limits
    xmin = barytime.min()
    xmax = barytime.max()
    ymin = flux.min()
    ymax = flux.max()
    xr = xmax - xmin
    yr = ymax - ymin
    flux[0] = 0.0
    flux[-1] = 0.0

    # plot new light curve
    plt.rcParams['figure.dpi'] = 80
    plt.figure(figsize=[17, 7])
    plotlc()

def plotlc():
    global aid, bid, cid, did, eid, fid, mask

    # load button
    plt.clf()
    plt.axes([0.06, 0.02, 0.22, 0.1])
    plt.text(0.5, 0.5, 'LOAD', fontsize=24, weight='heavy',
             horizontalalignment='center', verticalalignment='center')
    plt.setp(plt.gca(), xticklabels=[], xticks=[], yticklabels=[], yticks=[])
    plt.fill([0.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, 0.0], '#ffffff')
    plt.xlim(0.0, 1.0)
    plt.ylim(0.0, 1.0)
    aid = plt.connect('button_press_event', clicker1)

    # save button
    plt.axes([0.2933, 0.02, 0.22, 0.1])
    plt.text(0.5, 0.5, 'SAVE', fontsize=24, weight='heavy',
             horizontalalignment='center', verticalalignment='center')
    plt.setp(plt.gca(), xticklabels=[], xticks=[], yticklabels=[], yticks=[])
    plt.fill([0.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, 0.0], '#ffffff')
    plt.xlim(0.0, 1.0)
    plt.ylim(0.0, 1.0)
    bid = plt.connect('button_press_event', clicker2)

    # clear button
    plt.axes([0.5266, 0.02, 0.22, 0.1])
    plt.text(0.5, 0.5, 'CLEAR', fontsize=24, weight='heavy',
             horizontalalignment='center', verticalalignment='center')
    plt.setp(plt.gca(), xticklabels=[], xticks=[], yticklabels=[], yticks=[])
    plt.fill([0.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, 0.0], '#ffffff')
    plt.xlim(0.0, 1.0)
    plt.ylim(0.0, 1.0)
    cid = plt.connect('button_press_event', clicker3)

    # print button
    plt.axes([0.76, 0.02, 0.22, 0.1])
    plt.text(0.5, 0.5, 'PRINT', fontsize=24, weight='heavy',
             horizontalalignment='center', verticalalignment='center')
    plt.setp(plt.gca(), xticklabels=[], xticks=[], yticklabels=[], yticks=[])
    plt.fill([0.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, 0.0], '#ffffff')
    plt.xlim(0.0, 1.0)
    plt.ylim(0.0, 1.0)
    did = plt.connect('button_press_event', clicker4)

    # light curve
    plt.axes([0.06, 0.213, 0.92, 0.77])
    plt.gca().xaxis.set_major_formatter(plt.ScalarFormatter(useOffset=False))
    plt.gca().yaxis.set_major_formatter(plt.ScalarFormatter(useOffset=False))
    ltime = []; ldata = []
    work1 = instr[1].data.field(0) + bjdref
    work2 = instr[1].data.field(col) / cade
    for i in range(len(work1)):
        if np.isfinite(work1[i]) or np.isfinite(work2[i]):
            ltime.append(work1[i])
            ldata.append(work2[i])
        else:
            ltime = np.array(ltime, dtype=np.float64) - barytime0
            ldata = np.array(ldata, dtype=np.float64) / 10 ** nrm
            plt.scatter(ltime, ldata, color='#363636', marker='.' ,s=5,linewidth=1.0)
            ltime = []; ldata = []
    ltime = np.array(ltime, dtype=np.float64) - barytime0
    ldata = np.array(ldata, dtype=np.float64) / 10 ** nrm
    plt.scatter(ltime, ldata, color='#363636', marker='.', s=5, linewidth=1.0)
    plt.fill(barytime, flux, fc='#a8a7a7', linewidth=0.0, alpha=0.2)
    plt.xlabel(xlab, {'color' : 'k'})
    plt.ylabel(ylab, {'color' : 'k'})
    plt.grid(ls='--', alpha=0.3)

    # plt.plot masks
    for i in range(len(mask)):
        t = float(mask[i])
        plt.plot([t, t], [ymin, ymax], color='#c0392b', linestyle='-', linewidth=0.5)
    nt = 0
    for i in range(int(len(mask) / 2)):
        t1 = float(mask[nt])
        t2 = float(mask[nt + 1])
        nt += 2
        plt.fill([t1, t1, t2, t2, t1], [ymin, ymax, ymax, ymin, ymin],
                 fc='#c0392b', linewidth=0.0, alpha=0.5)
    # plot ranges
    plt.xlim(xmin - xr * 0.01, xmax + xr * 0.01)
    if ymin - yr * 0.01 <= 0.0:
        plt.ylim(1.0e-10, ymax + yr * 0.01)
    else:
        plt.ylim(ymin - yr * 0.01, ymax + yr * 0.01)

    # ranges
    eid = plt.connect('button_press_event', clicker6)

    # render plot
    plt.show()

def clicker1(event):

    global mask, aid, bid, cid, did, eid, fid

    if event.inaxes:
        if event.button == 1:
            if (event.x > 83 and event.x < 383 and
                event.y > 12 and event.y < 68):
                if kepio.fileexists(rinf):
                    mask = []
                    lines = kepio.openascii(rinf, 'r', logf, verb)
                    for line in lines:
                        line = line.strip().split(',')
                        try:
                            float(line[0])
                            float(line[1])
                            if barytime0 > 2.4e6:
                                mask.append(float(line[0]) - barytime0)
                                mask.append(float(line[1]) - barytime0)
                            else:
                                mask.append(float(line[0]) - barytime0 - 2.4e6)
                                mask.append(float(line[1]) - barytime0 - 2.4e6)
                        except:
                            message = 'ERROR -- KEPRANGE: ascii format of ranges '
                            message += 'file not recognized.'
                            kepmsg.err(logf, message, False)
                    plt.disconnect(aid)
                    plt.disconnect(bid)
                    plt.disconnect(cid)
                    plt.disconnect(did)
                    plt.disconnect(eid)
                    plt.disconnect(fid)
                    plotlc()
                else:
                    print('WARNING -- KEPRANGE: input ranges file does not'
                          ' exist or was not provided')
    return

# -----------------------------------------------------------
# save mask to ascii file

def clicker2(event):

    global mask, aid, bid, cid, did, eid, fid, clobb

    if clobb:
        kepio.overwrite(outf, logf, verb)
    if kepio.fileexists(outf):
        message = 'ERROR -- KEPRANGE: {} exists. Use --overwrite'.format(outf)
        kepmsg.err(logf, message, verb)
    else:
        if event.inaxes:
            if event.button == 1:
                if (event.x > 402 and event.x < 702 and
                    event.y > 12 and event.y < 68):
                    nt = 0; txt = ''
                    for i in range(int(len(mask)/2)):
                        t1 = float(mask[nt]) + barytime0
                        t2 = float(mask[nt+1]) + barytime0
                        if t1 < 2.4e6: t1 += 2.4e6
                        if t2 < 2.4e6: t2 += 2.4e6
                        txt += str(t1) + ',' + str(t2) + '\n'
                        nt += 2
                    txt = txt.strip()
                    kepmsg.log(outf, txt, True)
                    print('\nWrote ASCII file ' + outf)
                    plotlc()

# -----------------------------------------------------------
# clear time domain mask

def clicker3(event):

    global mask, aid, bid, cid, did, eid, fid

    if event.inaxes:
        if event.button == 1:
            if (event.x > 723 and event.x < 1022 and
                event.y > 12 and event.y < 68):
                mask = []
                plt.disconnect(aid)
                plt.disconnect(bid)
                plt.disconnect(cid)
                plt.disconnect(did)
                plt.disconnect(eid)
                plt.disconnect(fid)
                plotlc()

# -----------------------------------------------------------
# print time domain mask

def clicker4(event):

    global mask, aid, bid, cid, did, eid, fid

    if event.inaxes:
        if event.button == 1:
            if (event.x > 1042 and event.x < 1342 and
                event.y > 12 and event.y < 68):
                nt = 0; txt = ''
                for i in range(int(len(mask)/2)):
                    t1 = float(mask[nt]) + barytime0
                    t2 = float(mask[nt+1]) + barytime0
                    if t1 < 2.4e6: t1 += 2.4e6
                    if t2 < 2.4e6: t2 += 2.4e6
                    txt += str(t1) + ',' + str(t2) + '\n'
                    nt += 2
                txt = txt.strip()
                print('\n' + txt)
                plt.disconnect(aid)
                plt.disconnect(bid)
                plt.disconnect(cid)
                plt.disconnect(did)
                plt.disconnect(eid)
                plt.disconnect(fid)
                plotlc()

# -----------------------------------------------------------
# left-click create time ranges

def clicker6(event):

    global mask, aid, bid, cid, did, eid, fid

    if event.inaxes:
        if event.button == 1:
            if (event.x > 83 and event.x < 1337 and
                event.y > 122 and event.y < 558):
                if len(mask) % 2 == 0:
                    mask.append(event.xdata)
                else:
                    if event.xdata > mask[-1]:
                        mask.append(event.xdata)
                    else:
                        mask.append(mask[-1])
                        mask[-2] = event.xdata
                plotlc()

def keprange_main():
    import argparse
    parser = argparse.ArgumentParser(
             description=('Interactively define and store time ranges via a'
                          ' GUI'),
             formatter_class=PyKEArgumentHelpFormatter)
    parser.add_argument('infile', help='Name of input file', type=str)
    parser.add_argument('--outfile',
                        help=('Name of ASCII file to output time ranges.'
                              ' If None, outfile is infile-keprange.'),
                        default=None)
    parser.add_argument('--datacol', default='SAP_FLUX',
                        help='Name of diagnostic FITS column', type=str)
    parser.add_argument('--rinfile', default='',
                        help='Name of input ASCII time ranges file')
    parser.add_argument('--overwrite', action='store_true',
                        help='Overwrite output file?')
    parser.add_argument('--verbose', action='store_true',
                        help='Write to a log file?')
    parser.add_argument('--logfile', '-l', help='Name of ascii log file',
                        default='keprange.log', dest='logfile', type=str)
    args = parser.parse_args()
    keprange(args.infile, args.outfile, args.datacol, args.rinfile,
             args.overwrite, args.verbose, args.logfile)

ext: py

max_stars_repo_name: ecalifornica/pyke

max_issues_repo_name: ecalifornica/pyke

max_forks_repo_name: mirca/PyKE

reason: import numpy,from astropy


text:
# -*- coding: utf-8 -*-
# Copyright 2018-2019 Jacob M. Graving <jgraving@gmail.com>
#
# 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 tensorflow.keras import layers
from tensorflow.keras import backend as K
import numpy as np

def channel_squeeze_excite_block(input, ratio=0.25):
    init = input
    channel_axis = 1 if K.image_data_format() == "channels_first" else -1
    filters = init._keras_shape[channel_axis]
    cse_shape = (1, 1, filters)

    cse = layers.GlobalAveragePooling2D()(init)
    cse = layers.Reshape(cse_shape)(cse)
    ratio_filters = int(np.round(filters * ratio))
    if ratio_filters < 1:
        ratio_filters += 1
    cse = layers.Conv2D(
        ratio_filters,
        (1, 1),
        padding="same",
        activation="relu",
        kernel_initializer="he_normal",
        use_bias=False,
    )(cse)
    cse = layers.BatchNormalization()(cse)
    cse = layers.Conv2D(
        filters,
        (1, 1),
        activation="sigmoid",
        kernel_initializer="he_normal",
        use_bias=False,
    )(cse)

    if K.image_data_format() == "channels_first":
        cse = layers.Permute((3, 1, 2))(cse)

    cse = layers.Multiply()([init, cse])
    return cse

def spatial_squeeze_excite_block(input):
    sse = layers.Conv2D(
        1,
        (1, 1),
        activation="sigmoid",
        padding="same",
        kernel_initializer="he_normal",
        use_bias=False,
    )(input)
    sse = layers.Multiply()([input, sse])

    return sse

def squeeze_excite_block(input, ratio=0.25):
    cse = channel_squeeze_excite_block(input, ratio)
    sse = spatial_squeeze_excite_block(input)
    output = layers.Maximum()([cse, sse])
    return output

reason: import numpy


text:
#!/usr/bin/env python

import numpy as np
import pandas as pd
import random

from time import time, strftime

def get_current_time():
    return strftime('%Y-%m-%d_%H:%M:%S')

def duplicate_positives(slices, y):
    slices_new = []
    for index, s in slices:
        if y[index][:, s][:, -1].sum() > 0:
            slices_new.append((index, s))

    for _ in range(10):
        slices += slices_new
    random.shuffle(slices)
    return slices

def remove_easy_negatives(train_data, train_events):
    train_data_new, train_events_new = [], []
    for data, events in zip(train_data, train_events):
        indexes = np.zeros(data.shape[1], dtype=np.bool)
        for i in range(data.shape[1]):
            pre_event = np.sum(events[:, i:(i + 150)]) > 0
            post_event = np.sum(events[:, (i - 150):i]) > 0

            indexes[i] = (pre_event or
                          post_event or
                          np.random.choice([True, False], p=(0.5, 0.5)))

        train_data_new.append(data[:, indexes])
        train_events_new.append(events[:, indexes])

    return train_data_new, train_events_new

def load_subject_train(subj_id):
    data_list, events_list = [], []
    for series_id in range(1, 9):
        fname_data = 'data/train/subj%d_series%d_data.csv' % (
            subj_id, series_id)
        fname_events = 'data/train/subj%d_series%d_events.csv' % (
            subj_id, series_id)

        data = pd.read_csv(fname_data)
        events = pd.read_csv(fname_events)

        channel_names = data.columns[1:]
        data_list.append(data[channel_names].values.T)

        event_names = events.columns[1:]
        events_list.append(events[event_names].values.T.astype(np.int32))

    return data_list, events_list

def load_subject_test(subj_id):
    data_list, ids_list = [], []
    for series_id in range(9, 11):
        fname_data = 'data/test/subj%d_series%d_data.csv' % (
            subj_id, series_id)

        data = pd.read_csv(fname_data)
        channel_names = data.columns[1:]
        id_col = data.columns[0]

        data_list.append(data[channel_names].values.T)
        ids_list.append(data[id_col].values.T)

    return data_list, ids_list

# compute a normalizing transformation over all the training data,
# and apply this same transform to unseen data
def preprocess(train_data, test_data):
    train_data = [data.astype(np.float32) for data in train_data]
    test_data = [data.astype(np.float32) for data in test_data]

    print('normalizing...')
    # subtract the mean from all time series
    train_mean = np.mean(np.hstack(train_data), axis=1).reshape(-1, 1)
    train_data = [data - train_mean for data in train_data]

    # divide all time series by the standard deviation
    train_std = np.std(np.hstack(train_data), axis=1).reshape(-1, 1)
    train_data = [data / train_std for data in train_data]

    # apply the same transform to valid/test
    test_data = [data - train_mean for data in test_data]
    test_data = [data / train_std for data in test_data]

    return train_data, test_data

# split the time series into training, validation, and test
def split_train_test_data(data_list, events_list, val_size=2, rand=False):
    # randomly choose val_size time series for validation
    if rand:
        val_ind = np.random.choice(8, size=val_size, replace=False)
    # just use the last two time series for validation
    else:
        val_ind = np.arange(8 - val_size, 8)

    train_data, valid_data = [], []
    train_events, valid_events = [], []
    # separate the time series into training and validation
    for i in range(8):
        if i not in val_ind:
            train_data.append(data_list[i])
            train_events.append(events_list[i])
        else:
            valid_data.append(data_list[i])
            valid_events.append(events_list[i])

    return train_data, train_events, valid_data, valid_events

# time the loading of the files and print the shapes of the time series
# arrays
def verify_data():
    for subject in range(1, 13):
        # load the training data for each subject, time it, and print the shape
        t0 = time()
        data_list, events_list = load_subject_train(subject)
        print('loaded training data for subject %d in %.2f s' %
              (subject, time() - t0))
        print('verifying training data for subject %d...' % (subject))
        for i, (data, events) in enumerate(zip(data_list, events_list),
                                           start=1):
            print('  series %d:' % (i))
            print('    data.shape = %r' % (data.shape,))
            print('    events.shape = %r' % (events.shape,))

        # load the test data for each subject, time it, and print the shape
        t0 = time()
        data_list, ids_list = load_subject_test(subject)
        print('loaded test data for subject %d in %.2f s' %
              (subject, time() - t0))
        print('verifying test data for subject %d...' % (subject))
        for i, (data, ids) in enumerate(zip(data_list, ids_list),
                                        start=1):
            print('  series %d:' % (i))
            print('    data.shape = %r' % (data.shape,))
            print('    ids.shape = %r' % (ids.shape))

def main():
    #generate_data()
    verify_data()

if __name__ == '__main__':
    main()

max_stars_repo_name: hjweide/kaggle-grasp-and-lift

max_issues_repo_name: hjweide/kaggle-grasp-and-lift

max_forks_repo_name: hjweide/kaggle-grasp-and-lift


text:
#
# graphics/uq/hypocenter.py
#

import numpy as np
import subprocess

from matplotlib import pyplot
from pandas import DataFrame
from xarray import DataArray
from mtuq.graphics.uq._gmt import _plot_latlon_gmt
from mtuq.graphics.uq.depth import _misfit_regular, _likelihoods_regular
from mtuq.grid_search import MTUQDataArray, MTUQDataFrame
from mtuq.util import fullpath, warn
from mtuq.util.math import closed_interval, open_interval

def plot_misfit_latlon(filename, ds, origins, **kwargs):
    """ Plots misfit versus hypocenter location

    .. rubric :: Input arguments

    ``filename`` (`str`):
    Name of output image file

    ``ds`` (`DataArray` or `DataFrame`):
    Data structure containing moment tensors and corresponding misfit values

    ``origins`` (`list` of `Origin` objects)
    Origin objects corresponding to different hypocenters

    .. rubric :: Optional input arguments

    For optional argument descriptions,
    `see here <mtuq.graphics._plot_latlon.html>`_

    """

    _check(ds)
    ds = ds.copy()

    if issubclass(type(ds), DataArray):
        da = _misfit_regular(ds)

    elif issubclass(type(ds), DataFrame):
        raise NotImplementedError

    _plot_latlon(filename, da, origins, **kwargs)

def plot_likelihood_latlon(filename, ds, origins, **kwargs):
    """ Plots likelihood versus hypocenter location

    .. rubric :: Input arguments

    ``filename`` (`str`):
    Name of output image file

    ``ds`` (`DataArray` or `DataFrame`):
    Data structure containing moment tensors and corresponding misfit values

    ``origins`` (`list` of `Origin` objects)
    Origin objects corresponding to different hypocenters

    .. rubric :: Optional input arguments

    For optional argument descriptions,
    `see here <mtuq.graphics._plot_latlon.html>`_

    """

    _check(ds)
    ds = ds.copy()

    if issubclass(type(ds), DataArray):
        da = _likelihood_regular(ds)

    elif issubclass(type(ds), DataFrame):
        raise NotImplementedError

    _plot_latlon(filename, da, origins, **kwargs)

def plot_marginal_latlon(filename, ds, origins, **kwargs):
    """ Plots likelihood versus hypocenter location

    .. rubric :: Input arguments

    ``filename`` (`str`):
    Name of output image file

    ``ds`` (`DataArray` or `DataFrame`):
    Data structure containing moment tensors and corresponding misfit values

    ``origins`` (`list` of `Origin` objects)
    Origin objects corresponding to different hypocenters

    .. rubric :: Optional input arguments

    For optional argument descriptions,
    `see here <mtuq.graphics._plot_depth.html>`_

    """

    _check(ds)
    ds = ds.copy()

    if issubclass(type(ds), DataArray):
        raise NotImplementedError

    elif issubclass(type(ds), DataFrame):
        raise NotImplementedError

    _plot_latlon(filename, da, origins, **kwargs)

#
# wrappers
#

def _plot_latlon(filename, da, origins,show_best=False, show_tradeoffs=False,
    backend=_plot_latlon_gmt, **kwargs):

    """ Plots user-supplied DataArray values versus hypocenter (requires GMT)

    .. rubric :: Keyword arguments

    ``show_tradeoffs`` (`bool`):
    Show how focal mechanism trades off with depth

    ``xlabel`` (`str`):
    Optional x-axis label

    ``ylabel`` (`str`):
    Optional y-axis label

    ``title`` (`str`)
    Optional figure title

    """

    npts = len(origins)

    lon = np.empty(npts)
    lat = np.empty(npts)
    values = np.empty(npts)

    for _i, origin in enumerate(origins):
        lon[_i] = origin.longitude
        lat[_i] = origin.latitude
        values[_i] = da.values[_i]

    best_latlon = None
    if show_best:
        raise NotImplementedError

    lune_array = None
    if show_tradeoffs:
        lune_array = np.empty((npts, 6))
        for _i in range(npts):
            lune_array[_i, 0] = da[_i].coords['rho']
            lune_array[_i, 1] = da[_i].coords['v']
            lune_array[_i, 2] = da[_i].coords['w']
            lune_array[_i, 3] = da[_i].coords['kappa']
            lune_array[_i, 4] = da[_i].coords['sigma']
            lune_array[_i, 5] = da[_i].coords['h']

    backend(filename,
        lon, lat, values,
        best_latlon=best_latlon,
        lune_array=lune_array,
        **kwargs)

def _get_labeltype(x,y,labeltype):
    if labeltype=='latlon':
       xlabel = None
       ylabel = None

    if labeltype=='offset' and ((x.max()-x.min()) >= 10000.):
       x /= 1000.
       y /= 1000.
       xlabel = 'E-W offset (km)'
       ylabel = 'N-S offset (km)'
    elif labeltype=='offset' and ((x.max()-x.min()) < 10000.):
       xlabel = 'E-W offset (m)'
       ylabel = 'N-S offset (m)'

    return xlabel,ylabel

#
# utility functions
#

def _check(ds):
    """ Checks data structures
    """
    if type(ds) not in (DataArray, DataFrame, MTUQDataArray, MTUQDataFrame):
        raise TypeError("Unexpected grid format")

max_stars_repo_name: ammcpherson/mtuq

max_issues_repo_name: Liang-Ding/mtuq

max_forks_repo_name: Liang-Ding/mtuq


text:
import tempfile
import unittest
import uuid

import iomb.util as util
import numpy

class TestUtil(unittest.TestCase):

    def test_make_uuid(self):
        expected = str(uuid.uuid3(uuid.NAMESPACE_OID, "flow/a/1/b"))
        actual = util.make_uuid("Flow", None, "a", 1, "B")
        self.assertEqual(expected, actual)

    def test_read_csv_as_data_frame(self):
        text = """
        ,C1,C2,C3
        R1,1,2,3
        R2,4,5,6
        """.strip()
        data = ""
        for line in text.split('\n'):
            data += line.strip() + '\n'
        temp = tempfile.NamedTemporaryFile('w', encoding='utf-8', delete=False,
                                           prefix='iomb_tests_')
        temp.write(data)
        temp.close()
        data_frame = util.read_csv_data_frame(temp.name)
        self.assertAlmostEqual(1.0, data_frame['c1']['r1'], 1e-16)
        # self.assertTrue(type(data_frame['c1']['r1']) is numpy.float64)
        self.assertAlmostEqual(6.0, data_frame['c3']['r2'], 1e-16)
        # self.assertTrue(type(data_frame['c3']['r2']) is numpy.float64)

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

max_stars_repo_name: USEPA/IO-Model-Builder

max_issues_repo_name: USEPA/IO-Model-Builder

max_forks_repo_name: USEPA/IO-Model-Builder


text:
# Copyright 2015 Abhinav Maurya

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import fileinput
from sets import Set
import random
import scipy.special
import math
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats
from scipy.stats import beta
import pprint, pickle

def VisualizeTopics(phi, words, num_topics, viz_threshold=9e-3):
	phi_viz = np.transpose(phi)
	words_to_display = ~np.all(phi_viz <= viz_threshold, axis=1)
	words_viz = [words[i] for i in range(len(words_to_display)) if words_to_display[i]]
	phi_viz = phi_viz[words_to_display]

	fig, ax = plt.subplots()
	heatmap = plt.pcolor(phi_viz, cmap=plt.cm.Blues, alpha=0.8)
	plt.colorbar()

	#fig.set_size_inches(8, 11)
	ax.grid(False)
	ax.set_frame_on(False)

	ax.set_xticks(np.arange(phi_viz.shape[1]) + 0.5, minor=False)
	ax.set_yticks(np.arange(phi_viz.shape[0]) + 0.5, minor=False)
	ax.invert_yaxis()
	ax.xaxis.tick_top()
	#plt.xticks(rotation=45)

	for t in ax.xaxis.get_major_ticks():
	    t.tick1On = False
	    t.tick2On = False
	for t in ax.yaxis.get_major_ticks():
	    t.tick1On = False
	    t.tick2On = False

	column_labels = words_viz	#['Word ' + str(i) for i in range(1,1000)]
	row_labels = ['Topic ' + str(i) for i in range(1,num_topics+1)]
	ax.set_xticklabels(row_labels, minor=False)
	ax.set_yticklabels(column_labels, minor=False)

	plt.show()

def VisualizeEvolution(psi):
	xs = np.linspace(0, 1, num=1000)
	fig, ax = plt.subplots()

	for i in range(len(psi)):
		ys = [math.pow(1-x, psi[i][0]-1) * math.pow(x, psi[i][1]-1) / scipy.special.beta(psi[i][0],psi[i][1]) for x in xs]
		ax.plot(xs, ys, label='Topic ' + str(i+1))

	ax.legend(loc='best', frameon=False)
	plt.show()

def main():
	resultspath = '../results/pnas_tot/'
	tot_pickle_path = resultspath + 'pnas_tot.pickle'

	tot_pickle = open(tot_pickle_path, 'rb')
	par = pickle.load(tot_pickle)
	VisualizeTopics(par['n'], par['word_token'], par['T'])
	VisualizeEvolution(par['psi'])

if __name__ == "__main__":
    main()

reason: import numpy,import scipy,from scipy


text:
# Original file copied from https://github.com/mbeyeler/opencv-python-blueprints/tree/master/chapter4
# and edited to fit the task of the assignment and the re-designed processing pipeline in pipeline.py

import numpy as np

from pipeline import *

# Path to the directory containing the images to be used as input. The images should be present directly in this directory (i.e. not in folders inside it)
master_path_to_dataset = 'C:/Users/simon/GitRepositories/SSAIV_ACV/Dataset/2015-03-27_10-47-08_Seq2/monocular_left_calibrated/'
verbose = True #Whether text outputs should be given during the execution of the pipeline
verbose_img = True # Whether image outputs should be given during the execution of the pipeline
# For efficiency in executing the entire pipeline, set both of the above to False. For inspecting the steps of the pipeline, and the
# quality of the output, set them to True.

def main():
    # K and d of the left and right camera used for the 3 data sequences are known, if one of these is selected as the master_path_to_dataset load the appropriate K.

    K_right = np.array([1.2432403472640376e+003, 0., 6.6606587228414776e+002, 0.,
       1.2331555645540684e+003, 4.5437658085060713e+002, 0., 0., 1.]).reshape(3, 3)
    d_right = np.array([-4.6261205649774895e-001, 2.0732672793966445e-001,
       6.2375723417721863e-003, 2.6299185722505536e-003, -3.6689019973370124e-002]).reshape(1, 5)

    K_left = np.array([1.1930484416274905e+003, 0., 6.1463232877569749e+002, 0.,
       1.1864252309660656e+003, 4.3318083737428361e+002, 0., 0., 1.]).reshape(3, 3)
    d_left = np.array([-4.8441890104482732e-001, 3.1770182182461387e-001,
       4.8167296939537890e-003, 5.9334794668205733e-004, -1.4902486951308128e-001]).reshape(1, 5)

    # Check if the dataset is one of the 3 sequences, and if so if it's the right or left camera
    if '2015-03-27_10-47-08_' in master_path_to_dataset:
        if 'monocular_left_calibrated' in master_path_to_dataset:
            print("Using K for left sequences")
            K = np.copy(K_left)
            d = np.copy(d_left)
        elif 'monocular_right_calibrated' in master_path_to_dataset:
            print("Using K for right sequences")
            K = np.copy(K_right)
            d = np.copy(d_right)
        else:
            print('Error: Original data sequence identified, but neither right nor left camera chosen: {}'.format(master_path_to_dataset))
            K = np.zeros((3, 3))
            d = np.zeroes((1, 5))
    else:
        # Unexpected dataset: make the user confirm the images are in chronological order, and that they've inserted the values for K and d here
        #HERE
        K = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8]).reshape(3, 3)
        d = np.array([0,1,2,3,4]).reshape(1, 5)
        cont = input("A different dataset than the one given with the assignment was detected.\nPlease ensure that the images in the directory are in chronological order, alphabetically (i.e. first image alphabetically is also first image chronologically)\nAlso ensure that you have inserted the correct camera intrinsics values into K and d in the code, after the comment '#HERE'.\nContinue? (y/n): ")
        if('y' != cont.lower() and 'yes' != cont.lower()):
            quit()

    # Execute the SfM pipeline on the image sequence
    pipeline(master_path_to_dataset, K, verbose=verbose, verbose_img=verbose_img)

if __name__ == '__main__':
    main()

max_issues_repo_name: Hoclor/AdvancedComputerVision

max_forks_repo_name: Hoclor/AdvancedComputerVision


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

################################################################################
#
#   RMG - Reaction Mechanism Generator
#
#   Copyright (c) 2002-2009 Prof. William H. Green (whgreen@mit.edu) and the
#   RMG Team (rmg_dev@mit.edu)
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#   DEALINGS IN THE SOFTWARE.
#
################################################################################

import os.path
import numpy
import logging

from rmgpy.cantherm.output import prettify
from rmgpy.kinetics import *

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

class KineticsJob:
    """
    A representation of a CanTherm kinetics job. This job is used to compute
    and save the high-pressure-limit kinetics information for a single reaction.
    """

    def __init__(self, reaction):
        self.reaction = reaction
        self.kunits = None

    def execute(self, outputFile=None, plot=False):
        """
        Execute the kinetics job, saving the results to the given `outputFile`
        on disk.
        """
        self.generateKinetics()
        if outputFile is not None:
            self.save(outputFile)
            if plot:
                self.plot(os.path.dirname(outputFile))

    def generateKinetics(self):
        """
        Generate the kinetics data for the reaction and fit it to a modified
        Arrhenius model.
        """
        kineticsClass = 'Arrhenius'

        tunneling = self.reaction.transitionState.tunneling
        if isinstance(tunneling, Wigner) and tunneling.frequency is None:
            tunneling.frequency = (self.reaction.transitionState.frequency.value_si,"cm^-1")
        elif isinstance(tunneling, Eckart) and tunneling.frequency is None:
            tunneling.frequency = (self.reaction.transitionState.frequency.value_si,"cm^-1")
            tunneling.E0_reac = (sum([reactant.conformer.E0.value_si for reactant in self.reaction.reactants])*0.001,"kJ/mol")
            tunneling.E0_TS = (self.reaction.transitionState.conformer.E0.value_si*0.001,"kJ/mol")
            tunneling.E0_prod = (sum([product.conformer.E0.value_si for product in self.reaction.products])*0.001,"kJ/mol")
        elif tunneling is not None:
            raise ValueError('Unknown tunneling model {0!r}.'.format(tunneling))

        logging.info('Generating {0} kinetics model for {0}...'.format(kineticsClass, self.reaction))

        Tlist = 1000.0/numpy.arange(0.4, 3.35, 0.05)
        klist = numpy.zeros_like(Tlist)
        for i in range(Tlist.shape[0]):
            klist[i] = self.reaction.calculateTSTRateCoefficient(Tlist[i])

        order = len(self.reaction.reactants)
        klist *= 1e6 ** (order-1)
        self.kunits = {1: 's^-1', 2: 'cm^3/(mol*s)', 3: 'cm^6/(mol^2*s)'}[order]
        self.reaction.kinetics = Arrhenius().fitToData(Tlist, klist, kunits=self.kunits)

    def save(self, outputFile):
        """
        Save the results of the kinetics job to the file located
        at `path` on disk.
        """
        reaction = self.reaction

        logging.info('Saving kinetics for {0}...'.format(reaction))

        order = len(self.reaction.reactants)
        factor = 1e6 ** (order-1)

        f = open(outputFile, 'a')

        f.write('#   ======= =========== =========== =========== ===============\n')
        f.write('#   Temp.   k (TST)     Tunneling   k (TST+T)   Units\n')
        f.write('#   ======= =========== =========== =========== ===============\n')
        for T in [300,400,500,600,800,1000,1500,2000]:
            tunneling = reaction.transitionState.tunneling
            reaction.transitionState.tunneling = None
            k0 = reaction.calculateTSTRateCoefficient(T) * factor
            reaction.transitionState.tunneling = tunneling
            k = reaction.calculateTSTRateCoefficient(T) * factor
            tunneling = reaction.transitionState.tunneling
            kappa = k / k0
            f.write('#    {0:4g} K {1:11.3e} {2:11g} {3:11.3e} {4}\n'.format(T, k0, kappa, k, self.kunits))
        f.write('#   ======= =========== =========== =========== ===============\n')

        # Reaction path degeneracy is INCLUDED in the kinetics itself!
        string = 'kinetics(label={0!r}, kinetics={1!r})'.format(reaction.label, reaction.kinetics)
        f.write('{0}\n\n'.format(prettify(string)))

        f.close()

        # Also save the result to chem.inp
        f = open(os.path.join(os.path.dirname(outputFile), 'chem.inp'), 'a')

        reaction = self.reaction
        kinetics = reaction.kinetics

        string = '{0!s:51} {1:9.3e} {2:9.3f} {3:9.3f}\n'.format(
            reaction,
            kinetics.A.value_si * factor,
            kinetics.n.value_si,
            kinetics.Ea.value_si / 4184.,
        )

        f.write('{0}\n'.format(string))

        f.close()

    def plot(self, outputDirectory):
        """
        Plot both the raw kinetics data and the Arrhenius fit versus
        temperature. The plot is saved to the file ``kinetics.pdf`` in the
        output directory. The plot is not generated if ``matplotlib`` is not
        installed.
        """
        # Skip this step if matplotlib is not installed
        try:
            import pylab
        except ImportError:
            return

        Tlist = 1000.0/numpy.arange(0.4, 3.35, 0.05)
        klist = numpy.zeros_like(Tlist)
        klist2 = numpy.zeros_like(Tlist)
        for i in range(Tlist.shape[0]):
            klist[i] = self.reaction.calculateTSTRateCoefficient(Tlist[i])
            klist2[i] = self.reaction.kinetics.getRateCoefficient(Tlist[i])

        order = len(self.reaction.reactants)
        klist *= 1e6 ** (order-1)
        klist2 *= 1e6 ** (order-1)

        pylab.semilogy(1000.0 / Tlist, klist, 'ok')
        pylab.semilogy(1000.0 / Tlist, klist2, '-k')
        pylab.xlabel('1000 / Temperature (1000/K)')
        pylab.ylabel('Rate coefficient ({0})'.format(self.kunits))
        pylab.savefig(os.path.join(outputDirectory, 'kinetics.pdf'))
        pylab.close()

max_stars_repo_name: faribas/RMG-Py

max_issues_repo_name: faribas/RMG-Py

max_forks_repo_name: faribas/RMG-Py


text:
#!/usr/bin/env python3

import io
import sys
import requests
import numpy as np
import scipy.fft as fft

from typing import List
from scipy.io import wavfile

import mt

BASE_FREQUENCY = 256
TONE_DISTANCE = 1
TONE_DURATION = 0.25
TONE_BITS = 10

def read_frequency(data: np.ndarray, sample_rate: int, prec: int) -> int:
    size = prec * len(data)

    fft_data = fft.fft(data, n=size)
    fft_freqs = fft.fftfreq(size, 1 / sample_rate)

    freq_index = np.argmax(np.abs(fft_data))

    return int(fft_freqs[freq_index])

def extract_freqs(channel: np.ndarray, sample_rate: int, prec: int) -> List[int]:
    tone_length = int(sample_rate * TONE_DURATION)
    tones_count = len(channel) // tone_length + 1
    freqs = []

    for i in range(tones_count):
        tone = channel[i * tone_length: (i + 1) * tone_length]
        freq = read_frequency(tone, sample_rate, prec)
        freqs.append(freq)

    return freqs

def extract_state(freqs: List[int]) -> List[int]:
    freqs = list(freqs)

    outputs = [(x - BASE_FREQUENCY) // TONE_DISTANCE for x in freqs]
    bits = ''.join(bin(x)[2:].zfill(TONE_BITS) for x in outputs)

    state = []

    for i in range(mt.STATE_SIZE):
        part = bits[i * mt.BITS:(i + 1) * mt.BITS]
        state.append(int(part, 2))

    return state

def extract_data(wav: bytes) -> bytes:
    prec = 16

    sample_rate, data = wavfile.read(io.BytesIO(wav))
    all_freqs = []

    for i in range(data.ndim):
        channel = data[:, i]
        freqs = extract_freqs(channel, sample_rate, prec)

        if freqs[-1] == 0:
            freqs.pop()

        all_freqs.extend(freqs)

    state = extract_state(all_freqs)

    seed_array = mt.recover_seed(state)
    seed_data = mt.array_to_bytes(seed_array)

    return seed_data

def download_wav(url: str, chunks_count: int) -> bytes:
    offset = 0
    chunks = []

    for i in range(chunks_count):
        headers = {
            'Range': f'bytes={offset}-'
        }

        chunk = requests.get(url, headers=headers).content

        chunks.append(chunk)
        offset += len(chunk)

    return b''.join(chunks)

def main():
    IP = sys.argv[1]
    PORT = 17171

    url = f'http://{IP}:{PORT}'

    for id in requests.get(f'{url}/api/list/').json():
        wav_url = f'{url}/api/listen/{id}/'
        wav = download_wav(wav_url, 16)

        flag = extract_data(wav)
        print(flag)

if __name__ == '__main__':
    main()


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

# Predicting the housing prices of California City. This notebook uses simple Linear Regression model with proper visualizations and descriptions.

# ## Importing Libraries and Dataset

# In[ ]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import warnings
warnings.filterwarnings('ignore')

# In[ ]:

housing = pd.read_csv("../../../input/camnugent_california-housing-prices/housing.csv")
housing.head()

# In[ ]:

housing.info()

# In[ ]:

housing[housing['total_bedrooms'].isnull()]
housing.loc[290]

# ### Graph Visualizations for all given attributes

# In[ ]:

housing.hist(bins=50, figsize=(20,20))
print()

# ### Fillling all NAN values of Dataframe with Mean value

# In[ ]:

housing['total_bedrooms'][housing['total_bedrooms'].isnull()] = np.mean(housing['total_bedrooms'])
housing.loc[290]

# ### Calculating Average Rooms and Bedrooms

# In[ ]:

housing['avg_rooms'] = housing['total_rooms']/housing['households']
housing['avg_bedrooms'] = housing['total_bedrooms']/housing['households']
housing.head()

# ### Finding correlation between the predictors

# In[ ]:

housing.corr()

# ### Calculating population per household

# In[ ]:

housing['pop_household'] = housing['population']/housing['households']
housing[:10]

# In[ ]:

housing['NEAR BAY']=0
housing['INLAND']=0
housing['<1H OCEAN']=0
housing['ISLAND']=0
housing['NEAR OCEAN']=0
housing.head()

# ### Converting the ocean proximity data to one-hot vectors

# In[ ]:

housing.loc[housing['ocean_proximity']=='NEAR BAY','NEAR BAY']=1
housing.loc[housing['ocean_proximity']=='INLAND','INLAND']=1
housing.loc[housing['ocean_proximity']=='<1H OCEAN','<1H OCEAN']=1
housing.loc[housing['ocean_proximity']=='ISLAND','ISLAND']=1
housing.loc[housing['ocean_proximity']=='NEAR OCEAN','NEAR OCEAN']=1
housing.head()

# ## Applying Linear Regression

# In[ ]:

from sklearn.linear_model import LinearRegression
from sklearn.cross_validation import train_test_split

train_x = housing.drop(['total_rooms','total_bedrooms','households','ocean_proximity','median_house_value'],axis=1)
train_y = housing['median_house_value']

X,test_x,Y,test_y = train_test_split(train_x, train_y, test_size=0.2)

# In[ ]:

clf = LinearRegression()
clf.fit(np.array(X),Y)

# In[ ]:

import math

def roundup(x):
   return int(math.ceil(x / 100.0)) * 100
pred = list(map(roundup,clf.predict(test_x)))

print(pred[:10])
test_y[:10]

# ### Calculating root mean squared error in the regression model

# In[ ]:

from sklearn.metrics import mean_squared_error

predictions = clf.predict(test_x)
mse = mean_squared_error(test_y, predictions)
rmse = np.sqrt(mse)
rmse

max_stars_repo_name: Chenguang-Zhu/relancer

max_issues_repo_name: Chenguang-Zhu/relancer

max_forks_repo_name: Chenguang-Zhu/relancer


text:
import numpy as np
import scipy.constants as sc
import os
from ..utilities.ascii_files import read_ascii_columns
import matplotlib.pyplot as plt

class Bandpass(object):

    def __init__(self,filename):
        self.filename=filename
        (self.lamb,self.tran),meta=read_ascii_columns(self.filename,
                                                      names=('lamb','tran'))

        self.zeropoint=meta['ZEROPOINT']
        self.name=meta['NAME']

        # compute frequency and range
        self.freq=(sc.c/self.lamb)*1e10
        self.wmin=np.amin(self.lamb)
        self.wmax=np.amax(self.lamb)

        # compute pivot wavelength
        den=np.trapz(self.tran/self.lamb,x=self.lamb)
        num=np.trapz(self.tran*self.lamb,x=self.lamb)
        self.photplam=np.sqrt(num/den)

        # compute photflam
        self.photflam=10**(-0.4*self.zeropoint)*0.1088/(self.photplam*self.photplam)

        # compute the average wavelength
        den=np.trapz(self.tran,x=self.lamb)
        self.avelam=num/den

        # compute width
        num=np.trapz(self.tran*(self.lamb-self.avelam)**2,x=self.lamb)
        self.rms=np.sqrt(num/den)

        # remove the frequency
        self.tran/=self.freq

        # compute normalization for AB mag integration
        self.norm=np.trapz(self.tran,x=self.freq)

        # normalize the curve
        self.tran/=self.norm

    @classmethod
    def load(cls,fileroot):
        path=os.path.split(__file__)[0]
        filename=os.path.join(path,'filters','{}.filt'.format(fileroot))
        if os.path.exists(filename):
            return cls(filename)
        else:
            print('[warn]Filter not found: {}'.format(filename))
            return None

    def __str__(self):
        return 'Bandpass: {}'.format(self.filename)

    #@property
    #def nu2lamb(self):
    #    jacob=(sc.c/self.photplam)*(1e10/self.photplam)
    #    return jacob
    #
    def plot(self):
        plt.plot(self.lamb,self.tran)
        plt.show()

    def aveflux(self,sed,magnitude=False,flam=False):
        #ex=(sed.minmax[0] > self.minmax[0]) or (sed.minmax[1] < self.minmax[1])

        if (sed.wmin > self.wmin) or (sed.wmax < self.wmax):
            print("[warn]Extrapolation warning.")
            print('SED RANGE: {} {}'.format(sed.wmin,sed.wmax))
            print('BAND range: {} {}'.format(self.wmin,self.wmax))

        fnu=np.interp(self.lamb,sed.lamb,sed.fnu(),left=0.,right=0.)
        ave=np.trapz(fnu*self.tran,x=self.freq)

        if flam:
            ave*=(sc.c/self.photplam)*(1e10/self.photplam)

        if magnitude:
            ave=-2.5*np.log10(ave)+self.zeropoint

        return ave

if __name__=='__main__':
    x=pylinear.sedphot.Bandpass.load('hst_wfc3_f140w')
    x.plot()
    print(x)

max_stars_repo_name: Russell-Ryan/pyLINEAR

max_issues_repo_name: Russell-Ryan/pyLINEAR

max_forks_repo_name: Russell-Ryan/pyLINEAR

reason: import numpy,import scipy


text:
"""This file is a demo for using the DG whole body controller.

License BSD-3-Clause
Copyright (c) 2021, New York University and Max Planck Gesellschaft.

Author: Julian Viereck
Date:   Feb 26, 2021
"""

import numpy as np

np.set_printoptions(suppress=True, precision=3)

import pinocchio as pin
import dynamic_graph as dg

import dynamic_graph.sot.dynamic_pinocchio as dp
from dynamic_graph.sot.core.math_small_entities import (
    Selec_of_matrix,
    Stack_of_vector,
)

from mim_control.dynamic_graph.wbc_graph import WholeBodyController

from reactive_planners.dynamic_graph.walking import QuadrupedDcmReactiveStepper

from dg_tools.dynamic_graph.dg_tools_entities import PoseRPYToPoseQuaternion
from dg_tools.utils import (
    constVectorOp,
    subtract_vec_vec,
    hom2pos,
    add_vec_vec,
    stack_two_vectors,
    selec_vector,
    zero_vec,
    basePoseQuat2PoseRPY,
    multiply_mat_vec,
)

from robot_properties_solo.config import Solo12Config

class QuadrupedStepper:
    def __init__(self, prefix, pin_robot, endeff_names):
        self.prefix = prefix
        self.pin_robot = pin_robot
        self.endeff_names = endeff_names
        self.nv = pin_robot.model.nv

        self.dg_robot = dp.DynamicPinocchio(prefix + "_pinocchio")
        self.dg_robot.setModel(self.pin_robot.model)
        self.dg_robot.setData(self.pin_robot.data)

        self.sig_eff_pos = []
        self.sig_eff_vel = []

        # Create the objects of interests from pinocchio.
        self.com = self.dg_robot.signal("com")
        self.vcom = multiply_mat_vec(
            self.dg_robot.signal("Jcom"), self.dg_robot.signal("velocity")
        )

        for endeff_name in endeff_names:
            self.dg_robot.createPosition("pos_" + endeff_name, endeff_name)
            self.dg_robot.createJacobianEndEffWorld(
                "jac_" + endeff_name, endeff_name
            )

            # Store the endeffector position signal.
            self.sig_eff_pos.append(
                hom2pos(self.dg_robot.signal("pos_" + endeff_name))
            )

            # Compute the endeffector velocity signal.
            sel_linear = Selec_of_matrix(prefix + "_pinocchio_" + endeff_name)
            sel_linear.selecRows(0, 3)
            sel_linear.selecCols(0, self.nv + 6)
            dg.plug(self.dg_robot.signal("jac_" + endeff_name), sel_linear.sin)

            self.sig_eff_vel.append(
                multiply_mat_vec(
                    sel_linear.sout, self.dg_robot.signal("velocity")
                )
            )

        ###
        # Create the actual stepper object.
        self.stepper = QuadrupedDcmReactiveStepper(
            prefix + "_quadruped_stepper"
        )

        # Setup the pinocchio input quantities for the stepper.
        dg.plug(self.com, self.stepper.com_position_sin)
        dg.plug(self.vcom, self.stepper.com_velocity_sin)

        dg.plug(
            self.sig_eff_pos[0],
            self.stepper.current_front_left_foot_position_sin,
        )
        dg.plug(
            self.sig_eff_vel[0],
            self.stepper.current_front_left_foot_velocity_sin,
        )
        dg.plug(
            self.sig_eff_pos[1],
            self.stepper.current_front_right_foot_position_sin,
        )
        dg.plug(
            self.sig_eff_vel[1],
            self.stepper.current_front_right_foot_velocity_sin,
        )
        dg.plug(
            self.sig_eff_pos[2],
            self.stepper.current_hind_left_foot_position_sin,
        )
        dg.plug(
            self.sig_eff_vel[2],
            self.stepper.current_hind_left_foot_velocity_sin,
        )
        dg.plug(
            self.sig_eff_pos[3],
            self.stepper.current_hind_right_foot_position_sin,
        )
        dg.plug(
            self.sig_eff_vel[3],
            self.stepper.current_hind_right_foot_velocity_sin,
        )

        self.stepper.is_closed_loop_sin.value = 0.0

    def start(self):
        self.stepper.start()

    def stop(self):
        self.stepper.stop()

    def plug(self, robot, base_position, base_velocity):
        # Args:
        #   robot; DGM robot device
        #   base_position: The base position as a 7 dim vector signal
        #   base_velocity: The base velocity as a 6 dim vector signal

        ###
        # Plug the pinocchio entity.
        base_pose_rpy = basePoseQuat2PoseRPY(base_position)
        position = stack_two_vectors(
            base_pose_rpy, robot.device.joint_positions, 6, self.nv - 6
        )
        velocity = stack_two_vectors(
            base_velocity, robot.device.joint_velocities, 6, self.nv - 6
        )

        dg.plug(position, self.dg_robot.signal("position"))
        dg.plug(velocity, self.dg_robot.signal("velocity"))
        self.dg_robot.signal("acceleration").value = np.array(
            self.nv
            * [
                0.0,
            ]
        )

        ###
        # Plug the stepper base position.
        dg.plug(base_position, self.stepper.xyzquat_base_sin)


text:
"""
frenet.py

Spectral analysis of frenet variables given an x,y input.

"""
import sys
sys.path.insert(1, '/home/ryi/projects_py/lib')

import numpy as np
from scipy import signal

# Custom.
import interparc as ia
from twodimnav import curvature
from psd import psdfull

# Functions for data transformation.

def calc_dnds(x,y):
    """ dnds calculates the derivative dN/ds, with respect to the normal vector.
    """
    # Deal with arrays again.
    x = np.asarray(x)
    y = np.asarray(y)
    x = x.reshape(len(x),)
    y = y.reshape(len(y),)
    # Calculate curvature.
    kappa = curvature(x,y)
    tanvec = np.diff(x) + np.diff(y)*1j
    return -kappa*(tanvec[0:-1] + tanvec[1:])/2

def calc_dtds(x,y):
    """ dtds calculates the derivative dT/ds, with respect to the tangent.
    """
    # Deal with arrays again.
    x = np.asarray(x)
    y = np.asarray(y)
    x = x.reshape(len(x),)
    y = y.reshape(len(y),)
    # Calculate curvature.
    kappa = curvature(x,y)
    normvec = -np.diff(y) + np.diff(x)*1j
    return kappa*(normvec[0:-1] + normvec[1:])/2

# Functions for spectral analysis.

def frenet_periodogram_i(x, y, l=0):
    """ periodogram calculates per. for inputs x, y, after interpolating over l=len(x) (default).
    """
    if l==0:
        l = len(x)
    interp_coords = ia.interparc(l, x, y)
    x_i = interp_coords[:,0]
    y_i = interp_coords[:,1]
    # Calculate frenet.
    dtds_i = calc_dtds(x_i, y_i)
    steps_i = np.sqrt(np.diff(x_i, axis=0)**2 + np.diff(y_i, axis=0)**2)[:-1]
    fs = 1./steps_i[0]
    # Calculate periodogram.
#    per_f, per_p = psdfull(dtds_i, fs)
    per_f, per_p = signal.periodogram(dtds_i, fs, return_onesided=False)
    per_f = np.fft.fftshift(per_f)
    per_p = np.fft.fftshift(per_p)
    return per_f, per_p, fs, x_i, y_i

max_stars_repo_name: alexander-hagg/phdexperiments

max_issues_repo_name: alexander-hagg/phdexperiments

max_forks_repo_name: alexander-hagg/phdexperiments

reason: import numpy,from scipy


text:
"""
Code to generate the animated heatmaps
"""
import matplotlib.pyplot as plt
import numpy as np
import ambulance_game as abg

# Parameters
lambda_2_space = np.linspace(0.1, 0.6, 20)
lambda_1 = 0.1
mu = 0.1
num_of_servers = 5
threshold = num_of_servers
system_capacity = 20
buffer_capacity = 20
runtime = 10000
num_of_trials = 10
seed_num = 0

fig = 0
for lambda_2 in lambda_2_space:
    plt.figure(figsize=(20, 10))
    abg.get_heatmaps(
        lambda_2,
        lambda_1,
        mu,
        num_of_servers,
        threshold,
        system_capacity,
        buffer_capacity,
        runtime=runtime,
        num_of_trials=num_of_trials,
        seed_num=seed_num,
    )
    fig += 1
    plt.savefig("main_" + str(fig) + ".png")
    plt.close()


text:
#-------------------------------------------------------------------------------

class Priors(object):
    """
    Jonathan Zwart
    6 July 2013
    5 March 2014 - added general prior function

    Taken from MultiNEST priors.f90
    Usage e.g.:
        from priors import Priors
        pri=Priors()
        cube[0]=pri.UniformPrior(cube[0],1.0,199.0)
        cube[0]=pri.GeneralPrior(cube[0],'LOG',1.0,100.0)
        etc.
    """

    def __init__(self):
        pass

    def GeneralPrior(self,r,PriorType,x1,x2):
        if PriorType=='DELTA':
            return self.DeltaFunctionPrior(r,x1,x2)
        elif PriorType=='U':
            return self.UniformPrior(r,x1,x2)
        elif PriorType=='LOG':
            return self.LogPrior(r,x1,x2)
        elif PriorType=='GAUSS':
            return self.GaussianPrior(r,x1,x2)
        elif PriorType=='JEFF':
            return self.JeffreysPrior(r,x1,x2)
        else:
            print 'Unrecognised prior'
            return 1

    def DeltaFunctionPrior(self,r,x1,x2):
        """Uniform[0:1]  ->  Delta[x1]"""
        return x1

    def UniformPrior(self,r,x1,x2):
        """Uniform[0:1]  ->  Uniform[x1:x2]"""
        return x1+r*(x2-x1)

    def LogPrior(self,r,x1,x2):
        """Uniform[0:1]  ->  LogUniform[x1:x2]"""
        from math import log10
        if (r <= 0.0):
                return -1.0e32
        else:
            lx1=log10(x1); lx2=log10(x2)
            return 10.0**(lx1+r*(lx2-lx1))

    def GaussianPrior(self,r,mu,sigma):
        """Uniform[0:1]  ->  Gaussian[mean=mu,variance=sigma**2]"""
        from math import sqrt
        from scipy.special import erfcinv
        if (r <= 1.0e-16 or (1.0-r) <= 1.0e-16):
            return -1.0e32
        else:
            return mu+sigma*sqrt(2.0)*erfcinv(2.0*(1.0-r))

    def JeffreysPrior(self,r,x1,x2):
        """Needs to be tested"""
        if (r <= 0.0):
                return -1.0e32
        else:
            #from math import log
            from numpy import log
            if(x1==0):
                lx1 = sys.float_info.min
            else:
                lx1=log(x1)
            if(x2==0):
                lx2 = sys.float_info.min
            else:
                lx2=log(x2)
            return 1/(r*(lx2-lx1))

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

reason: from numpy,from scipy


text:
import numpy as np

SMALL_POSITIVE_CONST = 1e-4

def ot2bio_ate(ate_tag_sequence):
    """
    ot2bio function for ate task
    """
    n_tags = len(ate_tag_sequence)
    new_ate_sequence = []
    prev_ate_tag = '$$$'
    for i in range(n_tags):
        cur_ate_tag = ate_tag_sequence[i]
        if cur_ate_tag == 'O' or cur_ate_tag == 'EQ':
            # note that, EQ tag can be incorrectly predicted in the testing phase
            # when meet EQ, regard it as O
            new_ate_sequence.append('O')
            prev_ate_tag = 'O'
        else:
            # current ate tag is T
            if cur_ate_tag == prev_ate_tag:
                new_ate_sequence.append('I')
            else:
                new_ate_sequence.append('B')
            prev_ate_tag = cur_ate_tag
    assert len(new_ate_sequence) == len(ate_tag_sequence)
    return new_ate_sequence

def ot2bieos_ate(ate_tag_sequence):
    """
    ot2bieos function for ate task
    """
    n_tags = len(ate_tag_sequence)
    new_ate_sequence = []
    prev_ate_tag = '$$$'
    for i in range(n_tags):
        cur_ate_tag = ate_tag_sequence[i]
        if cur_ate_tag == 'O' or cur_ate_tag == 'EQ':
            # note that, EQ tag can be incorrectly predicted in the testing phase
            # when meet EQ, regard it as O
            new_ate_sequence.append('O')
            prev_ate_tag = 'O'
        else:
            # current ate tag is T
            if cur_ate_tag != prev_ate_tag:
                if i == n_tags - 1:
                    new_ate_sequence.append('S')
                else:
                    next_ate_tag = ate_tag_sequence[i + 1]
                    if next_ate_tag == 'O':
                        new_ate_sequence.append('S')
                    else:
                        new_ate_sequence.append('B')
            else:
                # previous ate tag is also T
                if i == n_tags - 1:
                    new_ate_sequence.append('E')
                else:
                    next_ate_tag = ate_tag_sequence[i + 1]
                    if next_ate_tag == 'O':
                        new_ate_sequence.append('E')
                    else:
                        new_ate_sequence.append('I')
            prev_ate_tag = 'T'
    return new_ate_sequence

def ot2bio_absa(absa_tag_sequence):
    """
    ot2bio function for ts tag sequence
    """
    #new_ts_sequence = []
    new_absa_sequence = []
    n_tag = len(absa_tag_sequence)
    prev_pos = '$$$'
    for i in range(n_tag):
        cur_absa_tag = absa_tag_sequence[i]
        if cur_absa_tag == 'O':
            new_absa_sequence.append('O')
            cur_pos = 'O'
        else:
            # current tag is subjective tag, i.e., cur_pos is T
            # print(cur_ts_tag)
            cur_pos, cur_sentiment = cur_absa_tag.split('-')
            if cur_pos == prev_pos:
                # prev_pos is T
                new_absa_sequence.append('I-%s' % cur_sentiment)
            else:
                # prev_pos is O
                new_absa_sequence.append('B-%s' % cur_sentiment)
        prev_pos = cur_pos
    return new_absa_sequence

def ot2bieos_absa(absa_tag_sequence):
    """
    ot2bieos function for end-to-end aspect-based sentiment analysis task
    """
    n_tags = len(absa_tag_sequence)
    #new_ts_sequence = []
    new_absa_sequence = []
    prev_pos = '$$$'

    for i in range(n_tags):
        cur_absa_tag = absa_tag_sequence[i]
        if cur_absa_tag == 'O' or cur_absa_tag == 'EQ':
            # when meet the EQ tag, regard it as O
            new_absa_sequence.append('O')
            cur_pos = 'O'
        else:
            cur_pos, cur_sentiment = cur_absa_tag.split('-')
            # cur_pos is T
            if cur_pos != prev_pos:
                # prev_pos is O and new_cur_pos can only be B or S
                if i == n_tags - 1:
                    new_absa_sequence.append('S-%s' % cur_sentiment)
                else:
                    next_absa_tag = absa_tag_sequence[i + 1]
                    if next_absa_tag == 'O':
                        new_absa_sequence.append('S-%s' % cur_sentiment)
                    else:
                        new_absa_sequence.append('B-%s' % cur_sentiment)
            else:
                # prev_pos is T and new_cur_pos can only be I or E
                if i == n_tags - 1:
                    new_absa_sequence.append('E-%s' % cur_sentiment)
                else:
                    next_absa_tag = absa_tag_sequence[i + 1]
                    if next_absa_tag == 'O':
                        new_absa_sequence.append('E-%s' % cur_sentiment)
                    else:
                        new_absa_sequence.append('I-%s' % cur_sentiment)
        prev_pos = cur_pos
    return new_absa_sequence

def ot2bio(tags, task):
    if task == 'ate':
        return ot2bio_ate(tags)
    elif task == 'absa':
        return ot2bio_absa(tags)
    else:
        raise Exception("Unsupported task!")

def ot2bieos(tags, task):
    if task == 'ate':
        return ot2bieos_ate(tags)
    elif task == 'absa':
        return ot2bieos_absa(tags)
    else:
        raise Exception("Unsupported task!")

def bio2ot_ate(ate_tag_sequence):
    """
    bio2ot function for ate task
    """
    n_tags = len(ate_tag_sequence)
    new_ate_sequence = []
    for i in range(n_tags):
        ate_tag = ate_tag_sequence[i]
        if ate_tag == 'O' or ate_tag == 'EQ':
            # note that, EQ tag can be incorrectly predicted in the testing phase
            # when meet EQ, regard it as O
            new_ate_sequence.append('O')
        else:
            new_ate_sequence.append('T')
    assert len(new_ate_sequence) == len(ate_tag_sequence)
    return new_ate_sequence

def bio2ot_absa(absa_tag_sequence):
    """
    bio2ot function for absa task
    """
    new_absa_sequence = []
    n_tags = len(absa_tag_sequence)
    for i in range(n_tags):
        absa_tag = absa_tag_sequence[i]
        #assert absa_tag != 'EQ'
        if absa_tag == 'O' or absa_tag == 'EQ':
            new_absa_sequence.append('O')
        else:
            pos, sentiment = absa_tag.split('-')
            new_absa_sequence.append('T-%s' % sentiment)
    return new_absa_sequence

def tag2ate(tag_sequence):
    """
    :param tag_sequence:
    """
    n_tags = len(tag_sequence)
    ate_sequence = []
    beg, end = -1, -1
    for i in range(n_tags):
        ate_tag = tag_sequence[i]
        if ate_tag == 'S':
            ate_sequence.append((i, i))
        elif ate_tag == 'B':
            beg = i
        elif ate_tag == 'E':
            end = i
            if end > beg > -1:
                ate_sequence.append((beg, end))
            beg, end = -1, -1
    return ate_sequence

def tag2absa(tag_sequence):
    """
    transform absa tag sequence to a list of absa triplet (b, e, sentiment)
    """
    n_tags = len(tag_sequence)
    absa_sequence, sentiments = [], []
    beg, end = -1, -1
    for i in range(n_tags):
        absa_tag = tag_sequence[i]
        # current position and sentiment
        # tag O and tag EQ will not be counted
        eles = absa_tag.split('-')
        if len(eles) == 2:
            pos, sentiment = eles
        else:
            pos, sentiment = 'O', 'O'
        if sentiment != 'O':
            # current word is a subjective word
            sentiments.append(sentiment)
        if pos == 'S':
            # singleton
            # assert len(sentiments) == 1
            absa_sequence.append((i, i, sentiments[-1]))
            sentiments = []
        elif pos == 'B':
            beg = i
            if len(sentiments) > 1:
                # remove the effect of the noisy I-{POS,NEG,NEU}
                sentiments = [sentiments[-1]]
        elif pos == 'E':
            end = i
            # schema1: only the consistent sentiment tags are accepted
            # that is, all of the sentiment tags are the same
            if end > beg > -1 and len(set(sentiments)) == 1:
                absa_sequence.append((beg, end, sentiment))
                sentiments = []
                beg, end = -1, -1
    return absa_sequence

def match_ate(gold_ate_sequence, pred_ate_sequence):
    """
    calculate the proportions of correctly predicted aspect terms
    """
    hit_count, gold_count, pred_count = 0, 0, 0
    gold_count = len(gold_ate_sequence)
    pred_count = len(pred_ate_sequence)
    for t in pred_ate_sequence:
        if t in gold_ate_sequence:
            hit_count += 1
    return hit_count, gold_count, pred_count

def match_absa(gold_absa_sequence, pred_absa_sequence):
    """
    calculate the number of correctly predicted aspect sentiment
    :param gold_absa_sequence: gold standard targeted sentiment sequence
    :param pred_absa_sequence: predicted targeted sentiment sequence
    """
    # positive, negative and neutral
    tag2tagid = {'POS': 0, 'NEG': 1, 'NEU': 2}
    hit_count, gold_count, pred_count = np.zeros(3), np.zeros(3), np.zeros(3)
    for t in gold_absa_sequence:
        #print(t)
        ts_tag = t[2]
        tid = tag2tagid[ts_tag]
        gold_count[tid] += 1
    for t in pred_absa_sequence:
        ts_tag = t[2]
        tid = tag2tagid[ts_tag]
        if t in gold_absa_sequence:
            hit_count[tid] += 1
        pred_count[tid] += 1
    return hit_count, gold_count, pred_count

def compute_metrics_absa(pred, gold, label_vocab, tagging_schema):
    """
    compute metric scores for absa task
    """
    assert len(pred) == len(gold)
    num_samples = len(pred)

    # number of true positive, gold standard, predicted aspect sentiment triplet
    n_tp_absa, n_gold_absa, n_pred_absa = np.zeros(3), np.zeros(3), np.zeros(3)
    class_count = np.zeros(3)
    class_precision, class_recall, class_f1 = np.zeros(3), np.zeros(3), np.zeros(3)
    absa_ground_truth, absa_predictions = [], []

    for i in range(num_samples):
        eval_positions = np.where(gold[i] != -100)[0]
        #print("eval_positions:", eval_positions)
        pred_labels = pred[i][eval_positions]
        gold_labels = gold[i][eval_positions]
        pred_tags = [label_vocab[label] for label in pred_labels]
        gold_tags = [label_vocab[label] for label in gold_labels]

        if tagging_schema == 'OT':
            gold_tags = ot2bieos_absa(gold_tags)
            pred_tags = ot2bieos_absa(pred_tags)
        elif tagging_schema == 'BIO':
            gold_tags = ot2bieos_absa(bio2ot_absa(gold_tags))
            pred_tags = ot2bieos_absa(bio2ot_absa(pred_tags))
        else:
            pass # current tagging schema is BIEOS, do nothing
        pred_absa_seq, gold_absa_seq = tag2absa(tag_sequence=pred_tags), tag2absa(tag_sequence=gold_tags)
        hit_count, gold_count, pred_count = match_absa(gold_absa_sequence=gold_absa_seq, pred_absa_sequence=pred_absa_seq)
        absa_ground_truth.append(gold_absa_seq)
        absa_predictions.append(pred_absa_seq)
        # true-positive count
        n_tp_absa += hit_count
        n_gold_absa += gold_count
        n_pred_absa += pred_count

        for (b, e, s) in gold_absa_seq:
            if s == 'POS':
                class_count[0] += 1
            elif s == 'NEG':
                class_count[1] += 1
            else:
                class_count[2] += 1
    print("#POS: {}, #NEG: {}, #NEU: {}".format(class_count[0], class_count[1], class_count[2]))
    for i in range(3):
        num_hit = n_tp_absa[i]
        num_gold = n_gold_absa[i]
        num_pred = n_pred_absa[i]
        class_precision[i] = float(num_hit) / float(num_pred + SMALL_POSITIVE_CONST)
        class_recall[i] = float(num_hit) / float(num_gold + SMALL_POSITIVE_CONST)
        class_f1[i] = 2 * class_precision[i] * class_recall[i] / (class_precision[i] + class_recall[i] + SMALL_POSITIVE_CONST)
    #print("class count:", class_count)
    macro_f1 = class_f1.mean()

    num_hit_total = sum(n_tp_absa)
    num_pred_total = sum(n_pred_absa)
    num_gold_total = sum(n_gold_absa)
    micro_precision = float(num_hit_total) / (num_pred_total + SMALL_POSITIVE_CONST)
    micro_recall = float(num_hit_total) / (num_gold_total + SMALL_POSITIVE_CONST)
    micro_f1 = 2 * micro_precision * micro_recall / (micro_precision + micro_recall + SMALL_POSITIVE_CONST)
    scores = {'macro_f1': macro_f1, 'precision': micro_precision,
              "recall": micro_recall, "micro_f1": micro_f1}

    return scores, absa_ground_truth, absa_predictions

def compute_metrics_ate(pred, gold, label_vocab, tagging_schema):
    """
    compute metric scores for ate task
    """
    assert len(pred) == len(gold)
    num_samples = len(pred)

    # number of true postive, gold standard, predicted aspect terms
    n_tp_ate, n_gold_ate, n_pred_ate = 0, 0, 0
    ate_ground_truth, ate_predictions = [], []

    for i in range(num_samples):
        # eval_positions = np.where(eval_mask[i] == 1)[0]
        eval_positions = np.where(gold[i] != -100)[0]
        pred_labels = pred[i][eval_positions]
        gold_labels = gold[i][eval_positions]
        pred_tags = [label_vocab[label] for label in pred_labels]
        gold_tags = [label_vocab[label] for label in gold_labels]
        if tagging_schema == 'OT':
            gold_tags = ot2bieos_ate(gold_tags)
            pred_tags = ot2bieos_ate(pred_tags)
        elif tagging_schema == 'BIO':
            gold_tags = ot2bieos_ate(bio2ot_ate(gold_tags))
            pred_tags = ot2bieos_ate(bio2ot_ate(pred_tags))
        else:
            pass # current tagging schema is BIEOS, do nothing
        # golden & predicted aspect terms
        gold_ate_seq, pred_ate_seq = tag2ate(tag_sequence=gold_tags), tag2ate(tag_sequence=pred_tags)
        hit_count, gold_count, pred_count = match_ate(gold_ate_sequence=gold_ate_seq, pred_ate_sequence=pred_ate_seq)
        ate_ground_truth.append(gold_ate_seq)
        ate_predictions.append(pred_ate_seq)
        n_tp_ate += hit_count
        n_gold_ate += gold_count
        n_pred_ate += pred_count
    print("number of gold aspect terms: {}".format(n_gold_ate))
    precision = float(n_tp_ate) / (float(n_pred_ate) + SMALL_POSITIVE_CONST)
    recall = float(n_tp_ate) / (float(n_gold_ate) + SMALL_POSITIVE_CONST)
    f1 = 2 * precision * recall / (precision + recall + SMALL_POSITIVE_CONST)
    scores = {'precision': precision, 'recall': recall, 'f1': f1}
    return scores, ate_ground_truth, ate_predictions

def write_sents_labels(word_seqs, label_seqs, file_name):
    lines = []
    for i in range(len(word_seqs)):
        for j in range(len(word_seqs[i])):
            if word_seqs[i][j] != '\u200b':
                lines.append(f"{word_seqs[i][j]}\t{label_seqs[i][j]}\tNONE-CATE\n")
        lines.append('\n')

    with open(file_name, 'w+', encoding='UTF-8') as fp:
        fp.writelines(lines)

    print(f"Write file to {file_name}")

max_stars_repo_name: IsakZhang/XABSA

max_issues_repo_name: IsakZhang/XABSA

max_forks_repo_name: IsakZhang/XABSA


text:
import networkx
from random import randint
import numpy as np
import time
import sys

'''
2opt algorithm for the traveling salesman problem

One parameter: the number of nodes to solve

Generates a set of random nodes and finds an "optimal" tour

'''

#No. of nodes
iter = int(sys.argv[1])

#Initialize cost variable
cost = 10**6

#-------CLASS-------#

#Node class
class Node:

    def __init__(self,x,y,id):
        self._x = x
        self._y = y
        self._id = id

    def dist(self,next):
        return ((self._x - next._x)**2+(self._y - next._y)**2)**.5

#-------DATA-------#

#Matrix of nodes (randomly generated)
route = []
for i in range(iter):
    x = randint(0,100)
    y = randint(0,100)
    route.append(Node(x,y,i))

#-------FUNCTIONS-------#

def totalDistance(route):
    dist = 0
    for k in range(0,len(route)-1):
        dist += route[k].dist(route[k+1])
    return dist

def swap(route,i,k):
    new_route = []
    for j in range(0,i):
        new_route.append(route[j])
    for j in range(k,i-1,-1):
        new_route.append(route[j])
    for j in range(k+1,len(route)):
        new_route.append(route[j])
    return new_route

def optimalRoute(route):
    cost = 10**6
    for i in range(len(route)):
        for j in range(len(route)):
            new_route = swap(route,i,j)
            new_cost = totalDistance(new_route)
            if new_cost < cost:
                route = new_route
                cost = new_cost
    return route,cost

#-------MAIN-------#

start = time.time()
optR,minCost = optimalRoute(route)
end = time.time()

for r in optR:
    print("Steg:",r._id)

print("Kostnad",minCost)
print("Antall minutter",(end-start)/60)

reason: import numpy,import networkx


text:
import pandas as pd
import numpy as np

class Division(dict):
    def __init__(self, division_dict, previouslevel=None):
        super().__init__(division_dict)
        self.regions = {}
        for i, c in division_dict.items():
            if not c in self.regions:
                self.regions[c] = set()
            self.regions[c].add(i)
        if previouslevel:
            previouslevel = previouslevel.copy()
        self.previouslevel = previouslevel

    def FromRegions(regions):
        return Division({
            i: l
            for l,c in regions.items()
            for i in c
        })

    def copy(self):
        c = super().copy()
        previous = self.previouslevel
        if previous != None:
            previous = previous.copy()
        return Division(c,previouslevel=previous)

    def __setitem__(self, key, value):
        self.regions[self[key]].remove(key)
        if len(self.regions[self[key]]) == 0:
            del self.regions[self[key]]
        if not value in self.regions:
            self.regions[value] = set()
        self.regions[value].add(key)
        super().__setitem__(key, value)

    def __str__(self):
        return self.regions.__str__()

    def labels(self):
        return self.regions.keys()

    def newlabel(self):
        return 1+max([-1,*(l for l in self.labels() if isinstance(l,int))])

    def relabel_regions(self,random=False):
        newlabels = list(range(len(self.labels())))
        if random:
            np.random.shuffle(newlabels)
        mapping = dict(zip(self.labels(), newlabels))
        return Division({
            a: mapping[r]
            for a,r in self.items()
        })

    def to_csv(self,file_name,index_label='item',cluster_label='cluster'):
        import pandas as pd
        pd.Series(self).to_csv(file_name,index_label=index_label,header=[cluster_label])

    @staticmethod
    def FromCSV(path):
        return Division(pd.read_csv(path, index_col='item')['cluster'].to_dict())
    '''
        The following functions allow for the division to be viewed as a hierarchical object.
    '''
    def flatClustering(self):
        if self.previouslevel == None:
            return self
        return Division({
            i: self[c]
            for i, c in self.previouslevel.flatClustering().items()
        })

    def flatitems(self,topitems):
        if self.previouslevel == None:
            return topitems
        return set().union(*[
            self.previouslevel.flatitems(self.previouslevel.regions[i])
            for i in topitems
        ])

    # Add an additional level to the hierarchy
    def nextlevel(self):
        return Division(dict(zip(self.labels(),self.labels())),self)

max_issues_repo_name: MartijnGosgens/CovidMobilityTradeOffs

max_forks_repo_name: MartijnGosgens/CovidMobilityTradeOffs