text:
#!/usr/bin/env python
# Filename : mgCFramePlot.py

import numpy
import argparse
from mgCFrameRead import CFrame as cf
from matplotlib import pyplot as plt

def HighlightOutput(width=40, symbol='#'):
    """
    Print something out in a highlight mode
    """
    bar = symbol * width
    print bar
    return None

def getFrameStr(name):
    if name[-1] is 'z':
        frameStr = name.replace(".fits.gz", "")
    elif name[-1] is 's':
        frameStr = name.replace(".fits", "")
    else:
        raise Exception("Wrong file format for mgCFrame !!")
    return frameStr

def plotSize19Avg1(wave, fluxarr, slitmap, name):
    slitindex = numpy.argwhere(slitmap['IFUSIZE'] == 19).reshape(2,21)
    fluxUse = fluxarr[slitindex[0]]
    fluxAvg = numpy.mean(fluxUse, axis=0)

    use = numpy.argwhere(numpy.logical_or(wave <= 5565, wave >=5590))
    minFlux = min(fluxAvg[use])
    maxFlux = max(fluxAvg[use])

    #------------------------------------------------------------
    # Plot the spectrum
    #------------------------------------------------------------
    plt.clf()
    ax1 = plt.axes()
    ax1.plot(wave, fluxAvg, '-', color='gray', label='error')
    ax1.set_xlabel(r'$\lambda (\AA)$')
    ax1.set_ylabel('Flux')

    ax1.set_xlim(min(wave), max(wave))
    ax1.set_ylim(minFlux, maxFlux)

    plt.savefig(name + "_ifusize19a_avg.png")

    return None

def plotSize19Avg2(wave, fluxarr, slitmap, name):
    slitindex = numpy.argwhere(slitmap['IFUSIZE'] == 19).reshape(2,21)
    fluxUse = fluxarr[slitindex[1]]
    fluxAvg = numpy.mean(fluxUse, axis=0)

    use = numpy.argwhere(numpy.logical_or(wave <= 5565, wave >=5590))
    minFlux = min(fluxAvg[use])
    maxFlux = max(fluxAvg[use])

    #------------------------------------------------------------
    # Plot the spectrum
    #------------------------------------------------------------
    plt.clf()
    ax2 = plt.axes()
    ax2.plot(wave, fluxAvg, '-', color='gray', label='error')
    ax2.set_xlabel(r'$\lambda (\AA)$')
    ax2.set_ylabel('Flux')

    ax2.set_xlim(min(wave), max(wave))
    ax2.set_ylim(minFlux, maxFlux)

    plt.savefig(name + "_ifusize19b_avg.png")

    return None

def plotSize37Avg(wave, fluxarr, slitmap, name):
    slitindex = numpy.argwhere(slitmap['IFUSIZE'] == 37).reshape(4,39)
    fluxUse = fluxarr[slitindex[3]]
    fluxAvg = numpy.mean(fluxUse, axis=0)

    use = numpy.argwhere(numpy.logical_or(wave <= 5565, wave >=5590))
    minFlux = min(fluxAvg[use])
    maxFlux = max(fluxAvg[use])

    #------------------------------------------------------------
    # Plot the spectrum
    #------------------------------------------------------------
    plt.clf()
    ax3 = plt.axes()
    ax3.plot(wave, fluxAvg, '-', color='gray', label='error')
    ax3.set_xlabel(r'$\lambda (\AA)$')
    ax3.set_ylabel('Flux')

    ax3.set_xlim(min(wave), max(wave))
    ax3.set_ylim(minFlux, maxFlux)

    plt.savefig(name + "_ifusize37_avg.png")

    return None

def main(name, fiber):

    #------------------------------------------------------------
    # Load in the mgCFrame file;
    # cframe is a HDUlist object from astropy.io.fits
    cframe = cf(name)
    wave    = cframe.getWave()
    fluxarr = cframe.getFlux()
    ferrarr = cframe.getError()
    fskyarr = cframe.getSky()
    maskarr = cframe.getMask()
    header  = cframe.getHeader()
    slitmap = cframe.getSlitMap()

    frameStr = getFrameStr(name)

    #plateRa, plateDec = cframe.getCenPos()
    plateType  = header["PLATETYP"]
    surveyMode = header["SRVYMODE"]
    exptime    = header["EXPTIME"]
    slitfile   = header["SLITFILE"]

    #------------------------------------------------------------
    # Check the index for fiber
    if (fiber < 1 or fiber >= cframe.nfiber):
        raise Exception("FIBER should be between 1 and %d"%(cframe.nfiber))
    flux = fluxarr[fiber-1]
    ferr = ferrarr[fiber-1]
    fsky = fskyarr[fiber-1]
    finfo = slitmap[fiber-1]
    #mask = maskarr[fiber-1]

    holetype   = finfo["HOLETYPE"]
    fiberid    = finfo["FIBERID"]
    fibertype  = finfo["FIBERTYPE"]
    plugstatus = finfo["PLUGSTATUS"]
    targettype = finfo["TARGETTYPE"]
    ifuinblock = finfo["IFUINBLOCK"]
    ifusize    = finfo["IFUSIZE"]
    ifudesign  = finfo["IFUDESIGN"]
    mangaid    = finfo["MANGAID"]

    use = numpy.argwhere(numpy.logical_or(wave <= 5565, wave >=5590))
    minFlux = min(min(flux[use]), min(fsky[use]))
    maxFlux = max(max(flux[use]), max(fsky[use]))

    #------------------------------------------------------------
    # Meng's plot
    #------------------------------------------------------------
    plotSize19Avg1(wave, fluxarr, slitmap, frameStr)
    plotSize19Avg2(wave, fluxarr, slitmap, frameStr)
    # TODO : Check the reshape part
    plotSize37Avg(wave, fluxarr, slitmap, frameStr)

    #------------------------------------------------------------
    # Display some useful information about thefile
    HighlightOutput(width=60)
    print " Read in mgCFrame file: " + name
    print "   This PLATE has %d fibers on it"%(cframe.nfiber)
    print "   Each spectrum has %d data points in it"%(cframe.npixel)
    print "   The wavelength range is %7.1d to %7.1d Angstrom"%(cframe.minwave,
                                                          cframe.maxwave)
    HighlightOutput(width=60)
    #print "   The central (RA,DEC) of the plate is (%d,%d)"%(plateRa,plateDec)
    print "   The type of this plate is : " + plateType
    print "   The survey mode for this plate is : " + surveyMode
    print "   The exposure time for this plate is : %6.1d seconds"%(exptime)
    print "   The associated slitmap name is :" + slitfile
    HighlightOutput(width=60)
    HighlightOutput(width=60)
    print " Will plot the spectra for fiber : " + str(fiber)
    HighlightOutput(width=60)

    #------------------------------------------------------------
    # Plot the spectrum
    #------------------------------------------------------------
    plt.clf()
    ax = plt.axes()
    ax.plot(wave, ferr, '-', color='gray', label='error')
    ax.plot(wave, fsky, '-', color='cyan', label='sky')
    ax.plot(wave, flux, '-k', label='spectrum')
    ax.set_title('File = ' + name + ', Fiber = %(fiber)i' % locals())

    ax.text(0.03, 0.95, "Holetype: " + holetype, size=12,
        ha='left', va='top', transform=ax.transAxes)
    ax.text(0.03, 0.90, "TargetType: " + targettype, size=12,
        ha='left', va='top', transform=ax.transAxes)
    ax.text(0.03, 0.85, "IFUinBlock: %d"%ifuinblock, size=12,
        ha='left', va='top', transform=ax.transAxes)
    ax.text(0.03, 0.80, "IFUSize: %d"%ifusize, size=12,
        ha='left', va='top', transform=ax.transAxes)
    ax.text(0.03, 0.75, "IFUDesign: %d"%ifudesign, size=12,
        ha='left', va='top', transform=ax.transAxes)
    ax.text(0.03, 0.70, "MaNGAID: " + mangaid, size=12,
        ha='left', va='top', transform=ax.transAxes)

    ax.set_xlabel(r'$\lambda (\AA)$')
    ax.set_ylabel('Flux')

    ax.set_xlim(cframe.minwave, cframe.maxwave)
    ax.set_ylim(minFlux, maxFlux)

    plt.savefig(frameStr + "-%d.png"%(fiber))

    return None

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("name", help="Name or location of the mgCFrame file")
    parser.add_argument("fiber", type=int, help="Number of the fiber")
    args = parser.parse_args()

    main(args.name, args.fiber)

reason: import numpy,from astropy


text:
import numpy as np
import argparse
import imutils
import time
import cv2
import os
from __future__ import print_function

#from abc import ABCMeta, abstractmethod
import numpy as np

from multiprocessing.pool import ThreadPool
from collections import deque
import pandas as pd

tasks=['object detection','classcification']
MODELADDRESS='list_topologies.yml'

class paraser:

    def __init__(self,task,config):
        self.videoAddress=config["video"]
        self.task=task
        self.confidence=config['confidence']
        self.threshold=config['threshold']
        self.classlabel=config['class']
        self.modelinfo=None
        self.modelWeight=None
        self.backend=config['backend']
        self.write=config['write']
        self.multiThread=config['multiThread']
        self.chooseModel=None
        self.inputSize=None

        def preprocess(self,config):
            if self.task not in tasks:
                print('Unknown task: ' + self.task)
                exit()
            if not config["modelChoose"]:
                #default model
                if self.task is "object detection":
                    self.chooseModel="mobileNet-SSD"
                if self.task is "classcification":
                    self.chooseModel="mobileNet"
            else: self.chooseModel=config['modelChoose']
            if not config["model"]:
                self.modelWeight,self.modelConfigs=downloadModel(self,MODELADDRESS)
            else:
                self.modelWeight,self.modelinfo=config["modelWeight"], config['modelinfo']
            if not config["inputSize"]:
                self.inputSize=findInputsize(self.modelinfo)

            modelConfigs={'framework':self.backend,
                            "modelTask":self.task,
                            "modelLabels":self.classlabel
                            "weightAddress":self.modelWeight
                            "configAddress":self.modelinfo
                            "inputSize":self.inputSize}

            Videoconfigs={"inputSize": self.inputSize,
                            "writeVideo": self.,
                            'videoAddress': }

        def downloadModel(self,YAML):
            pass

        def findInputSize(self.modelinfo):
            pass

class Video:
    def __init__(self, Videoconfigs):
        self.inputSize=Videoconfigs["inputSize"]
        self.writeVideo=Videoconfigs["writeVideo"]
        #self.threadn = 1
        self.threadn = cv.getNumberOfCPUs()
        self.pool = ThreadPool(processes = threadn)
        #self.pool = dests
        self.pending = deque()
        self.videoReader= cv2.VideoCapture(config['videoAddress'])

    def videoProcess(Model):
        #writer = None
        #(W, H) = (None, None)
        try:
            prop = cv2.cv.CV_CAP_PROP_FRAME_COUNT if imutils.is_cv2() \
                else cv2.CAP_PROP_FRAME_COUNT
            total = int(self.videoReader.get(prop))
            print("[INFO] {} total frames in video".format(total))

        # an error occurred while trying to determine the total
        # number of frames in the video file
        except:
            print("[INFO] could not determine # of frames in video")
            print("[INFO] no approx. completion time can be provided")
            total = -1
        output={}
        framesID=0
        last_frame_time = clock()
         while True:

            while len(pending) > 0 and pending[0].ready():
                frameDict=pending.popleft().get()
                output["framesID"]=frameDict
            if len(self.pending) < self.threadn:
                (grabbed, frame) = self.videoReader.read()
                framesID+=1
                if not grabbed:
                    break
                blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (self.inputSize, self.inputSize),
                    swapRB=True, crop=False)
                t = clock()
                #frame_interval.update(t - last_frame_time)
                last_frame_time = t

                task = pool.apply_async(Model.process_frame, (blob.copy()))
                pending.append(task)
                #Model.process

        return output
    '''
    def videoToFrame(Videoconfigs):
        return 1
    '''
# parse the user configs

