text:
import os
import PIL
import numpy as np
import scipy.io as sio
from matplotlib import cm
from matplotlib.colors import ListedColormap

import torch
from torchvision import transforms

def mkdirs(paths):
    if isinstance(paths, list) and not isinstance(paths, str):
        for path in paths:
            mkdir(path)
    else:
        mkdir(paths)

def mkdir(path):
    if not os.path.exists(path):
        os.makedirs(path)

def dict_to_cuda(dict):
    for k, v in dict.items():
        dict[k] = v.cuda()
    return dict

def dict_to_cpu(dict):
    for k, v in dict.items():
        dict[k] = v.cpu()
    return dict

def dict_detach(dict):
    for k, v in dict.items():
        dict[k] = v.detach()
    return dict

def save_image(image_numpy, image_path, create_dir=False):
    if create_dir:
        os.makedirs(os.path.dirname(image_path), exist_ok=True)
    if len(image_numpy.shape) == 2:
        image_numpy = np.expand_dims(image_numpy, axis=2)
    if image_numpy.shape[2] == 1:
        image_numpy = np.repeat(image_numpy, 3, 2)
    image_pil = PIL.Image.fromarray(image_numpy)

    # save to png
    image_pil.save(image_path.replace('.jpg', '.png'))

def create_colormap(opt):
    print(f"Loading colormat: {opt.colormat}")
    colormat = sio.loadmat('data/colormaps/%s.mat'%opt.colormat)
    return colormat['colors']

