text:
import sys
if sys.version_info.major < 3 or sys.version_info.minor < 4:
    print("Please using python3.4 or greater!")
    sys.exit(1)
import numpy as np
import cv2, io, time, argparse, re
from os import system
from os.path import isfile, join
from time import sleep
import multiprocessing as mp
from openvino.inference_engine import IENetwork, IEPlugin
import heapq
import threading
import GPy
import GPyOpt
import matplotlib.pyplot as plt
from pykalman import KalmanFilter
from dtw import dtw

lastresults = None
threads = []
processes = []
frameBuffer = None
results = None
fps = ""
detectfps = ""
framecount = 0
detectframecount = 0
time1 = 0
time2 = 0
cam = None
camera_width = 320
camera_height = 240
window_name = ""
ssd_detection_mode = 1
face_detection_mode = 0
elapsedtime = 0.0
flag = "wait"##
message1 = "Push [m] to measure reference."##
message2 = "Push [s] to start inspection."##
NO = 1##
thresh = 8##

LABELS = [['background','hand'],
          ['background', 'face']]

def camThread(LABELS, results, frameBuffer, camera_width, camera_height, vidfps, number_of_camera):
    global fps
    global detectfps
    global lastresults
    global framecount
    global detectframecount
    global time1
    global time2
    global cam
    global window_name
    global flag##
    global message1##
    global message2##
    global writer##
    global train##
    global test##

    cam = cv2.VideoCapture(number_of_camera)
    if cam.isOpened() != True:
        print("USB Camera Open Error!!!")
        sys.exit(0)
    cam.set(cv2.CAP_PROP_FPS, vidfps)
    cam.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width)
    cam.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height)
    window_name = "USB Camera"

    cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)

    while True:
        t1 = time.perf_counter()

        # USB Camera Stream Read
        s, color_image = cam.read()
        if not s:
            continue
        if frameBuffer.full():
            frameBuffer.get()
        frames = color_image

        height = color_image.shape[0]
        width = color_image.shape[1]
        frameBuffer.put(color_image.copy())
        res = None

        if not results.empty():
            res = results.get(False)
            detectframecount += 1
            imdraw = overlay_on_image(frames, res, LABELS)
            lastresults = res
        else:
            imdraw = overlay_on_image(frames, lastresults, LABELS)

        cv2.imshow(window_name, cv2.resize(imdraw, (width, height)))

        key = cv2.waitKey(1)&0xFF##

        if key == ord('q'):
            # Stop streaming
            sys.exit(0)

        if key == ord('m'):##
            # measure reference hand
            if flag == "wait":##
                flag = "train"##
                message1 = "Push [e] to stop measuring."##
                message2 = " "##
                fourcc = cv2.VideoWriter_fourcc(*'XVID')##movie save
                writer = cv2.VideoWriter('train.avi',fourcc, vidfps, (camera_width,camera_height))##movie save
                train = hand()##

        if key == ord('s'):##
            # start inspection
            if flag == "wait":##
                flag = "test"##
                message1 = " "##
                message2 = "Push [e] to finish inspection."##
                fourcc = cv2.VideoWriter_fourcc(*'XVID')##movie save
                writer = cv2.VideoWriter('test_' + str(NO) +'.avi',fourcc, vidfps, (camera_width,camera_height))##movie save
                test = hand()##

        if key == ord('e'):##
            # end measure reference and inspection
            writer.release()##movie save

            if flag == "train":##
                flag = "wait"##
            elif flag == "test":##
                flag = "test_finish"##

            message1 = "Push [m] to measure reference."##
            message2 = "Push [s] to start inspection."##

        ## Print FPS
        framecount += 1
        if framecount >= 15:
            fps       = "(Playback) {:.1f} FPS".format(time1/15)
            detectfps = "(Detection) {:.1f} FPS".format(detectframecount/time2)
            framecount = 0
            detectframecount = 0
            time1 = 0
            time2 = 0
        t2 = time.perf_counter()
        elapsedTime = t2-t1
        time1 += 1/elapsedTime
        time2 += elapsedTime

# l = Search list
# x = Search target value
def searchlist(l, x, notfoundvalue=-1):
    if x in l:
        return l.index(x)
    else:
        return notfoundvalue

def async_infer(ncsworker):

    while True:
        ncsworker.predict_async()

class hand:## --->
    def __init__(self):
        self.hand = []

    def save_position(self, hand_x, hand_y):
        left = np.argmin(hand_x)
        right = np.argmax(hand_x)
        self.hand.append([hand_x[left], hand_y[left], hand_x[right], hand_y[right]])
## <---

class bayesian_opt:## --->
    def __init__(self, train, test):
        self.exp_num = 10# exploration number
        train = self.kalman(train)
        test = self.kalman(test)
        self.train = cv2.resize(train, (100,4))
        self.test = cv2.resize(test, (100,4))
        np.savetxt("train.csv", self.train, delimiter=",")
        np.savetxt("test_" + str(NO) +".csv", self.test, delimiter=",")
        print("Bayesian Optimization")

    def kalman(self, x):
        x = np.array(x).T
        result = []
        for i in range(4):
            kf = KalmanFilter(transition_matrices=np.array([[1, 1], [0, 1]]),
                              transition_covariance=0.01*np.eye(2))

            result.append(kf.em(x[i]).smooth(x[i])[0][:, 0])

        return np.array(result)

    def dtw_sim(self, v1, v2):
        result = []
        for i in range(4):
            x = v1[i].reshape(-1, 1)
            y = v2[i].reshape(-1, 1)

            euclidean_norm = lambda x, y: np.abs(x - y)

            d, _, _, _ = dtw(x, y, dist=euclidean_norm)
            result.append(d)

        return np.mean(result)

    def f(self, x):
        score = []
        score.append(self.dtw_sim(self.train[:,0:25] , self.test[:,0:int(x[:,0])]))
        score.append(self.dtw_sim(self.train[:,25:50] , self.test[:,int(x[:,0]):int(x[:,1])]))
        score.append(self.dtw_sim(self.train[:,50:75] , self.test[:,int(x[:,1]):int(x[:,2])]))
        score.append(self.dtw_sim(self.train[:,75:100] , self.test[:,int(x[:,2]):]))
        score = np.mean(score)
        print(score)

        return score

    def plot_result(self, begin, end, begin_train, end_train, color, type_):
        if type_ == "train":
            data = self.train
        else:
            data = self.test

        plt.plot(np.arange(begin, end), data[0, begin:end], c=color, label="Score %.3f"%(self.dtw_sim(self.train[:,begin_train:end_train], data[:,begin:end])))
        plt.plot(np.arange(begin, end), data[1, begin:end], c=color, linestyle='dashed')
        plt.plot(np.arange(begin, end), data[2, begin:end], c=color, linestyle='dashdot')
        plt.plot(np.arange(begin, end), data[3, begin:end], c=color, linestyle='dotted')
        plt.legend()

    def main(self):
        bounds = [{'name': 'x1', 'type': 'continuous', 'domain': (15,35)},
                  {'name': 'x2', 'type': 'continuous', 'domain': (40,60)},
                  {'name': 'x3', 'type': 'continuous', 'domain': (65,85)}]# limit

        myBopt = GPyOpt.methods.BayesianOptimization(f = self.f,
                                                     domain = bounds,
                                                     initial_design_numdata = 50)
        myBopt.run_optimization(max_iter=self.exp_num)

        # bestparameter
        result = myBopt.x_opt
        result = np.array(result, dtype="int")

        plt.figure(figsize=(8,8))
        plt.subplot(2,1,1)
        self.plot_result(0, 25, 0, 25,"b", "train")
        self.plot_result(25, 50, 25, 50,"r", "train")
        self.plot_result(50, 75, 50, 75,"g", "train")
        self.plot_result(75, 100, 75, 100,"y", "train")
        plt.title("train")

        plt.subplot(2,1,2)
        self.plot_result(0, result[0], 0, 25,"b", "test")
        self.plot_result(result[0], result[1], 25, 50, "r", "test")
        self.plot_result(result[1], result[2], 50, 75, "g", "test")
        self.plot_result(result[2], 100, 75, 100, "y", "test")
        plt.title("test")
        plt.savefig("result" + str(NO) + "jpg")
        plt.close()

        print("finish")
        return result
## <---

class NcsWorker(object):

    def __init__(self, devid, frameBuffer, results, camera_width, camera_height, number_of_ncs):
        self.devid = devid
        self.frameBuffer = frameBuffer
        self.model_xml = "./lrmodel/MobileNetSSD/MobileNetSSD_deploy.xml"
        self.model_bin = "./lrmodel/MobileNetSSD/MobileNetSSD_deploy.bin"
        self.camera_width = camera_width
        self.camera_height = camera_height
        self.num_requests = 4
        self.inferred_request = [0] * self.num_requests
        self.heap_request = []
        self.inferred_cnt = 0
        self.plugin = IEPlugin(device="MYRIAD")
        self.net = IENetwork(model=self.model_xml, weights=self.model_bin)
        self.input_blob = next(iter(self.net.inputs))
        self.exec_net = self.plugin.load(network=self.net, num_requests=self.num_requests)
        self.results = results
        self.number_of_ncs = number_of_ncs

    def image_preprocessing(self, color_image):

        prepimg = cv2.resize(color_image, (300, 300))
        ##prepimg = prepimg - 127.5
        ##prepimg = prepimg * 0.007843
        prepimg = prepimg[np.newaxis, :, :, :]     # Batch size axis add
        prepimg = prepimg.transpose((0, 3, 1, 2))  # NHWC to NCHW
        return prepimg

    def predict_async(self):
        try:

            if self.frameBuffer.empty():
                return

            prepimg = self.image_preprocessing(self.frameBuffer.get())
            reqnum = searchlist(self.inferred_request, 0)

            if reqnum > -1:
                self.exec_net.start_async(request_id=reqnum, inputs={self.input_blob: prepimg})
                self.inferred_request[reqnum] = 1
                self.inferred_cnt += 1
                if self.inferred_cnt == sys.maxsize:
                    self.inferred_request = [0] * self.num_requests
                    self.heap_request = []
                    self.inferred_cnt = 0
                heapq.heappush(self.heap_request, (self.inferred_cnt, reqnum))

            cnt, dev = heapq.heappop(self.heap_request)

            if self.exec_net.requests[dev].wait(0) == 0:
                self.exec_net.requests[dev].wait(-1)
                out = self.exec_net.requests[dev].outputs["DetectionOutput"].flatten()
                self.results.put([out])
                self.inferred_request[dev] = 0
            else:
                heapq.heappush(self.heap_request, (cnt, dev))

        except:
            import traceback
            traceback.print_exc()

def inferencer(results, frameBuffer, ssd_detection_mode, face_detection_mode, camera_width, camera_height, number_of_ncs):

    # Init infer threads
    threads = []
    for devid in range(number_of_ncs):
        thworker = threading.Thread(target=async_infer, args=(NcsWorker(devid, frameBuffer, results, camera_width, camera_height, number_of_ncs),))
        thworker.start()
        threads.append(thworker)

    for th in threads:
        th.join()