class Model():
    def __init__(self, modelConfigs):
        self.modelNet = None
        self.lastLayer=None
        #self.ln=None
        self.modelTask = Task
        self.framework=modelConfigs['framework']
        self.labels = open(modelConfigs["modelLabels"]).read().strip().split("\n")
        self.weightAddress=modelConfigs["weightAddress"]
        self.configAddress=modelConfigs["configAddress"]
        #self.blobSize=modelConfigs["inputSize"]
        #self.toDownload=config["toDownload"]

    def loadmodel(self):

        self.modelNet=cv2.dnn.readNet(self.configAddress, self.weightAddress,self.framework)
        layerNames = self.modelNet.getLayerNames()
        lastLayerId = self.modelNet.getLayerId(layerNames[-1])
        self.lastLayer = self.modelNet.getLayer(lastLayerId)

        #ln = self.modelNet.getLayerNames()

    ### to check the method has to be
   def process(self,blob):
        if self.modelTask="object detection":
            return self.process_od(blob)
        else:
            return self.process_classcification(blob)

    def process_classcification(self,blob):
        pass

    def process_od(self,blob,frameWidth,frameHeight):
        self.modelNet.setInput(blob)
        outNames=self.modelNet.getUnconnectedOutLayersNames()
        outs=self.modelNet.forward(outNames)

        #sepecific for the object detection model
        output={}
        classIds = []
        confidences = []
        boxes = []
        if self.modelNet.getLayer(0).outputNameToIndex('im_info') != -1:  # Faster-RCNN or R-FCN
            # Network produces output blob with a shape 1x1xNx7 where N is a number of
            # detections and an every detection is a vector of values
            # [batchId, classId, confidence, left, top, right, bottom]
            for out in outs:
                for detection in out[0, 0]:
                    confidence = detection[2]
                    if confidence > self.confThreshold:
                        left = int(detection[3])
                        top = int(detection[4])
                        right = int(detection[5])
                        bottom = int(detection[6])
                        width = right - left + 1
                        height = bottom - top + 1
                        classIds.append(int(detection[1]) - 1)  # Skip background label
                        confidences.append(float(confidence))
                        boxes.append([left, top, width, height])
        elif self.lastLayer.type == 'DetectionOutput':
            # Network produces output blob with a shape 1x1xNx7 where N is a number of
            # detections and an every detection is a vector of values
            # [batchId, classId, confidence, left, top, right, bottom]
            for out in outs:
                for detection in out[0, 0]:
                    confidence = detection[2]
                    if confidence > self.confThreshold:
                        left = int(detection[3] * frameWidth)
                        top = int(detection[4] * frameHeight)
                        right = int(detection[5] * frameWidth)
                        bottom = int(detection[6] * frameHeight)
                        width = right - left + 1
                        height = bottom - top + 1
                        classIds.append(int(detection[1]) - 1)  # Skip background label
                        confidences.append(float(confidence))
                        boxes.append([left, top, width, height])
        elif lastLayer.type == 'Region':
            #it's for yolo output
            # Network produces output blob with a shape NxC where N is a number of
            # detected objects and C is a number of classes + 4 where the first 4
            # numbers are [center_x, center_y, width, height]
            classIds = []
            confidences = []
            boxes = []
            for out in outs:
                for detection in out:
                    scores = detection[5:]
                    classId = np.argmax(scores)
                    confidence = scores[classId]
                    if confidence > self.confThreshold:
                        center_x = int(detection[0] * frameWidth)
                        center_y = int(detection[1] * frameHeight)
                        width = int(detection[2] * frameWidth)
                        height = int(detection[3] * frameHeight)
                        left = int(center_x - width / 2)
                        top = int(center_y - height / 2)
                        classIds.append(classId)
                        confidences.append(float(confidence))
                        boxes.append([left, top, width, height])
        else:
            print('Unknown output layer type: ' + lastLayer.type)
            exit()

        indices = cv2.dnn.NMSBoxes(boxes, confidences, self.confThreshold, self.nmsThreshold)
        for i in indices:
            i = i[0]
            box = boxes[i]
            left = box[0]
            top = box[1]
            width = box[2]
            height = box[3]
            tempDict={}
            tempDict['className']=classIds[i]
            tempDict['confidences']=confidences[i]
            tempDict['left']=left
            tempDict['top']=top
            tempDict['right']=left + width
            tempDict['bottom']=top + height

            output['box'+str(i)]=tempDict
        return output

def postprocess(outputDict):
    pass

def videoAnalyze(videos,task,config):

    parser=parser(task,config)

    video=Video(videos,configs)

    model=Models(Task,config)
    model.loadmodel()

    outputDict = video.videoProcess(model)
    return outputDict

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video", required=True,
	    help="path to input video")
    ap.add_argument("-o", "--output", default="./",
	    help="path to output video")
    ap.add_argument("-t", "--task", required=True,
	    help="base path to model directory")
    ap.add_argument("-b", "--backend", default='caffe',
        help="modelbackend ")
    ap.add_argument("-choose", "--modelChoose",
        help="which model to use like ssd or FRCNN ")
    ap.add_argument("-m", "--model",
	    help="base path to model directory")
    ap.add_argument("-info", "--modelinfo",
        help="base path to modelinfo file address for tensorflow and caffe")
    ap.add_argument("-class", "--class",default="coco.name",
	    help="path to class file")
    ap.add_argument("-multiThread", "--multiThread",type=bool,default="True",
        help="use multiThread or not ")
    ap.add_argument("-write", "--write",type=bool,default="True",
        help="write the result on video or not ")

    ap.add_argument("-c", "--confidence", type=float, default=0.5,
	help="minimum probability to filter weak detections")
    ap.add_argument("-t", "--threshold", type=float, default=0.3,
	help="threshold when applyong non-maxima suppression")
    args = vars(ap.parse_args())

    Result_File=videoAnalyze(args["video"],args["task"],args)

ext: py

reason: import numpy


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

def minimize(f, x0, eta, h, precision):
    x = x0
    while True:
        prev = x
        finite_diff = f(x + h) - f(x)  # /h rolls into learning rate
        x = x - eta * finite_diff  # decelerates x step as it flattens out
        # print "f(%1.12f) = %1.12f delta = %1.20f" % (x, f(x), f(x) - f(prev))
        if abs(x - prev) < precision:
        # OR, if abs(finite_diff)<precision:
            return x

def minimize_trace(f, x0, eta, h, precision):
    tracex = [x0]
    x = x0
    while True:
        prev = x
        finite_diff = f(x + h) - f(x)  # /h rolls into learning rate
        x = x - eta * finite_diff  # decelerates x step as it flattens out
        tracex.append(x)
        if abs(x - prev) < precision:
            return tracex

def viz_trace(x0):
    # Plot the damped sine curve
    graphx = np.arange(.1, 1.3, 0.01)
    graphy = [f(x) for x in graphx] # or just f(graphx)!
    plt.plot(graphx, graphy)

    random.seed(int(round(time.time() * 1000)))

    tracex = minimize_trace(f, x0, ETA, h, PRECISION)
    tracey = [f(x) for x in tracex]
    plt.scatter(tracex, tracey, color="green", marker='o')
    plt.text(0.3, 4.5, "f(x=%1.5f) = %1.9f, steps = %d" %
             (tracex[-1], f(tracex[-1]), len(tracex)), fontsize=14,
             color="green")
    plt.show()

if __name__ == '__main__':
    ETA = 10
    h = 0.0001
    PRECISION = 0.0000001  # can't be too small as f(x)-f(xprev) prec is low

    def f(x): return np.cos(3 * np.pi * x) / x

    x0 = random.uniform(.1, 1.2)
    viz_trace(x0)

    # Or can just call to get x
    x0 = random.uniform(.1, 1.2)
    minx = minimize(f, x0, ETA, h, PRECISION)
    print "f(%f) = %f" % (minx, f(minx))

    plt.show()


text:
import argparse
import logging
import numpy as np
from time import time
import sys
from core import utils as U
import pickle as pk
import copy

logger = logging.getLogger(__name__)

"""
Helper.py

Contains helper functions needed by other modules
"""

def pad_sequences(sequences, maxlen=None, dtype='int32',
                  padding='pre', truncating='pre', value=0.):
    '''This function is copied from from KERAS version 1.1.1

    Pads each sequence to the same length:
    the length of the longest sequence.

    If maxlen is provided, any sequence longer
    than maxlen is truncated to maxlen.
    Truncation happens off either the beginning (default) or
    the end of the sequence.

    Supports post-padding and pre-padding (default).

    # Arguments
        sequences: list of lists where each element is a sequence
        maxlen: int, maximum length
        dtype: type to cast the resulting sequence.
        padding: 'pre' or 'post', pad either before or after each sequence.
        truncating: 'pre' or 'post', remove values from sequences larger than
            maxlen either in the beginning or in the end of the sequence
        value: float, value to pad the sequences to the desired value.

    # Returns
        x: numpy array with dimensions (number_of_sequences, maxlen)
    '''
    lengths = [len(s) for s in sequences]

    nb_samples = len(sequences)
    if maxlen is None:
        maxlen = np.max(lengths)

    # take the sample shape from the first non empty sequence
    # checking for consistency in the main loop below.
    sample_shape = tuple()
    for s in sequences:
        if len(s) > 0:
            sample_shape = np.asarray(s).shape[1:]
            break

    x = (np.ones((nb_samples, maxlen) + sample_shape) * value).astype(dtype)
    for idx, s in enumerate(sequences):
        if len(s) == 0:
            continue  # empty list was found
        if truncating == 'pre':
            trunc = s[-maxlen:]
        elif truncating == 'post':
            trunc = s[:maxlen]
        else:
            raise ValueError('Truncating type "%s" not understood' % truncating)

        # check `trunc` has expected shape
        trunc = np.asarray(trunc, dtype=dtype)
        if trunc.shape[1:] != sample_shape:
            raise ValueError('Shape of sample %s of sequence at position %s is different from expected shape %s' %
                             (trunc.shape[1:], idx, sample_shape))

        if padding == 'post':
            x[idx, :len(trunc)] = trunc
        elif padding == 'pre':
            x[idx, -len(trunc):] = trunc
        else:
            raise ValueError('Padding type "%s" not understood' % padding)
    return x

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]

def sort_data(x, y, filename_y, lab_x=None):
    """Sort data based on the length of x"""

    test_lab_x = lab_x
    test_xy = zip(x, y, filename_y)
    if not (lab_x is None):
        test_xy = zip(x, y, filename_y, lab_x)

    # Sort tuple based on the length of the first entry in the tuple
    test_xy = sorted(test_xy, key=lambda t: len(t[0]))

    if not (lab_x is None):
        test_x, test_y, test_filename_y, test_lab_x = zip(*test_xy)
    else:
        test_x, test_y, test_filename_y = zip(*test_xy)

    return test_x, test_y, test_filename_y, test_lab_x

def sort_data_given_index(x, y, perm_index, lab_x=None):
    """
    Arrange data sequence given permutation index.
    The index was randomly shuffled before hand (usually for training)
    """
    assert len(x) == len(y)
    train_x = [None]* len(x)
    train_y = [None]* len(y)
    train_lab_x = None
    if not (lab_x is None): train_lab_x = [None] * len(lab_x)

    counter = 0
    for idx in perm_index:
        train_x[idx] = x[counter]
        train_y[idx] = y[counter]
        if not (lab_x is None): train_lab_x[idx] = lab_x[counter]
        counter += 1

    return train_x, train_y, train_lab_x

