Best Python code snippet using pandera_python
evaluation.py
Source:evaluation.py  
1#!/usr/bin/env python3.52import numpy as np3from PIL import Image, ImageDraw4def two_d_iou(box, boxes):5    """Compute 2D IOU between a 2D bounding box 'box' and a list6    :param box: a numpy array in the form of [x1, y1, x2, y2] where (x1,y1) are7    image coordinates of the top-left corner of the bounding box, and (x2,y2)8    are the image coordinates of the bottom-right corner of the bounding box.9    :param boxes: a numpy array formed as a list of boxes in the form10    [[x1, y1, x2, y2], [x1, y1, x2, y2]].11    :return iou: a numpy array containing 2D IOUs between box and every element12    in numpy array boxes.13    """14    iou = np.zeros(len(boxes), np.float64)15    x1_int = np.maximum(box[0], boxes[:, 0])16    y1_int = np.maximum(box[1], boxes[:, 1])17    x2_int = np.minimum(box[2], boxes[:, 2])18    y2_int = np.minimum(box[3], boxes[:, 3])19    w_int = x2_int - x1_int20    h_int = y2_int - y1_int21    non_empty = np.logical_and(w_int > 0, h_int > 0)22    if non_empty.any():23        intersection_area = np.multiply(w_int[non_empty], h_int[non_empty])24        box_area = (box[2] - box[0]) * (box[3] - box[1])25        boxes_area = np.multiply(26            boxes[non_empty, 2] - boxes[non_empty, 0],27            boxes[non_empty, 3] - boxes[non_empty, 1])28        union_area = box_area + boxes_area - intersection_area29        iou[non_empty] = intersection_area / union_area30    return iou.round(3)31def two_half_d_iou(box, boxes):32    """Computes approximate 2.5D IOU on BEV between a 3D bounding box 'box' and a list33        of 3D bounding boxes 'boxes'. All boxes are assumed to be aligned with34        respect to gravity. Boxes are allowed to rotate only around their z-axis.35        :param box: a numpy array of the form: [ry, l, w, h, tx, ty, tz]36        :param boxes: a numpy array of the form:37            [[ry, l, h, w, tx, ty, tz], [ry, l, w, h, tx, ty, tz]]38        :return iou: a numpy array containing 3D IOUs between box and every element39            in numpy array boxes.40    """41    if len(boxes.shape) == 1:42        boxes = np.array([boxes])43    box_diag = np.sqrt(np.square(box[1]) +44                       np.square(box[2])) / 245    boxes_diag = np.sqrt(np.square(boxes[:, 1]) +46                         np.square(boxes[:, 2])) / 247    dist = np.sqrt(np.square(boxes[:, 4] - box[4]) +48                   np.square(boxes[:, 6] - box[6]))49    non_empty = box_diag + boxes_diag >= dist50    iou = np.zeros(len(boxes), np.float64)51    if non_empty.any():52        # height_int, _ = height_metrics(box, boxes[non_empty])53        intersection = get_rectangular_metrics(box, boxes[non_empty])54        vol_box = np.prod(box[1:3])55        vol_boxes = np.prod(boxes[non_empty, 1:3], axis=1)56        union = vol_box + vol_boxes - intersection57        iou[non_empty] = intersection / union58    if iou.shape[0] == 1:59        iou = iou[0]60    return iou61def three_d_iou(box, boxes):62    """Computes approximate 3D IOU between a 3D bounding box 'box' and a list63    of 3D bounding boxes 'boxes'. All boxes are assumed to be aligned with64    respect to gravity. Boxes are allowed to rotate only around their z-axis.65    :param box: a numpy array of the form: [ry, l, w, h, tx, ty, tz]66    :param boxes: a numpy array of the form:67        [[ry, l, h, w, tx, ty, tz], [ry, l, w, h, tx, ty, tz]]68    :return iou: a numpy array containing 3D IOUs between box and every element69        in numpy array boxes.70    """71    # First, rule out boxes that do not intersect by checking if the spheres72    # which inscribes them intersect.73    if len(boxes.shape) == 1:74        boxes = np.array([boxes])75    box_diag = np.sqrt(np.square(box[1]) +76                       np.square(box[2]) +77                       np.square(box[3])) / 278    boxes_diag = np.sqrt(np.square(boxes[:, 1]) +79                         np.square(boxes[:, 2]) +80                         np.square(boxes[:, 3])) / 281    dist = np.sqrt(np.square(boxes[:, 4] - box[4]) +82                   np.square(boxes[:, 5] - box[5]) +83                   np.square(boxes[:, 6] - box[6]))84    non_empty = box_diag + boxes_diag >= dist85    iou = np.zeros(len(boxes), np.float64)86    if non_empty.any():87        height_int, _ = height_metrics(box, boxes[non_empty])88        rect_int = get_rectangular_metrics(box, boxes[non_empty])89        intersection = np.multiply(height_int, rect_int)90        vol_box = np.prod(box[1:4])91        vol_boxes = np.prod(boxes[non_empty, 1:4], axis=1)92        union = vol_box + vol_boxes - intersection93        iou[non_empty] = intersection / union94    if iou.shape[0] == 1:95        iou = iou[0]96    return iou97def height_metrics(box, boxes):98    """Compute 3D height intersection and union between a box and a list of99    boxes100    :param box: a numpy array of the form: [ry, l, h, w, tx, ty, tz]101    :param boxes: a numpy array of the form: [[ry, l, h, w, tx, ty, tz],.....102                                        [ry, l, h, w, tx, ty, tz]]103    :return height_intersection: a numpy array containing the intersection along104    the gravity axis between the two bbs105    :return height_union: a numpy array containing the union along the gravity106    axis between the two bbs107    """108    boxes_heights = boxes[:, 2]109    boxes_centroid_heights = boxes[:, 5]110    min_y_boxes = boxes_centroid_heights - boxes_heights111    max_y_box = box[5]112    min_y_box = box[5] - box[2]113    max_of_mins = np.maximum(min_y_box, min_y_boxes)114    min_of_maxs = np.minimum(max_y_box, boxes_centroid_heights)115    offsets = min_of_maxs - max_of_mins116    height_intersection = np.maximum(0, offsets)117    height_union = np.maximum(min_y_box, boxes_centroid_heights) \118        - np.minimum(min_y_box, min_y_boxes) - \119        np.maximum(0, -offsets)120    return height_intersection, height_union121def get_rotated_3d_bb(boxes):122    """Compute rotated 3D bounding box coordinates.123    :param boxes: a numpy array of the form: [[ry, l, h, w, tx, ty, tz],...124                                         [ry, l, h, w, tx, ty, tz]]125    :return x: x coordinates of the four corners required to describe a 3D126    bounding box arranged as [[x1, x2, x3, x4],127                     [x1, x2, x3, x4],128                     ... ]129    :return z: z coordinates of the four corners required to describe a 3D130    bounding box arranged as [[z1, z2, z3, z4],131                     [z1, z2, z3, z4],132                     ... ].133    """134    if len(boxes.shape) == 1:135        boxes = np.array([boxes])136    x = np.array([[]])137    z = np.array([[]])138    for i in boxes:139        rot_mat = np.array([[np.cos(i[0]), np.sin(i[0])],140                            [-np.sin(i[0]), np.cos(i[0])]])141        x_corners = np.multiply(i[1] / 2, np.array([1, 1, -1, -1]))142        z_corners = np.multiply(i[3] / 2, np.array([1, -1, -1, 1]))143        temp_coor = np.dot(rot_mat, np.array([x_corners, z_corners]))144        # At the very first iteration, initialize x145        if x.shape[1] < 1:146            x = temp_coor[:1] + i[4]147            z = temp_coor[1:2] + i[6]148        # After that, append to the existing x149        else:150            x = np.append(x, temp_coor[:1] + i[4], axis=0)151            z = np.append(z, temp_coor[1:2] + i[6], axis=0)152    if x.shape[0] == 1:153        x = x[0]154        z = z[0]155    return x, z156def get_rectangular_metrics(box, boxes):157    """ Computes the intersection of the bases of oriented 3D bounding "box"158    and a set boxes of oriented 3D bounding boxes "boxes".159    :param box: a numpy array of the form: [ry, l, h, w, tx, ty, tz]160    :param boxes: a numpy array of the form: [[ry, l, h, w, tx, ty, tz],.....161                                        [ry, l, h, w, tx, ty, tz]]162    :return intersection: a numpy array containing intersection between the163    base of box and all other boxes.164    """165    if len(boxes.shape) == 1:166        boxes = np.array([boxes])167    mask_res = 0.01168    x_box, z_box = get_rotated_3d_bb(box)169    max_x_box = np.max(x_box)170    min_x_box = np.min(x_box)171    max_z_box = np.max(z_box)172    min_z_box = np.min(z_box)173    x_boxes, z_boxes = get_rotated_3d_bb(boxes)174    intersection = np.zeros(np.size(boxes, 0))175    if np.size(np.shape(x_boxes)) == 1:176        x_boxes = np.array([x_boxes])177        z_boxes = np.array([z_boxes])178    for i in range(np.size(boxes, 0)):179        x_i = x_boxes[i, :]180        z_i = z_boxes[i, :]181        test = max_x_box < np.min(x_i) or np.max(x_i) < min_x_box \182            or max_z_box < np.min(z_i) or np.max(z_i) < min_z_box183        if test:184            continue185        x_all = np.append(x_box, x_i)186        z_all = np.append(z_box, z_i)187        maxs = np.array([np.max(x_all), np.max(z_all)])188        mins = np.array([np.min(x_all), np.min(z_all)])189        mask_dims = np.int32(np.ceil((maxs - mins) / mask_res))190        mask_box_x = (x_box - mins[0]) / mask_res191        mask_box_z = (z_box - mins[1]) / mask_res192        mask_i_x = (x_i - mins[0]) / mask_res193        mask_i_z = (z_i - mins[1]) / mask_res194        # Drawing a binary image of the base of the two bounding boxes.195        # Then compute the element wise and of the two images to get the intersection.196        # Minor precision loss due to discretization.197        img = Image.new('L', (mask_dims[0], mask_dims[1]), 0)198        draw = ImageDraw.Draw(img, 'L')199        rect_coordinates = np.reshape(np.transpose(np.array([mask_box_x,200                                                             mask_box_z])), 8)201        rect_coordinates = np.append(rect_coordinates, rect_coordinates[0:2])202        draw.polygon(rect_coordinates.ravel().tolist(), outline=255, fill=255)203        del draw204        mask_box = np.asarray(img)205        img2 = Image.new('L', (mask_dims[0], mask_dims[1]), 0)206        draw = ImageDraw.Draw(img2, 'L')207        i_coordinates = np.reshape(np.transpose(np.array([mask_i_x,208                                                          mask_i_z])), 8)209        i_coordinates = np.append(i_coordinates, i_coordinates[0:2])210        draw.polygon(i_coordinates.ravel().tolist(), outline=255, fill=255)211        del draw212        mask_i = np.asarray(img2)213        mask_intersection = np.logical_and(mask_box, mask_i)214        intersection[i] = min(100, np.size(np.flatnonzero(215            mask_intersection)) * np.square(mask_res))216    if intersection.shape[0] == 1:217        intersection = intersection[0]...Practica1MartinezBotet.py
Source:Practica1MartinezBotet.py  
1from multiprocessing import Process2from multiprocessing import BoundedSemaphore, Semaphore, Lock3from multiprocessing import current_process4from multiprocessing import Value, Array, Queue5from time import sleep6from random import random, randint7"N es cuanto produce cada uno"8N = 209K = 310NPROD = 311NCONS = 112def delay(factor = 3):13    sleep(random()/factor)14"index es a quien le toca"15def add_data(storage, data, pid,  mutex):16    mutex.acquire()17    try:18        storage[pid] = randint(0,5) + data.value19        delay(6)20    finally:21        mutex.release()22"Cogemos el dato y lo cambiamos por 0, la idea del bucle es que te devuleve el min y la pos"23def get_data(storage,mutex,data):24    mutex.acquire()25    print(storage[0],storage[1],storage[2])26    i=127    m=storage[0]28    pos=029    while m==-1:30        m=storage[i]31        pos=i32        i=i+133    try:34        for i,c in enumerate(storage):35            if c<m and c!=-1:36                m=c37                pos=i38        data.value=m39        storage[pos]=040    finally:41        mutex.release()42    return data.value,pos43""44def producer(storage, empty, non_empty, mutex,data):45    for v in range(N):46        print (f"producer {current_process().name} produciendo")47        pid=int(current_process().name.split('_')[1])48        delay(6)49        empty[pid].acquire()50        try:51            add_data(storage,data, pid,52                     mutex)53        finally:    54            non_empty[pid].release()55        print (f"producer {current_process().name} almacenado {v}")56    empty[pid].acquire()57    storage[pid]=-158    non_empty[pid].release()59    60def hay_productor(storage):61    res=False62    i=063    while i<len(storage) and not(res):64        if storage[i]!=-1:65            res=True66        i=i+167    return res68def consumer(storage, data, empty, non_empty, mutex):69    l=[]70    for i in non_empty:71        i.acquire()72    while hay_productor(storage):73        valor,pos=get_data(storage,mutex,data)74        l.append(valor)75        empty[pos].release()76        non_empty[pos].acquire()77    print("Ya no hay productores")78    print(l)79def main():80    storage = Array('i', K)81    data=Value('i',0)82    for i in range(K):83        storage[i] = 084    #print ("almacen inicial", storage[:], "indice", index.value)85    non_empty = [Semaphore(0) for _ in range(NPROD)]86    empty = [Lock() for _ in range(NPROD)]87    mutex = Lock()88    prodlst = [ Process(target=producer,89                        name=f'prod_{i}',90                        args=(storage, empty, non_empty, mutex,data))91                for i in range(NPROD) ]92    conslst = [ Process(target=consumer,93                      name=f"cons_{i}",94                      args=(storage, data,empty, non_empty, mutex))]95    for p in prodlst + conslst:96        p.start()97    for p in prodlst + conslst:98        p.join()99        100if __name__ == '__main__':...analyze.py
Source:analyze.py  
1#!/usr/bin/env python2__author__ = 'greg'3import os4import cPickle as pickle5# for Greg - which computer am I on?6if os.path.exists("/home/ggdhines"):7    base_directory = "/home/ggdhines"8    code_directory = base_directory + "/github"9elif os.path.exists("/Users/greg"):10    base_directory = "/Users/greg"11    code_directory = base_directory + "/Code"12else:13    base_directory = "/home/greg"14    code_directory = base_directory + "/github"15results = pickle.load(open(base_directory+"/Databases/serengeti/results.pickle","rb"))16skipped = 017non_empty = 018for zooniverse_id in results:19    status = results[zooniverse_id]["status"]20    if status == "skipped":21        skipped += 122        continue23    classification = results[zooniverse_id]["name"]24    if "deer" in classification:25        non_empty += 126    elif "hyena" in classification:27        non_empty += 128    elif ("wildebeest" in classification) or ("wildebeast" in classification):29        non_empty += 130    elif "zebra" in classification:31        non_empty += 132    elif "impala" in classification:33        non_empty += 134    elif "gazelle" in classification:35        non_empty += 136    elif "buffalo" in classification:37        non_empty += 138    elif "elephant" in classification:39        non_empty += 140    elif "animal" in classification:41        non_empty += 142    elif "lion" in classification:43        non_empty += 144    elif ("aardvark" in classification) or ("aadvark" in classification):45        non_empty += 146    elif "horse" in classification:47        non_empty += 148    elif ("birds" in classification) or ("bird" in classification):49        non_empty += 150    elif "oryx" in classification:51        non_empty += 152    elif "antelope" in classification:53        non_empty += 154    elif "emu" in classification:55        non_empty += 156    elif "fowl" in classification:57        non_empty += 158    elif ("suv" in classification) or ("rover" in classification) or ("vehicle" in classification):59        non_empty += 160    elif "peacock" in classification:61        non_empty += 162    elif "dikdik" in classification:63        non_empty += 164    elif "giraffe" in classification:65        non_empty += 166    elif "bull" in classification:67        non_empty += 168    elif ("jeans" in classification) or ("board" in classification) or ("pants" in classification) or ("jacket" in classification):69        non_empty += 170    elif "cow" in classification:71        non_empty += 172    elif "warthog" in classification:73        non_empty += 174    elif "baboon" in classification:75        non_empty += 176    elif "mammal" in classification:77        non_empty += 178    elif "boar" in classification:79        non_empty += 180    else:81        print classification...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