def movie_make(result):
    global NO

    count = 0
    test_no = [0, result[0], result[1], result[2], 100]
    train_no = [0, 25, 50, 75, 100]

    video = cv2.VideoCapture('test_' + str(NO) + '.avi')

    W = video.get(cv2.CAP_PROP_FRAME_WIDTH)
    H = video.get(cv2.CAP_PROP_FRAME_HEIGHT)
    count_max = video.get(cv2.CAP_PROP_FRAME_COUNT)
    fps_movie = video.get(cv2.CAP_PROP_FPS)

    fourcc_movie = cv2.VideoWriter_fourcc(*'XVID')##movie save
    writer_movie = cv2.VideoWriter('result_' + str(NO) + '.avi',fourcc_movie, fps_movie, (int(W), int(H)))##movie save

    while(video.isOpened()):
        ret, frame = video.read()

        count += 1

        if ret == False:
            break

        if count < count_max/100*25:
            span = 0
        elif count < count_max/100*50:
            span = 1
        elif count < count_max/100*75:
            span = 2
        else:
            span = 3

        score = opt.dtw_sim(opt.train[:,train_no[span]:train_no[span+1]], opt.test[:,test_no[span]:test_no[span+1]])

        if score < thresh:
            color = (255, 0, 0)
        else:
            color = (0, 0, 255)

        frame = cv2.rectangle(frame, (0, 0), (639, 479), color, 20)
        frame = cv2.putText(frame, str(score), (int(W)-150,150), cv2.FONT_HERSHEY_SIMPLEX, 2, color, 2, cv2.LINE_AA)
        frame = cv2.putText(frame, "Score", (int(W)-350,150), cv2.FONT_HERSHEY_SIMPLEX, 2, color, 2, cv2.LINE_AA)
        writer_movie.write(frame)##movie save

    writer_movie.release()##movie save
    NO += 1
    video.release()