def split_data_into_chunks(x, y, batch_size, combine_y=True, filename_y=None, lab_x=None):
    """
    Split data into chunks/batches (with the specified batch size) for mini-batch processing in neural network.
    """

    test_x_chunks = list(chunks(x, batch_size))
    test_x = []
    test_x_len = 0

    test_y_chunks = list(chunks(y, batch_size))
    test_y = []
    test_y_len = 0

    test_filename_y_chunks = None
    test_filename_y = []
    test_filename_y_len = 0
    if not (filename_y is None): test_filename_y_chunks = list(chunks(filename_y, batch_size))

    test_lab_x_chunks = None
    test_lab_x = []
    test_lab_x_len = 0
    if not (lab_x is None): test_lab_x_chunks = list(chunks(lab_x, batch_size))

    assert len(test_x_chunks) == len(test_y_chunks)

    for i in range(len(test_x_chunks)):
        curr_test_x = test_x_chunks[i]
        curr_test_x = pad_sequences(curr_test_x)
        test_x.append(curr_test_x)
        test_x_len += len(curr_test_x)

        curr_test_y = test_y_chunks[i]
        curr_test_y = np.array(curr_test_y, dtype='float32')
        test_y.append(curr_test_y)
        test_y_len += len(curr_test_y)

        if not (filename_y is None):
            curr_test_filename_y = test_filename_y_chunks[i]
            test_filename_y.append(curr_test_filename_y)
            test_filename_y_len += len(curr_test_filename_y)

        if not (lab_x is None):
            curr_test_lab_x = test_lab_x_chunks[i]
            curr_test_lab_x = np.array(curr_test_lab_x, dtype='float32')
            test_lab_x.append(curr_test_lab_x)
            test_lab_x_len += len(curr_test_lab_x)

    assert test_x_len == test_y_len
    assert test_x_len == len(y)

    if not (filename_y is None):
        assert test_x_len == test_filename_y_len
    if not (lab_x is None):
        assert test_x_len == test_lab_x_len

    if (combine_y):
        test_y = np.array(y, dtype='float32')

    return test_x, test_y, test_filename_y, test_lab_x

def sort_and_split_data_into_chunks(x, y, filename_y, batch_size, lab_x=None):
    """
    Sort based on length of x
    Split test data into chunks of N (batch size) and pad them per chunk/batch
    Faster processing because of localized padding
    Usually used for validation and testing where the sequence of the dataset does not matter.
    """
    test_lab_x = None
    test_x, test_y, test_filename_y, test_lab_x = sort_data(x, y, filename_y, lab_x=lab_x)

    test_x, test_y, test_filename_y, test_lab_x = split_data_into_chunks(test_x, test_y, batch_size, filename_y=test_filename_y, lab_x=test_lab_x)
    return test_x, test_y, test_filename_y, test_lab_x

def calculate_performance(tps, fps, fns, tns):
    """
    Calculate the performance/evaluation metrics given the confusion matrix (TP, FP, FN, TN)
    The evaluation metrics are:
    Recall/Sensitivity, Precision, Specificity, F1-score, F0.5-score, F1-recall-specificity, F0.5-recall-specificity.
    """
    recall, precision, specificity, f1, f05, accuracy = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
    # f1ss is f1 score of sensitivity(recall) and specificity
    # f05ss is f0.5 score of sensitivity(recall) and specificity
    f1ss, f05ss = 0.0, 0.0

    if (tps + fns > 0): recall = float(tps) / float(tps + fns)
    if (tps + fps > 0): precision = float(tps) / float(tps + fps)
    if (tns + fps > 0): specificity = float(tns) / float(tns + fps)
    if (recall + precision > 0): f1 = 2.0 * recall * precision / (recall + precision)
    if (recall + precision > 0): f05 = 1.25 * recall * precision / ((0.25 * precision) + recall)
    if (tps + tns + fns + fps > 0): accuracy = float(tps + tns) / float (tps + tns + fns + fps)

    if (recall + specificity > 0): f1ss = 2.0 * recall * specificity / (recall + specificity)
    if (recall + specificity > 0): f05ss = 1.25 * recall * specificity / ((0.25 * specificity) + recall)

    return (recall, precision, specificity, f1, f05, accuracy, f1ss, f05ss)

def calculate_confusion_matrix(y_gold, y_pred):
    """
    Calculate the confusion matrix
    True Positive (TP), False Positive (FP), False Negative (FN), and True Negative (TN)
    """
    y_actual = copy.deepcopy(y_gold)
    y_hat = copy.deepcopy(y_pred)
    y_actual = y_actual.tolist()
    y_hat = y_hat.tolist()

    tps, fps, fns, tns = 0, 0, 0, 0
    y_hat_len = len(y_hat)

    for i in range(y_hat_len):
        if y_actual[i] == 1:
            if y_hat[i] == 1: tps += 1
            elif y_hat[i] == 0: fns += 1
        elif y_actual[i] == 0:
            if y_hat[i] == 0: tns += 1
            elif y_hat[i] == 1: fps += 1

    return (tps, fps, fns, tns)

def calculate_confusion_matrix_performance(y_gold, y_pred):
    """
    Calculate the confusion matrix and several evaluation metrics given ground truth and predicted class.
    They are:
    True Positive (TP), False Positive (FP), False Negative (FN), and True Negative (TN)
    Recall/Sensitivity, Precision, Specificity, F1-score, F0.5-score, F1-recall-specificity, F0.5-recall-specificity.
    """
    (tps, fps, fns, tns) = calculate_confusion_matrix(y_gold, y_pred)
    (recall, precision, specificity, f1, f05, accuracy, f1ss, f05ss) = calculate_performance(tps, fps, fns, tns)
    return (tps, fps, fns, tns, recall, precision, specificity, f1, f05, accuracy, f1ss, f05ss)

def get_binary_predictions(pred, threshold=0.5):
    """
    Convert real number predictions between 0.0 to 1.0 to binary predictions (either 0 or 1)
    Using 0.5 as its default threshold unless specified
    """
    binary_pred = copy.deepcopy(pred)
    high_indices = binary_pred >= threshold
    low_indices = binary_pred < threshold
    binary_pred[high_indices] = 1
    binary_pred[low_indices] = 0

    return binary_pred

def compute_class_weight(train_y):
    """
    Compute class weight given imbalanced training data
    Usually used in the neural network model to augment the loss function (weighted loss function)
    Favouring/giving more weights to the rare classes.
    """
    import sklearn.utils.class_weight as scikit_class_weight

    class_list = list(set(train_y))
    class_weight_value = scikit_class_weight.compute_class_weight('balanced', class_list, train_y)
    class_weight = dict()

    # Initialize all classes in the dictionary with weight 1
    curr_max = int(np.max(class_list))
    for i in range(curr_max):
        class_weight[i] = 1

    # Build the dictionary using the weight obtained the scikit function
    for i in range(len(class_list)):
        class_weight[class_list[i]] = class_weight_value[i]

    logger.info('Class weight dictionary: ' + str(class_weight))
    return class_weight

def get_permutation_list(args, train_y):
    """
    Produce a fixed list of permutation indices for training
    So that we dont depend on third party shuffling
    """
    if (args.shuffle_path):
        logger.info('Loading shuffle permutation list from : %s' % args.shuffle_path)
        permutation_list = np.loadtxt(args.shuffle_path, dtype=int)
        logger.info('Loading shuffle permutation list completed!')
        return permutation_list

    shuffle_list_filename = "shuffle_permutation_list_len" + str(len(train_y)) + "_seed" + str(args.shuffle_seed) + ".txt"

    logger.info('Creating and saving shuffle permutation list to %s' % shuffle_list_filename)
    if args.shuffle_seed > 0:
        np.random.seed(args.shuffle_seed)

    # Create permutation list with the number of 2 x epochs
    # Actually 1x number of epoch is enough but just to be safe if need for other purposes
    permutation_list = []
    for ii in range(args.epochs*2):
        p = np.random.permutation(len(train_y))
        permutation_list.append(p)

    permutation_list = np.asarray(permutation_list, dtype=int)
    np.savetxt(args.out_dir_path + "/" + shuffle_list_filename, permutation_list, fmt='%d')

    logger.info('Creating and saving shuffle permutation list completed!')
    return permutation_list

def get_reverse_vocab(vocab):
    """
    Given a vocab list (e.g., {'word': index}), reverse it and return
    (e.g., { index: 'word'})
    """
    reverse_vocab = {0:'<pad>', 1:'<unk>', 2:'<num>', 3:'<newline>'}
    for word, index in vocab.items():
        reverse_vocab[index] = word
    return reverse_vocab

def do_attention_visualization(attention_weights, test_x, vocab, filename_list, score_list, output_foldername='test_output'):
    """
    Given a set of attention weights, vocab, and other miscellaneous attributes,
    this function creates PDF files to visualize the words
    based on the attention weights in the specified directory
    """
    # Make sure the dimension of attention weights and the test set is the same
    assert (test_x.shape == attention_weights.shape)

    # Get reverse vocab
    reverse_vocab = get_reverse_vocab(vocab)

    num_essays = test_x.shape[0]
    num_words = test_x.shape[1]

    # Import all the necessary libraries for plotting
    import matplotlib
    matplotlib.use('Agg')

    import os
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_pdf import PdfPages

    # Loop the number of essays/reviews so that we can reuse the code for the batch processing
    for i in range(num_essays):
        filepath = filename_list[i] # Only one file, so use the original filename
        base = os.path.basename(filepath)
        curr_filename = os.path.splitext(base)[0]

        scaled_pred = score_list[i] # no scaled_prediction in this case, need to modify when threshold is not 0.5
        pred_string = "positive"
        if (scaled_pred < 50.0): pred_string = "negative"

        word_sequence = []
        attention_weights_normalized = []
        attention_weights_max = 0.0
        # Get the max value of the attention weights
        for j in range(num_words):
            if attention_weights_max < attention_weights[i][j]:
                attention_weights_max = attention_weights[i][j]
        # Encode the normalized weights for the words ignoring the padding
        for j in range(num_words):
            current_word = reverse_vocab[test_x[i][j]]
            if current_word == '<pad>':
                continue
            word_sequence.append(current_word)
            attention_weights_normalized.append(attention_weights[i][j]/float(attention_weights_max))

        assert len(word_sequence) == len(attention_weights_normalized)
        current_num_words = len(word_sequence)

        # Create PDF document
        pdf_pages = PdfPages(output_foldername +'/' + str(curr_filename) + '.pdf')
        j = 0
        done = False
        page = 0

        while not done:
            # A4 size to be printed on paper, dpi somehow cant increase beyond 100, therefore increasing the plot size
            # All numbers are magic numbers according to trial and error
            fig = plt.figure(figsize=(8.27*3, 11.69*3), dpi=100)
            curr_x = 0.005
            curr_y = 0.98
            plt.axes([0.025,0.025,0.95,0.95])
            plt.axis('off')
            while j < current_num_words:
                word = word_sequence[j]
                # If new line, print new line and continue
                if word == '<newline>':
                    curr_x = 0.005
                    curr_y -= 0.02
                    j += 1
                    continue

                # print words with the background colour
                word_len = len(word)
                t = plt.text(curr_x, curr_y, word, ha='left', va='center', color="#000000", alpha=1.0,
                        transform=plt.gca().transAxes, fontsize=30)
                t.set_bbox(dict(color='red', alpha=attention_weights_normalized[j]))

                # increase xy coordinate for the next text
                curr_x += (word_len**(0.75)) *0.022
                if (curr_x > 0.90):
                    curr_x = 0.005
                    curr_y -= 0.02

                j += 1

                if (curr_y < 0.01):
                    break

            # Show in the picture the prediction value and the filename
            if page == 0:
                ymin, ymax = plt.ylim()
                xmin, xmax = plt.xlim()
                plt.text(
                    xmin, ymax, 'ID : %s     Prediction : %s    Prediction score: %.4f%%' %
                    (curr_filename, pred_string, scaled_pred) , fontsize=30, style='normal',
                    bbox={'facecolor':'yellow', 'alpha':1.0, 'pad':10})

            pdf_pages.savefig(fig)
            plt.close(fig)
            page += 1

            if j == current_num_words: done = True

        pdf_pages.close()

        logger.info('Created Attention PDF file: ' + output_foldername + curr_filename + '.pdf' + ' (' + str(page) + ' pages)')

max_stars_repo_name: ovechkinVT/SkipRNN

max_issues_repo_name: ovechkinVT/SkipRNN

max_forks_repo_name: ovechkinVT/SkipRNN


text:
# Dhaval Kadia, 101622808
# Traffic-Sign Recognition

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

def binary(path):
    image = mpimg.imread(path)
    t = tf.cast(image, tf.float32)
    t = tf.reshape(t, [400, -1, 4])
    t = t[:, 0]
    t = t[:, 0]
    t = tf.reshape(t, [20, 20, 1])
    t = tf.div(t, 255.0)
    return t

def binary_transpose(path):
    image = mpimg.imread(path)
    t = tf.cast(image, tf.float32)
    t = tf.reshape(t, [400, -1, 4])
    t = t[:, 0]
    t = t[:, 0]
    t = tf.div(t, 255.0)

    t = tf.reshape(t, [20, 20])
    t = tf.transpose(t)
    t = tf.reshape(t, [20, 20, 1])

    return t