def color_transfer(im, colormap):
    im = im.cpu().numpy()
    if np.any(im == -1):
        bg_mask = im == -1
        im += 2
        new_colors = [[125., 125., 125.], [255., 255., 255.]]
        colormap = np.vstack([new_colors, colormap])
        cell = im.shape[2] // 60
        board = np.zeros(im.shape)
        for i in range(cell):
            board[:, :, i::2 * cell] += 1
            board[:, :, :, i::2 * cell] += 1
        board = board % 2
        im[bg_mask] = board[bg_mask]
    im_new = torch.Tensor(im.shape[0], 3, im.shape[2], im.shape[3])
    newcmp = ListedColormap(colormap / 255.0)
    for i in range(im.shape[0]):
        img = (im[i, 0, :, :]).astype('uint8')
        rgba_img = newcmp(img)
        rgb_img = PIL.Image.fromarray((255 * np.delete(rgba_img, 3, 2)).astype('uint8'))
        tt = transforms.Compose([transforms.ToTensor(),
                                 transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
        rgb_img = tt(rgb_img)
        im_new[i, :, :, :] = rgb_img
    im_new = im_new
    return im_new

def color_spread(spread, max_spread):
    im_spread = (spread - 1).numpy()
    im_spread[im_spread > 0] = im_spread[im_spread > 0] / max_spread
    im_spread = im_spread / 2 + 0.5
    top = cm.get_cmap('Greys_r', 128)
    bottom = cm.get_cmap('Oranges', 128)
    newcolors = np.vstack((top(np.linspace(0, 1, 128)),
                           bottom(np.linspace(0, 1, 128))))
    newcmp = ListedColormap(newcolors, name='GreyOrange')
    im_spread_rgba = newcmp(im_spread)
    im_spread_rgb = torch.tensor(im_spread_rgba[:, :, :, :3]).permute(0, 3, 1, 2)
    return im_spread_rgb

# def get_confusion_matrix(sem_index_real, sem_index_pred, num_semantics):
#     index = (sem_index_real * num_semantics + sem_index_pred).long().flatten()
#     count = torch.bincount(index)
#     confusion_matrix = torch.zeros((num_semantics, num_semantics)).to(sem_index_real.get_device())
#     for i_label in range(num_semantics):
#         for j_pred_label in range(num_semantics):
#             cur_index = i_label * num_semantics + j_pred_label
#             if cur_index < len(count):
#                 confusion_matrix[i_label, j_pred_label] = count[cur_index]
#     return confusion_matrix

# def get_confusion_matrix(sem_index_real, sem_index_pred, num_semantics):
#     confusion_matrix = None
#     for real_id, pred_id in zip(sem_index_real, sem_index_pred):

def get_confusion_matrix(sem_index_real, sem_index_pred, num_semantics):
    # bincount is much faster on cpu than gpu
    device = sem_index_real.device
    sem_index_real = sem_index_real.cpu()
    sem_index_pred = sem_index_pred.cpu()
    mask = (sem_index_real >= 0) & (sem_index_real < num_semantics)
    index = (sem_index_real[mask] * num_semantics + sem_index_pred[mask]).long().flatten()
    confusion_matrix = torch.bincount(index, minlength=num_semantics**2).view(num_semantics, num_semantics)
    return confusion_matrix.to(device)

def to_cuda(tensor_dic, key):
    if key in tensor_dic:
        if 0 in tensor_dic[key].size():
            tensor_dic[key] = torch.Tensor([])
        return tensor_dic[key].cuda()
    return torch.Tensor([])

def get_seg_size(num_semantics, num_things, panoptic, instance_type):
    seg_size = num_semantics
    if panoptic:
        if "density" in instance_type:
            seg_size += num_things
        if "center_offset" in instance_type:
            seg_size += 3
        if "edge" in instance_type:
            seg_size += 1
    return seg_size

max_stars_repo_name: valeoai/SemanticPalette

max_issues_repo_name: valeoai/SemanticPalette

max_forks_repo_name: valeoai/SemanticPalette


text:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Author    : allqoow
# Contact   : allqoow@gmail.com
# Started on: 20161107(yyyymmdd)
# Project   : exerciseML(Exercise for Machine Learning)

import matplotlib.pyplot
import math
import numpy.random

setX = [-1,0.3,2]
setT = [-0.1,0.5,0.5]

def getGradient(setX,setT,vecW):

	ret

ext: py

max_stars_repo_name: allqoow/exerciseML

max_issues_repo_name: allqoow/exerciseML

max_forks_repo_name: allqoow/exerciseML

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 15 12:22:31 2018

@author: iw1g17
"""
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A script that defines a function that can be used as a template for the
exercise in the script for Computer Lab 7.

Created on Wed Mar 14 18:12:44 2018

"""
import networkx

def print_towns():

    """
    A function to list a series of towns and their distance from Southampton.
    This function can be used as a template for the exercise in the Lab script 7.

    This function has no input parameters and does not return any value.

    Created on Wed Mar 14 18:12:44 2018

    """
    H = networkx.Graph()
    H.add_node(1, city = "Southampton")
    node_number = 2

    # We create a dictionary of towns and their distances from Southampton
    distances = {
            "London": 79,
            "Reading": 47,
            "Brighton": 69,
            "Exeter": 110,
            "Salisbury": 23,
            "Bath": 64
            }
    for city, distance in distances.items():
        H.add_node(node_number, city=city)
        H.add_edge(1,node_number, distance = distance)
        node_number += 1

    from matplotlib import pyplot

    pyplot.figure()
    positions = networkx.random_layout(H)
    networkx.draw(H, pos=positions, with_labels=True)
    node_labels = networkx.get_node_attributes(H, "city")
    edge_labels = networkx.get_edge_attributes(H, "distance")
#    networkx.draw_networkx_labels(H, positions, labels=node_labels)
    networkx.draw_networkx_edge_labels(H, positions, edge_labels=edge_labels)
    pyplot.show()
    print(positions)

print_towns()

reason: import networkx


text:
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 27 14:12:12 2018
Switchback square
@author: oddvi
"""
import matplotlib.pyplot as plt
import shapely.geometry
import shapely.affinity
import shapely.ops
import patternGenerators as gen

def make_square_switchback_gen_reg(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut='default'):
    """
    """
    import numpy as np
    a = cut_width; b = flexure_width; c = junction_length; d = edge_space
    if side_cut == 'default': # x displacement along diagonal cut
        ax = cut_width/(2**0.5)/2
    else:
        ax = side_cut
    dx = a+b # displacement y direction
    dy = dx # displacement y direction
    h0 = a+b/2+c # height in triangle
    l1 = b/2 # height baseline -> flexure bottom
    l2 = a+b/2 # height baseline -> flexure top
    x = np.array([])
    y = np.array([])
    x = np.append(x, 0) # 0
    y = np.append(y, h0) # 0
    x = np.append(x, -h0+l2+ax/2) # 1
    y = np.append(y, l2+ax/2) # 1
    x = np.append(x, -h0+l2+ax) # 2
    y = np.append(y, l2) # 2
    x = np.append(x, -h0+ax) # 3
    y = np.append(y, 0) # 3
    x = np.append(x, h0-ax) # 4
    y = np.append(y, 0) # 4
    x = np.append(x, h0-l1-ax) # 5
    y = np.append(y, l1) # 5
    x = np.append(x, -h0+l1+d+ax) # 6
    y = np.append(y, l1) # 6
    x = np.append(x, -h0+l2+d+ax) # 7
    y = np.append(y, l2) # 7
    x = np.append(x, h0-l2-ax) # 8
    y = np.append(y, l2) # 8
    x = np.append(x, h0-l2-ax/2) # 9
    y = np.append(y, l2+ax/2) # 9
    x = np.append(x, 0) # 0
    y = np.append(y, h0) # 0
    insert_index = 4
    for n in range(num_flex):
        h = (n+1)*(a+b)+h0
        vec_x = np.array([])
        vec_y = np.array([])
        vec_x = np.append(vec_x, -h+l2+ax) # 0
        vec_y = np.append(vec_y, l2)
        vec_x = np.append(vec_x, h-l2-ax-d) # 1
        vec_y = np.append(vec_y, l2)
        vec_x = np.append(vec_x, h-l1-d-ax) # 2
        vec_y = np.append(vec_y, l1)
        vec_x = np.append(vec_x, -h+l1+ax) # 3
        vec_y = np.append(vec_y, l1)
        vec_x = np.append(vec_x, -h+ax) # 4
        vec_y = np.append(vec_y, 0)
        vec_x = np.append(vec_x, h-ax) # 5
        vec_y = np.append(vec_y, 0)
        if n%2:
            vec_x = -vec_x
            vec_x = np.flipud(vec_x)
            vec_y = np.flipud(vec_y)
            insert_index += 4
        y += dy # shifts existing coordinates a distance dy
        x = np.concatenate((x[:insert_index],vec_x, x[insert_index:]),axis=0) # inserts new geometry from origo between the right coordinates
        y = np.concatenate((y[:insert_index],vec_y, y[insert_index:]),axis=0)
        insert_index +=1 # adds to index counter
    coords = [(x[i],y[i]) for i in range(len(x))]
    return shapely.geometry.Polygon(coords)

def make_triangular_switchback_gen_reg(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut='default'):
    """
    Scales the square quart with 3**0.5 to match 30-120-30 triangle. addects edge_space and side_cut
    """
    xfact = 1.00001*3**0.5 # added tolerance for scaling
    edge_space /= xfact
    if side_cut == 'default':
        side_cut = cut_width/xfact / (2**0.5/2)
    sb_square_mod = make_square_switchback_gen_reg(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut)
    return shapely.affinity.scale(geom=sb_square_mod, xfact=xfact)

def make_hexagonal_switchback_gen_reg(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut='default'):
    """
    Scales the square quart with 1/3**0.5 to match 60-60-60 triangle. addects edge_space and side_cut
    """
    xfact = 1.00001/3**0.5 # added tolerance for scaling
    edge_space /= xfact
    if side_cut == 'default':
        side_cut = cut_width/xfact /(2**0.5/2)
    sb_square_mod = make_square_switchback_gen_reg(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut='default')
    return shapely.affinity.scale(geom=sb_square_mod, xfact=xfact)

"""
Tiles
"""

def make_triangular_switchback_tile(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut='default'):
    sb_third = make_triangular_switchback_gen_reg(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut)
    xmin, ymin, xmax, ycoord = sb_third.bounds
    xcoord = (xmax+xmin)/2
    sb_third_r120 = shapely.affinity.rotate(geom=sb_third, angle=120, origin=(xcoord,ycoord))
    sb_third_r240 = shapely.affinity.rotate(geom=sb_third, angle=240, origin=(xcoord,ycoord))
    return shapely.ops.cascaded_union([sb_third, sb_third_r120, sb_third_r240])

def make_square_switchback_tile(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut='default'):
    sb_quart = make_square_switchback_gen_reg(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut)
    xmin, ymin, xmax, ycoord = sb_quart.bounds # ycoord is ymax
    xcoord = (xmax+xmin)/2
    sb_quart_r90 = shapely.affinity.rotate(geom=sb_quart, angle=90, origin=(xcoord,ycoord))
    sb_quart_r180 = shapely.affinity.rotate(geom=sb_quart, angle=180, origin=(xcoord,ycoord))
    sb_quart_r270 = shapely.affinity.rotate(geom=sb_quart, angle=270, origin=(xcoord,ycoord))
    return shapely.ops.cascaded_union([sb_quart, sb_quart_r90, sb_quart_r180, sb_quart_r270])

def make_hexagonal_switchback_tile(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut='default'):
    sb_sixt = make_hexagonal_switchback_gen_reg(cut_width, flexure_width, junction_length, edge_space, num_flex, side_cut)
    xmin, ymin, xmax, ycoord = sb_sixt.bounds
    xcoord = (xmax+xmin)/2
    sb_rotated = [sb_sixt]
    for n in range(6):
        sb_rotated.append(shapely.affinity.rotate(geom=sb_sixt, angle=60*(1*n), origin=(xcoord,ycoord)))
    return shapely.ops.cascaded_union(sb_rotated)

#gen.plotPolygon(make_triangular_switchback_gen_reg(cut_width=1, flexure_width=2, junction_length=5, edge_space=3, num_flex=4, side_cut=1))
#gen.plotPolygon(make_triangular_switchback_tile(cut_width=1, flexure_width=2, junction_length=5, edge_space=3, num_flex=2, side_cut=1))
#gen.plotPolygon(make_hexagonal_switchback_gen_reg(cut_width=1, flexure_width=2, junction_length=5, edge_space=3, num_flex=4, side_cut=1))
gen.plotPolygon(make_hexagonal_switchback_tile(cut_width=1, flexure_width=2, junction_length=5, edge_space=3, num_flex=5, side_cut='default'))

max_stars_repo_name: oddvinostmo/patterns-code

max_issues_repo_name: oddvinostmo/patterns-code

max_forks_repo_name: oddvinostmo/patterns-code


text:
import ast
import json
import os
from io import BytesIO
from sys import argv

import cv2
import numpy as np
import pandas as pd
import requests
from PIL import Image

def get_box_centers_all_emotions(sample_row):
    """
    input: A dictionary of the format
    {
        emotion1: [{geometry_box1: [list of 4 corner coordinates],
                    geometry_box1: [list of 4 corner coordinates],
                    ...}]
        emotion2: [...],
        ...
        emotionN: [...]
    }
    output: A dictionary of the format
        {
            emotion1: [(center1 coordinates), (center1 coordinates), ...],
            emotion2: [...],
            ...
            emotionN: [...]
        }
    """

    lab = json.loads(sample_row)
    centers = {}
    for key, val in lab.items():
        lab_df = pd.DataFrame(val)
        centers[key] = lab_df.iloc[:, 0].apply(lambda s: get_box_center(s)).tolist()
    return centers

def get_box_center(box_row):
    """
    input: A list of 4 coordinates, representing the four corners of the bounding box.
        Each coordinate is a dictionary of {'x': xcoord, 'y': ycoord}
    output: A tuple representing the coordinates of the center of the bounding box, and its width and height.
    """
    x_coords = []
    y_coords = []
    for item in box_row:
        x_coords.append(item['x'])
        y_coords.append(item['y'])
    x_center = (np.max(x_coords) - np.min(x_coords)) / 2 + np.min(x_coords)
    y_center = (np.max(y_coords) - np.min(y_coords)) / 2 + np.min(y_coords)
    box_width = np.max(x_coords) - np.min(x_coords)
    box_height = np.max(y_coords) - np.min(y_coords)
    return x_center, y_center, box_width, box_height

def get_width_height_labelbox(sample_row):
    response = requests.get(sample_row)
    if response.status_code == 200:  # !!!
        img = Image.open(BytesIO(response.content))
        return img.width, img.height
    else:
        return 0, 0

def get_width_height_local(file_name, image_folder_path):
    file_path = os.path.join(image_folder_path, file_name)

    img_width, img_height = 0, 0
    img = cv2.imread(file_path)
    if img is not None:
        img_height, img_width, channels = img.shape
        print('returned local dimensions for {}: ({}, {})'.format(file_name, img_width, img_height))
    else:
        print('could not load image for {}'.format(file_name))

    return img_width, img_height

def get_yolo_formats(emotion_dict, total_width, total_height):
    """
    Input: a dictionary of
        {emotion: [
            (box_x_center, box_y_center, box_width, box_height),
            (box_x_center, box_y_center, box_width, box_height), ...]
        }
    """
    yolo_formats_to_write = []

    if total_width == 0 or total_height == 0:
        print('have 0 height or width. skipping file')
        return yolo_formats_to_write

    for emotion, boxes in emotion_dict.items():
        for box in boxes:
            if emotion == 'happy':
                class_id = 0
            elif emotion == 'neutral':
                class_id = 1
            elif emotion == 'surprised':
                class_id = 2
            elif emotion == 'sad':
                class_id = 3
            elif emotion == 'angry':
                class_id = 4
            else:
                continue

            box_x_center, box_y_center, box_width, box_height = box[0], box[1], box[2], box[3]
            yolo_formats_to_write.append(' '.join([str(class_id),
                                                    str(box_x_center / total_width),
                                                    str(box_y_center / total_height),
                                                    str(box_width / total_width), str(box_height / total_height)]))

    return yolo_formats_to_write

if __name__ == '__main__':

    script_name, path_to_coords, image_folder_path = argv

    # Read raw data into memory
    raw = pd.read_csv(path_to_coords)

    # Grab the relevant columns
    # External ID is the file name of the image
    # Label is the labeled data
    # Labeled Data is the URL of the image stored in label box.
    coords = raw[['External ID', 'Label', 'Labeled Data', 'ID']]

    # Remove the rows with no data
    coords = coords[coords['Label'] != 'Skip']
    coords['local_file'] = coords['ID'] + '-' + coords['External ID']

    # Grab original image dimension.
    # first try local file, if no local file, requests labelbox.
    # if no image, return 0, which we will skip the file.
    coords['width_height'] = coords.apply(
        lambda row: get_width_height_local(row['local_file'], image_folder_path),
        axis=1
    )

    # Extract total width and height of image from tuple.
    coords['total_width'] = coords['width_height'].apply(lambda s: int(str(s).split(',')[0][1:]))
    coords['total_height'] = coords['width_height'].apply(lambda s: int(str(s).split(',')[1].strip()[:-1]))

    # Get box centers for each image sample
    coords['Centers'] = coords['Label'].apply(lambda s: get_box_centers_all_emotions(s))

    # Save the centers data to file
    # coords.to_csv(os.path.join(path_to_coords, 'box_centers.csv'), index=False)
    # coords = pd.read_csv(os.path.join(path_to_coords, 'box_centers.csv'))

    processed_count = 0
    failed_count = 0

    # no label will exceed this limit
    class_limit = 2000
    total_counter = {
        '0': 0,
        '1': 0,
        '2': 0
    }

    write_counter = {
        '0': 0,
        '1': 0,
        '2': 0
    }

    for (img_file_name, cents, w, h) in zip(coords['local_file'],
                                            coords['Centers'],
                                            coords['total_width'],
                                            coords['total_height']):

        if isinstance(cents, str):
            cents = ast.literal_eval(cents)

        yolo_formats_to_write = get_yolo_formats(cents, w, h)
        print(yolo_formats_to_write)

        if not yolo_formats_to_write:
            failed_count += 1
            print('---xxx failed for file {}'.format(img_file_name))
            continue

        # check if limit exceeded
        lines_to_write = []
        for box in yolo_formats_to_write:
            class_id = box.split(' ')[0]
            total_counter[class_id] += 1
            if total_counter[class_id] < class_limit:
                lines_to_write.append(box)
            else:
                print('skipping for {} at {}'.format(
                    box,
                    img_file_name
                ))

        # save results
        if len(lines_to_write) > 0:
            file_name = img_file_name.split('.')[0] + '.txt'
            with open(os.path.join(image_folder_path, file_name), 'w') as f:
                for box in lines_to_write:
                    class_id = box.split(' ')[0]
                    write_counter[class_id] += 1
                    f.write('%s\n' % box)
                print('--->>>> saved for file {}'.format(img_file_name))

        processed_count += 1
        print('processed: {}, failed: {}'.format(processed_count, failed_count))

    print(write_counter)
    print(total_counter)


text:
import os
import sys
import re
import json
import math
import argparse
import time
import subprocess
import numpy as np
import networkx as nx
import tensorflow as tf
import datetime
from operator import itemgetter
import random
random.seed(157)
np.random.seed(157)

from utils.env import *
from utils.metric import Metric
from utils.data_handler import DataHandler as dh

FILE_PATH = os.path.dirname(os.path.abspath(__file__))

def main():
    parser = argparse.ArgumentParser(formatter_class = argparse.RawTextHelpFormatter)
    parser.add_argument('--operation', type = str, default = "all", help = "[all | init | train]")
    parser.add_argument('--conf', type = str, default = "amherst0.25")
    args = parser.parse_args()
    params = dh.load_json_file(os.path.join(CONF_PATH, args.conf + ".json"))
    fw=open('my_embedding.txt','w')
    metric_path_pre = os.path.join(RES_PATH, args.conf)
    if os.path.exists(metric_path_pre) == False:
        os.mkdir(metric_path_pre)
    output_path = os.path.join(metric_path_pre, dh.get_time_str())
    print(output_path)
    metric_path = output_path + "_metric"

    def metric(embeddings):
        if "metrics" not in params:
            return
        for metric in params["metrics"]:
            print("[] Start node classification...")
            res = getattr(Metric, metric["func"])(embeddings, metric)
            dh.append_to_file(metric_path, str(res) + "\n")
            print("[+] Metric: " + str(res))
    dh.symlink(metric_path, os.path.join(metric_path_pre, "new_metric"))

    if args.operation == "all":
        G, embeddings, weights = __import__(
                "init." + params["init"]["func"],
                fromlist = ["init"]
                ).init(params["init"], metric, output_path)
        __import__(
                "dynamic_loop." + params["main_loop"]["func"],
                fromlist = ["dynamic_loop"]
                ).loop(params["main_loop"], G, embeddings, weights, metric, output_path)
        print(embeddings)
    elif args.operation == "init":
        G, embeddings, weights = __import__("init." + params["init"]["func"], fromlist = ["init"]).init(params["init"], metric, output_path)
    else:
        print ("Not Support!")

if __name__ == "__main__":
    main()

reason: import numpy,import networkx


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

def ArrowLine(fig,X,Y,color=[0.0,0.0,0.0],zorder=None,Spacing=20,SingleArrow=False,EndArrow=False,linewidth=1,linestyle='-',HeadWidth=0.05, HeadLength=0.1,Reverse=False,**kwargs):

	if Reverse == True:
		x=X[::-1]
		y=Y[::-1]
	else:
		x=X
		y=Y

	if type(fig) == type(plt):
		ax=fig.gca()
	else:
		#assume that axes have been provided instead of a figure
		ax = fig

	fig.plot(x,y,color=color,zorder=zorder,linewidth=linewidth,linestyle=linestyle,**kwargs)

	if EndArrow:
		n=1
		i0=np.array([np.size(x)])-2
		i1=i0+1
	elif SingleArrow:
		n=1
		i0=np.array([np.size(x)//2])-1
		i1=i0+1
	else:
		l=np.size(x)
		n=l//Spacing -1
		i0=(np.arange(n)+1)*Spacing+np.int32(0.5*Spacing)
		i1=i0+1

	for i in range(0,n):
		ax.arrow(x[i0[i]], y[i0[i]], x[i1[i]]-x[i0[i]], y[i1[i]]-y[i0[i]], head_width=HeadWidth, head_length=HeadLength,color=color,zorder=zorder)


text:
# Error detection methods
import json, sys
import math
import re
from urllib.parse import urlencode
from urllib.request import Request, urlopen

import numpy as np
import time

sys.path.append("../utils")
from code_process import tokenize_err_msg

LINE_OFFSET_DEEPFIX=1
LINE_OFFSET_SPOC=5

def parse_error(raw_err_msg, line_offset, tokenize=True):
    """
    Return the first error line number and error message.

    Args:
        raw_err_msg (str): Raw string from g++
    Returns:
        stmt_index: real line number - line_offset.
        That is, the line number where the first non preamble line is line 0,
        and where DUMMY lines are still included.
    """
    lines = raw_err_msg.split('\n')
    for line in lines:
        m = re.match('[^:]*:(\d+):[:0-9 ]+error: (.*)', line)
        if not m:
            continue
        lineno, message = m.groups()
        if tokenize:
            message = ' '.join(tokenize_err_msg(message))
        return int(lineno) - line_offset, message.strip()

    for line in lines:
        m = re.match('[^:]*:(\d+):[:0-9 ]+: (.*)', line)
        if not m:
            continue
        lineno, message = m.groups()
        if tokenize:
            message = ' '.join(tokenize_err_msg(message))
        return int(lineno) - line_offset, message.strip()

    return None, None

def post_request(server, post_fields):
    for attempt in range(10):
        try:
            request = Request(server, urlencode(post_fields).encode())
            with urlopen(request, timeout=5) as x:
                response = x.read().decode()
            response = json.loads(response)
            return response
        except Exception as e:
            time.sleep(1 + attempt)
    return None

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

class RepairPolicy(object):
    """
    Ask the PyTorch server. return n-best
    """
    def __init__(self, args, for_deepfix=False):
        self.server_list = args.repairer_server
        try:
            self.info = {'probno': args.probno}
        except:
            self.info = {'probno': 'N/A'}
        self.threshold = args.err_localize_threshold
        self.for_deepfix = for_deepfix

    def policy_localize(self, code_lines, feedback, threshold_ON=False):
        if self.for_deepfix:
            DUMMY_linenos = {idx: None for (idx, code_line) in enumerate(code_lines) if code_line[0].strip() == "DUMMY"}
        else:
            DUMMY_linenos = {idx: None for (idx, code_line) in enumerate(code_lines) if code_line[0].strip() in ["", "DUMMY"]}

        if self.for_deepfix:
            lineno, msg = parse_error(feedback, LINE_OFFSET_DEEPFIX, tokenize=True)
        else:
            lineno, msg = parse_error(feedback, LINE_OFFSET_SPOC, tokenize=True)
        if msg is None:
            return None, None, None
        err_line_obj = {'lineno': lineno, 'msg': msg}
        q = {
            'info': self.info,
            'code_lines': code_lines,
            'err_line': err_line_obj,
            "gold_linenos": [0]*len(code_lines), #temporary for now
            "edit_linenos": [0]*len(code_lines), #temporary for now
            "gold_code_lines": code_lines,  #dummy
            "comment": {"method": "localize_only", "beam_size": None}
        }
        _server = self.server_list[np.random.choice(len(self.server_list))]
        response = post_request(_server, {'q': json.dumps(q)})
        if response is None:
            assert False, "server failed!"
        logit = response['logit_localize'][0]
        probs = self.softmax(logit)
        pred_lineno = None #response['pred'][0]
        for (i, idx) in enumerate((-np.array(logit)).argsort()):
            if idx not in DUMMY_linenos:
                pred_lineno = idx
                break
        if threshold_ON and probs[pred_lineno] < self.threshold: #abstain
            return None, None, err_line_obj
        return pred_lineno, None, err_line_obj

    def policy_edit(self, code_lines, feedback, pred_lineno, beam_size=10):
        # pred_lineno: int
        if self.for_deepfix:
            lineno, msg = parse_error(feedback, LINE_OFFSET_DEEPFIX, tokenize=True)
        else:
            lineno, msg = parse_error(feedback, LINE_OFFSET_SPOC, tokenize=True)
        if msg is None:
            return None, None, None
        err_line_obj = {'lineno': lineno, 'msg': msg}
        indicators = [0]*len(code_lines)
        indicators[pred_lineno] = 1
        q = {
            'info': self.info,
            'code_lines': code_lines,
            'err_line': err_line_obj,
            "gold_linenos": indicators,
            "edit_linenos": indicators,
            "gold_code_lines": code_lines,  #dummy
            "comment": {"method": "edit_only", "beam_size": beam_size}
        }
        _server = self.server_list[np.random.choice(len(self.server_list))]
        response = post_request(_server, {'q': json.dumps(q)})
        if response is None:
            assert False, "server_compiler_localize_edit failed!"
        response["pred"] = response["pred_edit"]
        return pred_lineno, response, err_line_obj

    def policy_localize_edit(self, code_lines, feedback, beam_size=10):
        # pred_lineno: int
        if self.for_deepfix:
            DUMMY_linenos = {idx: None for (idx, code_line) in enumerate(code_lines) if code_line[0].strip() == "DUMMY"}
        else:
            DUMMY_linenos = {idx: None for (idx, code_line) in enumerate(code_lines) if code_line[0].strip() in ["", "DUMMY"]}

        if self.for_deepfix:
            lineno, msg = parse_error(feedback, LINE_OFFSET_DEEPFIX, tokenize=True)
        else:
            lineno, msg = parse_error(feedback, LINE_OFFSET_SPOC, tokenize=True)
        if msg is None:
            return None, None, None
        err_line_obj = {'lineno': lineno, 'msg': msg}
        q = {
            'info': self.info,
            'code_lines': code_lines,
            'err_line': err_line_obj,
            "gold_linenos": [0]*len(code_lines), #temporary for now
            "edit_linenos": [0]*len(code_lines), #temporary for now
            "gold_code_lines": code_lines,  #dummy
            "comment": {"method": "localize_edit", "beam_size": beam_size}
        }
        _server = self.server_list[np.random.choice(len(self.server_list))]
        response = post_request(_server, {'q': json.dumps(q)})
        if response is None:
            assert False, "server_compiler_localize_edit failed!"
        logit = response['logit_localize'][0]
        probs = self.softmax(logit)
        pred_lineno = None #response['pred'][0]
        for (i, idx) in enumerate((-np.array(logit)).argsort()):
            if idx not in DUMMY_linenos:
                pred_lineno = idx
                break
        if threshold_ON and probs[pred_lineno] < self.threshold: #abstain
            return None, None, err_line_obj
        response["pred"] = response["pred_edit"]
        return pred_lineno, response, err_line_obj

    def softmax(self, numbers):
        numbers = [math.exp(x - max(numbers)) for x in numbers]
        return [x / sum(numbers) for x in numbers]

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

max_stars_repo_name: michiyasunaga/DrRepair

max_issues_repo_name: ssChecker/DrRepair

max_forks_repo_name: ssChecker/DrRepair


text:
import os
import json

import numpy as np
import pandas as pd
from flask_cors import CORS
from keras.models import load_model
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing.image import img_to_array, load_img
from flask import Flask, request

def keras_load_model():
    model = load_model('../weights/model-v1.h5')
    return model

def normalize_probs(unnormalized_probs):
    factor  = sum(top_probs)
    return (np.array(top_probs)/factor).tolist()

def get_label(probabilities):
    probabilities = probabilities[0].astype('float64')
    top_outputs = probabilities.argsort()[-5:][::-1]
    top_probs = [probabilities[output] for output in top_outputs]
    top_probs = normalize_probs(top_probs)
    result = [(prob, classes[str(output)]) for prob, output in zip(top_probs, top_outputs)]
    return result

app = Flask(__name__)
cors = CORS(app)
UPLOAD_FOLDER = 'images/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
model = keras_load_model()
model._make_predict_function()
classes = json.load(open("../data/classes.json"))
wine_table = pd.read_csv("../data/wine_table.csv")
food_wine = json.load(open('../data/food_wine.json'))
wine_table.index = wine_table.name

@app.route('/food', methods=['POST'])
def food_kind():
    file = request.files['file']
    file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
    img = load_img(os.path.join(app.config['UPLOAD_FOLDER'], file.filename), target_size=(224, 224))
    img_array = img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = preprocess_input(img_array)
    prediction = model.predict(img_array)
    foods = get_label(prediction)
    return json.dumps(foods), 200

@app.route('/wine/<food>', methods=['GET'])
def wine_that_matched(food):
    try:
        wine = food_wine[food]
    except KeyError:
        return "Food not found", 404
    wine_info = wine_table.loc[wine].to_json()
    return wine_info

if __name__ == '__main__':
    app.run(debug=True)

max_stars_repo_name: prcastro/hackaton-cotidiano

max_issues_repo_name: prcastro/hackaton-cotidiano

max_forks_repo_name: prcastro/hackaton-cotidiano


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
# Copyright (c) 2020 University of Dundee.
#
#   Redistribution and use in source and binary forms, with or without modification,
#   are permitted provided that the following conditions are met:
#
#   Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#   Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
#   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
#   INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
#   OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Version: 1.0
#

import numpy
import os

import omero.clients
from omero.gateway import BlitzGateway
from getpass import getpass
from collections import OrderedDict

from ilastik import app
from ilastik.applets.dataSelection.opDataSelection import PreloadedArrayDatasetInfo  # noqa

# Connect to the server
def connect(hostname, username, password):
    conn = BlitzGateway(username, password,
                        host=hostname, secure=True)
    conn.connect()
    return conn

# Load-images
def load_images(conn, dataset_id):
    return conn.getObjects('Image', opts={'dataset': dataset_id})

# Create-dataset
def create_dataset(conn, dataset_id):
    dataset = omero.model.DatasetI()
    v = "ilastik_probabilities_from_dataset_%s" % dataset_id
    dataset.setName(omero.rtypes.rstring(v))
    v = "ilatisk results probabilities from Dataset:%s" % dataset_id
    dataset.setDescription(omero.rtypes.rstring(v))
    return conn.getUpdateService().saveAndReturnObject(dataset)

# Load-data
def load_numpy_array(image):
    pixels = image.getPrimaryPixels()
    size_z = image.getSizeZ()
    size_c = image.getSizeC()
    size_t = image.getSizeT()
    size_y = image.getSizeY()
    size_x = image.getSizeX()
    z, t, c = 0, 0, 0  # first plane of the image
    zct_list = []
    for t in range(size_t):
        for z in range(size_z):  # get the Z-stack
            for c in range(size_c):  # all channels
                zct_list.append((z, c, t))
    values = []
    # Load all the planes as YX numpy array
    planes = pixels.getPlanes(zct_list)
    j = 0
    k = 0
    tmp_c = []
    tmp_z = []
    s = "z:%s t:%s c:%s y:%s x:%s" % (size_z, size_t, size_c, size_y, size_x)
    print(s)
    # axis tzyxc
    print("Downloading image %s" % image.getName())
    for i, p in enumerate(planes):
        if k < size_z:
            if j < size_c:
                tmp_c.append(p)
                j = j + 1
                if j == size_c:
                    # use dstack to have c at the end
                    tmp_z.append(numpy.dstack(tmp_c))
                    tmp_c = []
                    j = 0
                    k = k + 1
        if k == size_z:  # done with the stack
            values.append(numpy.stack(tmp_z))
            tmp_z = []
            k = 0
    return numpy.stack(values)

# Analyze-data
def analyze(conn, images, model, new_dataset):
    # Prepare ilastik
    os.environ["LAZYFLOW_THREADS"] = "2"
    os.environ["LAZYFLOW_TOTAL_RAM_MB"] = "2000"
    args = app.parse_args([])
    args.headless = True
    args.project = model
    shell = app.main(args)
    for image in images:
        input_data = load_numpy_array(image)
        # run ilastik headless
        print('running ilastik using %s and %s' % (model, image.getName()))
        data = [ {"Raw Data": PreloadedArrayDatasetInfo(preloaded_array=input_data, axistags=vigra.defaultAxistags("tzyxc"))}]  # noqa
        predictions = shell.workflow.batchProcessingApplet.run_export(data,
                                                                      export_to_array=True)  # noqa
        for d in predictions:
            save_results(conn, image, d, new_dataset)

# Save-results
def save_results(conn, image, data, dataset):
    filename, file_extension = os.path.splitext(image.getName())
    # Save the probabilities file as an image
    print("Saving Probabilities as an Image in OMERO")
    name = filename + "_Probabilities"
    desc = "ilastik probabilities from Image:%s" % image.getId()
    # Re-organise array from tzyxc to zctyx order expected by OMERO
    data = data.swapaxes(0, 1).swapaxes(3, 4).swapaxes(2, 3).swapaxes(1, 2)

    def plane_gen():
        """
        Set up a generator of 2D numpy arrays.
        The createImage method below expects planes in the order specified here
        (for z.. for c.. for t..)
        """
        size_z = data.shape[0]-1
        for z in range(data.shape[0]):  # all Z sections data.shape[0]
            print('z: %s/%s' % (z, size_z))
            for c in range(data.shape[1]):  # all channels
                for t in range(data.shape[2]):  # all time-points
                    yield data[z][c][t]

    conn.createImageFromNumpySeq(plane_gen(), name, data.shape[0],
                                 data.shape[1], data.shape[2],
                                 description=desc, dataset=dataset)

# Disconnect
def disconnect(conn):
    conn.close()

# main
def main():
    try:
        # Collect user credentials
        host = input("Host [wss://workshop.openmicroscopy.org/omero-ws]: ") or 'wss://workshop.openmicroscopy.org/omero-ws'  # noqa
        username = input("Username [trainer-1]: ") or 'trainer-1'
        password = getpass("Password: ")
        dataset_id = input("Dataset ID [6210]: ") or '6210'
        # Connect to the server
        conn = connect(host, username, password)
        conn.c.enableKeepAlive(60)

        # path to the ilastik project
        ilastik_project = "../notebooks/pipelines/pixel-class-133.ilp"

        # Load the images in the dataset
        images = load_images(conn, dataset_id)

        new_dataset = create_dataset(conn, dataset_id)

        analyze(conn, images, ilastik_project, new_dataset)

    finally:
        disconnect(conn)
    print("done")

if __name__ == "__main__":
    main()

max_stars_repo_name: pwalczysko/ilastik

max_issues_repo_name: pwalczysko/ilastik

max_forks_repo_name: pwalczysko/ilastik


text:
import os

import cv2
import skimage
import numpy as np
import torch
import torch.nn.functional as F
from medpy import metric
from PIL import Image
from tqdm import tqdm
import matplotlib.pyplot as plt
from skimage import measure
import cv2
import numpy as np
from scipy.ndimage import filters, measurements, distance_transform_edt
from scipy.ndimage.morphology import (binary_dilation, binary_fill_holes,
                                      distance_transform_cdt,
                                      distance_transform_edt)
from skimage.morphology import remove_small_objects, watershed

def compute_iou(prediction, ground_truth):
    smooth = 1e-5
    prediction = prediction.flatten()
    ground_truth = ground_truth.flatten()
    intersection = (prediction * ground_truth).sum()
    return (intersection + smooth) / (prediction.sum() + ground_truth.sum() - intersection + smooth)

def accuracy(prediction, ground_truth):
    prediction = prediction.flatten()
    ground_truth = ground_truth.flatten()
    return (prediction == ground_truth).sum() / len(ground_truth)

def test_all_case(net, image_list, patch_size=(400, 400), stride_hw=(60, 60), save_result=True, test_save_path=None):
    total_metric = 0.0
    for image_path in tqdm(image_list):
        img = np.array(Image.open(image_path[0]))
        lab = np.array(Image.open(image_path[1]).convert("L"))
        lab[lab > 0] = 1
        prediction = test_single_case(net, img, patch_size, stride_hw)
        if np.sum(prediction) == 0:
            single_metric = (0, 0, 0, 0, 0, 0)
        else:
            single_metric = calculate_metric_percase(prediction, lab)
        total_metric += np.asarray(single_metric)
        if save_result:
            case_name = image_path[1].split(".")[0] + "_pred.png"
            prediction[prediction > 0.5] = 255
            cv2.imwrite(case_name, prediction)
    avg_metric = total_metric / len(image_list)
    print('average metric is {}'.format(avg_metric))
    return avg_metric

def test_single_case(net, x, patch_size=(400, 400), stride_hw=(60, 60)):
    stridew, strideh = stride_hw
    winW, winH = patch_size
    shapeW, shapeH, channel = x.shape
    score_map = np.zeros((shapeW, shapeH))
    item_counting = np.zeros((shapeW, shapeH))
    for h in range((shapeH-winH) // strideh + 1):
        for w in range((shapeW-winW) // stridew + 1):
            test_patch = x[w*stridew:w*stridew+winW, h*strideh:h*strideh+winH]
            test_patch = (test_patch / test_patch.max())
            test_patch = test_patch.transpose((2, 0, 1))
            test_patch = torch.from_numpy(
                np.array([test_patch])).float().cuda()
            patch_output = net(test_patch)
            patch_output = patch_output.squeeze().squeeze().cpu().data.numpy()
            score_map[w*stridew:w*stridew+winW, h *
                      strideh:h*strideh+winH] += patch_output
            item_counting[w*stridew:w*stridew +
                          winW, h*strideh:h*strideh+winH] += 1
    score_map = score_map / item_counting
    score_map[score_map >= 0.5] = 1
    score_map[score_map < 0.5] = 0
    return score_map.astype(np.uint8)

def calculate_metric_percase(pred, gt):
    aji = fast_aji(gt, pred)
    iou = compute_iou(pred, gt)
    dice = metric.binary.dc(pred, gt)
    jc = metric.binary.jc(pred, gt)
    hd = metric.binary.hd95(pred, gt)
    asd = metric.binary.asd(pred, gt)

    return aji, iou, dice, jc, hd, asd

def fast_aji(anno_arr, pred_arr):
    anno_arr = post_processing(anno_arr)
    anno_arr = measure.label(anno_arr)
    pred_arr = measure.label(pred_arr)
    gs, g_areas = np.unique(anno_arr, return_counts=True)
    assert np.all(gs == np.arange(len(gs)))
    ss, s_areas = np.unique(pred_arr, return_counts=True)
    assert np.all(ss == np.arange(len(ss)))

    i_idx, i_cnt = np.unique(np.concatenate([anno_arr.reshape(1, -1), pred_arr.reshape(1, -1)]),
                             return_counts=True, axis=1)
    i_arr = np.zeros(shape=(len(gs), len(ss)), dtype=np.int)
    i_arr[i_idx[0], i_idx[1]] += i_cnt
    u_arr = g_areas.reshape(-1, 1) + s_areas.reshape(1, -1) - i_arr
    iou_arr = 1.0 * i_arr / u_arr

    i_arr = i_arr[1:, 1:]
    u_arr = u_arr[1:, 1:]
    iou_arr = iou_arr[1:, 1:]

    j = np.argmax(iou_arr, axis=1)

    c = np.sum(i_arr[np.arange(len(gs) - 1), j])
    u = np.sum(u_arr[np.arange(len(gs) - 1), j])
    used = np.zeros(shape=(len(ss) - 1), dtype=np.int)
    used[j] = 1
    u += (np.sum(s_areas[1:] * (1 - used)))
    return 1.0 * c / u

def post_processing(pred):
    lab = pred
    distance = distance_transform_edt(pred)
    blb_raw = lab
    dst_raw = distance
    blb = np.copy(blb_raw)
    blb[blb >  0.5] = 1
    blb[blb <= 0.5] = 0
    blb = measurements.label(blb)[0]
    blb = remove_small_objects(blb, min_size=10)
    blb[blb > 0] = 1

    dst_raw[dst_raw < 0] = 0
    dst = np.copy(dst_raw)
    dst = dst * blb
    dst[dst  > 0.5] = 1
    dst[dst <= 0.5] = 0

    marker = dst.copy()
    marker = binary_fill_holes(marker)
    marker = measurements.label(marker)[0]
    marker = remove_small_objects(marker, min_size=10)
    proced_pred = watershed(-dst_raw, marker, mask=blb)
    proced_pred[proced_pred > 0] = 1
    return proced_pred

reason: import numpy,from scipy


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

# =========================================================================== #
import numpy as np

# =========================================================================== #
def fracsum(num, denom):
    """
    this functions returns sum(top/bottom) where top and bottom are 1d arrays.
    if bottom == 0 at a index, that index is skipped when creating the sum.

    its basically a smarter np.nansum
    """
    result = 0.0
    assert len(np.where(denom < 0)[0])==0 #denom should be positive or 0
    goodIDXs = np.where(denom != 0)[0]
    for i in range(0, len(goodIDXs)):
        idx = goodIDXs[i]
        result = result + num[idx]/denom[idx]

    """
    # for debugging
    print
    print num
    print
    print denom
    print
    print 'RESULT: ', result
    print "=========================================="
    """

    return result

# =========================================================================== #
def cartesian(arrays, out=None):
    '''
    Magic function that takes arrays and returns all possible combinations of their
    entries.  This function is responsable for allowing me to go from parameterPointsRange
    to all the unique combinations that are pluged into python-fsps.

    example: input=[ [0,1,2]    would yield output=[ [0,7]
                     [7,8,9] ]                       [0,8]
                                                     [0,9]
                                                     [1,7]
                                                     [1,8]
                                                     [1,9]
                                                     [2,7]
                                                     [2,8]
                                                     [2,9] ]

    CODE FROM:  http://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays
    '''
    import numpy as np
    arrays = [np.asarray(x) for x in arrays]
    dtype = arrays[0].dtype
    n = np.prod([x.size for x in arrays])
    if out is None:
        out = np.zeros([n, len(arrays)], dtype=dtype)
    m = n / arrays[0].size
    out[:,0] = np.repeat(arrays[0], m)
    if arrays[1:]:
        cartesian(arrays[1:], out=out[0:m,1:])
        for j in xrange(1, arrays[0].size):
            out[j*m:(j+1)*m,1:] = out[0:m,1:]
    return out

# =========================================================================== #
def getMode(array, decimal_places):
    '''More intellegent way to get the mode of a 1d array'''
    ''' THIS MIGHT BE BROKEN '''
    # round data
    array = np.around(array, decimal_places)
    # get unique entries
    unique = np.unique(array)
    # create count array
    count = np.zeros(len(unique))
    # loop through unique entries, geting their number of occurence
    for i in range(0, len(unique)):
        count[i] = len(np.where(array == unique[i])[0])
    # modes are values where count = max(count), get them all
    modeIDXs = np.where(count == np.max(count))[0]
    return unique[modeIDXs], count[modeIDXs]

# =========================================================================== #

max_stars_repo_name: astronomeralex/mcsed

max_issues_repo_name: astronomeralex/mcsed

max_forks_repo_name: astronomeralex/mcsed


text:
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
import os

def configuration(parent_package='',top_path=None):

    from numpy.distutils.misc_util import Configuration

    config = Configuration('image', parent_package, top_path)
    config.add_data_dir('tests')
    config.add_data_dir('benchmarks')
    config.add_include_dirs(config.name.replace('.', os.sep))
    config.add_extension('image_module', sources=['image_module.pyx', 'cubic_spline.c'])

    print config

    return config

if __name__ == '__main__':
    print 'This is the wrong setup.py file to run'

max_stars_repo_name: fperez/nipy

max_issues_repo_name: fperez/nipy

max_forks_repo_name: fperez/nipy

reason: from numpy


text:
# Copyright 2021 The Flax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""ImageNet example.

This script trains a ResNet-50 on the ImageNet dataset.
The data is loaded using tensorflow_datasets.
"""

import functools
import time
from typing import Any

from absl import logging
from clu import metric_writers
from clu import periodic_actions

import flax
from flax import jax_utils
from flax import optim

import input_pipeline
import models

from flax.training import checkpoints
from flax.training import common_utils
from flax.training import train_state

import jax
from jax import lax
from jax import random

import jax.numpy as jnp

import ml_collections

import optax

import tensorflow as tf
import tensorflow_datasets as tfds

NUM_CLASSES = 1000

def create_model(*, model_cls, half_precision, **kwargs):
  platform = jax.local_devices()[0].platform
  if half_precision:
    if platform == 'tpu':
      model_dtype = jnp.bfloat16
    else:
      model_dtype = jnp.float16
  else:
    model_dtype = jnp.float32
  return model_cls(num_classes=NUM_CLASSES, dtype=model_dtype, **kwargs)

def initialized(key, image_size, model):
  input_shape = (1, image_size, image_size, 3)
  @jax.jit
  def init(*args):
    return model.init(*args)
  variables = init({'params': key}, jnp.ones(input_shape, model.dtype))
  return variables['params'], variables['batch_stats']

def cross_entropy_loss(logits, labels):
  one_hot_labels = common_utils.onehot(labels, num_classes=NUM_CLASSES)
  xentropy = optax.softmax_cross_entropy(logits=logits, labels=one_hot_labels)
  return jnp.mean(xentropy)

def compute_metrics(logits, labels):
  loss = cross_entropy_loss(logits, labels)
  accuracy = jnp.mean(jnp.argmax(logits, -1) == labels)
  metrics = {
      'loss': loss,
      'accuracy': accuracy,
  }
  metrics = lax.pmean(metrics, axis_name='batch')
  return metrics

def create_learning_rate_fn(
    config: ml_collections.ConfigDict,
    base_learning_rate: float,
    steps_per_epoch: int):
  """Create learning rate schedule."""
  warmup_fn = optax.linear_schedule(
      init_value=0., end_value=base_learning_rate,
      transition_steps=config.warmup_epochs * steps_per_epoch)
  cosine_epochs = max(config.num_epochs - config.warmup_epochs, 1)
  cosine_fn = optax.cosine_decay_schedule(
      init_value=base_learning_rate,
      decay_steps=cosine_epochs * steps_per_epoch)
  schedule_fn = optax.join_schedules(
      schedules=[warmup_fn, cosine_fn],
      boundaries=[config.warmup_epochs * steps_per_epoch])
  return schedule_fn

def train_step(state, batch, learning_rate_fn):
  """Perform a single training step."""
  def loss_fn(params):
    """loss function used for training."""
    logits, new_model_state = state.apply_fn(
        {'params': params, 'batch_stats': state.batch_stats},
        batch['image'],
        mutable=['batch_stats'])
    loss = cross_entropy_loss(logits, batch['label'])
    weight_penalty_params = jax.tree_leaves(params)
    weight_decay = 0.0001
    weight_l2 = sum([jnp.sum(x ** 2)
                     for x in weight_penalty_params
                     if x.ndim > 1])
    weight_penalty = weight_decay * 0.5 * weight_l2
    loss = loss + weight_penalty
    return loss, (new_model_state, logits)

  step = state.step
  dynamic_scale = state.dynamic_scale
  lr = learning_rate_fn(step)

  if dynamic_scale:
    grad_fn = dynamic_scale.value_and_grad(
        loss_fn, has_aux=True, axis_name='batch')
    dynamic_scale, is_fin, aux, grads = grad_fn(state.params)
    # dynamic loss takes care of averaging gradients across replicas
  else:
    grad_fn = jax.value_and_grad(loss_fn, has_aux=True)
    aux, grads = grad_fn(state.params)
    # Re-use same axis_name as in the call to `pmap(...train_step...)` below.
    grads = lax.pmean(grads, axis_name='batch')
  new_model_state, logits = aux[1]
  metrics = compute_metrics(logits, batch['label'])
  metrics['learning_rate'] = lr

  new_state = state.apply_gradients(
      grads=grads, batch_stats=new_model_state['batch_stats'])
  if dynamic_scale:
    # if is_fin == False the gradients contain Inf/NaNs and optimizer state and
    # params should be restored (= skip this step).
    new_state = new_state.replace(
        opt_state=jax.tree_multimap(
            functools.partial(jnp.where, is_fin),
            new_state.opt_state,
            state.opt_state),
        params=jax.tree_multimap(
            functools.partial(jnp.where, is_fin),
            new_state.params,
            state.params))
    metrics['scale'] = dynamic_scale.scale

  return new_state, metrics

def eval_step(state, batch):
  variables = {'params': state.params, 'batch_stats': state.batch_stats}
  logits = state.apply_fn(
      variables, batch['image'], train=False, mutable=False)
  return compute_metrics(logits, batch['label'])

def prepare_tf_data(xs):
  """Convert a input batch from tf Tensors to numpy arrays."""
  local_device_count = jax.local_device_count()
  def _prepare(x):
    # Use _numpy() for zero-copy conversion between TF and NumPy.
    x = x._numpy()  # pylint: disable=protected-access

    # reshape (host_batch_size, height, width, 3) to
    # (local_devices, device_batch_size, height, width, 3)
    return x.reshape((local_device_count, -1) + x.shape[1:])

  return jax.tree_map(_prepare, xs)

def create_input_iter(dataset_builder, batch_size, image_size, dtype, train,
                      cache):
  ds = input_pipeline.create_split(
      dataset_builder, batch_size, image_size=image_size, dtype=dtype,
      train=train, cache=cache)
  it = map(prepare_tf_data, ds)
  it = jax_utils.prefetch_to_device(it, 2)
  return it

class TrainState(train_state.TrainState):
  batch_stats: Any
  dynamic_scale: flax.optim.DynamicScale

def restore_checkpoint(state, workdir):
  return checkpoints.restore_checkpoint(workdir, state)

def save_checkpoint(state, workdir):
  if jax.process_index() == 0:
    # get train state from the first replica
    state = jax.device_get(jax.tree_map(lambda x: x[0], state))
    step = int(state.step)
    checkpoints.save_checkpoint(workdir, state, step, keep=3)

# pmean only works inside pmap because it needs an axis name.
# This function will average the inputs across all devices.
cross_replica_mean = jax.pmap(lambda x: lax.pmean(x, 'x'), 'x')

def sync_batch_stats(state):
  """Sync the batch statistics across replicas."""
  # Each device has its own version of the running average batch statistics and
  # we sync them before evaluation.
  return state.replace(batch_stats=cross_replica_mean(state.batch_stats))

def create_train_state(rng, config: ml_collections.ConfigDict,
                       model, image_size, learning_rate_fn):
  """Create initial training state."""
  dynamic_scale = None
  platform = jax.local_devices()[0].platform
  if config.half_precision and platform == 'gpu':
    dynamic_scale = optim.DynamicScale()
  else:
    dynamic_scale = None

  params, batch_stats = initialized(rng, image_size, model)
  tx = optax.sgd(
      learning_rate=learning_rate_fn,
      momentum=config.momentum,
      nesterov=True,
  )
  state = TrainState.create(
      apply_fn=model.apply,
      params=params,
      tx=tx,
      batch_stats=batch_stats,
      dynamic_scale=dynamic_scale)
  return state

def train_and_evaluate(config: ml_collections.ConfigDict,
                       workdir: str) -> TrainState:
  """Execute model training and evaluation loop.

  Args:
    config: Hyperparameter configuration for training and evaluation.
    workdir: Directory where the tensorboard summaries are written to.

  Returns:
    Final TrainState.
  """

  writer = metric_writers.create_default_writer(
      logdir=workdir, just_logging=jax.host_id() != 0)

  rng = random.PRNGKey(0)
  futures=[]
  image_size = 224

  if config.batch_size % jax.device_count() > 0:
    raise ValueError('Batch size must be divisible by the number of devices')
  local_batch_size = config.batch_size // jax.process_count()

  platform = jax.local_devices()[0].platform

  if config.half_precision:
    if platform == 'tpu':
      input_dtype = tf.bfloat16
    else:
      input_dtype = tf.float16
  else:
    input_dtype = tf.float32

  dataset_builder = tfds.builder(config.dataset)
  train_iter = create_input_iter(
      dataset_builder, local_batch_size, image_size, input_dtype, train=True,
      cache=config.cache)
  eval_iter = create_input_iter(
      dataset_builder, local_batch_size, image_size, input_dtype, train=False,
      cache=config.cache)

  steps_per_epoch = (
      dataset_builder.info.splits['train'].num_examples // config.batch_size
  )

  if config.num_train_steps == -1:
    num_steps = int(steps_per_epoch * config.num_epochs)
  else:
    num_steps = config.num_train_steps

  if config.steps_per_eval == -1:
    num_validation_examples = dataset_builder.info.splits[
        'validation'].num_examples
    steps_per_eval = num_validation_examples // config.batch_size
  else:
    steps_per_eval = config.steps_per_eval

  steps_per_checkpoint = steps_per_epoch * 10

  base_learning_rate = config.learning_rate * config.batch_size / 256.

  model_cls = getattr(models, config.model)
  model = create_model(
      model_cls=model_cls, half_precision=config.half_precision)

  learning_rate_fn = create_learning_rate_fn(
      config, base_learning_rate, steps_per_epoch)

  state = create_train_state(rng, config, model, image_size, learning_rate_fn)
  state = restore_checkpoint(state, workdir)
  # step_offset > 0 if restarting from checkpoint
  step_offset = int(state.step)
  state = jax_utils.replicate(state)

  p_train_step = jax.pmap(
      functools.partial(train_step, learning_rate_fn=learning_rate_fn),
      axis_name='batch')
  p_eval_step = jax.pmap(eval_step, axis_name='batch')

  train_metrics = []
  hooks = []
  if jax.process_index() == 0:
    hooks += [periodic_actions.Profile(num_profile_steps=5, logdir=workdir)]
  train_metrics_last_t = time.time()
  logging.info('Initial compilation, this might take some minutes...')
  for step, batch in zip(range(step_offset, num_steps), train_iter):
    state, metrics = p_train_step(state, batch)
    for h in hooks:
      h(step)
    if step == step_offset:
      logging.info('Initial compilation completed.')

    if config.get('log_every_steps'):
      train_metrics.append(metrics)
      if (step + 1) % config.log_every_steps == 0:
        train_metrics = common_utils.get_metrics(train_metrics)
        summary = {
            f'train_{k}': v
            for k, v in jax.tree_map(lambda x: x.mean(), train_metrics).items()
        }
        summary['steps_per_second'] = config.log_every_steps / (
            time.time() - train_metrics_last_t)
        writer.write_scalars(step + 1, summary)
        train_metrics = []
        train_metrics_last_t = time.time()

    if (step + 1) % steps_per_epoch == 0:
      epoch = step // steps_per_epoch
      eval_metrics = []

      # sync batch statistics across replicas
      state = sync_batch_stats(state)
      for _ in range(steps_per_eval):
        eval_batch = next(eval_iter)
        metrics = p_eval_step(state, eval_batch)
        eval_metrics.append(metrics)
      eval_metrics = common_utils.get_metrics(eval_metrics)
      summary = jax.tree_map(lambda x: x.mean(), eval_metrics)
      logging.info('eval epoch: %d, loss: %.4f, accuracy: %.2f',
                   epoch, summary['loss'], summary['accuracy'] * 100)
      writer.write_scalars(
          step + 1, {f'eval_{key}': val for key, val in summary.items()})
      writer.flush()
    if (step + 1) % steps_per_checkpoint == 0 or step + 1 == num_steps:
      state = sync_batch_stats(state)
      save_checkpoint(state, workdir)

  # Wait until computations are done before exiting
  jax.random.normal(jax.random.PRNGKey(0), ()).block_until_ready()
  return state

reason: import jax,from jax


text:
import datetime
import logging
import os
import time
import math

import keyboard
import numpy as np

from screen import Screen
from config import Config
from logger import Logger
from npc_manager import NpcManager, Npc
from template_finder import TemplateFinder
from utils.custom_mouse import mouse
from utils.misc import wait, load_template, send_discord
import cv2

class ExtendedTemplateFinder(TemplateFinder):
    def __init__(self, screen):
        super().__init__(screen)
        custom_templates = {
            "CLAW1": [load_template("assets/shop/claws/claw1.png", 1.0), 1.0],
            "CLAW2": [load_template("assets/shop/claws/claw2.png", 1.0), 1.0],
            "CLAW3": [load_template("assets/shop/claws/claw3.png", 1.0), 1.0],
            "TO_TRAPS": [load_template("assets/shop/claws/to_traps.png", 1.0), 1.0],
            "3_TO_TRAPS": [load_template("assets/shop/claws/3_to_traps.png", 1.0), 1.0],
            "2_TO_ASSA": [load_template("assets/shop/claws/2_to_assa.png", 1.0), 1.0],
            "TO_LIGHT": [load_template("assets/shop/claws/to_light.png", 1.0), 1.0],
            "TO_WB": [load_template("assets/shop/claws/wb.png", 1.0), 1.0],
            "TO_DS": [load_template("assets/shop/claws/to_ds.png", 1.0), 1.0],
            "TO_VENOM": [load_template("assets/shop/claws/to_venom.png", 1.0), 1.0],
        }
        self._templates.update(custom_templates)

def exit(run_obj):
    run_time = str(datetime.timedelta(seconds=round(time.time() - run_obj.start_time)))
    Logger.info("Exiting shopping mall...")
    print(
        "STATS \truns \t\ttime \t\tias_gloves_seen \tgloves_bought \tclaws_evaluated \tclaws_bought\n"
        f"\t{run_obj.run_count} \t\t{run_time}"
        f"\t\t{run_obj.ias_gloves_seen} \t\t\t{run_obj.gloves_bought} \t\t{run_obj.claws_evaluated} \t\t\t{run_obj.claws_bought}"
    )
    os._exit(0)

def wait_for_loading_screen(screen: Screen, time_out):
    start = time.time()
    while time.time() - start < time_out:
        img = screen.grab()
        is_loading_black_roi = np.average(img[:700, 0:250]) < 4.0
        if is_loading_black_roi:
            return True
    return False

class AnyaShopper:
    """
    Shop at Anya for 20 ias, +3 java skill gloves and more...

    In order to start the shopping bot:
    1.) Run this this file in Python.
    2.) Be ingame in Harrogath (act 5 town).
    3.) Stand close to Anya and the red portal.
    4.) While being ingame, press resume_key (default F11) to start the shopping, and exit_key (default F12) to stop it.
    """

    def __init__(self, config: Config):

        # Configurable part, dont touch anything above
        # Set look_for variables to False if you dont like your personal shopper to look for these
        # Obviously something need to be set to True, or your shopper will be very confused
        # For the trap claw scores use:
        # 7 if you are happy with any + to traps or +2 assassin
        # 9 if you want at least +2 assassin or two useful trap stats
        # 11 if you want at least +3 traps or +2 and a sentry bonus
        # Similar for melee claws but not really worth keeping any less that 11 here since you really want both +2 assassin and a useful other stat, feedback needed

        self.look_for_plus_2_gloves = True
        self.look_for_plus_3_gloves = True
        self.look_for_trap_claws = True
        self.trap_claw_min_score = 11
        self.look_for_melee_claws = True
        self.melee_claw_min_score = 11

        # Dont touch anything below here
        self._config = config
        self._screen = Screen(config.general["monitor"])
        self._template_finder = ExtendedTemplateFinder(self._screen)
        self._npc_manager = NpcManager(
            screen=self._screen, template_finder=self._template_finder
        )
        self.run_count = 0
        self.start_time = time.time()
        self.ias_gloves_seen = 0
        self.gloves_bought = 0

        # Claws config
        self.roi_claw_stats = [0, 0, config.ui_pos["screen_width"] // 2, config.ui_pos["screen_height"] - 100]
        self.roi_vendor = config.ui_roi["vendor_stash"]
        self.rx, self.ry, _, _ = self.roi_vendor
        self.sb_x, self.sb_y = self._screen.convert_screen_to_monitor((180, 77))
        self.c_x, self.c_y = self._screen.convert_screen_to_monitor((config.ui_pos["center_x"], config.ui_pos["center_y"]))
        self.claws_evaluated = 0
        self.claws_bought = 0

    def run(self):
        Logger.info("Personal Anya Shopper at your service! Hang on, running some errands...")
        self.reset_shop()
        self.shop_loop()

    def shop_loop(self):

        asset_folder = "assets/shop/gloves/"

        while True:

            self._npc_manager.open_npc_menu(Npc.ANYA)
            self._npc_manager.press_npc_btn(Npc.ANYA, "trade")
            time.sleep(0.1)
            img = self._screen.grab()

            # 20 IAS gloves have a unique color so we can skip all others
            ias_glove = self._template_finder.search(
                ref=load_template(asset_folder + "ias_gloves.png", 1.0),
                inp_img=img,
                threshold=0.96,
                roi=self._config.ui_roi["vendor_stash"],
                normalize_monitor=True,
            )
            if ias_glove.valid:
                self.ias_gloves_seen += 1
                mouse.move(*ias_glove.position)
                time.sleep(0.1)
                img = self._screen.grab()

                if self.look_for_plus_3_gloves is True:
                    gg_gloves = self._template_finder.search(
                        ref=load_template(
                            asset_folder + "gg_gloves.png", 1.0 # assets for javazon gloves are mixed up, this one need +3 as in the 1080p version
                        ),
                        inp_img=img,
                        threshold=0.80,
                        normalize_monitor=True,
                    )
                    if gg_gloves.valid:
                        mouse.click(button="right")
                        send_discord(f"Bought awesome IAS/+3 gloves!", self._config.general["custom_discord_hook"])
                        Logger.info("IAS/+3 gloves bought!")
                        self.gloves_bought += 1
                        time.sleep(1)

                else:
                    if self.look_for_plus_2_gloves is True:
                        g_gloves = self._template_finder.search(
                            ref=load_template(
                                asset_folder + "g_gloves.png", 1.0
                            ),
                            inp_img=img,
                            threshold=0.80,
                            normalize_monitor=True,
                        )
                        if g_gloves.valid:
                            mouse.click(button="right")
                            send_discord(f"Bought some decent IAS/+2 gloves", self._config.general["custom_discord_hook"])
                            Logger.info("IAS/+2 gloves bought!")
                            self.gloves_bought += 1
                            time.sleep(1)

            # Select Weapons section
            if self.look_for_trap_claws is True or self.look_for_melee_claws is True:
                mouse.move(self.sb_x, self.sb_y, randomize=3, delay_factor=[0.6, 0.8])
                wait(0.05, 0.1)
                mouse.press(button="left")
                wait(0.3, 0.4)
                # Search for claws
                claw_pos = []
                img = self._screen.grab().copy()
                claw_keys = ["CLAW1", "CLAW2", "CLAW3"]
                for ck in claw_keys:
                    template_match = self._template_finder.search(ck, img, roi=self.roi_vendor)
                    if template_match.valid:
                        (y, x) = np.where(self._template_finder.last_res >= 0.6)
                        for (x, y) in zip(x, y):
                            new_pos = [x + self.rx + 16, y + self.ry + 50]
                            # check if pos already exists in claw_pos
                            exists_already = False
                            for pos in claw_pos:
                                dist = math.dist(new_pos, pos)
                                if dist < 10:
                                    exists_already = True
                            if not exists_already:
                                claw_pos.append(new_pos)
                # check out each claw
                for pos in claw_pos:
                    # cv2.circle(img, pos, 3, (0, 255, 0), 2)
                    x_m, y_m = self._screen.convert_screen_to_monitor(pos)
                    mouse.move(x_m, y_m, randomize=3, delay_factor=[0.5, 0.6])
                    wait(0.5, 0.6)
                    img_stats = self._screen.grab()
                    trap_score = 0
                    melee_score = 0
                    if self._template_finder.search("3_TO_TRAPS", img_stats, roi=self.roi_claw_stats, threshold=0.94).valid:
                        trap_score += 12
                    elif self._template_finder.search("TO_TRAPS", img_stats, roi=self.roi_claw_stats, threshold=0.9).valid:
                        trap_score += 8

                    if self._template_finder.search("2_TO_ASSA", img_stats, roi=self.roi_claw_stats, threshold=0.9).valid:
                        trap_score += 10
                        melee_score += 10
                    if self._template_finder.search("TO_VENOM", img_stats, roi=self.roi_claw_stats, threshold=0.9).valid:
                        melee_score += 6
                    if self._template_finder.search("TO_LIGHT", img_stats, roi=self.roi_claw_stats, threshold=0.9).valid:
                        trap_score += 6
                    if self._template_finder.search("TO_WB", img_stats, roi=self.roi_claw_stats, threshold=0.9).valid:
                        melee_score += 2
                    if self._template_finder.search("TO_DS", img_stats, roi=self.roi_claw_stats, threshold=0.9).valid:
                        trap_score += 4

                    self.claws_evaluated += 1

                    if trap_score > 11 and self.look_for_trap_claws is True:
                        # pick it up
                        mouse.click(button="right")
                        send_discord(f"Bought some terrific trap Claws (score: {trap_score})", self._config.general["custom_discord_hook"])
                        Logger.info(f"Trap Claws (score: {trap_score}) bought!")
                        self.claws_bought += 1
                        time.sleep(1)

                    if melee_score > 11 and self.look_for_melee_claws is True:
                        # pick it up
                        mouse.click(button="right")
                        send_discord(f"Bought some mad melee Claws (score: {melee_score})", self._config.general["custom_discord_hook"])
                        Logger.info(f"Melee Claws (score: {melee_score}) bought!")
                        self.claws_bought += 1
                        time.sleep(1)

            # Done with this shopping round
            self.reset_shop()
            self.run_count += 1

    def reset_shop(self):
        while 1:
            success = self.select_by_template("A5_RED_PORTAL")
            success &= wait_for_loading_screen(self._screen, 2)
            if success:
                break
            else:
                mouse.move(800, 450, randomize=50, delay_factor=[0.7, 0.7])
        time.sleep(3)
        while 1:
            success = self.select_by_template("A5_RED_PORTAL")
            success &= wait_for_loading_screen(self._screen, 2)
            if success:
                break
            else:
                mouse.move(800, 450, randomize=50, delay_factor=[0.7, 0.7])

    def select_by_template(self, template_type: str) -> bool:
        Logger.debug(f"Select {template_type}")
        template_match = self._template_finder.search_and_wait(template_type, time_out=10)
        if template_match.valid:
            x_m, y_m = self._screen.convert_screen_to_monitor(template_match.position)
            mouse.move(x_m, y_m)
            wait(0.1, 0.2)
            mouse.click(button="left")
            return True
        return False

def main():
    config = Config()
    print("Anya Shopper running, click resume key (Default F11) when close to Anya to start shopping")

    anyashopper = AnyaShopper(config)
    keyboard.add_hotkey(config.general["exit_key"], lambda: exit(run_obj=anyashopper))

    while True:
        if keyboard.is_pressed(config.general["resume_key"]):
            try:
                anyashopper.run()
            except Exception as e:
                Logger.logger.exception(e)
                exit(anyashopper)
            break
        if keyboard.is_pressed(config.general["graphic_debugger_key"]):
            screen = Screen(config.general["monitor"])
            tf = ExtendedTemplateFinder(screen)
            search_templates = ["CLAW1", "CLAW2", "CLAW3", "TO_TRAPS", "3_TO_TRAPS", "2_TO_ASSA", "TO_LIGHT", "TO_WB", "TO_DS", "TO_VENOM"]
            scores = {}
            while True:
                img = screen.grab()
                display_img = img.copy()
                for template_name in search_templates:
                    template_match = tf.search(template_name, img)
                    scores[template_name] = template_match.score
                    if template_match.valid:
                        pos = template_match.position
                        cv2.putText(display_img, str(template_name), pos, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA)
                        cv2.circle(display_img, pos, 7, (255, 0, 0), thickness=5)
                display_img = cv2.resize(display_img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)
                print(scores)
                cv2.imshow('test', display_img)
                cv2.waitKey(1)
        time.sleep(0.05)

if __name__ == "__main__":
    main()


text:
import abc

import numpy as np

class AbstractUDMIS(abc.ABC):
    @abc.abstractmethod
    def energy(self):
        """Returns the energy of the current Rydberg occupation configuration"""

    @abc.abstractmethod
    def energy_diff(self, *coords):
        """Returns the energy difference resulting from flipping the occupation at the given coordinates"""

    @abc.abstractmethod
    def rand_vertex(self):
        """Selects a site in the graph at random"""

    def mc_step(self, T):
        """Performs a full update of the Rydberg model using the Metropolis-Hastings algorithm"""
        current_energy = self.energy()
        for _ in range(self.num_vertices):
            vertex = self.rand_vertex()
            dE = self.energy_diff(vertex)

            if (dE < 0) or (np.random.rand() < np.exp(-dE / T)):
                current_energy += dE
                self.occupations[vertex] ^= 1
                # flips occupation 0 --> 1 OR 1 --> 0

        return current_energy