def overlay_on_image(frames, object_infos, LABELS):
    global flag##
    global opt##

    try:

        color_image = frames

        if isinstance(object_infos, type(None)):
            return color_image

        # Show images
        height = color_image.shape[0]
        width = color_image.shape[1]
        entire_pixel = height * width
        img_cp = color_image.copy()

        for (object_info, LABEL) in zip(object_infos, LABELS):

            drawing_initial_flag = True
            hand_x, hand_y = [], []##

            for box_index in range(2):
                if object_info[box_index + 1] == 0.0:
                    break

                base_index = box_index * 7
                if (not np.isfinite(object_info[base_index]) or
                    not np.isfinite(object_info[base_index + 1]) or
                    not np.isfinite(object_info[base_index + 2]) or
                    not np.isfinite(object_info[base_index + 3]) or
                    not np.isfinite(object_info[base_index + 4]) or
                    not np.isfinite(object_info[base_index + 5]) or
                    not np.isfinite(object_info[base_index + 6])):
                    continue

                object_info_overlay = object_info[base_index:base_index + 7]

                min_score_percent = 30##

                source_image_width = width
                source_image_height = height

                base_index = 0
                class_id = object_info_overlay[base_index + 1]
                percentage = int(object_info_overlay[base_index + 2] * 100)
                if (percentage <= min_score_percent):
                    continue

                box_left = int(object_info_overlay[base_index + 3] * source_image_width)
                box_top = int(object_info_overlay[base_index + 4] * source_image_height)
                box_right = int(object_info_overlay[base_index + 5] * source_image_width)
                box_bottom = int(object_info_overlay[base_index + 6] * source_image_height)

                hand_x.append(box_left + (box_right - box_left)/2)
                hand_y.append(box_top + (box_bottom - box_top)/2)

                label_text = LABEL[int(class_id)] + " (" + str(percentage) + "%)"

                box_color = (0, 255, 0)##
                box_thickness = 5##
                cv2.rectangle(img_cp, (box_left, box_top), (box_right, box_bottom), box_color, box_thickness)
                ##label_background_color = (125, 175, 75)
                ##label_text_color = (255, 255, 255)##
                ##label_size = cv2.getTextSize(label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)[0]
                ##label_left = box_left
                ##label_top = box_top - label_size[1]
                ##if (label_top < 1):
                ##    label_top = 1
                ##label_right = label_left + label_size[0]
                ##label_bottom = label_top + label_size[1]
                ##cv2.rectangle(img_cp, (label_left - 1, label_top - 1), (label_right + 1, label_bottom + 1), label_background_color, -1)
                ##cv2.putText(img_cp, label_text, (label_left, label_bottom), cv2.FONT_HERSHEY_SIMPLEX, 0.5, label_text_color, 1)

        cv2.putText(img_cp, fps,       (width-170,15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1, cv2.LINE_AA)
        cv2.putText(img_cp, detectfps, (width-170,30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1, cv2.LINE_AA)
        cv2.putText(img_cp, message1,  (width-280,45), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1, cv2.LINE_AA)##
        cv2.putText(img_cp, message2,  (width-280,60), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1, cv2.LINE_AA)##

        if len(hand_x) == 2:##
            if flag == "train":##
                writer.write(img_cp)##movie save
                train.save_position(hand_x, hand_y)##
            elif flag == "test":##
                writer.write(img_cp)##movie save
                test.save_position(hand_x, hand_y)##
            else:##
                pass##

        if flag == "test_finish":##
            opt = bayesian_opt(train.hand, test.hand)##
            result = opt.main()##
            movie_make(result)##
            flag = "wait"
        return img_cp

    except:
        import traceback
        traceback.print_exc()

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('-cn','--numberofcamera',dest='number_of_camera',type=int,default=0,help='USB camera number. (Default=0)')
    parser.add_argument('-wd','--width',dest='camera_width',type=int,default=320,help='Width of the frames in the video stream. (Default=320)')
    parser.add_argument('-ht','--height',dest='camera_height',type=int,default=240,help='Height of the frames in the video stream. (Default=240)')
    parser.add_argument('-sd','--ssddetection',dest='ssd_detection_mode',type=int,default=1,help='[Future functions] SSDDetectionMode. (0:=Disabled, 1:=Enabled Default=1)')
    parser.add_argument('-fd','--facedetection',dest='face_detection_mode',type=int,default=0,help='[Future functions] FaceDetectionMode. (0:=Disabled, 1:=Full, 2:=Short Default=0)')
    parser.add_argument('-numncs','--numberofncs',dest='number_of_ncs',type=int,default=1,help='Number of NCS. (Default=1)')
    parser.add_argument('-vidfps','--fpsofvideo',dest='fps_of_video',type=int,default=30,help='FPS of Video. (Default=30)')

    args = parser.parse_args()

    number_of_camera = args.number_of_camera
    camera_width  = args.camera_width
    camera_height = args.camera_height
    ssd_detection_mode = args.ssd_detection_mode
    face_detection_mode = args.face_detection_mode
    number_of_ncs = args.number_of_ncs
    vidfps = args.fps_of_video

    if ssd_detection_mode == 0 and face_detection_mode != 0:
        del(LABELS[0])

    try:

        mp.set_start_method('forkserver')
        frameBuffer = mp.Queue(10)
        results = mp.Queue()

        # Start streaming
        p = mp.Process(target=camThread,
                       args=(LABELS, results, frameBuffer, camera_width, camera_height, vidfps, number_of_camera),
                       daemon=True)
        p.start()
        processes.append(p)

        # Start detection MultiStick
        # Activation of inferencer
        p = mp.Process(target=inferencer,
                       args=(results, frameBuffer, ssd_detection_mode, face_detection_mode, camera_width, camera_height, number_of_ncs),
                       daemon=True)
        p.start()
        processes.append(p)

        while True:
            sleep(1)

    except:
        import traceback
        traceback.print_exc()
    finally:
        for p in range(len(processes)):
            processes[p].terminate()

        print("\n\nFinished\n\n")


text:
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
import category_encoders as ce
from tensorflow.keras.models import load_model
import pickle

# Loads transformation pipeline
infile = open("pipe.pkl", 'rb')
pipeline = pickle.load(infile)
infile.close()

# Loads model
model = load_model(
    r'C:\\Users\\Luke Melto\\Documents\\GitHub\\DS\\airbnb_clone\\new model\\saved_model\\my_model')

def predict_price(input, model=model, pipeline=pipeline):
    '''
    Function the runs input through the model and
    returns the result
    '''

    x = pipeline.transform(input)
    suggested_price = model.predict(x)

    return suggested_price

ext: py

reason: import numpy


text:
import numpy
from numpy import zeros, ones
from numpy import asarray

from keras.preprocessing.text import Tokenizer
from keras.models import Sequential
from keras import backend as K
from keras.models import load_model

from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO

import math
import os
import pickle
import re
import sys

from PyPDF2 import PdfFileReader

import os
import os.path

#cur_dir = os.getcwd() # Dir from where search starts can be replaced with any path

def find_file(file_name,cur_dir):
    while True:
        file_list = os.listdir(cur_dir)
        parent_dir = os.path.dirname(cur_dir)
        if file_name in file_list:
            print("File Exists in: ", cur_dir)
            break
        else:
            if cur_dir == parent_dir: #if dir is root dir
                print("File not found")
                break
            else:
                cur_dir = parent_dir
    return os.path.join(cur_dir, file_name)

def custom_sig(x):
    return (K.sigmoid(x) + math.pow(.1, 6))

def convert(fname, word_amount):
    output = StringIO()
    manager = PDFResourceManager()
    converter = TextConverter(manager, output, 'utf-8', laparams=LAParams())
    interpreter = PDFPageInterpreter(manager, converter)
    with open(fname, 'rb') as infile:
        for page_num, page in enumerate(PDFPage.get_pages(infile)):
            interpreter.process_page(page)
            text = output.getvalue()
            if(len(text.split()) >= 256):
                break
    converter.close()
    output.close
    return text

print("------LOOK FOR PATHS TO MODEL FILES-------")
#get urls to models files using the main script
#model_location = find_file("TEXT_IDENTIFY_MODEL_long.h5","/Users/zinebbenameur/clean/datasheet-scrubber/src")
#tokenizer_location = find_file("tokenizer_long.pickle","/Users/zinebbenameur/clean/datasheet-scrubber/src")

model_location = "/Users/zinebbenameur/clean/datasheet-scrubber/TEXT_IDENTIFY_MODEL_long.h5" #Change to the location of the model
tokenizer_location = "/Users/zinebbenameur/clean/datasheet-scrubber/tokenizer_long.pickle" #Change to the location of the tokenizer

print(str(model_location),str(tokenizer_location))

#pdf_location = r"D:/Full_Dataset/ADC/PDFs/ad7819.pdf" #Change to the current pdf

#pdf_location = str(sys.argv[1])
pdf_location = input("Enter a PDF's location and name: ")

#pdf_location = get_pdf()
#print("PDF LOCATION", pdf_location)

word_amount = 256
model = load_model(model_location, custom_objects={'custom_sig':custom_sig})

with open(tokenizer_location, 'rb') as handle:
    t = pickle.load(handle)

data = convert(pdf_location, word_amount)
data = data.lower()
cleaned_data = ""
skip_eol = False
for char in data:
    if(char == "-"):
        skip_eol = True
    elif(skip_eol and char != " " and char != r"\n"):
        cleaned_data += " "
        skip_eol = False

    if(not skip_eol):
        cleaned_data += char

data = cleaned_data.replace(r"\n", " ")
regex = re.compile('[^a-z" "]')
data = regex.sub(" ", data)

encoded_data = t.texts_to_sequences([data])
#print(encoded_data)
data_final = []
for data in encoded_data:
    #print(data)
    temp_array1D = []
    for iter in range(word_amount):
        if(iter < len(data)):
            temp_array1D.append(data[iter])
        else:
            temp_array1D.append(0)
    data_final.append(temp_array1D)
data_final = numpy.array(data_final)

final_identification = model.predict(data_final)

#print(final_identification)

Types = ['ADC', 'AFE', 'Amplifiers - Audio', 'Amplifiers - Video Amps and Modules', 'Clock Buffers, Drivers', 'Clocks',
        'CODECs', 'Controllers', 'CPLDs', 'DAC', 'Delay Lines', 'Drivers, Receivers, Transceivers', 'DSP', 'FPGAs', 'Instrumentation, OP Amps, Buffer Amps',
        'Interface Signal Buffers, Repeaters, Splitters', 'IO Expanders', 'Linear - Comparators', 'Linear - Video Processing', 'Logic - Buffers, Drivers, Receivers, Transceivers',
        'Logic - Counters, Dividers', 'Logic - FIFOs Memory', 'Logic - Flip Flops', 'Logic - Gates', 'Logic - Latches', 'Logic - Shift Registers',
        'Logic - Signal Switches, Multiplexers, Decoders', 'Logic - Translators, Level Shifters', 'Logic - Universal Bus Functions', 'Memory', 'Microcontrollers',
        'Microprocessors', 'PMIC - AC DC Converters, Offline Switchers', 'PMIC - Current Regulation Management', 'PMIC - Display Drivers', 'PMIC - Energy Metering',
        'PMIC - Full, Half-Bridge Drivers', 'PMIC - Gate Drivers', 'PMIC - Hot Swap Controllers', 'PMIC - Motor Drivers, Controllers', 'PMIC - PFC (Power Factor Correction)',
        'PMIC - Power Distribution', 'PMIC - Power Supply Controllers, Monitors', 'PMIC - Supervisors', 'PMIC - Voltage Reference', 'PMIC - Voltage Regulators', 'Potentiometer',
        'SoC', 'Switches', 'Timers', 'UARTs']

print(Types[numpy.argmax(final_identification[0])])

def get_number_pages():
    return PdfFileReader(open(pdf_location,'rb'))

max_stars_repo_name: idea-fasoc/datasheet-scrubber

max_issues_repo_name: umich-cadre/FASoC-Datasheet-Scrubber

max_forks_repo_name: umich-cadre/FASoC-Datasheet-Scrubber

reason: import numpy,from numpy


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

N = 6	#number of sites
V = 0.
t = 1.

def getHam(N,t,V):
	'''
	Input:
		N = number of sites
		t = nearest neighbour interaction
		V = self interaction
	Output:
		N*N matrix
	'''
	H = np.zeros((N,N))

	#zeroth and (N-1)th diagonal element
	H[0][0] = V
	H[N-1][N-1] = V
	#zeroth row
	H[0][1] = -t
	H[0][N-1] = -t
	#(N-1)th row
	H[N-1][0] = -t
	H[N-1][N-2] = -t

	for i in range(1,N-1):
		H[i][i] = V
		H[i][i+1] = -t
		H[i][i-1] = -t

	return H

H = getHam(N,t,V)
if np.allclose(H.T,H):
	print "Hermitian Hamiltonian !!"
else:
	print "Bad Hamiltonian!!!"
#eigvalue:	ndarray with ascending eigenvalue
#eigvec:	eigvec[:,i] is eigenvector corresponding to eigvalue[i]
eigvalue, eigvec = np.linalg.eigh(H)

index = range(N)

for k in range(N):
	plt.plot(index,abs(eigvec[:,k])**2)
	plt.pause(1.0)

plt.show()
print H


text:
#!/usr/bin/env python
""" Test modules for Driven Cavity Stokes preconditioners. """

import proteus.test_utils.TestTools as TestTools
from proteus.iproteus import *

Profiling.logLevel = 7
Profiling.verbose = True

import os
import sys
import inspect
import numpy as np
import tables
import pickle
import petsc4py
import pytest

TestTools.addSubFolders( inspect.currentframe() )
import stokesDrivenCavity_2d_p
import stokesDrivenCavity_2d_n

@pytest.mark.LinearSolvers
@pytest.mark.modelTest
class TestStokes(proteus.test_utils.TestTools.SimulationTest):
    """Run a Stokes test with mumps LU factorization """

    def setup_method(self):
        reload(stokesDrivenCavity_2d_p)
        reload(stokesDrivenCavity_2d_n)
        self.pList = [stokesDrivenCavity_2d_p]
        self.nList = [stokesDrivenCavity_2d_n]
        self.so = default_so
        self.so.tnList = [0.,1.]
        self.so.name = self.pList[0].name
        self.so.sList = self.pList[0].name
        self.so.sList = [default_s]

    def teardown_method(self):
        """Tear down function. """
        Profiling.closeLog()
        FileList = ['proteus_default.log',
                    'proteus.log',
                    #'rdomain.ele',
                    #'rdomain.edge',
                    #'rdomain.neig',
                    #'rdomain.node',
                    #'rdomain.poly',
                    'drivenCavityStokesTrial.h5',
                    'drivenCavityStokesTrial.xmf']
        self.remove_files(FileList)

    def _setPETSc(self):
        petsc4py.PETSc.Options().setValue("ksp_type","fgmres")
        petsc4py.PETSc.Options().setValue("ksp_atol",1e-20)
        petsc4py.PETSc.Options().setValue("ksp_atol",1e-12)
        petsc4py.PETSc.Options().setValue("pc_fieldsplit_type","schur")
        petsc4py.PETSc.Options().setValue("pc_fieldsplit_schur_fact_type","upper")
        petsc4py.PETSc.Options().setValue("fieldsplit_velocity_ksp_type","preonly")
        petsc4py.PETSc.Options().setValue("fieldsplit_velocity_pc_type","lu")
        petsc4py.PETSc.Options().setValue("fieldsplit_pressure_ksp_type","preonly")

    def _setPETSc_LU(self):
        petsc4py.PETSc.Options().setValue("ksp_type","preonly")
        petsc4py.PETSc.Options().setValue("pc_type","lu")
        petsc4py.PETSc.Options().setValue("pc_factor_mat_solver_package","mumps")

    def _runTest(self):
        Profiling.openLog('proteus.log',7)
        self._scriptdir = os.path.dirname(__file__)
        self.ns = NumericalSolution.NS_base(self.so,
                                            self.pList,
                                            self.nList,
                                            self.so.sList,
                                            opts)
        self.ns.calculateSolution('stokes')
        actual = tables.open_file('drivenCavityStokesTrial.h5','r')
        expected_path = 'comparison_files/' + 'comparison_' + 'drivenCavityStokes' + '_velocity_t1.csv'
        #write comparison file
        #np.array(actual.root.velocity_t1).tofile(os.path.join(self._scriptdir, expected_path),sep=",")
        np.testing.assert_almost_equal(np.fromfile(os.path.join(self._scriptdir, expected_path),sep=","),np.array(actual.root.velocity_t1).flatten(),decimal=2)
        actual.close()

    @pytest.mark.slowTest
    def test_01_FullRun(self):
        stokesDrivenCavity_2d_n.linearSmoother = proteus.LinearSolvers.Schur_Qp
        self._setPETSc()
        self._runTest()
        relpath = 'comparison_files/Qp_expected.log'
        actual_log = TestTools.NumericResults.build_from_proteus_log('proteus.log')
        expected_log = TestTools.NumericResults.build_from_proteus_log(os.path.join(self._scriptdir,
                                                                                relpath))
        plot_lst = [(1.0,0,0),(1.0,1,0),(1.0,2,0)]
        L1 = expected_log.get_ksp_resid_it_info(plot_lst)
        L2 = actual_log.get_ksp_resid_it_info(plot_lst)
        assert L1 == L2

    @pytest.mark.slowTest
    def test_02_FullRun(self):
        self._setPETSc_LU()
        self._runTest()

if __name__ == '__main__':
    pass


text:
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft

Fs = 1200       # Sample frequence
N = 300         # Number of sample points
T = 1.0 / Fs    # Sample spacing
x = np.linspace(T, N * T, N)

# I Component
f1 = 130
y1 = np.sin(f1 * 2.0 * np.pi * x)

# II Component
f2 = 288
y2 = np.sin(f2 * 2.0 * np.pi * x)

# Complete Component
y = y1 + y2

# Calculate FFT
Y = fft(y)

xf = np.linspace(0.0, 1.0 / (2.0 * T), N // 2)

# Display FFT
plt.figure(1)
plt.stem(xf, 2.0 / N * np.abs(Y[0:N//2]))
plt.title('FFT Spectrum\n$x(t) = \sin(2\pi130t) + \sin(2\pi288t)$')
plt.xlabel('Frequency [Hz]')
plt.grid()
plt.show()

max_stars_repo_name: SKantar/SignalProcessing

max_issues_repo_name: SKantar/SignalProcessing

max_forks_repo_name: SKantar/SignalProcessing

reason: import numpy,from scipy


text:
import sys
from configs.reuters_args import *
import json
from models import *
from dataLoader.batch import batcher
import torch.nn.functional as F
import torch.optim as optim
import torch.nn as nn
import torch as T
from utils import *
import argparse
import math
import random
import numpy as np
import pickle
import logging
logging.disable(logging.CRITICAL)

parser = argparse.ArgumentParser(description='Model Name and stuff')
parser.add_argument('--model', type=str, default="CNN_custom2")
flags = parser.parse_args()

device = T.device('cuda' if T.cuda.is_available() else 'cpu')
if device == T.device('cuda'):
    T.set_default_tensor_type(T.cuda.FloatTensor)
else:
    T.set_default_tensor_type(T.FloatTensor)

model_name = flags.model
dataset = "Reuters"

config_dict = {'CNN': CNN_config,
               'CNN_att': CNN_att_config,
               'CNN_capsule': CNN_capsule_config,
               'CNN_heinsen_capsule': CNN_heinsen_capsule_config,
               'CNN_DSA': CNN_DSA_config,
               'CNN_DSA_global': CNN_DSA_config,
               'CNN_PCaps': CNN_PCaps_config,
               'CNN_custom': CNN_custom_config,
               'CNN_custom_alpha_ablation': CNN_custom_config,
               'CNN_custom_global': CNN_custom_config,
               'CNN_custom2': CNN_custom2_config}

model_dict = {'CNN': CNN,
              'CNN_att': CNN_att,
              'CNN_capsule': CNN_capsule,
              'CNN_heinsen_capsule': CNN_heinsen_capsule,
              'CNN_DSA': CNN_DSA,
              'CNN_DSA_global': CNN_DSA_global,
              'CNN_PCaps': CNN_PCaps,
              'CNN_custom': CNN_custom,
              'CNN_custom_alpha_ablation': CNN_custom_alpha_ablation,
              'CNN_custom_global': CNN_custom_global,
              'CNN_custom2': CNN_custom2}

config = config_dict[model_name]
config = config()
accu_step = config.total_batch_size//config.train_batch_size

print("\n\nTesting Model: {}\n\n".format(model_name))

Encoder = model_dict[model_name]

embedding_filename = 'processed_data/Reuters_vocab.pkl'
test_filename = 'processed_data/Reuters_test.json'
classes_num = 90

with open(embedding_filename, 'rb') as file:
    data = pickle.load(file)
vocab2idx = data['vocab2idx']
embeddings = data['embeddings']
PAD = vocab2idx['<PAD>']

val_classes = []
val_texts = []
val_texts_idx = []
with open(test_filename, 'r') as file:
    for obj in file:
        row = json.loads(obj)
        val_classes.append(row['Class'])
        val_texts.append(row['Text'])
        val_texts_idx.append(row['Text_idx'])

def test(time):

    model = Encoder(embeddings=embeddings,
                    pad_idx=PAD,
                    classes_num=classes_num,
                    config=config,
                    device=device)

    model = model.to(device)
    # model = T.nn.DataParallel(model)

    # Prepare optimizer
    optimizer = loadAdamW(model, config)

    display_step = 100  # 100
    example_display_step = 500
    patience = 5

    checkpoint_path = "saved_params/{}_{}_{}.pt".format(dataset, model_name, time)

    print('Loading pre-trained weights for the model...')
    checkpoint = T.load(checkpoint_path)
    model.load_state_dict(checkpoint['model_state_dict'])

    total_val_cost = 0
    all_labels = []
    all_predictions = []
    n = 0

    for batch_texts, batch_texts_idx, \
        batch_labels, batch_mask in batcher(val_texts,
                                            val_texts_idx,
                                            val_classes,
                                            PAD,
                                            config.val_batch_size):

        if n % display_step == 0:
            print("Testing Batch {}".format(n+1))

        with T.no_grad():

            predictions, loss = predict(model=model,
                                        text_idx=batch_texts_idx,
                                        labels=batch_labels,
                                        input_mask=batch_mask,
                                        device=device,
                                        train=False)

            cost = loss.item()

            total_val_cost += cost
            labels = batch_labels.reshape(-1).tolist()
            all_labels += labels
            all_predictions += predictions.tolist()

        n += 1
        # T.cuda.empty_cache()

    prec, rec, acc = multi_micro_metrics(all_predictions,
                                         all_labels,
                                         verbose=False)
    val_F1 = compute_F1(prec, rec)
    avg_val_cost = total_val_cost/n

    print("\n\nTEST\n\n")

    print("Cost = " +
          "{:.3f}".format(avg_val_cost)+", F1 = " +
          "{:.3f}".format(val_F1)+", Accuracy = " +
          "{:.3f}".format(acc))

    return val_F1, acc

F1s = []
accs = []

for time in range(5):
    print("\n\nCurrent Run: {}\n\n".format(time))
    F1, acc = test(time)
    F1s.append(F1)
    accs.append(acc)

print("\n\n")

print("Mean-std F1: {} +- {}".format(np.mean(F1s), np.std(F1s)))
print("Mean-std Accuracy: {} +- {}".format(np.mean(accs), np.std(accs)))
print("Max F1: {}".format(np.max(F1s)))
print("Corresponding Accuracy: {}".format(accs[np.argmax(F1s)]))


text:
# implementation based on the paper "Ant Colony System for Graph Coloring Problem", IJESC 2017, Volume 7 Issue No.7
# http://ijesc.org/upload/c6c0941d337a1b5b634062a54bb33d5c.Ant%20Colony%20System%20for%20Graph%20Coloring%20Problem.pdf
# author: brygida czech
import networkx as nx
import random
import numpy as np

class Ant:
    # create new ant
    # alpha: the relative importance of pheromone (si_ij)
    # beta: the relative importance of heuristic value (n_ij)
    def __init__(self, alpha=1, beta=3):
        self.graph = None
        self.colors = {}
        self.start = None
        self.visited = []
        self.unvisited = []
        self.alpha = alpha
        self.beta = beta
        self.distance = 0 # number of used colors on a valid solution
        self.number_colisions = 0 # only for consistency check, should be always 0
        self.colors_available = []
        self.colors_assigned = {}

    # reset everything for a new solution
    # start: starting node in g (random by default)
    # return: Ant
    def initialize(self, g, colors, start=None):
        self.colors_available = sorted(colors.copy())
        # init assigned colors with None
        keys = [n for n in g_nodes_int]
        self.colors_assigned = {key: None for key in keys}

        # start node
        if start is None:
            self.start = random.choice(g_nodes_int)
        else:
            self.start = start

        self.visited = []
        self.unvisited = g_nodes_int.copy()

        # assign min. color number to the start node
        if (len(self.visited)==0):
            self.assign_color(self.start, self.colors_available[0])
        return self

    # assign color to node and update the node lists
    def assign_color(self, node, color):
        self.colors_assigned[node] = color
        self.visited.append(node)
        self.unvisited.remove(node)

    # assign a color to each node in the graph
    def colorize(self):
        len_unvisited = len(self.unvisited)
        tabu_colors = []
        # assign color to each unvisited node
        for i in range(len_unvisited):
            next = self.next_candidate()
            tabu_colors = []
            # add colors of neighbours to tabu list
            for j in range(number_nodes):
                if (adj_matrix[next,j]==1):
                    tabu_colors.append(self.colors_assigned[j])
            # assign color with the smallest number that is not tabu
            for k in self.colors_available:
                if (k not in tabu_colors):
                    self.assign_color(next,k)
                    break
        # save distance of the current solution
        self.distance = len(set(self.colors_assigned.values()))
        # consitency check
        ##self.number_colisions = self.colisions()
        ##print('colisions: ' + str(self.number_colisions))

    # return the number of different colors among the neighbours of node
    def dsat(self, node=None):
        if node is None:
            node = self.start
        col_neighbors = []
        for j in range(number_nodes):
            if (adj_matrix[node, j]==1):
                col_neighbors.append(self.colors_assigned[j])
        return len(set(col_neighbors))

    # return the pheromone trail of the pair (node,adj_node)
    def si(self, node, adj_node):
        return phero_matrix[node, adj_node]

    # select next candidate node according to the transition rule
    def next_candidate(self):
        if (len(self.unvisited)==0):
           candidate = None
        elif (len(self.unvisited)==1):
            candidate = self.unvisited[0]
        else:
            max_value = 0
            heuristic_values = []
            candidates = []
            candidates_available = []
            for j in self.unvisited:
                heuristic_values.append((self.si(self.start, j)**self.alpha)*(self.dsat(j)**self.beta))
                candidates.append(j)
            max_value = max(heuristic_values)
            for i in range(len(candidates)):
                if (heuristic_values[i] >= max_value):
                   candidates_available.append(candidates[i])
            candidate = random.choice(candidates_available)
        self.start = candidate
        return candidate

    # return your own pheromone trail
    def pheromone_trail(self):
        phero_trail = np.zeros((number_nodes, number_nodes), float)
        for i in g_nodes_int:
            for j in g_nodes_int:
                if (self.colors_assigned[i]==self.colors_assigned[j]):
                    phero_trail[i,j] = 1
        return phero_trail

    # consistency check --> should always return 0
    def colisions(self):
        colisions = 0
        for key in self.colors_assigned:
            node = key
            col = self.colors_assigned[key]
            # check colors of neighbours
            for j in range(number_nodes):
                if (adj_matrix[node, j]==1 and self.colors_assigned[j]==col):
                    colisions = colisions+1
        return colisions

# take input from the txt.file and create an undirected graph
def create_graph():
    global number_nodes
    p = 0.15
    g = nx.fast_gnp_random_graph(number_nodes, p)
    for (u, v) in g.edges():
        g.edges[u, v]['weight'] = random.randint(1, 4)
    return g

#draw the graph and display the weights on the edges
def draw_graph(g):
	pos = nx.spring_layout(g)
	#values = [col_val.get(node, 'blue') for node in g.nodes()]
	nx.draw(g, pos, with_labels = True, edge_color = 'black' ,width = 1, alpha = 0.7)  #with_labels=true is to show the node number in the output graph

# initiate a selection of colors for the coloring and compute the min. number of colors needed for a proper coloring
def init_colors(g):
    # grundy (max degree+1)
    colors = []
    grundy = len(nx.degree_histogram(g))
    for c in range(grundy):
       colors.append(c)
    return colors

# create a pheromone matrix with init pheromone values: 1 if nodes not adjacent, 0 if adjacent
def init_pheromones(g):
    phero_matrix = np.ones((number_nodes, number_nodes), float)
    for node in g:
        for adj_node in g.neighbors(node):
            phero_matrix[node, adj_node] = 0
    return phero_matrix

# calculate the adjacency matrix of the graph
def adjacency_matrix(g):
    adj_matrix = np.zeros((number_nodes, number_nodes), int)
    for node in g_nodes_int:
        for adj_node in g.neighbors(node):
            adj_matrix[node, adj_node] = 1
    return adj_matrix

# create new colony
def create_colony():
    ants = []
    ants.extend([Ant().initialize(g, colors) for i in range(number_ants)])
    return ants

# apply decay rate to the phero_matrix
def apply_decay():
    for node in g_nodes_int:
        for adj_node in g_nodes_int:
            phero_matrix[node, adj_node] = phero_matrix[node, adj_node]*(1-phero_decay)

# select colony's best solution
# update pheromone_matrix according to the elite solution
# return elite solution (coloring) with its distance (number of used colors)
def update_elite():
    global phero_matrix
    # select elite
    best_dist = 0
    elite_ant = None
    for ant in ants:
        if (best_dist==0):
            best_dist = ant.distance
            elite_ant = ant
        elif (ant.distance < best_dist):
            best_dist = ant.distance
            elite_ant = ant
    # update global phero_matrix
    elite_phero_matrix = elite_ant.pheromone_trail()
    phero_matrix = phero_matrix + elite_phero_matrix
    return elite_ant.distance, elite_ant.colors_assigned

# ------------- entry point -------------
# param input_graph - a networkx graph to be colored (node coloring)
# param num_ants - number of ants in the colony
# param iter - number of iterations to be performed
# param a - relative importance of elite pheromones
# param b - relative importance of heuristic value (DSAT)
# param decay - evaporation of pheromones after each iteration
def solve(input_graph, num_ants=10, iter=10, a=1, b=3, decay=0.8):
    global g # graph to be colored (a networkx graph)
    global number_nodes
    global g_nodes_int
    global number_ants
    global alpha
    global beta
    global phero_decay
    global adj_matrix
    global phero_matrix
    global colors
    global ants

    # params
    g = input_graph
    number_ants=num_ants
    number_iterations=iter
    alpha = a # relative importance of pheromone (si_ij)
    beta = b # relative importance of heuristic value (n_ij)
    phero_decay=decay # rate of pheromone decay

    # results
    final_solution = {} # coloring of the graph
    final_costs = 0 # number of colors in the solution
    iterations_needed = 0

    # init
    number_nodes = nx.number_of_nodes(g)
    g_nodes_int = []
    for node in g.nodes():
        g_nodes_int.append(node)
    g_nodes_int = list(map(int, sorted(g_nodes_int)))
    adj_matrix = adjacency_matrix(g)
    colors = init_colors(g)
    phero_matrix = init_pheromones(g)

    # ACO_GCP daemon
    for i in range(number_iterations):
        # create colony
        ants = []
        ants = create_colony()
        # let colony find solutions
        for ant in ants:
            ant.colorize()
        # apply decay rate
        apply_decay()
        # select elite and update si_matrix
        elite_dist, elite_sol = update_elite()
        # estimate global solution so far
        if (final_costs==0):
            final_costs = elite_dist
            final_solution = elite_sol
            iterations_needed = i+1
        elif (elite_dist<final_costs):
            final_costs = elite_dist
            final_solution = elite_sol
            iterations_needed = i+1
    return final_costs, final_solution, iterations_needed

# global vars
#g = None # graph to be colored
number_nodes = 20
g_nodes_int = []
number_ants = 10
alpha = 1
beta = 3
phero_decay = 0.8
adj_matrix = np.zeros((number_nodes, number_nodes), int)
phero_matrix = np.ones((number_nodes, number_nodes), float)
colors = []
ants = []

def main():
    g = create_graph()
    draw_graph(g)
    s = solve(g)
    print(f"final cost is {s[0]}")
    print()
    print(f"final solution is {s[1]}")
    print()
    print(f"the number of iterations needed was {s[2]}")

if __name__ == '__main__':
    main()

max_stars_repo_name: andremtsilva/dissertacao

max_issues_repo_name: andremtsilva/dissertacao

max_forks_repo_name: andremtsilva/dissertacao

reason: import numpy,import networkx


text:
from absl import app
from absl import flags
from absl import logging
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import math

FLAGS = flags.FLAGS

flags.DEFINE_string("tfhub_module", None, "TF Hub module handle")
flags.DEFINE_string("tfhub_tag", None, "TF Hub tag to use")
flags.DEFINE_enum("tfhub_type", "image", ["image", "text"], "Type of the tf hub module")
flags.DEFINE_boolean("tfhub_newmodule", True, "New or old module type (call type)")

from .reader import generic
from .reader.tfds import get_batch_iterator
from .reader.folder import read as read_from_folder
from .reader.tfds import read as read_from_tfds
from .reader.textfile import read as read_from_textfile
from .reader.matrix import apply_fn_matrices

def setup():
    if not FLAGS.tfhub_module:
        raise app.UsageError("--tfhub_module has to be specified!")

def get_text_fn(to_numpy = False, string_input = False):
    embedding = FLAGS.tfhub_module
    if FLAGS.tfhub_newmodule:
        module = hub.KerasLayer(embedding, input_shape=[], dtype=tf.string, trainable=False)

    else:
        module = hub.load(FLAGS.tfhub_module)

    if to_numpy:
        output = lambda z: module(z).numpy()
    else:
        output = lambda z: module(z)

    if string_input and FLAGS.tfhub_newmodule:
        compute = lambda z: output(tf.constant(z))
    else:
        compute = lambda z: output(z)

    return compute

def load_and_apply_from_tfds():
    if FLAGS.tfhub_type == "image":
        if FLAGS.tfhub_tag is None:
            module = hub.load(FLAGS.tfhub_module)
        elif FLAGS.tfhub_tag == "":
            module = hub.load(FLAGS.tfhub_module, tags=[])
        else:
            module = hub.load(FLAGS.tfhub_module, tags=[FLAGS.tfhub_tag])

        compute = lambda z: module.signatures["default"](z)["default"]
        return read_from_tfds(compute, True, True, True)

    # ELSE Text
    return read_from_tfds(get_text_fn(False, False), True, False, False)

def load_and_apply_from_folder():
    if FLAGS.tfhub_type == "text":
        raise app.UsageError("Hub module for Text only alowed in combination with tfds or textfile so far!")

    if FLAGS.tfhub_tag is None:
        module = hub.load(FLAGS.tfhub_module)
    elif FLAGS.tfhub_tag == "":
        module = hub.load(FLAGS.tfhub_module, tags=[])
    else:
        module = hub.load(FLAGS.tfhub_module, tags=[FLAGS.tfhub_tag])

    def apply_tf_fn(images):
        images = tf.cast(images, tf.float32)
        compute = lambda z: module.signatures["default"](z)["default"]
        return compute(images)

    return read_from_folder(apply_tf_fn)

def load_and_apply_from_textfile():
    if FLAGS.tfhub_type == "image":
        raise app.UsageError("Hub module for images not allowed for text files inputs!")

    return read_from_textfile(get_text_fn(True, True))

def load_and_apply():
    setup()

    if FLAGS.variant == "tfds":
        return load_and_apply_from_tfds()

    if FLAGS.variant == "folder":
        return load_and_apply_from_folder()

    if FLAGS.variant == "textfile":
        return load_and_apply_from_textfile()

    # if not directly supported -> use generic reader
    features, dim, samples, labels = generic.read()
    return apply(features, dim, samples, labels)

def apply(features, dim, samples, labels):
    setup()

    if FLAGS.tfhub_type == "text":
        raise app.UsageError("Hub module for Text only alowed in combination with tfds or textfile so far!")

    if FLAGS.tfhub_tag is None:
        module = hub.load(FLAGS.tfhub_module)
    elif FLAGS.tfhub_tag == "":
        module = hub.load(FLAGS.tfhub_module, tags=[])
    else:
        module = hub.load(FLAGS.tfhub_module, tags=[FLAGS.tfhub_tag])

    def fn(images):
        # TODO dynamically check input signature
        images = [tf.image.resize(tf.cast(x, tf.float32), [FLAGS.resize_height, FLAGS.resize_width]) for x in images]
        images = tf.stack(images)
        compute = lambda z: module.signatures["default"](z)["default"]
        return compute(images).numpy()

    return apply_fn_matrices(features, dim, samples, labels, fn)


text:
import cv2
import mediapipe as mp
import math
import numpy as np
import requests
import serial
import time

arduino = serial.Serial(port='COM5', baudrate=115200, timeout=.1)
def write_read(x):
	arduino.write(x.encode())
	data = arduino.readline()
	return data

###################  initializations  ################################################################
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands

#url = 'http://192.168.43.206:8080/shot.jpg'
cap = cv2.VideoCapture(0)

x=y=0
zmem = 30 #For the memory of the previous z value and 30 is an initial z value
######################################################################################################

######################### For calculating the z value from reference sphere ##########################
def zvalue(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray_blurred = cv2.blur(gray, (3, 3))
    detected_circles = cv2.HoughCircles(gray_blurred,cv2.HOUGH_GRADIENT, 1, 20, param1 = 200, param2 = 50, minRadius = 20, maxRadius = 60)
    # Draw circles that are detected.
    if detected_circles is not None:
        # Convert the circle parameters a, b and r to integers.
        detected_circles = np.uint16(np.around(detected_circles))
        for pt in detected_circles[0, :]:
            a, b, r = pt[0], pt[1], pt[2]
            z =  r
            #print(r,' ',z)
			# Draw the circumference of the circle.
            cv2.circle(image, (a, b), r, (0, 255, 0), 2)

            # Draw a small circle (of radius 1) to show the center.
            cv2.circle(image, (a, b), 1, (0, 0, 255), 3)
            return z
    else:
    	return zmem
######################################################################################################

############################## Calculating the angle #################################################
def angle_calculator(x,y,z):
	# add a if with contiton that robot spherical ball should be within range of obtained x,y,z for robot logic to work
	xcm = (x * .02171875) - 6.95   #since we take value from half of the sceen. width = 13.9 cm  = .02171875 cm per pixel
	if z is not None:
		angle = math.degrees(math.atan2(xcm,z))
	else:
		angle = math.degrees(math.atan2(xcm,30))
	if x > 320:
		print('RIGHT',' ',angle )
	else:
		print('LEFT',' ',angle)
	angle = str(angle)
	#print(angle)
	print(write_read(angle))
######################################################################################################

############################## Hand Detection ########################################################
with mp_hands.Hands(min_detection_confidence=0.5,min_tracking_confidence=0.5) as hands:
  while True:
#    img_resp = requests.get(url)
#    img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
#    image = cv2.imdecode(img_arr, -1)
#    image = cv2.flip(image, 1)
    ret, image = cap.read()  #option for webcam

    image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
    image.flags.writeable = False
    results = hands.process(image)
    image.flags.writeable = True
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    image_height, image_width, _ = image.shape

    if results.multi_hand_landmarks:
      for hand_landmarks in results.multi_hand_landmarks:
        mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
        #print('hand_landmarks:', hand_landmarks)
        x = (hand_landmarks.landmark[4].x )* image_width # - (hand_landmarks.landmark[8].x )
        y = (hand_landmarks.landmark[4].y )# - (hand_landmarks.landmark[8].y )
        #myradians = math.degrees(math.atan2(y,x))
        #print(
        #  f'Index finger tip coordinates: (',
        #  f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width}, '
        #  f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_height},')
          #f'{myradians})')

    zmem = zvalue(image)
    angle_calculator(x,y,zmem)
    print(zmem)

    cv2.imshow('MediaPipe Hands', image)
    #cv2.imshow('MediaPipe Hand', image1)
    if cv2.waitKey(5) & 0xFF == 27:
      break
######################################################################################################

cap.release()


text:
import assemble_core
import unittest
import merino.setup_logger as setup_logger
import logging
import prism_metadata
import davepool_data
import numpy
import os
import glob

logger = logging.getLogger(setup_logger.LOGGER_NAME)

class TestAssembleWithObjects(unittest.TestCase):
    def test_build_davepool_to_cells_map(self):
        l = [prism_metadata.PrismCell(pool_id=str(x + 20), analyte_id=str(x + 10), davepool_id=str(x / 2)) for x in range(6)]
        logger.debug("l:  {}".format(l))

        r = assemble_core.build_davepool_id_to_cells_map(l)
        assert len(r) == 3, len(r)
        logger.debug("r:  {}".format(r))

        for x in range(3):
            y = str(x)
            assert y in r, y
            assert len(r[y]) == 2, r[y]

    def test_build_data_by_cell(self):
        cells = [prism_metadata.PrismCell(pool_id=str(x + 20), analyte_id=str(x + 10), davepool_id=str(x / 2)) for x in range(2)]
        logger.debug("cells:  {}".format(cells))

        davepool_data_obj = davepool_data.DavepoolData()
        davepool_data_obj.median_headers = ["Location", "10", "11"]
        davepool_data_obj.median_headers.extend([str(x) for x in range(30,40)])

        davepool_data_obj.median_data = {"A01": [1,2], "J13": [3,5]}
        davepool_data_obj.median_data['A01'].extend([x for x in range(40,50)])
        davepool_data_obj.median_data['J13'].extend([x for x in range(50,60)])

        davepool_data_obj.count_headers = davepool_data_obj.median_headers
        davepool_data_obj.count_data = {"B03": [7,11], "L17": [13,17]}
        davepool_data_obj.count_data["B03"].extend([x for x in range(60,70)])
        davepool_data_obj.count_data["L17"].extend([x for x in range(70,80)])
        logger.debug("davepool_data_obj:  {}".format(davepool_data_obj))

        rep = assemble_core.build_data_by_cell(cells, davepool_data_obj)

        r_med = rep[0]
        assert r_med is not None
        logger.debug("r_med:  {}".format(r_med))
        assert r_med.well_list is not None
        assert len(r_med.well_list) == 2, len(r_med.well_list)
        assert "A01" in r_med.well_list, r_med.well_list
        assert "J13" in r_med.well_list, r_med.well_list
        assert len(r_med.cell_data_map) == len(cells), (len(r_med.cell_data_map), len(cells))

        r_count = rep[1]
        assert len(r_count.well_list) == 2, len(r_count.well_list)
        logger.debug("r_count.well_list:  {}".format(r_count.well_list))
        assert "B03" in r_count.well_list, r_count.well_list
        assert "L17" in r_count.well_list, r_count.well_list

        assert len(r_count.cell_data_map) == len(cells), (len(r_count.cell_data_map), len(cells))
        logger.debug("r_count.cell_data_map:  {}".format(r_count.cell_data_map))

    def test_process_data(self):
        cells = [prism_metadata.PrismCell(pool_id=str(x + 20), analyte_id=str(x + 10), davepool_id=str(x / 2)) for x in range(4)]
        logger.debug("cells:  {}".format(cells))
        cells_map = assemble_core.build_davepool_id_to_cells_map(cells)
        logger.debug("cells_map:  {}".format(cells_map))

        davepool_list = []
        davepool_data_obj = davepool_data.DavepoolData()
        davepool_list.append(davepool_data_obj)

        davepool_data_obj.davepool_id = "0"
        davepool_data_obj.median_headers = [str(x) for x in range(10,22)]
        davepool_data_obj.median_headers.insert(0, "Location")
        davepool_data_obj.median_data = {"A01": [1,2], "J13": [3,5]}
        davepool_data_obj.median_data["A01"].extend([x for x in range(40,50)])
        davepool_data_obj.median_data["J13"].extend([x for x in range(50,60)])

        davepool_data_obj.count_headers = davepool_data_obj.median_headers
        davepool_data_obj.count_data = {"A01": [7,11], "J13": [13,17]}
        davepool_data_obj.count_data["A01"].extend([x for x in range(60,70)])
        davepool_data_obj.count_data["J13"].extend([x for x in range(70,80)])
        logger.debug("davepool_data_obj:  {}".format(davepool_data_obj))

        davepool_data_obj = davepool_data.DavepoolData()
        davepool_list.append(davepool_data_obj)

        davepool_data_obj.davepool_id = "1"
        davepool_data_obj.median_headers = [str(x) for x in range(10,22)]
        davepool_data_obj.median_headers.insert(0, "Location")
        davepool_data_obj.median_data = {"A01": [-1,-2,19,23], "J13": [-3,-5,29,31]}
        davepool_data_obj.median_data["A01"].extend([x for x in range(80,88)])
        davepool_data_obj.median_data["J13"].extend([x for x in range(90,98)])

        davepool_data_obj.count_headers = davepool_data_obj.median_headers
        davepool_data_obj.count_data = {"A01": [-7,-11,37,41], "J13": [-13,-17,43,47]}
        davepool_data_obj.count_data["A01"].extend([x for x in range(100,108)])
        davepool_data_obj.count_data["J13"].extend([x for x in range(110,118)])
        logger.debug("davepool_data_obj:  {}".format(davepool_data_obj))

        r = assemble_core.process_data(davepool_list, cells_map)
        assert len(r) == 2, len(r)

        median_data_by_cell = r[0]
        assert median_data_by_cell is not None
        logger.debug("median_data_by_cell:  {}".format(median_data_by_cell))
        assert 2 == median_data_by_cell.cell_data_map[cells[1]][0]
        assert 3 == median_data_by_cell.cell_data_map[cells[0]][1]
        t_wells = median_data_by_cell.well_list
        logger.debug("t_wells:  {}".format(t_wells))
        assert "J13" in t_wells

        assert 23 == median_data_by_cell.cell_data_map[cells[3]][0]
        assert 29 == median_data_by_cell.cell_data_map[cells[2]][1]

    def test_build_gctoo(self):
        filepath = "functional_tests/test_assemble/test_write_output_gct.txt"
        if os.path.exists(filepath):
            os.remove(filepath)

        prn = "my_prism_replicate"

        pert_list = []
        p = prism_metadata.Perturbagen()
        p.pert_well= "J01"
        p.pert_id = "BRD-K12345678"
        p.pert_dose = 11.0
        p.pert_dose_unit = "uM"
        pert_list.append(p)
        p = prism_metadata.Perturbagen()
        p.pert_well = "M03"
        p.pert_id = "BRD-K91011121"
        p.pert_dose = 13.0
        p.pert_dose_unit = "mg/mL"
        pert_list.append(p)
        p = prism_metadata.Perturbagen()
        p.pert_well = "B02"
        p.pert_id = "BRD-K31415171"
        p.pert_dose = 17.0
        p.pert_dose_unit = "nM"
        pert_list.append(p)

        cell_list = []
        c = prism_metadata.PrismCell(pool_id="fake pool", analyte_id="fake analyte",
                                     davepool_id="fake davepool", feature_id='c-3')
        cell_list.append(c)
        c = prism_metadata.PrismCell(pool_id="fake pool 2", analyte_id="fake analyte 2",
                                     davepool_id="fake davepool 2", feature_id='c-5')
        cell_list.append(c)
        c = prism_metadata.PrismCell(pool_id="fake pool 2", analyte_id="fake analyte 3",
                                     davepool_id="fake davepool 2", feature_id='c-7')
        cell_list.append(c)

        data_by_cell = assemble_core.DataByCell({cell_list[1]:[1, 2, 11], cell_list[2]:[13, 17, 19], cell_list[0]:[23, 29, 31]},
                                                ["J01", "M03", "B02"])

        r = assemble_core.build_gctoo(prn, pert_list, data_by_cell)
        self.assertIsNotNone(r)
        logger.debug("r:  {}".format(r))
        logger.debug("r.col_metadata_df:  {}".format(r.col_metadata_df))
        logger.debug("r.row_metadata_df:  {}".format(r.row_metadata_df))
        logger.debug("r.data_df:  {}".format(r.data_df))

    def test_build_gctoo_data_df(self):
        #happy path - all numbers
        cell_id_data_map = {"cell1":[0,1,2,3], "cell2":[5,7,11,13]}
        data_df_column_ids = ["s1", "s2", "s3", "s5"]
        r = assemble_core.build_gctoo_data_df(cell_id_data_map, data_df_column_ids)
        self.assertIsNotNone(r)
        logger.debug("r:  {}".format(r))
        self.assertEquals(True, all(data_df_column_ids == r.columns),
                          "columns of result do not match provided columns - data_df_column_ids:  {}  r.columns:  {}".format(
                              data_df_column_ids, r.columns))
        #check that index entries are present and sorted
        self.assertEquals("cell1", r.index[0])
        self.assertEquals("cell2", r.index[1])

        #2nd happy path - blank entry present in data
        cell_id_data_map["cell1"][1] = ""
        cell_id_data_map["cell2"][2] = ""
        r = assemble_core.build_gctoo_data_df(cell_id_data_map, data_df_column_ids)
        self.assertIsNotNone(r)
        logger.debug("r:  {}".format(r))
        s = r["s2"].loc["cell1"]
        logger.debug("s:  {}".format(s))
        self.assertEquals(assemble_core._NaN, s, "expected blank empty string to be replaced with _NaN, was not")

        s = r["s3"].loc["cell2"]
        logger.debug("s:  {}".format(s))
        self.assertEquals(assemble_core._NaN, s, "expected blank empty string to be replaced with _NaN, was not")

if __name__ == "__main__":
    setup_logger.setup(verbose=True)

    unittest.main()

max_stars_repo_name: cmap/merino

max_issues_repo_name: cmap/merino

max_forks_repo_name: cmap/merino


text:
import pandas as pd
import numpy as np
from preprocess.util import generic_groupby

def preprocess_time(df):

    df['global_time'] = loctm_to_global_time(df)
    df['last_time_days'] = df.groupby(['cano','days'])['global_time'].diff(periods = 1)
    df['next_time_days'] = df.groupby(['cano','days'])['global_time'].diff(periods = -1)

    groups = ['cano','locdt']
    feature = 'global_time'
    agg_list = [np.std]
    df = generic_groupby(df, groups, feature, agg_list)

    return df

def loctm_to_global_time(df):

    df = df.copy()
    df['loctm'] = df['loctm'].astype(str)
    df['loctm'] = df['loctm'].str[:-2]
    df['hours'] = df['loctm'].str[-6:-4]
    df['hours'] = np.where(df['hours']=='', '0', df['hours']).astype(int)
    df['minutes'] = df['loctm'].str[-4:-2]
    df['minutes'] = np.where(df['minutes']=='', '0', df['minutes']).astype(int)
    df['second'] = df['loctm'].str[-2:].astype(int)
    df['loctm'] = df['hours']*60*60 + df['minutes']*60 + df['second']
    df['global_time'] = df['locdt']*24*60*60 + df['hours']*60*60+df['minutes']*60+df['second']

    return df['global_time']


text:
from elephant.spike_train_correlation import covariance
from elephant.conversion import BinnedSpikeTrain
from numpy import triu_indices
from quantities import ms
from networkunit.tests.test_two_sample_test import two_sample_test
from networkunit.capabilities.cap_ProducesSpikeTrains import ProducesSpikeTrains

class covariance_test(two_sample_test):
    """
    Test to compare the pairwise covariances of a set of neurons in a network.
    The statistical testing method needs to be set in form of a
    sciunit.Score as score_type.

    Parameters (in dict params):
    ----------
    binsize: quantity, None (default: 2*ms)
        Size of bins used to calculate the correlation coefficients.
    num_bins: int, None (default: None)
        Number of bins within t_start and t_stop used to calculate
        the correlation coefficients.
    t_start: quantity, None
        Start of time window used to calculate the correlation coefficients.
    t_stop: quantity, None
        Stop of time window used to calculate the correlation coefficients.
    binary: bool
        If true, the binned spike trains are set to be binary.
    """

    required_capabilities = (ProducesSpikeTrains, )

    def generate_prediction(self, model, **kwargs):
        # call the function of the required capability of the model
        # and pass the parameters of the test class instance in case the
        covariances = self.get_prediction(model)
        if covariances is None:
            if kwargs:
                self.params.update(kwargs)
            if 'binsize' not in self.params and 'num_bins' not in self.params:
                self.params['binsize'] = 2*ms
            self.spiketrains = model.produce_spiketrains(**self.params)
            covariances = self.generate_covariances(self.spiketrains,
                                                    **self.params)
            self.set_prediction(model, covariances)
        return covariances

    def validate_observation(self, observation):
        # ToDo: Check if observation values are legit (non nan, positive, ...)
        pass

    def generate_covariances(self, spiketrain_list=None, binary=False,
                             ** kwargs):
        """
        Calculates the covariances between all pairs of spike trains.

        Parameters
        ----------
        spiketrain_list : list of neo.SpikeTrain (default None)
            If no list is passed the function tries to access the class
            parameter 'spiketrains'.

        binary: bool (default False)
            Parameter is passed to
            elephant.spike_train_correlation.covariance()

        kwargs:
            Passed to elephant.conversion.BinnedSpikeTrain()

        Returns : list of floats
            list of covariances of length = (N^2 - N)/2 where N is the number
            of spike trains.
        -------
        """
        def robust_BinnedSpikeTrain(spiketrains, binsize=None, num_bins=None,
                                    t_start=None, t_stop=None, **add_args):
            return BinnedSpikeTrain(spiketrains, binsize=binsize,
                                    num_bins=num_bins, t_start=t_start,
                                    t_stop=t_stop)
        if spiketrain_list is None:
            # assuming the class has the property 'spiketrains' and it
            # contains a list of neo.Spiketrains
            binned_sts = robust_BinnedSpikeTrain(self.spiketrains, **kwargs)
        else:
            binned_sts = robust_BinnedSpikeTrain(spiketrain_list, **kwargs)
        cov_matrix = covariance(binned_sts, binary=binary)
        idx = triu_indices(len(cov_matrix), 1)
        return cov_matrix[idx]

max_stars_repo_name: morales-gregorio/NetworkUnit

max_issues_repo_name: morales-gregorio/NetworkUnit

max_forks_repo_name: russelljjarvis/NetworkUnit

reason: from numpy


text:
# Functions from `mpmath` tailor made to handle our matrix objects
# Original Source: https://github.com/fredrik-johansson/mpmath/blob/master/mpmath/matrices/linalg.py

from mpmath import (
    absmin, eps, fsum, inf, mp
)
from cake.helpers import copy

__all__ = ("mnorm", "_BC_DET", "lSolve", "uSolve", "unitVector")

def mnorm(M, p: int = 1):
    n, m = M.dimensions
    if p == 1:
        return max(fsum((M.matrix[i][j] for i in range(m)), absolute=1) for j in range(n))
    elif p == inf:
        return max(fsum((M.matrix[i][j] for j in range(n)), absolute=1) for i in range(m))
    else:
        raise NotImplementedError("matrix p-norm for arbitrary p")

def _BC_DET(M, *, cache: bool = False) -> tuple:
    if (M.dimensions[0] != M.dimensions[1]) or M.dimensions == (1, 1) or not M.matrix:
        raise ValueError(f'Non-square matrixes cannot have a determinant')

    if cache and getattr(M, 'c_dt', None):
        return M.c_dt
    orig = M
    M = M.copy()

    tol = absmin(mnorm(M, 1) * eps)
    rows = M.rows
    p = [None] * (rows - 1)
    for i in range(rows - 1):
        biggest = 0

        for j in range(i, rows):
            s = fsum([absmin(M.matrix[j][l]) for l in range(i, rows)])
            if absmin(s) <= tol:
                raise ZeroDivisionError(f'Matrix is numerically singular')
            current = 1 / s * absmin(M.matrix[j][i])
            if current > biggest:
                biggest = current
                p[i] = j

        M.swaprow(i, p[i])

        if absmin(M.matrix[i][i]) <= tol:
            raise ZeroDivisionError(f'Matrix is numerically singular')
        for k in range(i + 1, rows):
            M.matrix[k][i] /= M.matrix[i][i]
            for m in range(i + 1, rows):
                M.matrix[k][m] -= M.matrix[k][i] * M.matrix[i][m]

    if absmin(M.matrix[rows - 1][rows - 1]) <= tol:
        raise ZeroDivisionError(f'Matrix is numerically singular')

    if cache:
        orig.c_dt = (orig, M, p)
    return orig, M, p

def lSolve(M, b, p = None):
    if M.dimensions[0] != M.dimensions[1]:
        raise ValueError('Need a `n*n` matrix')
    rows = M.rows

    if len(b) != rows:
        raise ValueError('Value should be equal to N')
    b = copy(b)
    M = copy(M)

    if p:
        for i in range(1, len(p)):
            M.swaprow(i, p[i])

    for j in range(1, rows):
        for k in range(j):
            b[j] -= M.matrix[j][k] * b[j]
    return b

def uSolve(M, y):
    if M.dimensions[0] != M.dimensions[1]:
        raise ValueError('Need a `n*n` matrix')
    rows = M.rows

    if len(y) != rows:
        raise ValueError('Value should be equal to N')
    x = copy(y)
    M = copy(M)

    for i in range(rows - 1, -1, -1):
        for j in range(i + 1, rows):
            x[i] -= M.matrix[i][j] * x[j]
        x[i] /= M.matrix[i][i]

    return x

def unitVector(n, i):
    assert 0 < i <= n, 'this unit vector does not exist'

    return [mp.zero] * (i-1) + [mp.one] + [mp.zero] * (n-i)

max_stars_repo_name: Seniatical/cake

max_issues_repo_name: Seniatical/cake

max_forks_repo_name: polo-sec/Cake

reason: from mpmath


text:
from typing import Callable, List, Mapping, Optional, Sequence, Union

import numpy as np
from assertpy import assert_that
from bokeh import plotting
from bokeh.models import ColumnDataSource, HoverTool, LinearColorMapper
from bokeh.plotting import Figure
from sklearn import metrics as sklmetrics

from metriculous.evaluators._bokeh_utils import (
    BACKGROUND_COLOR,
    DARK_BLUE,
    FONT_SIZE,
    HISTOGRAM_ALPHA,
    SCATTER_CIRCLES_FILL_ALPHA,
    SCATTER_CIRCLES_LINE_ALPHA,
    TOOLBAR_LOCATION,
    TOOLS,
    add_title_rows,
    apply_default_style,
    color_palette,
    scatter_plot_circle_size,
)
from metriculous.evaluators._classification_utils import check_normalization

def _bokeh_output_histogram(
    y_true: np.ndarray,
    y_pred: np.ndarray,
    class_names: Sequence[str],
    title_rows: Sequence[str],
    sample_weights: Optional[np.ndarray] = None,
    x_label_rotation: Union[str, float] = "horizontal",
) -> Callable[[], Figure]:
    """ Histogram of ground truth and prediction.

    Args:
        y_true:
            1d integer array indicating the reference labels.
        y_pred:
            1d integer array indicating the predictions.
        class_names:
            Sequence of strings corresponding to the classes.
        title_rows:
            Sequence of strings to be used for the chart title.
        sample_weights:
            Sequence of floats to modify the influence of individual samples.
        x_label_rotation:
            Rotation of the class name labels.

    Returns:
        A callable that returns a fresh bokeh figure each time it is called

    """
    weights = np.ones_like(y_true) if sample_weights is None else sample_weights

    assert_that(np.shape(y_true)).is_equal_to(np.shape(y_pred))
    assert_that(np.shape(y_true)).is_equal_to(np.shape(weights))
    normalize = not np.allclose(weights, 1.0)

    def figure() -> Figure:
        n = len(class_names)

        bins = np.arange(0, n + 1, 1)

        p = plotting.figure(
            x_range=class_names,
            plot_height=350,
            plot_width=350,
            tools=TOOLS,
            toolbar_location=TOOLBAR_LOCATION,
        )

        # Class distribution in prediction
        p.vbar(
            x=class_names,
            top=np.histogram(y_pred, bins=bins, weights=weights, density=normalize)[0],
            width=0.85,
            color=DARK_BLUE,
            alpha=HISTOGRAM_ALPHA,
            legend_label="Prediction",
        )

        # Class distribution in ground truth
        p.vbar(
            x=class_names,
            top=np.histogram(y_true, bins=bins, weights=weights, density=normalize)[0],
            width=0.85,
            alpha=0.6,
            legend_label="Ground Truth",
            fill_color=None,
            line_color="black",
            line_width=2.5,
        )

        add_title_rows(p, title_rows)
        apply_default_style(p)

        p.yaxis.axis_label = (
            "Fraction of Instances" if normalize else "Number of Instances"
        )

        p.xaxis.major_label_orientation = x_label_rotation

        p.xgrid.grid_line_color = None

        # Prevent panning to empty regions
        p.x_range.bounds = (-0.5, 0.5 + len(class_names))
        return p

    return figure

def _bokeh_confusion_matrix(
    y_true: np.ndarray,
    y_pred: np.ndarray,
    class_names: Sequence[str],
    title_rows: Sequence[str],
    x_label_rotation: Union[str, float] = "horizontal",
    y_label_rotation: Union[str, float] = "vertical",
) -> Callable[[], Figure]:
    """ Confusion matrix heatmap.

    Args:
        y_true:
            1d integer array indicating the reference labels.
        y_pred:
            1d integer array indicating the predictions.
        class_names:
            Sequence of strings corresponding to the classes.
        title_rows:
            Sequence of strings to be used for the chart title.
        x_label_rotation:
            Rotation of the x-axis class name labels.
        y_label_rotation:
            Rotation of the y-axis class name labels.

    Returns:
        A callable that returns a fresh bokeh figure each time it is called

    """

    def figure() -> Figure:
        cm = sklmetrics.confusion_matrix(y_true, y_pred)
        cm_normalized = cm.astype("float") / cm.sum()
        cm_normalized_by_pred = cm.astype("float") / cm.sum(axis=0, keepdims=True)
        cm_normalized_by_true = cm.astype("float") / cm.sum(axis=1, keepdims=True)

        predicted = list()
        actual = list()
        count = list()
        normalized = list()
        normalized_by_pred = list()
        normalized_by_true = list()

        for i, i_class in enumerate(class_names):
            for j, j_class in enumerate(class_names):
                predicted.append(j_class)
                actual.append(i_class)
                count.append(cm[i, j])
                normalized.append(cm_normalized[i, j])
                normalized_by_pred.append(cm_normalized_by_pred[i, j])
                normalized_by_true.append(cm_normalized_by_true[i, j])

        # Compute an upper bound for the number of samples in any point in the matrix in any model
        # in the comparison. Exploit the fact that even though models in a comparison may have
        # different predictions, they share the same ground truth.
        upper_bound = max(cm.sum(axis=1))
        white_text_threshold = upper_bound / 2.0

        source = ColumnDataSource(
            data={
                "predicted": np.array(predicted),
                "actual": np.array(actual),
                "count": np.array(count),
                "normalized": np.array(normalized),
                "normalized_by_pred": np.array(normalized_by_pred),
                "normalized_by_true": np.array(normalized_by_true),
                "text_color": [
                    "white" if c > white_text_threshold else "black" for c in count
                ],
            }
        )

        p = plotting.figure(tools=TOOLS, x_range=class_names, y_range=class_names)

        mapper = LinearColorMapper(
            palette=color_palette(BACKGROUND_COLOR, DARK_BLUE, n=256),
            low=0.0,
            high=upper_bound,
        )

        rect = p.rect(
            source=source,
            x="actual",
            y="predicted",
            width=0.96,
            height=0.96,
            fill_color={"field": "count", "transform": mapper},
            line_color=None,
        )

        p.text(
            source=source,
            x="actual",
            y="predicted",
            text="count",
            text_color="text_color",
            text_font_size=FONT_SIZE if len(class_names) < 20 else "6pt",
            text_baseline="middle",
            text_align="center",
        )

        p.xaxis.axis_label = "Ground Truth"
        p.yaxis.axis_label = "Prediction"

        p.xaxis.major_label_orientation = x_label_rotation
        p.yaxis.major_label_orientation = y_label_rotation

        p.add_tools(
            HoverTool(
                renderers=[rect],
                tooltips=[
                    ("Predicted", "@predicted"),
                    ("Ground truth", "@actual"),
                    ("Count", "@count"),
                    ("Normalized", "@normalized"),
                    ("Normalized by prediction", "@normalized_by_pred"),
                    ("Normalized by ground truth", "@normalized_by_true"),
                ],
            )
        )

        add_title_rows(p, title_rows)
        apply_default_style(p)
        p.background_fill_color = "white"

        return p

    return figure

def _bokeh_confusion_scatter(
    y_true: np.ndarray,
    y_pred: np.ndarray,
    class_names: Sequence[str],
    title_rows: Sequence[str],
    x_label_rotation: Union[str, float] = "horizontal",
    y_label_rotation: Union[str, float] = "vertical",
) -> Callable[[], Figure]:
    """ Scatter plot that contains the same information as a confusion matrix.

    Args:
        y_true:
            1d integer array indicating the reference labels.
        y_pred:
            1d integer array indicating the predictions.
        class_names:
            Sequence of strings corresponding to the classes.
        title_rows:
            Sequence of strings to be used for the chart title.
        x_label_rotation:
            Rotation of the x-axis class name labels.
        y_label_rotation:
            Rotation of the y-axis class name labels.

    Returns:
        A callable that returns a fresh bokeh figure each time it is called

    """

    def figure() -> Figure:
        if len(y_true) != len(y_pred):
            raise ValueError("y_true and y_pred must have the same length!")

        p = plotting.figure(
            x_range=(-0.5, -0.5 + len(class_names)),
            y_range=(-0.5, -0.5 + len(class_names)),
            plot_height=350,
            plot_width=350,
            tools=TOOLS,
            toolbar_location=TOOLBAR_LOCATION,
            match_aspect=True,
        )

        def noise() -> np.ndarray:
            return (np.random.beta(1, 1, size=len(y_true)) - 0.5) * 0.6

        p.scatter(
            x=y_true + noise(),
            y=y_pred + noise(),
            size=scatter_plot_circle_size(
                num_points=len(y_true),
                biggest=4.0,
                smallest=1.0,
                use_smallest_when_num_points_at_least=5000,
            ),
            color=DARK_BLUE,
            fill_alpha=SCATTER_CIRCLES_FILL_ALPHA,
            line_alpha=SCATTER_CIRCLES_LINE_ALPHA,
        )

        add_title_rows(p, title_rows)
        apply_default_style(p)

        p.xaxis.axis_label = "Ground Truth"
        p.yaxis.axis_label = "Prediction"

        arange = np.arange(len(class_names))

        p.xaxis.ticker = arange
        p.yaxis.ticker = arange

        p.xaxis.major_label_overrides = {i: name for i, name in enumerate(class_names)}
        p.yaxis.major_label_overrides = {i: name for i, name in enumerate(class_names)}

        p.xaxis.major_label_orientation = x_label_rotation
        p.yaxis.major_label_orientation = y_label_rotation

        # grid between classes, not at classes
        p.xgrid.ticker = arange[0:-1] + 0.5
        p.ygrid.ticker = arange[0:-1] + 0.5

        p.xgrid.grid_line_width = 3
        p.ygrid.grid_line_width = 3

        # prevent panning to empty regions
        p.x_range.bounds = (-0.5, -0.5 + len(class_names))
        p.y_range.bounds = (-0.5, -0.5 + len(class_names))

        return p

    return figure

def _bokeh_roc_curve(
    y_true_binary: np.ndarray,
    y_pred_score: np.ndarray,
    title_rows: Sequence[str],
    sample_weights: Optional[np.ndarray],
) -> Callable[[], Figure]:
    """ Interactive receiver operator characteristic (ROC) curve.

    Args:
        y_true_binary:
            An array of zeros and ones.
        y_pred_score:
            A continuous value, such as a probability estimate for the positive class.
        title_rows:
            Sequence of strings to be used for the chart title.
        sample_weights:
            Sequence of floats to modify the influence of individual samples.

    Returns:
        A callable that returns a fresh bokeh figure each time it is called

    """

    def figure() -> Figure:
        assert y_true_binary.shape == y_pred_score.shape
        assert set(y_true_binary).issubset({0, 1}) or set(y_true_binary).issubset(
            {False, True}
        )
        assert np.ndim(y_true_binary) == 1

        fpr, tpr, thresholds = sklmetrics.roc_curve(
            y_true=y_true_binary, y_score=y_pred_score, sample_weight=sample_weights
        )

        source = ColumnDataSource(
            data={
                "FPR": fpr,
                "TPR": tpr,
                "threshold": thresholds,
                "specificity": 1.0 - fpr,
            }
        )

        p = plotting.figure(
            plot_height=400,
            plot_width=350,
            tools=TOOLS,
            toolbar_location=TOOLBAR_LOCATION,
            # toolbar_location=None,  # hides entire toolbar
            match_aspect=True,
        )

        p.xaxis.axis_label = "FPR"
        p.yaxis.axis_label = "TPR"

        add_title_rows(p, title_rows)
        apply_default_style(p)

        curve = p.line(x="FPR", y="TPR", line_width=2, color=DARK_BLUE, source=source)
        p.line(
            x=[0.0, 1.0],
            y=[0.0, 1.0],
            line_alpha=0.75,
            color="grey",
            line_dash="dotted",
        )

        p.add_tools(
            HoverTool(
                # make sure there is no tool tip for the diagonal baseline
                renderers=[curve],
                tooltips=[
                    ("TPR", "@TPR"),
                    ("FPR", "@FPR"),
                    ("Sensitivity", "@TPR"),
                    ("Specificity", "@specificity"),
                    ("Threshold", "@threshold"),
                ],
                # display a tooltip whenever the cursor is vertically in line with a glyph
                mode="vline",
            )
        )

        return p

    return figure

def _bokeh_precision_recall_curve(
    y_true_binary: np.ndarray,
    y_pred_score: np.ndarray,
    title_rows: Sequence[str],
    sample_weights: Optional[np.ndarray],
) -> Callable[[], Figure]:
    """ Interactive precision recall curve.

    Args:
        y_true_binary:
            An array of zeros and ones.
        y_pred_score:
            A continuous value, such as a probability estimate for the positive class.
        title_rows:
            Sequence of strings to be used for the chart title.
        sample_weights:
            Sequence of floats to modify the influence of individual samples.

    Returns:
        A callable that returns a fresh bokeh figure each time it is called

    """

    def figure() -> Figure:
        assert y_true_binary.shape == y_pred_score.shape
        assert set(y_true_binary).issubset({0, 1}) or set(y_true_binary).issubset(
            {False, True}
        )
        assert np.ndim(y_true_binary) == 1

        # Note: len(thresholds) == len(precision) - 1
        # The last precision recall pair does not have a corresponding threshold.
        precision, recall, thresholds = sklmetrics.precision_recall_curve(
            y_true=y_true_binary, probas_pred=y_pred_score, sample_weight=sample_weights
        )
        precision = precision[:-1]
        recall = recall[:-1]

        p = plotting.figure(
            plot_height=400,
            plot_width=350,
            x_range=(-0.05, 1.05),
            y_range=(-0.05, 1.05),
            tools=TOOLS,
            toolbar_location=TOOLBAR_LOCATION,
        )

        source = ColumnDataSource(
            data={"precision": precision, "recall": recall, "threshold": thresholds}
        )

        # Reminder: tpr == recall == sensitivity
        p.line(x="recall", y="precision", line_width=2, source=source)

        add_title_rows(p, title_rows)
        apply_default_style(p)

        p.xaxis.axis_label = "Recall"
        p.yaxis.axis_label = "Precision"

        p.add_tools(
            HoverTool(
                tooltips=[
                    ("Precision", "@precision"),
                    ("Recall", "@recall"),
                    ("Threshold", "@threshold"),
                ],
                # display a tooltip whenever the cursor is vertically in line with a glyph
                mode="vline",
            )
        )

        return p

    return figure

def _bokeh_automation_rate_analysis(
    y_target_one_hot: np.ndarray,
    y_pred_proba: np.ndarray,
    title_rows: Sequence[str],
    sample_weights: Optional[np.ndarray],
) -> Callable[[], Figure]:
    """
    Plots various quantities over automation rate, where a single probability threshold
    is used for all classes to decide if we are confident enough to automate the
    classification.

    Args:
        y_target_one_hot:
            Array with one-hot encoded ground truth, shape(n_samples, n_classes).
        y_pred_proba:
            Array with estimated probability distributions, shape(n_samples, n_classes).
        title_rows:
            Sequence of strings to be used for the chart title.
        sample_weights:
            Sequence of floats to modify the influence of individual samples.

    Returns:
        A callable that returns a fresh bokeh figure each time it is called

    """

    def figure() -> Figure:
        # ----- Check input -----
        assert y_target_one_hot.ndim == 2
        assert y_pred_proba.ndim == 2
        assert (
            y_target_one_hot.shape == y_pred_proba.shape
        ), f"{y_target_one_hot.shape} != {y_pred_proba.shape}"
        check_normalization(y_target_one_hot, axis=1)
        check_normalization(y_pred_proba, axis=1)
        assert set(y_target_one_hot.ravel()) == {0, 1}, set(y_target_one_hot.ravel())

        if sample_weights is not None:
            assert_that(sample_weights.shape).is_equal_to((len(y_target_one_hot),))

        # ----- Compute chart data -----
        y_target = y_target_one_hot.argmax(axis=1)
        argmaxes = y_pred_proba.argmax(axis=1)
        maxes = y_pred_proba.max(axis=1)
        assert isinstance(maxes, np.ndarray)  # making IntelliJ's type checker happy

        chart_data: Mapping[str, List[float]] = {
            "automation_rate": [],
            "threshold": [],
            "accuracy": [],
        }

        for threshold in np.sort(maxes):
            automated = maxes >= threshold
            chart_data["automation_rate"].append(
                np.average(automated, weights=sample_weights)
            )
            chart_data["threshold"].append(threshold)
            chart_data["accuracy"].append(
                _faster_accuracy(
                    y_true=y_target[automated],
                    y_pred=argmaxes[automated],
                    sample_weights=(
                        None if sample_weights is None else sample_weights[automated]
                    ),
                )
            )

        # ----- Bokeh plot -----
        p = plotting.figure(
            plot_height=400,
            plot_width=350,
            x_range=(-0.05, 1.05),
            y_range=(-0.05, 1.05),
            tools=TOOLS,
            toolbar_location=TOOLBAR_LOCATION,
        )

        source = ColumnDataSource(
            data={key: np.array(lst) for key, lst in chart_data.items()}
        )

        accuracy_line = p.line(
            x="automation_rate",
            y="accuracy",
            line_width=2,
            color=DARK_BLUE,
            source=source,
            legend_label="Accuracy",
        )

        p.line(
            x="automation_rate",
            y="threshold",
            line_width=2,
            color="grey",
            source=source,
            legend_label="Threshold",
        )

        # Make sure something is visible if lines consist of just a single point
        p.scatter(
            x=source.data["automation_rate"][[0, -1]],
            y=source.data["accuracy"][[0, -1]],
        )
        p.scatter(
            x=source.data["automation_rate"][[0, -1]],
            y=source.data["threshold"][[0, -1]],
            color="grey",
        )

        add_title_rows(p, title_rows)
        apply_default_style(p)

        p.xaxis.axis_label = "Automation Rate"
        p.legend.location = "bottom_left"

        p.add_tools(
            HoverTool(
                renderers=[accuracy_line],
                tooltips=[
                    ("Accuracy", "@accuracy"),
                    ("Threshold", "@threshold"),
                    ("Automation Rate", "@automation_rate"),
                ],
                # display a tooltip whenever the cursor is vertically in line with a glyph
                mode="vline",
            )
        )

        return p

    return figure

def _faster_accuracy(
    y_true: np.ndarray, y_pred: np.ndarray, sample_weights: Optional[np.ndarray]
) -> float:
    assert y_true.ndim == 1
    assert y_pred.ndim == 1
    return np.average(y_true == y_pred, weights=sample_weights)

max_stars_repo_name: magnusja/metriculous

max_issues_repo_name: magnusja/metriculous

max_forks_repo_name: magnusja/metriculous


text:
import cv2

import numpy as np

from scipy.stats import zscore

class TrafficLightColorDetector:
    def __init__(self, config):
        self.config = config

    def get_car_light_colors(self, img, debug=False):
        retval = []
        for i, bbox in enumerate(self.config.traffic_light_bboxes):
            light_roi = img[bbox[1] : bbox[3], bbox[0] : bbox[2]]
            light_roi_height, light_roi_width, _ = light_roi.shape
            light_roi_hls = cv2.cvtColor(light_roi, cv2.COLOR_BGR2HLS)
            _, light_roi_thresholded = cv2.threshold(light_roi_hls[:,:,1],
                                                    self.config.traffic_light_threshold,
                                                    255,
                                                    cv2.THRESH_TOZERO)

            regional_brightness = []

            if light_roi_height > light_roi_width:
                regional_brightness.append(np.mean(light_roi_thresholded[0 : light_roi_height // 3, :]))
                regional_brightness.append(np.mean(light_roi_thresholded[light_roi_height // 3 : light_roi_height // 3 * 2, :]))
                regional_brightness.append(np.mean(light_roi_thresholded[light_roi_height // 3 * 2:, :]))
            else:
                regional_brightness.append(np.mean(light_roi_thresholded[:, 0 : light_roi_height // 3]))
                regional_brightness.append(np.mean(light_roi_thresholded[:, light_roi_height // 3 : light_roi_height // 3 * 2]))
                regional_brightness.append(np.mean(light_roi_thresholded[:, light_roi_height // 3 * 2:]))

            if debug:
                print 'Regional brightness for bbox %2d: ' % i, regional_brightness

            # No significantly brighter light found
            if np.sum(regional_brightness) == 0.0:
                retval.append(None)
                continue

            brightest = np.argmax(regional_brightness)

            if brightest == 0:
                retval.append('red')
            elif brightest == 1:
                retval.append('yellow')
            elif brightest == 2:
                retval.append('green')
            else:
                retval.append(None)
        return retval

    def get_pedestrian_light_colors(self, img, debug=False):
        retval = []
        for i, bbox in enumerate(self.config.pedestrian_light_bboxes):
            light_roi = img[bbox[1] : bbox[3], bbox[0] : bbox[2]]
            light_roi_hls = cv2.cvtColor(light_roi, cv2.COLOR_BGR2HLS)

            _, light_roi_thresholded = cv2.threshold(light_roi_hls[:,:,1], self.config.pedestrian_light_thresholds[0], 0, cv2.THRESH_TOZERO)
            _, light_roi_thresholded = cv2.threshold(light_roi_thresholded, self.config.pedestrian_light_thresholds[1], 0, cv2.THRESH_TOZERO_INV)

            light_roi_hls[:,:,1] = light_roi_thresholded
            light_roi_bgr = cv2.cvtColor(light_roi_hls, cv2.COLOR_HLS2BGR)

            light_roi_bgr_mean = cv2.mean(light_roi_bgr)[:3]
            light_roi_bgr_zscore = zscore(light_roi_bgr_mean)

            if debug:
                print 'z-scores of BGR channels for bbox %2d: ' % i, light_roi_bgr_zscore

            if light_roi_bgr_zscore[2] > self.config.pedestrian_light_zscore:
                retval.append('red')
            else:
                retval.append('white')
        return retval


text:
from cv2 import cv2
import pandas as pd
import numpy as np

tiles = cv2.imread('tiles.png')
#customer_pic = tiles[5*32:6*32,15*32:16*32]

MARKET = """
##################
##..............##
#w..Be..eU..us..s#
#w..Be..eU..us..s#
#w..be..eU..uz..z#
#W..bE..fU..uz..S#
#W..bE..fU..uS..S#
##...............#
##..C#..c#..q#...#
##..##..##..##...#
##...............#
##############GG##
""".strip()

matrix = pd.read_csv('transition_matrix.csv', sep=';',index_col=['location'])

TILE_SIZE = 32
OFS = 50

def pick_customer():
    pic_dict = {
    'green_fish' : tiles[5*32:6*32,15*32:16*32],
    'darkblue_fish' : tiles[4*32:5*32,15*32:16*32],
    'blue_fish' : tiles[6*32:7*32,15*32:16*32],
    'butterfly' : tiles[5*32:6*32,14*32:15*32],
    'insect' : tiles[3*32:4*32,14*32:15*32],
    'ghost' : tiles[7*32:8*32,0*32:1*32],
    'shell' : tiles[7*32:8*32,14*32:15*32],
    'pacman' : tiles[3*32:4*32,0*32:1*32]
    }
    pic_list = ['green_fish', 'darkblue_fish', 'blue_fish', 'butterfly',
        'insect', 'ghost', 'shell', 'pacman']
    return pic_dict[np.random.choice(pic_list)]

max_stars_repo_name: saefsten/supermarket

max_issues_repo_name: saefsten/supermarket

max_forks_repo_name: saefsten/supermarket