def NN(mat):
    y1 = tf.nn.relu(tf.nn.conv2d(mat, w1, strides = [1, 1, 1, 1], padding = 'SAME') + b1)
    y2 = tf.nn.relu(tf.nn.conv2d(y1,  w2, strides = [1, 2, 2, 1], padding = 'SAME') + b2)
    y3 = tf.nn.relu(tf.nn.conv2d(y2,  w3, strides = [1, 2, 2, 1], padding = 'SAME') + b3)

    yy = tf.reshape(y3, shape = [-1, 5 * 5 * 10])

    y4 = tf.nn.relu(tf.matmul(yy, w4) + b4)
    y_ = tf.nn.softmax(tf.matmul(y4, w5) + b5)

    return y_

image_n = 360

t_ = tf.placeholder(tf.float32, shape = [image_n,4], name = "t-input")
x_ = tf.placeholder(tf.float32, shape = [image_n,20,20,1], name = "x-input")

w1 = tf.Variable(tf.truncated_normal([2, 2,  1,  6], stddev = 0.1))
b1 = tf.Variable(tf.ones([6])/10)

w2 = tf.Variable(tf.truncated_normal([4, 4,  6, 16], stddev = 0.1))
b2 = tf.Variable(tf.ones([16])/10)

w3 = tf.Variable(tf.truncated_normal([5, 5, 16, 10], stddev = 0.1))
b3 = tf.Variable(tf.ones([10])/10)

w4 = tf.Variable(tf.truncated_normal([5*5*10, 400], stddev = 0.1))
b4 = tf.Variable(tf.ones([400])/10)

w5 = tf.Variable(tf.truncated_normal([400, 4], stddev = 0.1))
b5 = tf.Variable(tf.ones([4])/10)

y_ = NN(x_)

cost = tf.reduce_mean(tf.square(y_ - t_))

learning_rate = 0.01

train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

ext = ".bmp"
left_train = "C:/Users/dhaval/Desktop/img/l/a"
right_train = "C:/Users/dhaval/Desktop/img/r/a"

x_val = []
x_val = tf.reshape(x_val, [-1, 20, 20, 1])
t_value = []
t_value = tf.reshape(t_value, [-1, 4])

t = tf.eye(4)

for n in range(1, 11):
    img_bin = binary(left_train + str(n) + ext)
    x_val = tf.concat([x_val, [img_bin]], 0)
    t_value = tf.concat([t_value,  tf.gather_nd(t, [[0]])], 0)

    img_bin = binary_transpose(left_train + str(n) + ext)
    x_val = tf.concat([x_val, [img_bin]], 0)
    t_value = tf.concat([t_value,  tf.gather_nd(t, [[2]])], 0)

for n in range(1, 11):
    img_bin = binary(right_train + str(n) + ext)
    x_val = tf.concat([x_val, [img_bin]], 0)
    t_value = tf.concat([t_value,  tf.gather_nd(t, [[1]])], 0)

    img_bin = binary_transpose(right_train + str(n) + ext)
    x_val = tf.concat([x_val, [img_bin]], 0)
    t_value = tf.concat([t_value,  tf.gather_nd(t, [[3]])], 0)

sh = 1

x_val_d = tf.roll(x_val, shift=[sh, 0], axis=[1, 2])
x_val_u = tf.roll(x_val, shift=[-sh, 0], axis=[1, 2])
x_val_r = tf.roll(x_val, shift=[0, sh], axis=[1, 2])
x_val_l = tf.roll(x_val, shift=[0, -sh], axis=[1, 2])

x_val_dr = tf.roll(x_val, shift=[sh, sh], axis=[1, 2])
x_val_dl = tf.roll(x_val, shift=[sh, -sh], axis=[1, 2])
x_val_ur = tf.roll(x_val, shift=[-sh, sh], axis=[1, 2])
x_val_ul = tf.roll(x_val, shift=[-sh, -sh], axis=[1, 2])

x_val = tf.concat([x_val, x_val_d], 0)
x_val = tf.concat([x_val, x_val_u], 0)
x_val = tf.concat([x_val, x_val_r], 0)
x_val = tf.concat([x_val, x_val_l], 0)

x_val = tf.concat([x_val, x_val_dr], 0)
x_val = tf.concat([x_val, x_val_dl], 0)
x_val = tf.concat([x_val, x_val_ur], 0)
x_val = tf.concat([x_val, x_val_ul], 0)

t_value_org = t_value
for x in range(0, 8):
    t_value = tf.concat([t_value, t_value_org], 0)

x_val = tf.cast(x_val, tf.float32)
x_value = x_val.eval(session=sess)

t_value = tf.cast(t_value, tf.float32)
t_value = t_value.eval(session=sess)

left_test = "C:/Users/dhaval/Desktop/img/l/t"
right_test = "C:/Users/dhaval/Desktop/img/r/t"
up_test = "C:/Users/dhaval/Desktop/img/u/t"
down_test = "C:/Users/dhaval/Desktop/img/d/t"

test_images = []
test_images = tf.reshape(test_images, [-1, 20, 20, 1])

for n in range(1, 7):
    img_bin = binary(left_test + str(n) + ext)
    test_images = tf.concat([test_images, [img_bin]], 0)

for n in range(1, 7):
    img_bin = binary(right_test + str(n) + ext)
    test_images = tf.concat([test_images, [img_bin]], 0)

for n in range(1, 7):
    img_bin = binary(up_test + str(n) + ext)
    test_images = tf.concat([test_images, [img_bin]], 0)

for n in range(1, 7):
    img_bin = binary(down_test + str(n) + ext)
    test_images = tf.concat([test_images, [img_bin]], 0)

for i in range(1000000):
    sess.run(train_step, feed_dict={x_: x_value, t_: t_value})
    if i % 100 == 0:
        print('Epoch ', i)
        sess.run(y_, feed_dict={x_: x_value, t_: t_value})

        sess.run(w1)
        sess.run(b1)
        sess.run(w2)
        sess.run(b2)
        sess.run(w3)
        sess.run(b3)
        sess.run(w4)
        sess.run(b4)
        sess.run(w5)
        sess.run(b5)

        sess.run(cost, feed_dict={x_: x_value, t_: t_value})

        learning_rate = learning_rate / 1.2

        for n in range(0, 24):           # CAUTION !!! Index starts from 0 for gather_nd
            test_img = tf.gather_nd(test_images, [[n]])

            y_ = NN(test_img)

            if n % 6 == 0:
                print('')

            print('Image ',n + 1,' : ', sess.run(y_))

max_stars_repo_name: DhavalKadia/machine-learning

max_issues_repo_name: DhavalKadia/machine-learning

max_forks_repo_name: DhavalKadia/machine-learning


text:
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import copy
import logging
import numpy as np
from typing import List, Union
import torch

import detectron.data.detection_utils as utils
import detectron.data.transforms as T
from detectron.config import configurable

from .detection_utils import annotations_to_instances, transform_instance_annotations

__all__ = [
    "PointSupDatasetMapper",
]

class PointSupDatasetMapper:
    """
    The callable currently does the following:
    1. Read the image from "file_name"
    2. Applies transforms to the image and annotations
    3. Prepare data and annotations to Tensor and :class:`Instances`
    """

    @configurable
    def __init__(
        self,
        is_train: bool,
        *,
        augmentations: List[Union[T.Augmentation, T.Transform]],
        image_format: str,
        # Extra data augmentation for point supervision
        sample_points: int = 0,
    ):
        """
        NOTE: this interface is experimental.

        Args:
            is_train: whether it's used in training or inference
            augmentations: a list of augmentations or deterministic transforms to apply
            image_format: an image format supported by :func:`detection_utils.read_image`.
            sample_points: subsample points at each iteration
        """
        # fmt: off
        self.is_train               = is_train
        self.augmentations          = T.AugmentationList(augmentations)
        self.image_format           = image_format
        self.sample_points          = sample_points
        # fmt: on
        logger = logging.getLogger(__name__)
        mode = "training" if is_train else "inference"
        logger.info(f"[DatasetMapper] Augmentations used in {mode}: {augmentations}")
        logger.info(f"Point Augmentations used in {mode}: sample {sample_points} points")

    @classmethod
    def from_config(cls, cfg, is_train: bool = True):
        augs = utils.build_augmentation(cfg, is_train)
        if cfg.INPUT.CROP.ENABLED and is_train:
            raise ValueError("Crop augmentation not supported to point supervision.")

        ret = {
            "is_train": is_train,
            "augmentations": augs,
            "image_format": cfg.INPUT.FORMAT,
            "sample_points": cfg.INPUT.SAMPLE_POINTS,
        }

        return ret

    def __call__(self, dataset_dict):
        """
        Args:
            dataset_dict (dict): Metadata of one image, in Detectron2 Dataset format.
        Returns:
            dict: a format that builtin models in detectron2 accept
        """
        dataset_dict = copy.deepcopy(dataset_dict)  # it will be modified by code below
        image = utils.read_image(dataset_dict["file_name"], format=self.image_format)
        utils.check_image_size(dataset_dict, image)

        aug_input = T.AugInput(image)
        transforms = self.augmentations(aug_input)
        image = aug_input.image

        image_shape = image.shape[:2]  # h, w
        # Pytorch's dataloader is efficient on torch.Tensor due to shared-memory,
        # but not efficient on large generic data structures due to the use of pickle & mp.Queue.
        # Therefore it's important to use torch.Tensor.
        dataset_dict["image"] = torch.as_tensor(np.ascontiguousarray(image.transpose(2, 0, 1)))

        if not self.is_train:
            dataset_dict.pop("annotations", None)
            return dataset_dict

        if "annotations" in dataset_dict:
            # Maps points from the closed interval [0, image_size - 1] on discrete
            # image coordinates to the half-open interval [x1, x2) on continuous image
            # coordinates. We use the continuous-discrete conversion from Heckbert
            # 1990 ("What is the coordinate of a pixel?"): d = floor(c) and c = d + 0.5,
            # where d is a discrete coordinate and c is a continuous coordinate.
            for ann in dataset_dict["annotations"]:
                point_coords_wrt_image = np.array(ann["point_coords"]).astype(np.float)
                point_coords_wrt_image = point_coords_wrt_image + 0.5
                ann["point_coords"] = point_coords_wrt_image

            annos = [
                # also need to transform point coordinates
                transform_instance_annotations(
                    obj,
                    transforms,
                    image_shape,
                )
                for obj in dataset_dict.pop("annotations")
                if obj.get("iscrowd", 0) == 0
            ]
            instances = annotations_to_instances(
                annos,
                image_shape,
                sample_points=self.sample_points,
            )

            dataset_dict["instances"] = utils.filter_empty_instances(instances)
        return dataset_dict


text:
from six.moves import range

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

color_in = 'b'
color_out = 'r'

d = .02
D = .075
e = 0.05
compt = 0
for s in [0.3, 0.7]:
    compt += 1
    fig = plt.figure(compt, figsize=(8, 8), facecolor='white')
    ax = fig.add_subplot(111, aspect='equal')
    ax.add_patch(Rectangle((s, -.5), -1.5-s, 1, alpha=0.1, fill=True, color=color_in))
    ax.plot([s,s],[-.5,.5], 'k-', linewidth=2)
    ax.plot([-1,1],[-1,-1],'k-')
    ax.plot([-1,-1],[-1-d,-1+d], 'k-')
    ax.plot([0,0],[-1-d,-1+d], 'k-')
    ax.plot([1,1],[-1-d,-1+d], 'k-')
    ax.text(-.5,-1-D, r'$\Delta x=1$',
            verticalalignment = 'center',
            horizontalalignment = 'center',)
    ax.text(.5,-1-D, r'$\Delta x$',
            verticalalignment = 'center',
            horizontalalignment = 'center',)

    ax.plot([0,0],[-.75-d,-.75+d], 'k-')
    ax.plot([s,s],[-.75-d,-.75+d], 'k-')
    ax.plot([1,1],[-.75-d,-.75+d], 'k-')
    ax.text(s/2,-.75-D, r'$s$',
            verticalalignment = 'center',
            horizontalalignment = 'center',)
    ax.text((1+s)/2,-.75-D, r'$1-s$',
            verticalalignment = 'center',
            horizontalalignment = 'center',)
    if s<.5:
        ax.plot([-1,1],[-.75,-.75],'k-')
        ax.plot([-1,-1],[-.75-d,-.75+d], 'k-')
        ax.plot([2*s-1,2*s-1],[-.75-d,-.75+d], 'k-')
        ax.text(s-.5,-.75-D, r'$1-2s$',
                verticalalignment = 'center',
                horizontalalignment = 'center',)
        ax.text(s-1,-.75-D, r'$2s$',
                verticalalignment = 'center',
                horizontalalignment = 'center',)
        ax.plot([2*s-1,2*s-1],[-.75,.5],'k--')
    else:
        ax.plot([0,1],[-.75,-.75],'k-')
        ax.plot([2*s-1,2*s-1],[-.65,.5],'k--')
        ax.plot([0,s],[-.65,-.65],'k-')
        ax.plot([0,0],[-.65-d,-.65+d],'k-')
        ax.plot([s,s],[-.65-d,-.65+d],'k-')
        ax.plot([2*s-1,2*s-1],[-.65-d,-.65+d],'k-')
        ax.text((3*s-1)/2,-.65+D, r'$1{-}s$',
                verticalalignment = 'center',
                horizontalalignment = 'center',)
        ax.text(s-.5,-.65+D, r'$2s{-}1$',
                verticalalignment = 'center',
                horizontalalignment = 'center',)

    ax.scatter([-1,0],[0,0], marker='o', color=color_in)
    ax.scatter([1],[0], marker='s', color=color_out)

    ax.plot([1-e,s],[0,0],'--', color=color_out)
    ax.plot([2*s-1,s], [e, e], '-', color=color_in)
    ax.arrow(s, -e, -s, 0.,
             length_includes_head=True,
             head_width=.5*e,
             head_length=e,
             fc=color_out,
             ec=color_out)

    ax.axis('off')
    if s<.5:
        signe = "<"
    else:
        signe = ">"
    plt.title(r"Bouzidi condition: $s{0}1/2$".format(signe))
    plt.show()

max_forks_repo_name: gouarin/pylbm


text:
import sys
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
from data.read_data import read_data
import json
from sklearn import metrics
import os
import yaml
import numpy as np

def validate(model,data):
    print("-- RUNNING VALIDATION --", flush=True)

    # The data, split between train and test sets.

    # Training error (Client validates global model on same data as it trains on.)
    (x_train, y_train) = read_data(data, trainset=True)

    # Test error (Client has a small dataset set aside for validation)
    (x_test, y_test) = read_data(data, trainset=False)

    try:
        model_score = model.evaluate(x_train, y_train, verbose=0)
        print('Training loss:', model_score[0])
        print('Training accuracy:', model_score[1])

        model_score_test = model.evaluate(x_test, y_test, verbose=0)
        print('Test loss:', model_score_test[0])
        print('Test accuracy:', model_score_test[1])
        y_pred = model.predict(x_test)
        y_pred = np.argmax(y_pred, axis=1)
        clf_report = metrics.classification_report(y_test.argmax(axis=-1),y_pred)

    except Exception as e:
        print("failed to validate the model {}".format(e),flush=True)
        raise

    report = {
                "classification_report": clf_report,
                "training_loss": model_score[0],
                "training_accuracy": model_score[1],
                "test_loss": model_score_test[0],
                "test_accuracy": model_score_test[1],
            }

    print("-- VALIDATION COMPLETE! --", flush=True)
    return report

if __name__ == '__main__':

    with open('settings.yaml', 'r') as fh:
        try:
            settings = dict(yaml.safe_load(fh))
        except yaml.YAMLError as e:
            raise(e)

    from fedn.utils.kerashelper import KerasHelper
    helper = KerasHelper()
    weights = helper.load_model(sys.argv[1])

    from models.mnist_model import create_seed_model
    model = create_seed_model()
    model.set_weights(weights)

    report = validate(model,'../data/mnist.npz')

    with open(sys.argv[2],"w") as fh:
        fh.write(json.dumps(report))

max_stars_repo_name: etesami/examples

max_issues_repo_name: etesami/examples

max_forks_repo_name: etesami/examples


text:
import os
import time
from collections import namedtuple
import altair as alt
import math
import pop_music_highlighter.extractor as pmhe
import myller.extractor as me

import pandas as pd
import streamlit as st
import numpy as np
from matplotlib import pyplot as plt

"""
# Thumbnail.me
Upload a .wav or .mp3 file below and get the respective audio thumbnail and self similarity matrix.
"""

COLOR = 'white'
plt.rcParams['figure.facecolor'] = '#035a85'
plt.rcParams['axes.facecolor'] = COLOR
plt.rcParams['savefig.facecolor'] = '#035a85'
plt.rcParams['text.color'] = COLOR
plt.rcParams['axes.labelcolor'] = COLOR
plt.rcParams['xtick.color'] = COLOR
plt.rcParams['ytick.color'] = COLOR

length = None
analyte = st.radio(
    "Pick one..", ('None', 'Repetition', 'Attention', 'Both'))
if analyte != "None":
    length = st.slider('How long a thumbnail would you like?', 1, 31, value=0)

if length in range(1, 31):

    uploaded_file = st.file_uploader("Choose a file", type=['mp3', 'wav'])

    if uploaded_file is not None:
        file_details = {"FileName": uploaded_file.name, "FileType": uploaded_file.type}
        st.audio(uploaded_file)
        uploaded_file_path = os.path.join("data", uploaded_file.name)

        name = os.path.split(uploaded_file.name)[-1][:-4]
        with open(uploaded_file_path, "wb") as f:
            f.write(uploaded_file.getbuffer())

        if analyte is not None and analyte == "Repetition" or analyte == "Both":

            path_ssm_norm = 'output' + os.path.sep + 'repetition' + os.path.sep + '{}_SSM_norm.npy'.format(name)
            path_myller_wav = 'output' + os.path.sep + 'repetition' + os.path.sep + '{}_audio.wav'.format(name)
            path_scape_plot = 'output' + os.path.sep + 'repetition' + os.path.sep + '{}_SP.npy'.format(name)

            with st.spinner("Processing repetition..."):
                me.extract([uploaded_file_path], name=name, length=length, st=st)
            st.success("Repetition Success!")

            if os.path.isfile(path_myller_wav):
                st.audio(path_myller_wav)

            if os.path.isfile(path_myller_wav):
                os.remove(path_myller_wav)

        if analyte is not None and analyte == "Attention" or analyte == "Both":
            score = 'output' + os.path.sep + 'attention' + os.path.sep + '{}_score.npy'.format(name)
            highlight = 'output' + os.path.sep + 'attention' + os.path.sep + '{}_highlight.npy'.format(name)
            path_neural_wav = 'output' + os.path.sep + 'attention' + os.path.sep + '{}_audio.wav'.format(name)
            #print(highlight)
            #print(score)
            with st.spinner("Processing attention..."):
                pmhe.extract([uploaded_file.name], name=name, length=length, save_score=True, save_thumbnail=True,
                             save_wav=True, st=st)
                st.success("Attention Success!")

            if os.path.isfile(path_neural_wav):
                st.audio(path_neural_wav)

            if os.path.isfile(path_neural_wav):
                os.remove(path_neural_wav)

        if os.path.isfile(uploaded_file_path):
            os.remove(uploaded_file_path)


text:
#!/usr/bin/env python
# encoding: utf-8
r"""
F-wave Riemann solver for nonlinear elasticity in heterogeneous media

.. math:: q_t + f(q,x)_x = 0

where

.. math::
    q(x,t) = \left [ \begin{array}{c} \epsilon(x,t) \\ \rho(x) u(x,t) \end{array} \right ]

and the flux vector is

.. math::

    f(q,x) = \left [ \begin{array}{c} -u \\ \sigma(\epsilon,x) \end{array} \right ]

Two stress-strain relations are included.  The default is

..  math:: \sigma(\epsilon,K) = \exp(K(x) \epsilon) - 1

By setting problem_data['stress_relation'] = 'quadratic' one can use instead

..  math:: \sigma(\epsilon,K_1,K_2) = K_1 \epsilon + K_2 \epsilon^2

For the latter relation, K_2 is stored in a third aux entry (aux[2,:]).
"""

from __future__ import absolute_import
import numpy as np
from six.moves import range

strain = 0
momentum = 1
density = 0
K = 1
K1 = 1
K2 = 2

def nonlinear_elasticity_1D(q_l, q_r, aux_l, aux_r, problem_data):
    r"""
    1d nonlinear elasticity riemann solver

    *aux* is expected to contain -
     - aux[0,i] - density in cell i
     - aux[1,i] - bulk modulus in cell i
     - aux[2,i] - K2 (needed for quadratic stress relation only)

    See :ref:`pyclaw_rp` for more details.
    """

    num_eqn = 2
    num_waves = 2
    # Convenience
    num_rp = np.size(q_l, 1)

    # Set up arrays for return values
    fwave = np.empty( (num_eqn, num_waves, num_rp) )
    s = np.empty( (num_waves, num_rp) )
    amdq = np.empty( (num_eqn, num_rp) )
    apdq = np.empty( (num_eqn, num_rp) )

    sigma_l = sigma(q_l, aux_l, problem_data)
    sigma_r = sigma(q_r, aux_r, problem_data)
    bulk_l = sigmap(q_l, aux_l, problem_data)
    bulk_r = sigmap(q_r, aux_r, problem_data)

    # Linearized bulk modulus, sound speed, and impedance:
    cl = np.sqrt(bulk_l/aux_l[density,:])
    cr = np.sqrt(bulk_r/aux_r[density,:])
    zl = cl*aux_l[density,:]
    zr = cr*aux_r[density,:]

    # Jumps:
    du = q_r[momentum,:]/aux_r[density,:]-q_l[momentum,:]/aux_l[density,:]
    dsig = sigma_r - sigma_l

    b1 = - (zr*du + dsig) / (zr+zl)
    b2 = - (zl*du - dsig) / (zr+zl)

    # Compute the f-waves
    # 1-Wave
    fwave[0,0,:] = b1
    fwave[1,0,:] = b1 * zl
    s[0,:] = -cl

    # 2-Wave
    fwave[0,1,:] = b2
    fwave[1,1,:] = b2 * (-zr)
    s[1,:] = cr

    # Compute the left going and right going fluctuations
    for m in range(num_eqn):
        amdq[m,:] = fwave[m,0,:]
        apdq[m,:] = fwave[m,1,:]

    return fwave, s, amdq, apdq

def sigma(q, aux, problem_data):
    r"""Stress-strain relation."""
    stress_relation = problem_data.get('stress_relation', 'exponential')
    if stress_relation == 'exponential':
        return np.exp(aux[K,:]*q[strain,:]) - 1.
    elif stress_relation == 'quadratic':
        return aux[K1,:]*q[strain,:] + aux[K2,:]*q[strain,:]**2

def sigmap(q, aux, problem_data):
    r"""Derivative of stress-strain relation w.r.t. strain."""
    stress_relation = problem_data.get('stress_relation', 'exponential')
    if stress_relation == 'exponential':
        return aux[K,:]*np.exp(aux[K,:]*q[strain,:])
    elif stress_relation == 'quadratic':
        return aux[K1,:] + 2*aux[K2,:]*q[strain,:]

max_stars_repo_name: ranocha/riemann

max_issues_repo_name: ranocha/riemann

max_forks_repo_name: ranocha/riemann


text:
import pickle
import pandas as pd
import numpy as np
import nltk

a=pd.read_table('tweets_pos_clean.txt')
b=pd.read_table('tweets_neg_clean.txt')

aux1=[]
aux2=[]
auxiliar1=[]
auxiliar2=[]
for element in a['Text']:
        for w in element.split():
                if (w==':)' or len(w)>3):
                        auxiliar1.append(w)
        aux1.append((auxiliar1,'positive'))
        auxiliar1=[]

for element in b['text']:
        for w in element.split():
                if (w==':(' or len(w)>3):
                        auxiliar2.append(w)
        aux2.append((auxiliar2,'negative'))
        auxiliar2=[]

aux1=aux1[11000:11100]
aux2=aux2[21000:21100]

table_set=aux1+aux2

def get_words_in_tweets(tweets):
        all_words = []
        for (words, sentiment) in tweets:
                all_words.extend(words)
        return all_words

def get_word_features(wordlist):
        wordlist = nltk.FreqDist(wordlist)
        word_features = wordlist.keys()
        return word_features

def extract_features(document):
        document_words = set(document)
        features = {}
        for word in word_features:
                features['contains(%s)' % word] = (word in document_words)
        return features

with open('objs.pickle', "rb") as f:
	classifier, word_features=pickle.load(f)

def extract_features(document):
	document_words = set(document)
	features = {}
	for word in word_features:
		features['contains(%s)' % word] = (word in document_words)
	return features

tweet1=':)'
print(classifier.classify(extract_features(tweet1.split())))
tweet2=':('
print(classifier.classify(extract_features(tweet2.split())))

test_set = nltk.classify.apply_features(extract_features, table_set)

print(nltk.classify.accuracy(classifier,test_set))


text:
from __future__ import division
from wxtbx import bitmaps
import wxtbx
import wx
from libtbx import object_oriented_patterns as oop
from libtbx.math_utils import ifloor
from libtbx import adopt_init_args
import math
import sys
if (sys.version_info[2] >= 6):
  import warnings
  warnings.simplefilter('ignore', DeprecationWarning)
  warnings.simplefilter('ignore', UserWarning) # for matplotlib 1.5.1

# explicitly set locale for matplotlib 2.0.0, otherwise, on macOS,
# locale.getpreferredencoding(do_setlocale=False) returns an empty string
# for consistency, set locale according to LC_ALL for both linux and macOS
if (sys.platform != 'win32'):
  import locale
  locale.setlocale(locale.LC_ALL, '')

class plot_container(wx.BoxSizer, wxtbx.MouseWheelTransparencyMixin):
  def __init__(self,
                parent,
                figure_size=(8,6),
                font_size=12,
                title_font_size=10,
                facecolor='white',
                transparent=False,
                handle_left_click=False,
                show_data_points=True,
                point_types=('o', '^', '+', 's', 'D'),
                title_alignment="right"):
    wx.BoxSizer.__init__(self, wx.VERTICAL)
    adopt_init_args(self, locals())
    self._fonts = {}
    self.disabled = False
    try :
      import matplotlib
      from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
      from matplotlib.backends.backend_wxagg import FigureManager
      import matplotlib.ticker
      import matplotlib.cm
      import matplotlib.figure
      import matplotlib.font_manager
    except ImportError, e :
      print ""
      print "Error loading matplotlib module:"
      print e
      print ""
      self.disabled = True
      self.canvas = oop.null()
      self.figure = oop.null()
      self.text_font = oop.null()
      self.p = oop.null()
      w = int(figure_size[0] * 72)
      h = int(figure_size[1] * 72)
      panel = wx.Panel(parent=parent,
        size=(w,h))
      panel.SetBackgroundColour((150,150,150))
      szr = wx.BoxSizer(wx.VERTICAL)
      panel.SetSizer(szr)
      txt = wx.StaticText(parent=panel,
        label="Plotting disabled due to missing libraries.")
      szr.Add(txt, 1, wx.ALL|wx.ALIGN_CENTER, 10)
      txt.SetForegroundColour((255,0,0))
      font = txt.GetFont()
      font.SetWeight(wx.FONTWEIGHT_BOLD)
      txt.SetFont(font)
      txt2 = wx.StaticText(parent=panel, label="Original error:")
      txt2.SetForegroundColour((255,0,0))
      szr.Add(txt2, 1, wx.ALL|wx.ALIGN_CENTER, 10)
      txt3 = wx.StaticText(parent=panel, label=str(e))
      txt3.SetForegroundColour((255,0,0))
      szr.Add(txt3, 1, wx.ALL|wx.ALIGN_CENTER, 10)
      self.Add(panel, 1, wx.EXPAND|wx.ALL)
      self.null_fmt = oop.null()
    else :
      self.figure = matplotlib.figure.Figure(figure_size, 72, linewidth=0,
        facecolor=facecolor)
      if transparent :
        self.figure.figurePatch.set_alpha(0.0)
      self.canvas = FigureCanvasWxAgg(self.parent, -1, self.figure)
      self.canvas.toolbar = oop.null()
      self.figmgr = FigureManager(self.canvas, 1, self)
      self.Add(self.canvas, 1, wx.EXPAND|wx.ALL)
      self.setup_fonts()
      self.null_fmt = matplotlib.ticker.NullFormatter()
      if self.handle_left_click :
        self.canvas.mpl_connect("button_release_event", self.OnClick)
      else :
        self.canvas.Bind(wx.EVT_CONTEXT_MENU, self.OnRightClick, self.canvas)
      if (wx.Platform == '__WXMAC__') : # FIXME MSW okay, check GTK
        self.canvas.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseWheel, self.canvas)

  def GetParent(self):
    return self.parent

  def setup_fonts(self):
    import matplotlib.font_manager
    self._fonts["basic"] = matplotlib.font_manager.FontProperties(
      family = ["Courier", "Monaco", "monospace"],
      weight = "normal",
      size   = 10)
    self._fonts["value_label"] = matplotlib.font_manager.FontProperties(
      family = ["Courier", "Monaco", "monospace"],
      weight = "normal",
      size   = self.font_size)
    self._fonts["axis_label"] = matplotlib.font_manager.FontProperties(
      family = ["Helvetica", "sans-serif"],
      weight = "bold",
      size   = self.font_size)
    self._fonts["text"] = matplotlib.font_manager.FontProperties(
      family = ["Helvetica", "sans-serif"],
      weight = "normal",
      size   = self.font_size)
    self._fonts["title"] = matplotlib.font_manager.FontProperties(
      family = ["Helvetica", "sans-serif"],
      weight = "bold",
      size   = self.title_font_size)

  def get_font(self, font_type):
    font = self._fonts.get(font_type, None)
    if (font is None):
      font = self._fonts.get("basic", None)
    return font

  def GetToolBar(self):
    return None

  def OnRightClick(self, event):
    pass

  def OnClick(self, event):
    if not hasattr(event, "button"):
      raise RuntimeError("The OnClick method of plot_container handles "+
        "matplotlib events only.  Please fix your code!")
    if event.button == 1 :
      self.process_mouse_click(event)

  def process_mouse_click(self, mpl_event):
    pass

  def save_image(self, default_path="", default_filename="plot.png"):
    output_file = wx.FileSelector("Saved image name",
      default_path=default_path,
      default_filename=default_filename,
      wildcard="Adobe PDF figure (*.pdf)|*.pdf|" + \
               "PNG image (*.png)|*.png|" + \
               "Postscript figure (*.ps)|*.ps", flags=wx.SAVE)
    if output_file != "" :
      if output_file[-3:] == "pdf" :
        self.figure.savefig(output_file, orientation="landscape", format="pdf")
      elif output_file[-2:] == "ps" :
        self.figure.savefig(output_file, orientation="landscape", format="ps")
      else :
        self.figure.savefig(output_file, format="png")

class histogram(plot_container):
  def show_histogram(self, data, n_bins, reference_value=None, pos=(1,1,1),
                      draw_now=True, x_label=None, y_label=None, title=None,
                      log_scale=False, x_lim=None, y_lim=None):
    assert len(pos) == 3
    self.figure.clear()
    p = self.figure.add_subplot(*pos)
    p.hist(data, n_bins, facecolor='blue', log=log_scale)
    if reference_value is not None :
      p.axvline(reference_value, color='r')
    if (x_label is not None):
      p.set_xlabel(x_label)
    if (y_label is not None):
      p.set_ylabel(y_label)
    if (x_lim is not None):
      p.set_xlim(x_lim[0], x_lim[1])
    if (y_lim is not None):
      p.set_ylim(y_lim[0], y_lim[1])
    if (title is not None):
      p.set_title(title)
    if draw_now :
      self.canvas.draw()
    return p

def convert_xyz_value_list(values, null_value=0.0):
  import numpy
  values = sorted(list(values), cmp=lambda x,y: cmp(x[0], y[0]))
  x_rows = [[values[0]]]
  for i, xyz in enumerate(values[1:]):
    if (xyz[0] != x_rows[-1][-1][0]):
      x_rows.append([])
    x_rows[-1].append(xyz)
  x_values = [ x_row[0][0] for x_row in x_rows ]
  y_values = sorted([ x_rows[0][n][1] for n in range(len(x_rows[0])) ])
  assert (len(values) == (len(x_values) * len(y_values)))
  z_values = []
  for j in range(len(y_values)):
    z_values.append([])
  for i, x_row in enumerate(x_rows):
    x_row = sorted(x_row, cmp=lambda x,y: cmp(x[1], y[1]))
    for j, (x,y,z) in enumerate(x_row):
      assert (y in y_values)
      if (z is not None):
        assert (isinstance(z, int) or isinstance(z, float))
        z_values[j].append(z)
      else :
        z_values[j].append(numpy.NaN)
      #  z_values[j].append(null_value)
  return (numpy.array(x_values), numpy.array(y_values), numpy.array(z_values))

class image_plot(plot_container):
  def show_plot(self,
                 x_data=(),
                 y_data=(),
                 z_data=(),
                 values=(),
                 x_label=None,
                 y_label=None,
                 title=None,
                 cmap=None,
                 interpolation="nearest"):
    if (len(values) > 0):
      assert (len(x_data) == len(y_data) == len(z_data) == 0)
      (x_data, y_data, z_data) = convert_xyz_value_list(values)
    from matplotlib.image import NonUniformImage
    self.figure.clear()
    ax = self.figure.add_subplot(111)
    im = NonUniformImage(ax, interpolation=interpolation)
    if (cmap is not None):
      im.set_cmap(get_colormap(cmap))
    im.set_data(x_data, y_data, z_data)
    ax.images.append(im)
    ax.set_xlim(x_data[0], x_data[-1])
    ax.set_ylim(y_data[0], y_data[-1])
    if (x_label is not None):
      ax.set_xlabel(x_label)
    if (y_label is not None):
      ax.set_ylabel(y_label)
    if (title is not None):
      ax.set_title(title)
    self.canvas.draw()

class iotbx_data_plot_base(plot_container):
  def __init__(self,
                parent,
                tables,
                size=(640,480),
                **kwds):
    adopt_init_args(self, locals())
    (x, y, w, h) = tuple(wx.GetClientDisplayRect())
    (width, height) = size
    fig_w = float(width) / 72.0
    fig_h = float(height) / 72.0
    if (fig_w * 72) > (w - 20):
      fig_w = int(math.floor((w-40) / 72))
    if (fig_h * 72) > (h - 120):
      fig_h = int(math.floor((h-160) / 72))
    plot_container.__init__(self, parent, (fig_w, fig_h), **kwds)
    self.p = self.figure.add_subplot(111)
    self.plot_type = None

  def set_tables(self, tables):
    self.tables = tables

  def set_plot(self, graph_name=None, table_name=None, table_index=0):
    table = None
    if table_name is None :
      table = self.tables[table_index]
    else :
      for t in self.tables :
        if t.title == table_name :
          table = t
    if table is not None :
      self.plot_type = getattr(table, "plot_type", "GRAPH")
      graph = table.get_graph(graph_name)
      self.show_plot(graph, reference_lines=table.get_reference_marks())

  def show_plot(self, graph, line_width=1, show_points=True, show_grid=True,
      reference_lines=None):
    if self.disabled :
      return
    self.figure.clear()
    self.graph = graph
    self.p = self.figure.add_subplot(111)
    if graph is None :
      return
    if self.plot_type == "SCATTER" :
      show_points = True
      show_lines = False
    else :
      show_lines = True
    if self.show_data_points :
      point_types = self.point_types #['o', '^', '+', 's', 'D']
    else :
      point_types = [""]
    point_index = 0
    for (x_values, y_values) in self.graph.get_plots():
      plot_type = ""
      if show_points :
        plot_type = "%s" % point_types[point_index]
        point_index += 1
        if point_index >= len(point_types):
          point_index = 0
      if show_lines :
        plot_type += "-"
      self.p.plot(x_values, y_values, plot_type, linewidth=line_width)
    if (reference_lines is not None):
      for x in reference_lines :
        if (x is not None):
          self.p.axvline(x=x, linewidth=1)
    self.format_labels()
    if show_grid :
      self.p.grid(True, color="0.75")
    self.p.set_autoscale_on(True)
    self.p.set_title(graph.name, fontproperties=self.get_font("title"),
      horizontalalignment=self.title_alignment)
    self.canvas.draw()
    self.parent.Refresh()

  def format_x_axis(self):
    if self.tables[0].x_is_inverse_d_min :
      xdata = self.tables[0].get_x_as_resolution()
      self.p.set_xlabel("Resolution",
        fontproperties=self.get_font("axis_label"))
      if (getattr(self.tables[0], "force_exact_x_labels", False)):
        xticks_ = self.tables[0].get_x_values()
      else :
        xticks_ = self.p.get_xticks()
      n_skip = max(1, ifloor(len(xticks_) / 10))
      xticks = []
      xticklabels = []
      k = 0
      while (k < len(xticks_)):
        x = xticks_[k]
        xticks.append(x)
        if (x != 0):
          if x > 0.0:
            x = math.sqrt(1 / x)
          else:
            x = -math.sqrt(1 / abs(x) )
          xticklabels.append("%.2f" % x)
        else : # FIXME?
          xticklabels.append("")
        k += n_skip
      self.p.set_xticks(xticks)
      self.p.set_xticklabels(xticklabels)
    else :
      if self.graph.x_axis_label is not None :
        self.p.set_xlabel(self.graph.x_axis_label,
          fontproperties=self.get_font("axis_label"))
      else :
        self.p.set_xlabel(self.graph.x_label,
          fontproperties=self.get_font("axis_label"))
    for ticklabel in self.p.get_xticklabels():
      ticklabel.set_fontproperties(self.get_font("value_label"))

  def axvline(self, x, **kwargs):
    if self.tables[0].x_is_inverse_d_min :
      self.p.axvline(x=(1.0 / (x**2)), **kwargs)
    else :
      self.p.axvline(x=x, **kwargs)
    self.canvas.draw()
    self.parent.Refresh()

  def format_y_axis(self):
    for ticklabel in self.p.get_yticklabels():
      ticklabel.set_fontproperties(self.get_font("value_label"))
    if self.graph.y_axis_label is not None :
      self.p.set_ylabel(self.graph.y_axis_label,
          fontproperties=self.get_font("axis_label"))

  def format_labels(self):
    self.figure.legend(self.p.lines, self.graph.y_labels,
      prop=self.get_font("text"))
    self.format_x_axis()
    self.format_y_axis()

  def show_grid(self, show=True):
    if show :
      self.p.grid(True, color="0.75")
    else :
      self.p.grid(False)
    self.canvas.draw()
    self.parent.Refresh()

  def OnRightClick(self, event):
    if (wx.Platform != '__WXMAC__') : # FIXME
      menu = wx.Menu()
      menu_item = menu.Append(-1, "Save image")
      self.parent.Bind(wx.EVT_MENU, self.OnSave, menu_item)
      self.parent.PopupMenu(menu)
      menu.Destroy()

  def OnSave(self, event=None):
    self.save_image()

class small_plot(iotbx_data_plot_base):
  def __init__(self, parent, table, size=(320,320)):
    iotbx_data_plot_base.__init__(self,
      parent=parent,
      tables=[table],
      size=size,
      font_size=9,
      title_font_size=10,
      title_alignment="center",
      point_types=('+'))

class plot_frame(wx.Frame):
  controls_on_top = True
  show_controls_default = True
  def __init__(self, *args, **kwds):
    wx.Frame.__init__(self, *args, **kwds)
    self.setup_toolbar()
    self.toolbar.Realize()
    self.sizer = wx.BoxSizer(wx.VERTICAL)
    self.draw_top_panel()
    self.plot_panel = self.create_plot_panel()
    if self.controls_on_top :
      self.sizer.Add(self.top_panel, 0, wx.EXPAND|wx.ALL)
      self.sizer.Add(self.plot_panel, 1, wx.EXPAND|wx.ALL)
    else :
      self.sizer.Add(self.plot_panel, 1, wx.EXPAND|wx.ALL)
      self.sizer.Add(self.top_panel, 0, wx.EXPAND|wx.ALL)
    self._show_controls = self.show_controls_default
    if not self.show_controls_default :
      self.top_panel.Hide()
    self.SetSizer(self.sizer)
    self.sizer.Layout()
    self.Fit()
    self.Bind(wx.EVT_CLOSE, self.OnClose, self)
    self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy, self)

  def setup_toolbar(self):
    tb_buttons = [
      ("Show/hide controls",
       bitmaps.fetch_icon_bitmap("apps", "advancedsettings"),
       self.OnToggleControls),
      ("Save",
       bitmaps.fetch_icon_bitmap("actions", "save_all"),
       self.OnSave),
      #("Print",
      # bitmaps.fetch_icon_bitmap("devices", "printer1"),
      # self.OnPrint),
    ]
    tb = wx.ToolBar(self, style=wx.TB_3DBUTTONS|wx.TB_TEXT)
    tb.SetToolBitmapSize((32,32))
    self.SetToolBar(tb)
    for (name, bitmap, function) in tb_buttons :
      tool_button = tb.AddLabelTool(-1, name, bitmap, kind=wx.ITEM_NORMAL)
      self.Bind(wx.EVT_MENU, function, tool_button)
    self.toolbar = tb

  def draw_top_panel(self):
    self.top_panel = wx.Panel(parent=self, style=wx.SUNKEN_BORDER)

  def create_plot_panel(self):
    return (0,0)

  def OnExit(self, event):
    self.Close()

  def OnClose(self, event):
    wx.CallAfter(self.Destroy)

  def OnDestroy(self, event):
    pass

  def OnSave(self, event):
    self.plot_panel.save_image()

  def OnToggleControls(self, event):
    if self._show_controls :
      self._show_controls = False
      self.top_panel.Hide()
    else :
      self._show_controls = True
      self.top_panel.Show()
    self.sizer.Layout()
    self.Fit()

class loggraph(plot_frame):
  plot_type = "loggraph"
  controls_on_top = True
  show_controls_default = True
  table_selection_label = "Table:"
  plot_selection_label = "Plot:"
  def __init__(self, parent, title, tables=None, file_name=None,
      processed_lines=None):
    adopt_init_args(self, locals())
    self.tables = []
    self.graph = None
    self.table_frame = None
    self.table_chooser = None
    self.plot_chooser = None
    self.current_table = None
    self.current_plot = None
    if tables is not None :
      self.tables = tables
    elif file_name is not None or processed_lines is not None :
      self.load_log(file_name, processed_lines, update=False)
      if len(self.tables) > 0 :
        self.current_table = self.tables[0].title
        self.current_plot = self.tables[0].graph_names[0]
    plot_frame.__init__(self, parent=parent, title=title)
    self.Bind(wx.EVT_CLOSE, self.OnClose)
    self.Centre()
    self.switch_plot()

  def draw_top_panel(self):
    self.top_panel = wx.Panel(parent=self, style=wx.SUNKEN_BORDER)
    cp = self.top_panel
    cp_sizer = wx.BoxSizer(wx.VERTICAL)
    cp.SetSizer(cp_sizer)
    grid = wx.FlexGridSizer(cols=2)
    txt = wx.StaticText(self.top_panel, -1, "Table:")
    # TODO: set bold-face
    grid.Add(txt, 0, wx.ALL|wx.EXPAND, 5)
    self.table_chooser = wx.Choice(parent=cp,
      choices=[ t.title for t in self.tables])
    if (len(self.tables) > 0):
      self.table_chooser.SetSelection(0)
    self.Bind(wx.EVT_CHOICE, self.OnSelectTable, self.table_chooser)
    grid.Add(self.table_chooser, 0, wx.ALL|wx.EXPAND, 5)
    txt2 = wx.StaticText(self.top_panel, -1, "Plot:")
    grid.Add(txt2, 0, wx.ALL|wx.EXPAND, 5)
    plot_choices = self.tables[0].graph_names
    self.plot_chooser = wx.Choice(parent=cp,
      choices=plot_choices)
    if (len(plot_choices) > 0):
      self.plot_chooser.SetSelection(0)
    self.Bind(wx.EVT_CHOICE, self.OnSelectPlot, self.plot_chooser)
    grid.Add(self.plot_chooser, 0, wx.ALL|wx.EXPAND, 5)
    cp_sizer.Add(grid, 0, wx.EXPAND)
    szr = wx.BoxSizer(wx.HORIZONTAL)
    cp_sizer.Add(szr, 0, wx.EXPAND)
    grid_box = wx.CheckBox(parent=cp,
      label="Show grid")
    grid_box.SetValue(True)
    self.Bind(wx.EVT_CHECKBOX, self.OnToggleGrid, grid_box)
    szr.Add(grid_box, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
    point_box = wx.CheckBox(parent=cp,
      label="Show data points")
    point_box.SetValue(True)
    self.Bind(wx.EVT_CHECKBOX, self.OnTogglePoints, point_box)
    szr.Add(point_box, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
    return cp

  def create_plot_panel(self):
    self.plot = iotbx_data_plot_base(
      parent=self,
      tables=self.tables,
      transparent=False)
    return self.plot

  def load_log(self, file_name=None, processed_lines=None, update=True):
    from iotbx import data_plots
    if processed_lines :
      self.tables = data_plots.import_ccp4i_logfile(log_lines=processed_lines)
    else :
      self.tables = data_plots.import_ccp4i_logfile(file_name=file_name)
    if update :
      self.update_interface()

  def update_interface(self, update_tables=True):
    if len(self.tables) > 0 :
      if update_tables :
        table_choices = [t.title for t in self.tables]
        self.table_chooser.SetItems(table_choices)
        self.table_chooser.SetSelection(0)
      self.plot_chooser.SetItems(self.get_table().graph_names)
      self.plot_chooser.SetSelection(0)
      self.top_panel.Layout()
      self.switch_plot()

  def set_current_plot(self, plot_name):
    for t in self.tables :
      for t_plot_name in t.graph_names :
        if plot_name == t_plot_name :
          self.table_chooser.SetStringSelection(t.title)
          self.plot_chooser.SetStringSelection(plot_name)
          break
    self.switch_plot()

  def switch_plot(self):
    table = self.table_chooser.GetStringSelection()
    plot = self.plot_chooser.GetStringSelection()
    if table != "" and plot != "" :
      self.plot.set_plot(graph_name=plot, table_name=table)
      #self.Refresh()

  #--- EVENTS
  def OnSelectTable(self, event):
    table_name = self.table_chooser.GetStringSelection()
    current_plot = self.plot_chooser.GetStringSelection()
    plot_choices = []
    for t in self.tables :
      if t.title == table_name :
        plot_choices = t.graph_names
    self.plot_chooser.SetItems(plot_choices)
    if current_plot in plot_choices :
      self.plot_chooser.SetStringSelection(current_plot)
    self.switch_plot()

  def OnSelectPlot(self, event):
    self.switch_plot()

  def OnPrint(self, event):
    pass

  def OnToggleGrid(self, event):
    show = event.GetEventObject().GetValue()
    self.plot.show_grid(show)

  def OnTogglePoints(self, event):
    self.plot.show_data_points = event.GetEventObject().GetValue()
    self.switch_plot()

standard_colormaps = [ ("jet", "Rainbow"),
                       ("Greys", "Greyscale"),
                       ("Reds", "Red"),
                       ("YlGn", "Yellow/Green"),
                       ("Greens", "Green"),
                       ("Blues", "Blue"),
                     ]
colormap_names = [ cm_name for cm_id, cm_name in standard_colormaps ]
colormap_id_dict = dict([ (name, id) for id, name in standard_colormaps ])

def get_colormap(cm_name):
  import matplotlib.cm
  cm = None
  for cm_id, cm_name2 in standard_colormaps :
    if cm_name2 == cm_name :
      cm = getattr(matplotlib.cm, cm_id, None)
      break
  else :
    cm = getattr(matplotlib.cm, cm_name, None)
  if cm is None :
    cm = matplotlib.cm.jet
  return cm

def exercise():
  values = [
    (1.5, 3.0, 40.2),
    (2.5, 3.5, 35.1),
    (1.5, 3.5, 45.6),
    (2.0, 3.5, 54.2),
    (1.5, 4.5, 48.2),
    (2.5, 4.5, 28.4),
    (2.5, 4.0, 37.6),
    (2.0, 2.5, None),
    (2.0, 3.0, 29.8),
    (2.0, 4.0, 36.7),
    (2.0, 4.5, 33.2),
    (2.5, 2.5, None),
    (1.5, 2.5, 39.5),
    (2.5, 3.0, None),
    (1.5, 4.0, 50.9),
  ]
  sys.path.pop(0)
  from iotbx import data_plots
  loggraph1 = """\
$TABLE: Resolution shell statistics
$GRAPHS
:R-free vs. resolution
:A:1,3:
:FOM vs. resolution
:A:1,4:
$$
1/resol^2  Nrefl      R-free     FOM       $$
$$
0.02       2004       0.25       0.89
0.04       2084       0.23       0.88
0.06       2037       0.27       0.83
0.08       1949       0.28       0.75
0.1        1783       0.38       *
$$
"""
  app = wx.App(0)
  frame = loggraph(parent=None,
    title="Loggraph test",
    tables=None,
    processed_lines=loggraph1.splitlines())
  frame.Show()
  frame2 = wx.Frame(None, -1, "Heat map")
  panel = wx.Panel(frame2, -1)
  sizer2 = wx.BoxSizer(wx.VERTICAL)
  frame2.SetSizer(sizer2)
  sizer2.Add(panel, 1, wx.EXPAND)
  implot = image_plot(panel)
  implot.show_plot(values=values, x_label="RMSD", y_label="resolution",
    cmap="autumn")
  sizer2.Layout()
  frame2.Fit()
  frame2.Show()
  app.MainLoop()

if __name__ == "__main__" :
  exercise()


text:
import math
import os.path
from collections import Counter

import click
import numpy as np
from scipy.io import wavfile
from aubio import source, onset, pitch

A4 = 440
C0 = A4 * pow(2, -4.75)
note_name = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"]
notes_sharp = ['A', 'Bb', 'B', 'C', 'Dd', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab']

def get_note_frequency(note):
    octave = int(note[-1])

    key_number = notes_sharp.index(note[0:-1])

    if key_number < 3:
        key_number += 12

    key_number = key_number + ((octave - 1) * 12) + 1

    return A4 * 2**((key_number - 49) / 12)

def get_note_name(freq):
    h = round(12 * math.log2(freq / C0))
    octave = h // 12
    n = h % 12
    return note_name[n] + str(octave)

def get_freq(sample, samplerate, min_confidence=0.95):
    win_s = 4096  # fft size
    hop_s = 512  # hop size

    pitch_o = pitch("yin", win_s, hop_s, samplerate)
    pitch_o.set_unit("freq")
    pitch_o.set_tolerance(0.8)

    pitches = []
    confidences = []

    for i in range(0, sample.shape[0] - hop_s + 1, hop_s):
        p = pitch_o(sample[i:i + hop_s])[0]
        confidence = pitch_o.get_confidence()
        if confidence >= min_confidence:
            pitches.append(p)
            confidences.append(confidence)

    return np.mean(pitches), np.mean(confidences)

def detect_onsets(wav_fname, samplerate):
    """
    return onsets frames position
    """
    win_s = 512                 # fft size
    hop_s = win_s // 2          # hop size

    s = source(wav_fname, samplerate, hop_s)
    samplerate = s.samplerate
    o = onset("default", win_s, hop_s, samplerate)
    # list of onsets, in samples
    onsets = []
    # total number of frames read
    total_frames = 0
    while True:
        samples, read = s()
        if o(samples):
            onsets.append(o.get_last())
            # keep some data to plot it later
        total_frames += read
        if read < hop_s:
            break

    return onsets

def save_samples(samples,
                 onsets,
                 save_path,
                 samplerate,
                 open_string='E2',
                 dynamics='mp',
                 backtrack_length=512,  # -512 to avoid boundary glitch
                 max_sample_seconds=4,
                 dry_run=False,
                 freq_confidence=0.95,
                 save_all=False
                 ):

    sample_cnt = Counter()
    for i in range(0, len(onsets) - 1):
        start_pos = onsets[i] - backtrack_length
        max_end = min(onsets[i + 1], onsets[i] + int(samplerate * max_sample_seconds))
        sample = samples[start_pos:max_end]
        freq, confidence = get_freq(sample[:, 0].astype(np.float32), samplerate, min_confidence=freq_confidence)
        note_name = get_note_name(freq)
        sample_cnt[note_name] += 1
        click.echo(
            f'''#{i} freq: {freq} note: {note_name} length: {sample.shape[0] / samplerate}
 confidence: {confidence} total sample {sample_cnt[note_name]}''')
        if dry_run:
            continue
        if save_all or click.confirm('save to sample?'):
            wavfile.write(
                os.path.join(
                    save_path,
                    f'{open_string}_{note_name}_{dynamics}_{sample_cnt[note_name]}.wav'),
                samplerate,
                sample)
        else:
            sample_cnt[note_name] -= 1  # exclude sample
    click.echo(f'total notes: {sum(sample_cnt.values())}')

def draw_graph(sample, samplerate):
    import matplotlib.pyplot as plt

    time = np.linspace(0., sample.shape[0] / samplerate, sample.shape[0])
    plt.plot(time, sample[:, 0], label="Left channel")
    plt.plot(time, sample[:, 1], label="Right channel")
    plt.legend()
    plt.xlabel("Time [s]")
    plt.ylabel("Amplitude")
    plt.show()

@click.command()
@click.argument("input_sample", type=str)
@click.argument("save_path", type=str)
@click.option("--open-string", type=str, default='E2')
@click.option("--dynamics", type=str, default='mp')
@click.option("--freq-confidence", type=float, default=0.95)
@click.option("--dry-run", type=bool, is_flag=True)
@click.option("--yes", type=bool, is_flag=True)
def cli(input_sample, save_path, open_string, dynamics, freq_confidence, dry_run, yes):
    samplerate, data = wavfile.read(input_sample)
    onsets = detect_onsets(input_sample, samplerate)
    onsets.append(int(data.shape[0]))
    save_samples(
        data,
        onsets,
        save_path,
        samplerate,
        open_string=open_string,
        dynamics=dynamics,
        dry_run=dry_run,
        freq_confidence=freq_confidence,
        save_all=yes
    )

if __name__ == '__main__':
    cli()

reason: import numpy,from scipy


text:
import copy
from time_series_transform.transform_core_api.base import (
    Time_Series_Data,
    Time_Series_Data_Collection
    )
import numpy as np

class io_base (object):
    def __init__(self,time_series,timeSeriesCol,mainCategoryCol):
        """
        io_base
        IO class

        Parameters
        ----------
        time_series : Time_Series_Data or Time_Series_Data_Collection
            input data
        timeSeriesCol : str or int
            index of time period column
        mainCategoryCol : str of int
            index of category column
        """
        if isinstance(time_series,(Time_Series_Data,Time_Series_Data_Collection)):
            self.time_series = copy.deepcopy(time_series)
            self.dictList = None
        else:
            self.time_series = None
            self.dictList = copy.deepcopy(time_series)
        self.timeSeriesCol = timeSeriesCol
        self.mainCategoryCol = mainCategoryCol

    def to_single(self):
        """
        to_single transform data to Time_Series_Data

        Returns
        -------
        Time_Series_Data

        Raises
        ------
        KeyError
            invalid data
        """
        tsd = Time_Series_Data()
        if self.timeSeriesCol is None:
            raise KeyError("time series index is required")
        tsd.set_time_index(self.dictList[self.timeSeriesCol],self.timeSeriesCol)
        for i in self.dictList:
            if i == self.timeSeriesCol:
                continue
            tsd.set_data(self.dictList[i],i)
        return tsd

    def to_collection(self):
        """
        to_collection transform data into Time_Series_Data_Collection

        Returns
        -------
        Time_Series_Data_Collection

        Raises
        ------
        KeyError
            invalid input
        """
        if self.timeSeriesCol is None:
            raise KeyError("time series index is required")
        tsd = Time_Series_Data(self.dictList,self.timeSeriesCol)
        tsc = Time_Series_Data_Collection(tsd,self.timeSeriesCol,self.mainCategoryCol)
        return tsc

    def from_collection(self,expandCategory,expandTimeIx,preprocessType='ignore'):
        """
        from_collection prepare Time_Series_Data_Collection into dict of list

        Parameters
        ----------
        expandCategory : bool
            whether to expand category
        expandTime : bool
            whether to expand time
        preprocessType : ['ignore','pad','remove']
            preprocess data time across categories

        Returns
        -------
        dict of list

        Raises
        ------
        ValueError
            invalid data
        KeyError
            invalid key
        """
        transCollection = copy.deepcopy(self.time_series)
        transCollection =  transCollection.sort()
        if preprocessType == 'remove':
            transCollection = transCollection.remove_different_time_index()
        elif preprocessType == 'pad':
            transCollection = transCollection.pad_time_index()
        elif preprocessType == 'ignore':
            tmp = None
            diffTime = False
            for i in transCollection:
                if tmp is None:
                    tmp = transCollection[i].time_index[transCollection._time_series_Ix].tolist()
                    continue
                timeList = transCollection[i].time_index[transCollection._time_series_Ix].tolist()
                if set(tmp) != set(timeList):
                    diffTime = True
                tmp = timeList
                if diffTime and (False == (expandCategory == expandTimeIx)):
                    raise ValueError('category time length should be in consist. otherwise, use pad or remove pre-process type. ')
        else:
            raise KeyError('preprocess type must be remove, pad, or ignore')

        if expandCategory:
            transCollection = self._expand_dict_category(transCollection)
        if expandTimeIx:
            transCollection = self._expand_dict_date(transCollection)
        res = {}
        for i in transCollection:
            if isinstance(transCollection[i],Time_Series_Data):
                data = transCollection[i][:]
                catLen = transCollection[i].time_length
            else:
                data = transCollection[i]
                tmpKey =list(data.keys())[0]
                catLen = len(data[tmpKey])
            if not expandCategory:
                categoryList = [i for _ in range(catLen)]
                data[self.mainCategoryCol] = categoryList
            for key in data:
                if key not in res:
                    res[key] = list(data[key])
                else:
                    res[key] += list(data[key])
        return res

    def from_single(self,expandTime):
        """
        from_single transform Time_Series_Data into dict of list

        Parameters
        ----------
        expandTime : bool
            whether to expand Time

        Returns
        -------
        Time_Series_Data
        """
        if expandTime:
            tmp = {"1":self.time_series}
            return self._expand_dict_date(tmp)['1']
        else:
            dfDict = {}
            dfDict.update(self.time_series.time_index)
            dfDict.update(self.time_series.labels)
            dfDict.update(self.time_series.data)
        return dfDict

    def _expand_dict_category(self,collectionDict):
        time_series = Time_Series_Data()
        for i in collectionDict:
            tmp =collectionDict[i]
            tmp.sort()
            for t in tmp.time_index:
                time_series.set_time_index(tmp.time_index[t],t)
            for d in tmp.data:
                time_series.set_data(tmp.data[d],f'{d}_{i}')
            for l in tmp.labels:
                time_series.set_labels(tmp.labels[l],f'{l}_{i}')
        return {'1':time_series}

    def _expand_dict_date(self,collectionDict):
        dct = {}
        for k in collectionDict:
            tmp = {}
            a = collectionDict[k]
            for i in range(a.time_length):
                timeIx = list(a.time_index.keys())[0]
                for t in a[i]:
                    if t in a.time_index:
                        continue
                    if not isinstance(a[i][t],list) or not isinstance(a[i][t],np.ndarray):
                        tmp[f"{t}_{a[i][timeIx]}"]=[a[i][t]]
                    else:
                        tmp[f"{t}_{a[i][timeIx]}"]=a[i][t]
            dct[k] = tmp
        return dct

max_stars_repo_name: allen-chiang/Time-Series-Transformer

max_issues_repo_name: allen-chiang/Time-Series-Transformer

max_forks_repo_name: allen-chiang/Time-Series-Transformer