text:
import typing as ty

import warnings

import numba as nb
import numpy as np
from numba.core.errors import NumbaWarning
from PIL import Image, ImageDraw

from . import utility

warnings.simplefilter("ignore", category=NumbaWarning)

class Box:
    def __init__(
        self,
        min_box: ty.Union[ty.List[float], np.ndarray],
        max_box: ty.Union[ty.List[float], np.ndarray],
    ):
        self.box = np.array([min_box, max_box], dtype=np.float64)

    @classmethod
    def empty(cls):
        min_box = np.zeros(3)
        min_box[:] = utility.INF
        return Box(min_box, np.zeros(3))

    @classmethod
    def BoxForShapes(cls, shapes) -> "Box":
        if not len(shapes):
            return Box.empty()
        box = shapes[0].bounding_box()
        for shape in shapes:
            box = box.extend(shape.bounding_box())
        return box

    @property
    def min(self) -> np.ndarray:
        return self.box[0]

    @property
    def max(self) -> np.ndarray:
        return self.box[1]

    def anchor(self, anchor: np.ndarray) -> np.ndarray:
        return self.min + (self.size() * anchor)

    def center(self) -> np.ndarray:
        return self.anchor(np.array([0.5, 0.5, 0.5]))

    def size(self) -> np.ndarray:
        return self.max - self.min

    def contains(self, b: np.ndarray) -> bool:
        return all(self.min[i] <= b[i] <= self.max[i] for i in range(3))

    def extend(self, other):
        return Box(
            np.minimum(self.min, other.min), np.maximum(self.max, other.max)
        )

    def intersect(
        self, ray_origin: np.ndarray, ray_direction: np.ndarray
    ) -> ty.Tuple[float, float]:
        return Box._intersect(self.min, self.max, ray_origin, ray_direction)

    @staticmethod
    @nb.njit(
        "Tuple((float64, float64))(float64[:], float64[:], float64[:], float64[:])",
        cache=True,
    )
    def _intersect(
        min_box: np.ndarray,
        max_box: np.ndarray,
        ray_origin: np.ndarray,
        ray_direction: np.ndarray,
    ) -> ty.Tuple[float, float]:
        difference_min = (min_box - ray_origin) / ray_direction
        difference_max = (max_box - ray_origin) / ray_direction
        t1 = np.amax(np.minimum(difference_min, difference_max))
        t2 = np.amin(np.maximum(difference_min, difference_max))
        return t1, t2

    def partition(self, axis: int, point) -> ty.Tuple[bool, bool]:
        return self.min[axis] <= point, self.max[axis] >= point

    def __str__(self):
        return f"Box: {self.min} {self.max}"

    def __repr__(self):
        return f"Box: {self.min} {self.max}"

class Filter:
    def __init__(self):
        pass

    def filter(self, v: np.ndarray) -> ty.Tuple[ty.Optional[np.ndarray], bool]:
        return None, False

class ClipFilter(Filter):
    def __init__(self, matrix: np.ndarray, eye: np.ndarray, scene):
        super().__init__()
        self.matrix = matrix
        self.eye = eye
        self.scene = scene
        self.clip_box = Box([-1, -1, -1], [1, 1, 1])

    def filter(self, v: np.ndarray) -> ty.Tuple[np.ndarray, bool]:
        w = utility.matrix_mul_position_vector(self.matrix, v)
        if not self.scene.visible(self.eye, v):
            return w, False
        if not self.clip_box.contains(w):
            return w, False
        return w, True

class Path:
    def __init__(self, path: ty.Union[np.ndarray, ty.List[np.ndarray]] = None):
        self.path: ty.List[np.ndarray] = (
            [np.array(p) for p in path] if path is not None else []
        )

    def bounding_box(self) -> Box:
        box = Box(self.path[0], self.path[0])
        for point in self.path:
            box = box.extend(Box(point, point))
        return box

    def transform(self, matrix: np.ndarray):
        return [
            utility.matrix_mul_position_vector(matrix, v) for v in self.path
        ]

    def chop(self, step):
        result = []
        for i in range(len(self.path) - 1):
            a = self.path[i]
            b = self.path[i + 1]
            v = b - a
            length = utility.vector_length(v)
            if i == 0:
                result.append(a)
            d = step
            while d < length:
                result.append(a + (v * (d / length)))
                d += step
            result.append(b)
        return result

    def filter(self, clip_filter: Filter):
        result = []
        path = []
        for v in self.path:
            v, ok = clip_filter.filter(v)
            if ok:
                path.append(v)
            else:
                if len(path) > 1:
                    result.append(path)
                path = []
        if len(path) > 1:
            result.append(path)
        return result

    def simplify(self, threshold):
        path_length = len(self.path)
        if path_length < 3:
            return self.path
        a = self.path[0]
        b = self.path[path_length - 1]
        index = -1
        distance = 0.0
        for i in range(1, path_length - 1):
            d = utility.segment_distance(self.path[i], a, b)
            if d > distance:
                index = i
                distance = d

        if distance > threshold:
            r1 = Path(self.path[: index + 1]).simplify(threshold)
            r2 = Path(self.path[index:]).simplify(threshold)
            return r1[: len(r1) - 1] + r2
        else:
            return [a, b]

    def to_string(self) -> str:
        result = ""
        for v in self.path:
            result += f"{v[0]:.4f},{v[1]:.4f},{v[2]:.4f};"
        return result

    def to_svg(self, color="black") -> str:
        coords = []
        for v in self.path:
            coords.append(f"{v[0]},{v[1]}")
        points = " ".join(coords)
        return f'<polyline stroke="{color}" fill="none" points="{points}" />'

class Paths:
    def __init__(
        self,
        paths: ty.Union[
            np.ndarray,
            ty.List[
                ty.Union[
                    np.ndarray,
                    Path,
                    ty.List[np.ndarray],
                    ty.List[ty.List[float]],
                ]
            ],
        ] = None,
    ):
        if paths is None or not len(paths):
            self.paths = []
        else:
            self.paths = [Path(p) if type(p) != Path else p for p in paths]

    def bounding_box(self) -> Box:
        box = self.paths[0].bounding_box()
        for path in self.paths:
            box = box.extend(path.bounding_box())
        return box

    def transform(self, matrix: np.ndarray) -> "Paths":
        return Paths([path.transform(matrix) for path in self.paths])

    def chop(self, step) -> "Paths":
        return Paths([path.chop(step) for path in self.paths])

    def filter(self, f: Filter):
        result = []
        for path in self.paths:
            result.extend(path.filter(f))
        return Paths(result)

    def simplify(self, threshold):
        return Paths([path.simplify(threshold) for path in self.paths])

    def __str__(self):
        return "\n".join(path.to_string() for path in self.paths)

    def to_image(self, width, height, fill="black", linewidth=3):
        canvas = (int(width), int(height))
        im = Image.new("RGBA", canvas, (255, 255, 255, 255))
        draw = ImageDraw.Draw(im)
        for ps in self.paths:
            for i, v1 in enumerate(ps.path):
                if i >= len(ps.path) - 1:
                    break
                v2 = ps.path[i + 1]
                draw.line(
                    (v1[0], height - v1[1], v2[0], height - v2[1]),
                    fill=fill,
                    width=linewidth,
                )
        return im

    def write_to_png(
        self, file_path: str, width, height, fill="black", linewidth=3
    ):
        self.to_image(width, height, fill, linewidth).save(file_path)

    def to_svg(
        self, width, height, line_color="black", background_color=None
    ) -> str:
        if background_color is None:
            bg = ""
        else:
            bg = f' style="background-color:{background_color}"'

        lines = [
            f'<svg width="{width}" height="{height}"{bg} '
            f'version="1.1" baseProfile="full" '
            f'xmlns="http://www.w3.org/2000/svg">',
            f'<g transform="translate(0,{height}) scale(1,-1)">',
        ]
        lines += [path.to_svg(line_color) for path in self.paths]
        lines.append("</g></svg>")
        return "\n".join(lines)

    def write_to_svg(
        self,
        path: str,
        width,
        height,
        line_color="black",
        background_color=None,
    ):
        with open(path, "w") as file:
            file.write(self.to_svg(width, height, line_color, background_color))

    def write_to_txt(self, path: str):
        with open(path, "w") as file:
            file.write(str(self))

max_stars_repo_name: out-of-cheese-error/pyln

max_issues_repo_name: out-of-cheese-error/pyln

max_forks_repo_name: out-of-cheese-error/pyln

reason: import numpy,import numba,from numba


text:
"""
TFLite model base class
"""
from typing import Any, List

import numpy as np

try:
    import tflite_runtime.interpreter as tflite
except ModuleNotFoundError:
    import tensorflow.lite as tflite

class TFLiteModel:
    """TFLite model base class for managing multiple inputs/outputs

    Args:
        model_path (str): Path to .tflite model file
        **kwargs (Any): Additional keywords arguments for the TFLite Interpreter.
                        [https://www.tensorflow.org/api_docs/python/tf/lite/Interpreter]
    """

    def __init__(self, model_path: str, **kwargs: Any) -> None:

        self._interpreter: tflite.Interpreter = tflite.Interpreter(
            model_path=model_path, **kwargs
        )
        self._input_details = self._interpreter.get_input_details()
        self._output_details = self._interpreter.get_output_details()
        self._interpreter.allocate_tensors()

    def __call__(self, *args: Any) -> List[np.ndarray]:
        """Forward pass of the TFLite model

        Args:
            inputs (Any): inputs to the TFLite model

        Returns: outputs of TFLite model

        """

        for detail, arg in zip(self._input_details, args):
            self._interpreter.set_tensor(detail["index"], arg)

        self._interpreter.invoke()

        return [
            self._interpreter.get_tensor(tensor["index"])
            for tensor in self._output_details
        ]

    @property
    def input_details(self) -> List[Any]:
        """Property for accesing the TFLite model input_details

        Returns: Input details for the TFLite model

        """
        return self._input_details

    @property
    def output_details(self) -> List[Any]:
        """Property for accesing the TFLite model output_details

        Returns: Output details for the TFLite model

        """
        return self._output_details

    def resize(self, index: int, shape: List[int]) -> None:
        """Resize and allocate an input tensor

        Args:
            index: index of the input tensor to resize
            shape: new shape of the input tensor
        """

        self._interpreter.resize_tensor_input(index, shape, strict=True)
        self._interpreter.allocate_tensors()

ext: py

max_stars_repo_name: spokestack/spokestack-python

max_issues_repo_name: spokestack/spokestack-python

max_forks_repo_name: spokestack/spokestack-python

reason: import numpy


text:
import numpy as np

decay_rate = 0.97

total_reward = None
last_bandit = None
his_overall_record = None
total_steps = 2000

record_size = 7
bandits_record = None
record_index = None
my_hits_p_1 = None
expQ = 0

def new_bandit(step):
    global bandits_record, his_overall_record, last_bandit
    exploration_score = 1 / my_hits_p_1
    scores = bandits_record.sum(axis=0)  +  expQ * exploration_score
    winner = np.argmax(scores)
    return int(winner)

def agent(obs, conf):
    global bandits_record, his_overall_record
    global last_bandit, total_reward, my_hits_p_1, record_index
    if obs.step == 0:
        his_overall_record = np.zeros(conf.banditCount)
        total_reward = 0
        bandits_record = np.zeros([record_size, conf.banditCount])
        record_index = np.zeros(conf.banditCount,'int')
        my_hits_p_1 = np.ones(conf.banditCount)
        bandit = np.random.randint(conf.banditCount)
        last_bandit = bandit
        return bandit
    if obs.lastActions[0] == last_bandit:
        his_action = obs.lastActions[1]
    else:
        his_action = obs.lastActions[0]
    his_overall_record[his_action] += 1
    my_hits_p_1[last_bandit] += 1
    if obs.reward > total_reward:
        total_reward += 1
        bandits_record[record_index[last_bandit], last_bandit] = 1
    else:
        bandits_record[record_index[last_bandit], last_bandit] = -1
    record_index[last_bandit] = (record_index[last_bandit] + 1) % record_size
    bandit = new_bandit(obs.step)
    last_bandit = bandit
    return bandit

max_stars_repo_name: a-pedram/kaggle-mab

max_issues_repo_name: a-pedram/kaggle-mab

max_forks_repo_name: a-pedram/kaggle-mab


text:
import numpy as np
import pyautogui
import imutils
import cv2

i = 1
image = pyautogui.screenshot()
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
resizedimage = cv2.resize(image,(256,256))
cv2.imwrite("/home/labuser/DeepLearning/screenshots/"+str(i)+".png", resizedimage)
i = i + 1

max_stars_repo_name: adarshsehgal/DeepLearning

max_issues_repo_name: adarshsehgal/DeepLearning

max_forks_repo_name: adarshsehgal/DeepLearning


text:
import matplotlib.pyplot as plt
import os
import pandas as pd
import argparse
import numpy as np

def plotRewards(episode_rewards, label, alpha, gamma, plot_path):

    plt.figure()
    plt.suptitle(variant+" - "+environment)
    plt.title(r"$\alpha $ = "+alpha+", $\gamma$ = "+gamma)
    plt.plot(range(len(episode_rewards)),episode_rewards, '.-',label=label)
    plt.xlabel('Number of Episodes')
    plt.ylabel('Total reward')
    plt.legend()
    plt.savefig(plot_path+"/finalReward.png")

    plt.figure()
    plt.suptitle(variant+" - "+environment)
    z1=pd.Series(episode_rewards).rolling(50).mean()
    plt.title(r"$\alpha $ = "+alpha+", $\gamma$ = "+gamma+ ",  Best average reward: "+ str(np.max(z1)))
    plt.plot(z1,label=label)
    plt.xlabel('Number of Episodes')
    plt.ylabel('Average Rewards over past 50 episodes')
    plt.legend()

    plt.savefig(plot_path+"/finalcumulative.png")
    plt.show()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("path",help = "Folder Path to reward file")
    args = parser.parse_args()

    path = args.path

    directory = os.path.dirname(path)
    #Model data
    modeldata = os.path.split(directory)[1]
    parentdir = os.path.dirname(directory)
    #Variant
    variant = os.path.split(parentdir)[1]
    #Environment
    environment = os.path.dirname(parentdir)

    temp = modeldata.rsplit("_")
    if(variant == "Reinforce"):
        label = variant+"-"+temp[0]
        gamma = temp[1]
        alpha = temp[2]
    else:
        label = variant
        gamma = temp[7]
        alpha = temp[1]+", "+temp[3]

    score_history = np.load(path)
    plotRewards(score_history,label, alpha, gamma, directory)


text:
from routines import *
from zscore import zscore
from emp_null import ENN, FDR, three_classes_GMM_fit, Gamma_Gaussian_fit

from numpy.testing import Tester

test = Tester().test
bench = Tester().bench

max_stars_repo_name: yarikoptic/NiPy-OLD

max_issues_repo_name: yarikoptic/NiPy-OLD

max_forks_repo_name: yarikoptic/NiPy-OLD

reason: from numpy


text:
import os
import numpy as np
import pandas as pd
import json
import matplotlib.pyplot as plt

def stat_avgs(species, datafile):
	"""
	Computes average kurtosis and entropy for the list of species
	"""
	k = []
	H = []
	for s in species:
		# If there were not enough samples, the species may not have associated stats
		if s not in datafile:
			continue

		if not np.isnan(float(datafile[s]['kurtosis'])):
			k.append(float(datafile[s]['kurtosis']))
			H.append(float(datafile[s]['entropy']))

	k_avg = np.mean(k)
	H_avg = np.mean(H)

	# Compute standard deviation
	k_std = np.std(k)
	H_std = np.std(H)

	return k_avg, H_avg, k_std, H_std

if __name__ == "__main__":
	# Load in-out class data
	data = pd.read_csv('in_out_class.csv')
	birds = data[data['Biological Group']=='Aves']

	lab_birds = birds[birds['Annotator'].notnull()]

	# Extract ImageNet relationships of labelled birds
	rels = lab_birds[lab_birds['Relation to Imagenet']=='relative in imagenet']['Class']
	is_in = lab_birds[lab_birds['Relation to Imagenet']=='in imagenet']['Class']
	par = lab_birds[lab_birds['Relation to Imagenet']=='parent in imagenet']['Class']
	not_in = lab_birds[lab_birds['Relation to Imagenet']=='not in imagenet']['Class']

	# Load data with kurtosis and entropy values
	fname = os.getcwd() + '/plots_category_dist_alexnet/Aves_distros.json'
	with open(fname, 'r') as f:
		f = json.load(f)

		# Get average kurtosis and entropy for each of the 4 categories
		rels_k, rels_h, rels_k_std, rels_h_std = stat_avgs(rels, f)
		in_k, in_h, in_k_std, in_h_std = stat_avgs(is_in, f)
		par_k, par_h, par_k_std, par_h_std = stat_avgs(par, f)
		not_k, not_h, not_k_std, not_h_std = stat_avgs(not_in, f)

	# Organize data for plot
	xs = np.arange(0, 4.)
	labels = ['In ImageNet', 'Not In ImageNet', 'Parent In ImageNet', 'Relative In ImageNet']
	ks = [in_k, not_k, par_k, rels_k]
	Hs = [in_h, not_h, par_h, rels_h]
	ks_std = [in_k_std, not_k_std, par_k_std, rels_k_std]
	Hs_std = [in_h_std, not_h_std, par_h_std, rels_h_std]

	print(ks)
	# Plot results - kurtosis
	plt.figure()
	w = 0.2
	plt.bar(xs-w/4, ks, width=w, label='kurtosis', color='red')
	plt.errorbar(xs-w/4, ks, yerr=ks_std, linewidth=0, elinewidth=1, color='black', capsize=2)

	print(ks_std)
	plt.title('Aves Average Kurtosis by Category', fontsize=14)
	plt.xlabel('Label Categories', fontsize=12)
	plt.ylabel('Kurtosis Value', fontsize=12)

	ylocs, _ = plt.yticks()
	plt.hlines(ylocs, xmin=0, xmax=len(xs)-1, colors='lightgrey', linestyles='dashed', zorder=1, linewidth=0.5)
	plt.xticks(np.arange(len(labels)), labels, fontsize=9)
	plt.tight_layout()
	plt.show()

	# plot results - entropy
	plt.figure()
	w = 0.2
	plt.bar(xs-w/4, Hs, width=w, label='entropy', color='red')
	plt.errorbar(xs-w/4, Hs, yerr=Hs_std, linewidth=0, elinewidth=1, color='black', capsize=2)

	plt.title('Aves Average Entropy by Category', fontsize=14)
	plt.xlabel('Label Categories', fontsize=12)
	plt.ylabel('Entropy Value', fontsize=12)

	ylocs, _ = plt.yticks()
	plt.hlines(ylocs, xmin=0, xmax=len(xs)-1, colors='lightgrey', linestyles='dashed', zorder=1, linewidth=0.5)
	plt.xticks(np.arange(len(labels)), labels,  fontsize=9)
	plt.tight_layout()
	plt.show()

max_stars_repo_name: noameshed/novelty-detection

max_issues_repo_name: noameshed/novelty-detection

max_forks_repo_name: noameshed/novelty-detection


text:
"""
This baseline is a simple partioning followed by a routing problem.

It uses pure K-Means to partition the problem into K convex regions and them uses the ORTools solver
to solve each subinstance. It's similar to the method proposed by Ruhan et al. [1], but without the
balancing component.

Refs:

[1] R. He, W. Xu, J. Sun and B. Zu, "Balanced K-Means Algorithm for Partitioning Areas in Large-Scale
Vehicle Routing Problem," 2009 Third International Symposium on Intelligent Information Technology
Application, Shanghai, 2009, pp. 87-90, doi: 10.1109/IITA.2009.307. Available at
https://ieeexplore.ieee.org/abstract/document/5369502.
"""

import logging
from dataclasses import dataclass
from typing import Optional

import numpy as np
from sklearn.cluster import KMeans

from loggibud.v1.types import CVRPInstance, CVRPSolution
from loggibud.v1.baselines.shared.ortools import (
    solve as ortools_solve,
    ORToolsParams,
)

logger = logging.getLogger(__name__)

@dataclass
class KmeansPartitionORToolsParams:
    fixed_num_clusters: Optional[int] = None
    variable_num_clusters: Optional[int] = None
    seed: int = 0

    ortools_params: Optional[ORToolsParams] = None

    @classmethod
    def get_baseline(cls):
        return cls(
            variable_num_clusters=500,
            ortools_params=ORToolsParams(
                time_limit_ms=120_000,
                solution_limit=1_000,
            ),
        )

def solve(
    instance: CVRPInstance,
    params: Optional[KmeansPartitionORToolsParams] = None,
) -> Optional[CVRPSolution]:

    params = params or KmeansPartitionORToolsParams.get_baseline()

    num_deliveries = len(instance.deliveries)
    num_clusters = int(
        params.fixed_num_clusters
        or np.ceil(
            num_deliveries / (params.variable_num_clusters or num_deliveries)
        )
    )

    logger.info(f"Clustering instance into {num_clusters} subinstances")
    clustering = KMeans(num_clusters, random_state=params.seed)

    points = np.array(
        [[d.point.lng, d.point.lat] for d in instance.deliveries]
    )
    clusters = clustering.fit_predict(points)

    delivery_array = np.array(instance.deliveries)

    subsinstance_deliveries = [
        delivery_array[clusters == i] for i in range(num_clusters)
    ]

    subinstances = [
        CVRPInstance(
            name=instance.name,
            deliveries=subinstance.tolist(),
            origin=instance.origin,
            vehicle_capacity=instance.vehicle_capacity,
        )
        for subinstance in subsinstance_deliveries
    ]

    subsolutions = [
        ortools_solve(subinstance, params.ortools_params)
        for subinstance in subinstances
    ]

    return CVRPSolution(
        name=instance.name,
        vehicles=[v for sol in subsolutions for v in sol.vehicles],
    )

max_stars_repo_name: thiagopbueno/loggibud

max_issues_repo_name: thiagopbueno/loggibud

max_forks_repo_name: thiagopbueno/loggibud


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 25 12:00:00 2019

@author: divyanshvinayak
"""

import numpy as np
poly = list(map(float, input('Enter coefficients of polynomial in order of their degrees\n').split()))
print(np.roots(poly))


text:
from tensorboardX import SummaryWriter
from .singleton_model import SingletonModel
from .reservoir import reservoir
import torch
import random
import pprint
import colorful
import numpy as np

class SLabReservoir(SingletonModel):
    def __init__(self, config, writer: SummaryWriter):
        super().__init__(config, writer)
        self.rsvr_size = config['reservoir_size']
        self.replay_multiple = config['replay_multiple']
        self.cur_t = self.config['data_schedule'][0]['subsets'][0][1]  # First Task idx.
        # Set-Up reservoir.
        self.rsvr = reservoir[config['reservoir_name']](config)
        self.rsvr_name = config['reservoir_name']
        self.n = 0

        self.per_cat_grad_info = None

    def learn(self, x, y, t, step=None, cat_str_lst=None, split_cats_dict=None, data_obj=None):
        imgs, cats = x.to(self.device), y.to(self.device)

        # Replay reservoir
        if len(self.rsvr) > 0:
            sample_num = imgs.size(0)
            k = min(len(self.rsvr), sample_num * self.replay_multiple)
            # randomly sample k from the reservoir, and merge it.
            replay_dict = self.rsvr.sample(num=k, model=self.component, aux_info=self.per_cat_grad_info)

            if self.rsvr_name == 'random' or self.rsvr_name == 'prs_mlab':
                merged_imgs = torch.cat([imgs, replay_dict['imgs']], dim=0)
                merged_cats = torch.cat([cats, replay_dict['cats']], dim=0)
            elif 'prs' in self.rsvr_name:
                if self.config['batch_sampler'] == 'random' or self.config['batch_sampler'] == "hard_sampling":
                    merged_imgs = torch.cat([imgs, replay_dict['imgs']], dim=0)
                    merged_cats = torch.cat([cats, replay_dict['cats']], dim=0)
                else:
                    merged_imgs = torch.cat([imgs, replay_dict['imgs']], dim=0)
                    merged_cats = torch.cat([cats, replay_dict['cats']], dim=0)

        else:
            merged_imgs, merged_cats = imgs, cats

        nll = self.component.nll(merged_imgs, merged_cats, step=step)
        weight_decay = self.component.weight_decay_loss()
        self.component.zero_grad()

        # update.
        mean_loss = nll.mean() + self.config['weight_decay'] * weight_decay
        mean_loss.backward()
        self.component.clip_grad()
        self.component.optimizer.step()
        self.component.lr_scheduler.step()

        # Update reservoir
        if self.config['reservoir_size'] > 0:
            self.rsvr.update(imgs=imgs, cats=cats)

        if step % self.config['summary_step'] == 0:
            print('Task: {0} [step: {1}]\t'
                  'Loss {2}))'.format(t, step, mean_loss))

            # summary.write(self.writer, step)
            # self.writer.add_histogram('grad', grad, step)
            self.writer.add_scalar(
                'num_params', sum([p.numel() for p in self.parameters()]),
                step)
            self.writer.add_scalar(
                'train_nll', nll.mean(),
                step)

            train_cat_loss = dict()
            for c in merged_cats:
                c = c.item()
                if c not in train_cat_loss:
                    cat_loss_mean = nll[(merged_cats==c).nonzero().flatten()].mean()
                    train_cat_loss[c] = cat_loss_mean

            for cat, loss in train_cat_loss.items():
                self.writer.add_scalar(
                    'train_nll/{}_loss'.format(cat), loss, step
                )

            if self.config['reservoir_size'] == 0:
                return

            weight_norm = 0.
            for p in filter((lambda p: p.requires_grad), self.parameters()):
                weight_norm += torch.norm(p, p=2).item()
            # print('Weight Norm: {}'.format(weight_norm))
            self.writer.add_scalar(
                'train/norm_weight', weight_norm, step
            )
            self.component.train()

max_stars_repo_name: ggraffieti/PRS

max_issues_repo_name: ggraffieti/PRS

max_forks_repo_name: ggraffieti/PRS


text:
from dpa.attacker import GaussianAttacker
from dpa.purch_query import Model_Purch
from dpa.adult_query import Model_Adult
from dpa.query import Model

import numpy as np
import tensorflow as tf
import tensorflow.keras.datasets.mnist as mnist
from random import shuffle
from os import path
from tensorflow.keras.datasets.mnist import load_data
#from skimage.metrics import structural_similarity as ssim

class RepeatedAttack:

    def synthetic_attack(self, dimensions, epochs, upperbound, composed_delta, worst, sequential, iterations):
        adversary = GaussianAttacker()
        final_confs = []
        for i in range(iterations):
            a = (np.random.randint(10, size=(2,dimensions)))
            composed_epsilon = adversary.get_epsilon_for_confidence_bound(upperbound)
            compare = []
            for i in range(0,2):
                compare.append(np.repeat([a[i]], epochs, axis=0))
            sensitivity_synthetic = np.linalg.norm(a[0]-a[1])
            if worst:
                epsilon = composed_epsilon/epochs
                sigma = sensitivity_synthetic * adversary.get_noise_multiplier_for_seq_eps(composed_epsilon, composed_delta, epochs)
                magnitude = epsilon*(sigma)**2/sensitivity_synthetic- sensitivity_synthetic/2
                direction = (a[0]-a[1])/np.linalg.norm(a[0]-a[1])
                tails = (direction*magnitude)
                private = a[0]+tails
                compare.append(np.repeat([private], epochs, axis=0))
            else:
                if sequential:
                    sigma_part = adversary.get_noise_multiplier_for_seq_eps(composed_epsilon, composed_delta, epochs)
                else:
                    sigma_part = adversary.get_noise_multiplier_for_rdp_eps(composed_epsilon, composed_delta, epochs)
                sigma = sigma_part * sensitivity_synthetic
                comp = []
                for z in range(0, epochs):
                    private = []
                    for j in range(0,len(a[0])):
                        private.append(a[0][j] +np.random.normal(0, sigma))
                    comp.append(private)
                compare.append(comp)
            beliefs_synthetic, beliefs_synthetic_noprior = adversary.infer(compare[0], compare[1], compare[2], sigma)
            final_confs.append(beliefs_synthetic[0][-1])
        return final_confs

    def load_purch_train_test_data(self, orig, alt, filepath):
        orig = np.load(path.join(filepath, f"{orig}.npz"), allow_pickle=True)["lookup"]
        alt = np.load(path.join(filepath, f"{alt}.npz"), allow_pickle=True)["lookup"]

        output = [index for index, elem in enumerate(orig) if (elem != alt[index]).any()][0]
        print(output)

        all_data = np.load('../../../data/purch/shokri_purchases_100_classes.npz')
        X = all_data['x'][:80000]
        X = X.reshape((len(X), 600))
        Y = tf.keras.utils.to_categorical(all_data['y'][:80000])

        index = -1
        for i in range(len(X)):
            if (X[i]==alt[output]).all():
                index = i

        x_train_orig = X[:len(orig)]
        x_train_alt = np.copy(X[:len(orig)])
        x_train_alt[output] = X[index]
        y_train_orig = Y[:len(orig)]
        y_train_alt = np.copy(Y[:len(orig)])
        y_train_alt[output] = Y[index]
        x_test = X[60000:]
        y_test = Y[60000:]
        if index > 60000:
            x_test[index-60000] = X[59999]
            y_test[index-60000] = Y[59999]

        return x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test

    def load_adult_train_test_data(self, orig, alt, filepath):
        orig = np.load(path.join(filepath, f"{orig}.npz"), allow_pickle=True)["lookup"]
        alt = np.load(path.join(filepath, f"{alt}.npz"), allow_pickle=True)["lookup"]

        output = [index for index, elem in enumerate(orig) if (elem != alt[index]).any()][0]
        #print(output)

        all_data = np.load('../../../data/adult/adult_wo_na.npz')
        X = all_data['x']
        X = X.reshape((len(X), 104))
        Y = np.load('../../../data/adult/adult_wo_na_labels.npz')['y']

        index = -1
        for i in range(len(X)):
            if (X[i]==alt[output]).all():
                index = i

        # Train size from original paper 30162
        # Test size from original paper 15060
        x_train_orig = X[:len(orig)]
        x_train_alt = np.copy(X[:len(orig)])
        x_train_alt[output] = X[index]
        y_train_orig = Y[:len(orig)]
        y_train_alt = np.copy(Y[:len(orig)])
        y_train_alt[output] = Y[index]
        x_test = X[30162:]
        y_test = Y[30162:]

        return x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test

    def load_train_test_data(self, orig, alt, filepath):
        orig = np.load(path.join(filepath, f"{orig}.npz"), allow_pickle=True)["lookup"]
        alt = np.load(path.join(filepath, f"{alt}.npz"), allow_pickle=True)["lookup"]

        output = [index for index, elem in enumerate(orig) if (elem != alt[index]).any()][0]
        print(output)

        (x_train, y_train), (x_test, y_test) = load_data()
        X = np.concatenate((x_train, x_test))
        X = X.reshape((len(X), 28 * 28))
        y_train = tf.keras.utils.to_categorical(y_train)
        y_test = tf.keras.utils.to_categorical(y_test)

        x_train = (x_train[...,None] / 255)
        x_test = (x_test[...,None] / 255)

        index = -1
        for i in range(len(X)):
            if (X[i]==alt[output]).all():
                index = i

        x_train_orig = x_train[:len(orig)]
        x_train_alt = np.copy(x_train[:len(orig)])
        x_train_alt[output] = x_train[index]
        y_train_orig = y_train[:len(orig)]
        y_train_alt = np.copy(y_train[:len(orig)])
        y_train_alt[output] = y_train[index]
        if index > 60000:
            x_test[index-60000] = x_train[-1]
            y_test[index-60000] = y_train[-1]

        return x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test

    def load_unbounded_adult_train_test_data(self, orig, alt, filepath):
        orig = np.load(path.join(filepath, f"{orig}.npz"), allow_pickle=True)["lookup"]
        alt = np.load(path.join(filepath, f"{alt}.npz"), allow_pickle=True)["lookup"]
        all_data = np.load('../../../data/adult/adult_wo_na.npz')
        X = all_data['x']
        Y = np.load('../../../data/adult/adult_wo_na_labels.npz')['y']

        index = -1
        for i in range(len(X)):
            if not (X[i]==alt[i]).all():
                index = i
                break

        x_train_orig = X[:len(orig)]
        x_train_alt = np.copy(X[:len(orig)])
        x_train_alt = np.delete(x_train_alt, index,0)

        y_train_orig = Y[:len(orig)]
        y_train_alt = np.copy(Y[:len(orig)])
        y_train_alt = np.delete(y_train_alt, index,0)

        x_test = X[30162:]
        y_test = Y[30162:]

        return x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test

    def load_unbounded_purch_train_test_data(self, orig, alt, filepath):
        orig = np.load(path.join(filepath, f"{orig}.npz"), allow_pickle=True)["lookup"]
        alt = np.load(path.join(filepath, f"{alt}.npz"), allow_pickle=True)["lookup"]
        all_data = np.load('../../../data/purch/shokri_purchases_100_classes.npz')
        X = all_data['x'][:10000]
        Y = tf.keras.utils.to_categorical(all_data['y'][:10000])

        index = -1
        for i in range(len(X)):
            if not (X[i]==alt[i]).all():
                index = i
                break

        #if index > 8000:
        #    raise Exception('must change test data, adjust implementation')

        x_train_orig = X[:len(orig)]
        x_train_alt = np.copy(X[:len(orig)])
        x_train_alt = np.delete(x_train_alt, index,0)

        y_train_orig = Y[:len(orig)]
        y_train_alt = np.copy(Y[:len(orig)])
        y_train_alt = np.delete(y_train_alt, index,0)

        x_test = X[8000:]
        y_test = Y[8000:]

        return x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test

    def load_unbounded_train_test_data(self, orig, alt, filepath):
        orig = np.load(path.join(filepath, f"{orig}.npz"), allow_pickle=True)["lookup"]
        alt = np.load(path.join(filepath, f"{alt}.npz"), allow_pickle=True)["lookup"]

        (x_train, y_train), (x_test, y_test) = load_data()
        X = np.concatenate((x_train, x_test))
        X = X.reshape((len(X), 28 * 28))
        y_train = tf.keras.utils.to_categorical(y_train)
        y_test = tf.keras.utils.to_categorical(y_test)

        x_train = (x_train[...,None] / 255)
        x_test = (x_test[...,None] / 255)

        index = -1
        for i in range(len(orig)):
            if not (X[i]==alt[i]).all():
                index = i
                break

        x_train_orig = x_train[:len(orig)]
        x_train_alt = np.copy(x_train[:len(orig)])
        x_train_alt = np.delete(x_train_alt, index,0)

        y_train_orig = y_train[:len(orig)]
        y_train_alt = np.copy(y_train[:len(orig)])
        y_train_alt = np.delete(y_train_alt, index,0)

        return x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test

    def nn_attack_graph(self, epochs, composed_delta, rho, worst_case, local, sequential, iterations, learning_rate, l2_norm_clip, bounded, MNIST, ADULT, PURCH):
        adversary = GaussianAttacker()

        if bounded:
            print("Running bounded")
            if MNIST:
                n_train = 100
                x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test = self.load_train_test_data(orig="original_data_set_ssim_size_100_place_1", alt="alt_data_set_ssim_size_100_place_1", filepath="../../../data/mnist/ssim/")
            elif PURCH:
                n_train = 1000
                x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test = self.load_purch_train_test_data(orig="original_data_set_ham_size_100_place_1", alt="alt_data_set_ham_size_1000_place_1", filepath="../../../data/purch/pca/")
            elif ADULT:
                n_train = 1000
                x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test = self.load_adult_train_test_data(orig="original_data_set_adult_wo_na_size_1000_place_1", alt="alt_data_set_adult_wo_na_size_1000_place_1", filepath="../../../data/adult/")
        else:
            print("Running unbounded")
            if MNIST:
                n_train = 100
                x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test = self.load_unbounded_train_test_data(orig="unbounded_original_data_set_ssim_size_100", alt="unbounded_alt_data_set_ssim_size_100", filepath="../../../data/mnist/ssim/")
            elif PURCH:
                n_train = 1000
                x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test = self.load_unbounded_purch_train_test_data(orig="unbounded_original_data_set_ham_size_1000", alt="unbounded_alt_data_set_ham_size_1000", filepath="../../../data/purch/hamming/")
            elif ADULT:
                n_train = 1000
                x_train_orig, x_train_alt, y_train_orig, y_train_alt, x_test, y_test = self.load_unbounded_adult_train_test_data(orig="unbounded_original_adult_data_set_manh_size_1000", alt="unbounded_alt_adult_data_set_manh_size_1000", filepath="../../../data/adult/")

        composed_eps = adversary.get_epsilon_for_confidence_bound(rho)

        if worst_case:
            # sequential composition due, only valid if worst case noise enforced due to loose bounds
            noise_multiplier = adversary.get_noise_multiplier_for_seq_eps(composed_eps, composed_delta, epochs)
            expected_success_rate = adversary.expected_seq_success_rate(composed_eps, composed_delta, epochs)
        else:
            if sequential:
                noise_multiplier = adversary.get_noise_multiplier_for_seq_eps(composed_eps, composed_delta, epochs)
                expected_success_rate = adversary.expected_seq_success_rate(composed_eps, composed_delta, epochs)
            else:
                #rdp world
                noise_multiplier = adversary.get_noise_multiplier_for_rdp_eps(composed_eps, composed_delta, epochs)
                expected_success_rate = adversary.expected_rdp_success_rate(noise_multiplier,epochs)

        run_summary = {}

        print(f'Adversary confidence: {adversary.get_confidence_bound(composed_eps)}')
        print(f'Adversary success rate: {expected_success_rate}')

        for i in range(iterations):
            # fit the model
            if MNIST:
                print("Running MNIST")
                m = Model()
            elif PURCH:
                print("Running PURCHASE-100")
                m = Model_Purch()
            elif ADULT:
                print("Running Adult")
                m = Model_Adult()
            m.set_training_params(noise_multiplier=noise_multiplier,
                                learning_rate=learning_rate,
                                l2_norm_clip=l2_norm_clip,
                                epochs=epochs,
                                delta=composed_delta)

            results = m.train(train_set=(x_train_orig, y_train_orig),
                            alternative_set=(x_train_alt, y_train_alt),
                            test_set=(x_test, y_test),
                            local=local,
                            worst_case=worst_case,
                            bounded=bounded)

            original, alt, private, train_acc, test_acc = results
            sensitivities = [np.linalg.norm(np.subtract(origin, alter))
                            for (origin, alter) in zip(original, alt)]
            #print("sensitivities ", sensitivities)
            # sensitivities
            if local:
                sensitivities = [np.linalg.norm(np.subtract(origin, alter))
                            for (origin, alter) in zip(original, alt)]
            elif bounded:
                sensitivities = 2*l2_norm_clip
            else:
                sensitivities = l2_norm_clip
            print("sensitivities ", sensitivities)

            sigmas = np.multiply(noise_multiplier, sensitivities)
            biased, unbiased = adversary.infer(original, alt, private, sigmas)

            run_summary[f"run_{i}"] = {"biased_beliefs": biased, "unbiased_beliefs": unbiased, "sensitivites": sensitivities, "train_acc":train_acc, "test_acc":test_acc}
            #m.save('./experiments/local_sensitivity_mnist/model.h5')
            print("Belief iteration ", i, ": ", biased[0][-1])
            tf.keras.backend.clear_session()
        return run_summary


text:
import copy
from abc import abstractmethod
from typing import Optional, Sequence

import numpy as np
import torch
from torch.optim import Optimizer

from ...algos.torch.base import TorchImplBase
from ...algos.torch.utility import (
    ContinuousQFunctionMixin,
    DiscreteQFunctionMixin,
)
from ...augmentation import DrQPipeline
from ...gpu import Device
from ...models.builders import (
    create_continuous_q_function,
    create_discrete_q_function,
)
from ...models.encoders import EncoderFactory
from ...models.optimizers import OptimizerFactory
from ...models.q_functions import QFunctionFactory
from ...models.torch import (
    EnsembleContinuousQFunction,
    EnsembleDiscreteQFunction,
    EnsembleQFunction,
)
from ...preprocessing import ActionScaler, Scaler
from ...torch_utility import hard_sync, torch_api, train_api

class FQEBaseImpl(TorchImplBase):

    _learning_rate: float
    _optim_factory: OptimizerFactory
    _encoder_factory: EncoderFactory
    _q_func_factory: QFunctionFactory
    _gamma: float
    _n_critics: int
    _use_gpu: Optional[Device]
    _q_func: Optional[EnsembleQFunction]
    _targ_q_func: Optional[EnsembleQFunction]
    _optim: Optional[Optimizer]

    def __init__(
        self,
        observation_shape: Sequence[int],
        action_size: int,
        learning_rate: float,
        optim_factory: OptimizerFactory,
        encoder_factory: EncoderFactory,
        q_func_factory: QFunctionFactory,
        gamma: float,
        n_critics: int,
        use_gpu: Optional[Device],
        scaler: Optional[Scaler],
        action_scaler: Optional[ActionScaler],
    ):
        super().__init__(
            observation_shape, action_size, scaler, action_scaler, DrQPipeline()
        )
        self._learning_rate = learning_rate
        self._optim_factory = optim_factory
        self._encoder_factory = encoder_factory
        self._q_func_factory = q_func_factory
        self._gamma = gamma
        self._n_critics = n_critics
        self._use_gpu = use_gpu

        # initialized in build
        self._q_func = None
        self._targ_q_func = None
        self._optim = None

    def build(self) -> None:
        self._build_network()

        self._targ_q_func = copy.deepcopy(self._q_func)

        if self._use_gpu:
            self.to_gpu(self._use_gpu)
        else:
            self.to_cpu()

        self._build_optim()

    @abstractmethod
    def _build_network(self) -> None:
        pass

    def _build_optim(self) -> None:
        assert self._q_func is not None
        self._optim = self._optim_factory.create(
            self._q_func.parameters(), lr=self._learning_rate
        )

    @train_api
    @torch_api(
        scaler_targets=["obs_t", "obs_tpn"],
        action_scaler_targets=["act_t", "act_tpn"],
    )
    def update(
        self,
        obs_t: torch.Tensor,
        act_t: torch.Tensor,
        rew_tpn: torch.Tensor,
        act_tpn: torch.Tensor,
        obs_tpn: torch.Tensor,
        ter_tpn: torch.Tensor,
        n_steps: torch.Tensor,
    ) -> np.ndarray:
        assert self._optim is not None

        q_tpn = self.compute_target(obs_tpn, act_tpn)
        q_tpn *= 1.0 - ter_tpn
        loss = self._compute_loss(obs_t, act_t, rew_tpn, q_tpn, n_steps)

        self._optim.zero_grad()
        loss.backward()
        self._optim.step()

        return loss.cpu().detach().numpy()

    def _compute_loss(
        self,
        obs_t: torch.Tensor,
        act_t: torch.Tensor,
        rew_tpn: torch.Tensor,
        q_tpn: torch.Tensor,
        n_steps: torch.Tensor,
    ) -> torch.Tensor:
        assert self._q_func is not None
        return self._q_func.compute_error(
            obs_t, act_t, rew_tpn, q_tpn, self._gamma ** n_steps
        )

    def compute_target(
        self, x: torch.Tensor, action: torch.Tensor
    ) -> torch.Tensor:
        assert self._targ_q_func is not None
        with torch.no_grad():
            return self._targ_q_func.compute_target(x, action)

    def update_target(self) -> None:
        assert self._q_func is not None
        assert self._targ_q_func is not None
        hard_sync(self._targ_q_func, self._q_func)

    def save_policy(self, fname: str, as_onnx: bool) -> None:
        raise NotImplementedError

class FQEImpl(ContinuousQFunctionMixin, FQEBaseImpl):

    _q_func: Optional[EnsembleContinuousQFunction]
    _targ_q_func: Optional[EnsembleContinuousQFunction]

    def _build_network(self) -> None:
        self._q_func = create_continuous_q_function(
            self._observation_shape,
            self._action_size,
            self._encoder_factory,
            self._q_func_factory,
            n_ensembles=self._n_critics,
        )

class DiscreteFQEImpl(DiscreteQFunctionMixin, FQEBaseImpl):

    _q_func: Optional[EnsembleDiscreteQFunction]
    _targ_q_func: Optional[EnsembleDiscreteQFunction]

    def _build_network(self) -> None:
        self._q_func = create_discrete_q_function(
            self._observation_shape,
            self._action_size,
            self._encoder_factory,
            self._q_func_factory,
            n_ensembles=self._n_critics,
        )

    def _compute_loss(
        self,
        obs_t: torch.Tensor,
        act_t: torch.Tensor,
        rew_tpn: torch.Tensor,
        q_tpn: torch.Tensor,
        n_steps: torch.Tensor,
    ) -> torch.Tensor:
        return super()._compute_loss(
            obs_t, act_t.long(), rew_tpn, q_tpn, n_steps
        )

    def compute_target(
        self, x: torch.Tensor, action: torch.Tensor
    ) -> torch.Tensor:
        return super().compute_target(x, action.long())


text:
# Copyright (c) Microsoft. All rights reserved.

# Licensed under the MIT license. See LICENSE.md file in the project root
# for full license information.
# ==============================================================================

from __future__ import print_function
from builtins import str
import sys, os, time
import numpy as np
from easydict import EasyDict
from builtins import range
import copy, textwrap
from PIL import Image, ImageFont, ImageDraw
from PIL.ExifTags import TAGS

# this is important when deploying to headless server environment (non-GUI)
###################################################
import matplotlib
# force headless backend, or set 'backend' to 'Agg'
# in your ~/.matplotlib/matplotlibrc
matplotlib.use('Agg')

import matplotlib.pyplot
# force non-interactive mode, or set 'interactive' to False
# in your ~/.matplotlib/matplotlibrc
from matplotlib.pyplot import imsave
matplotlib.pyplot.ioff()
###################################################

import cntk
from cntk import input_variable, Axis
from utils.nms.nms_wrapper import apply_nms_to_single_image_results
from cntk_helpers import regress_rois
import cv2 # pip install opencv-python

available_font = "arial.ttf"
try:
    dummy = ImageFont.truetype(available_font, 16)
except:
    available_font = "FreeMono.ttf"

####################################
# Visualize results
####################################
def visualizeResultsFaster(imgPath, roiLabels, roiScores, roiRelCoords, padWidth, padHeight, classes,
                     nmsKeepIndices = None, boDrawNegativeRois = True, decisionThreshold = 0.0):
    # read and resize image
    imgWidth, imgHeight = imWidthHeight(imgPath)
    scale = 800.0 / max(imgWidth, imgHeight)
    imgHeight = int(imgHeight * scale)
    imgWidth = int(imgWidth * scale)
    if imgWidth > imgHeight:
        h_border = 0
        v_border = int((imgWidth - imgHeight)/2)
    else:
        h_border = int((imgHeight - imgWidth)/2)
        v_border = 0

    PAD_COLOR = [103, 116, 123] # [114, 114, 114]
    cv_img = cv2.imread(imgPath)
    rgb_img = cv2.cvtColor(cv_img,cv2.COLOR_BGR2RGB)
    resized = cv2.resize(rgb_img, (imgWidth, imgHeight), interpolation=cv2.INTER_NEAREST)
    imgDebug = cv2.copyMakeBorder(resized,v_border,v_border,h_border,h_border,cv2.BORDER_CONSTANT,value=PAD_COLOR)
    rect_scale = 800 / padWidth

    assert(len(roiLabels) == len(roiRelCoords))
    if roiScores:
        assert(len(roiLabels) == len(roiScores))
        minScore = min(roiScores)
        print("roiScores min: {}, max: {}, threshold: {}".format(minScore, max(roiScores), decisionThreshold))
        if minScore > decisionThreshold:
            decisionThreshold = minScore * 0.5
            print("reset decision threshold to: {}".format(decisionThreshold))

    # draw multiple times to avoid occlusions
    for iter in range(0,3):
        for roiIndex in range(len(roiRelCoords)):
            label = roiLabels[roiIndex]
            if roiScores:
                score = roiScores[roiIndex]
                if decisionThreshold and score < decisionThreshold:
                    label = 0

            # init drawing parameters
            thickness = 1
            if label == 0:
                color = (255, 0, 0)
            else:
                color = getColorsPalette()[label]

            rect = [(rect_scale * i) for i in roiRelCoords[roiIndex]]
            rect[0] = int(max(0, min(padWidth, rect[0])))
            rect[1] = int(max(0, min(padHeight, rect[1])))
            rect[2] = int(max(0, min(padWidth, rect[2])))
            rect[3] = int(max(0, min(padHeight, rect[3])))

            # draw in higher iterations only the detections
            if iter == 0 and boDrawNegativeRois:
                drawRectangles(imgDebug, [rect], color=color, thickness=thickness)
            elif iter==1 and label > 0:
                if not nmsKeepIndices or (roiIndex in nmsKeepIndices):
                    thickness = 4
                drawRectangles(imgDebug, [rect], color=color, thickness=thickness)
            elif iter == 2 and label > 0:
                if not nmsKeepIndices or (roiIndex in nmsKeepIndices):
                    font = ImageFont.truetype(available_font, 18)
                    text = classes[label]
                    if roiScores:
                        text += "(" + str(round(score, 2)) + ")"
                    imgDebug = drawText(imgDebug, (rect[0],rect[1]), text, color = (255,255,255), font = font, colorBackground=color)
    return imgDebug

def load_resize_and_pad(image_path, width, height, pad_value=114):
    if "@" in image_path:
        print("WARNING: zipped image archives are not supported for visualizing results.")
        exit(0)

    img = cv2.imread(image_path)
    img_width = len(img[0])
    img_height = len(img)
    scale_w = img_width > img_height
    target_w = width
    target_h = height

    if scale_w:
        target_h = int(np.round(img_height * float(width) / float(img_width)))
    else:
        target_w = int(np.round(img_width * float(height) / float(img_height)))

    resized = cv2.resize(img, (target_w, target_h), 0, 0, interpolation=cv2.INTER_NEAREST)

    top = int(max(0, np.round((height - target_h) / 2)))
    left = int(max(0, np.round((width - target_w) / 2)))
    bottom = height - top - target_h
    right = width - left - target_w
    resized_with_pad = cv2.copyMakeBorder(resized, top, bottom, left, right,
                                          cv2.BORDER_CONSTANT, value=[pad_value, pad_value, pad_value])

    # transpose(2,0,1) converts the image to the HWC format which CNTK accepts
    model_arg_rep = np.ascontiguousarray(np.array(resized_with_pad, dtype=np.float32).transpose(2, 0, 1))

    dims = (width, height, target_w, target_h, img_width, img_height)
    return resized_with_pad, model_arg_rep, dims

# Tests a Faster R-CNN model and plots images with detected boxes
def eval_and_plot_faster_rcnn(eval_model, num_images_to_plot, test_map_file, img_shape,
                              results_base_path, feature_node_name, classes,
                              drawUnregressedRois=False, drawNegativeRois=False,
                              nmsThreshold=0.5, nmsConfThreshold=0.0, bgrPlotThreshold = 0.8):
    # get image paths
    with open(test_map_file) as f:
        content = f.readlines()
    img_base_path = os.path.dirname(os.path.abspath(test_map_file))
    img_file_names = [os.path.join(img_base_path, x.split('\t')[1]) for x in content]

    # prepare model
    image_input = input_variable(img_shape, dynamic_axes=[Axis.default_batch_axis()], name=feature_node_name)
    dims_input = input_variable((1,6), dynamic_axes=[Axis.default_batch_axis()], name='dims_input')
    frcn_eval = eval_model(image_input, dims_input)

    #dims_input_const = cntk.constant([image_width, image_height, image_width, image_height, image_width, image_height], (1, 6))
    print("Plotting results from Faster R-CNN model for %s images." % num_images_to_plot)
    for i in range(0, num_images_to_plot):
        imgPath = img_file_names[i]

        # evaluate single image
        _, cntk_img_input, dims = load_resize_and_pad(imgPath, img_shape[2], img_shape[1])

        dims_input = np.array(dims, dtype=np.float32)
        dims_input.shape = (1,) + dims_input.shape
        output = frcn_eval.eval({frcn_eval.arguments[0]: [cntk_img_input], frcn_eval.arguments[1]: dims_input})

        out_dict = dict([(k.name, k) for k in output])
        out_cls_pred = output[out_dict['cls_pred']][0]
        out_rpn_rois = output[out_dict['rpn_rois']][0]
        out_bbox_regr = output[out_dict['bbox_regr']][0]

        labels = out_cls_pred.argmax(axis=1)
        scores = out_cls_pred.max(axis=1).tolist()

        if drawUnregressedRois:
            # plot results without final regression
            imgDebug = visualizeResultsFaster(imgPath, labels, scores, out_rpn_rois, img_shape[2], img_shape[1],
                                              classes, nmsKeepIndices=None, boDrawNegativeRois=drawNegativeRois,
                                              decisionThreshold=bgrPlotThreshold)
            imsave("{}/{}_{}".format(results_base_path, i, os.path.basename(imgPath)), imgDebug)

        # apply regression and nms to bbox coordinates
        regressed_rois = regress_rois(out_rpn_rois, out_bbox_regr, labels, dims)

        nmsKeepIndices = apply_nms_to_single_image_results(regressed_rois, labels, scores,
                                                    nms_threshold=nmsThreshold,
                                                    conf_threshold=nmsConfThreshold)

        img = visualizeResultsFaster(imgPath, labels, scores, regressed_rois, img_shape[2], img_shape[1],
                                     classes, nmsKeepIndices=nmsKeepIndices,
                                     boDrawNegativeRois=drawNegativeRois,
                                     decisionThreshold=bgrPlotThreshold)
        imsave("{}/{}_regr_{}".format(results_base_path, i, os.path.basename(imgPath)), img)

####################################
# helper library
####################################

def imread(imgPath, boThrowErrorIfExifRotationTagSet = True):
    if not os.path.exists(imgPath):
        print("ERROR: image path does not exist.")
        error

    rotation = rotationFromExifTag(imgPath)
    if boThrowErrorIfExifRotationTagSet and rotation != 0:
        print ("Error: exif roation tag set, image needs to be rotated by %d degrees." % rotation)
    img = cv2.imread(imgPath)
    if img is None:
        print ("ERROR: cannot load image " + imgPath)
        error
    if rotation != 0:
        img = imrotate(img, -90).copy()  # got this error occassionally without copy "TypeError: Layout of the output array img is incompatible with cv::Mat"
    return img

def rotationFromExifTag(imgPath):
    TAGSinverted = {v: k for k, v in TAGS.items()}
    orientationExifId = TAGSinverted['Orientation']
    try:
        imageExifTags = Image.open(imgPath)._getexif()
    except:
        imageExifTags = None

    # rotate the image if orientation exif tag is present
    rotation = 0
    if imageExifTags != None and orientationExifId != None and orientationExifId in imageExifTags:
        orientation = imageExifTags[orientationExifId]
        # print ("orientation = " + str(imageExifTags[orientationExifId]))
        if orientation == 1 or orientation == 0:
            rotation = 0 # no need to do anything
        elif orientation == 6:
            rotation = -90
        elif orientation == 8:
            rotation = 90
        else:
            print ("ERROR: orientation = " + str(orientation) + " not_supported!")
            error
    return rotation

def imwrite(img, imgPath):
    cv2.imwrite(imgPath, img)

def imresize(img, scale, interpolation = cv2.INTER_LINEAR):
    return cv2.resize(img, (0,0), fx=scale, fy=scale, interpolation=interpolation)

def imresizeMaxDim(img, maxDim, boUpscale = False, interpolation = cv2.INTER_LINEAR):
    scale = 1.0 * maxDim / max(img.shape[:2])
    if scale < 1  or boUpscale:
        img = imresize(img, scale, interpolation)
    else:
        scale = 1.0
    return img, scale

def imWidth(input):
    return imWidthHeight(input)[0]

def imHeight(input):
    return imWidthHeight(input)[1]

def imWidthHeight(input):
    width, height = Image.open(input).size #this does not load the full image
    return width,height

def imArrayWidth(input):
    return imArrayWidthHeight(input)[0]

def imArrayHeight(input):
    return imArrayWidthHeight(input)[1]

def imArrayWidthHeight(input):
    width =  input.shape[1]
    height = input.shape[0]
    return width,height

def imshow(img, waitDuration=0, maxDim = None, windowName = 'img'):
    if isinstance(img, str): #test if 'img' is a string
        img = cv2.imread(img)
    if maxDim is not None:
        scaleVal = 1.0 * maxDim / max(img.shape[:2])
        if scaleVal < 1:
            img = imresize(img, scaleVal)
    cv2.imshow(windowName, img)
    cv2.waitKey(waitDuration)

def drawRectangles(img, rects, color = (0, 255, 0), thickness = 2):
    for rect in rects:
        pt1 = tuple(ToIntegers(rect[0:2]))
        pt2 = tuple(ToIntegers(rect[2:]))
        try:
            cv2.rectangle(img, pt1, pt2, color, thickness)
        except:
            import pdb; pdb.set_trace()
            print("Unexpected error:", sys.exc_info()[0])

def drawCrossbar(img, pt):
    (x,y) = pt
    cv2.rectangle(img, (0, y), (x, y), (255, 255, 0), 1)
    cv2.rectangle(img, (x, 0), (x, y), (255, 255, 0), 1)
    cv2.rectangle(img, (img.shape[1],y), (x, y), (255, 255, 0), 1)
    cv2.rectangle(img, (x, img.shape[0]), (x, y), (255, 255, 0), 1)

def ptClip(pt, maxWidth, maxHeight):
    pt = list(pt)
    pt[0] = max(pt[0], 0)
    pt[1] = max(pt[1], 0)
    pt[0] = min(pt[0], maxWidth)
    pt[1] = min(pt[1], maxHeight)
    return pt

def drawText(img, pt, text, textWidth=None, color = (255,255,255), colorBackground = None, font = ImageFont.truetype(available_font, 16)):
    pilImg = imconvertCv2Pil(img)
    pilImg = pilDrawText(pilImg,  pt, text, textWidth, color, colorBackground, font)
    return imconvertPil2Cv(pilImg)

def pilDrawText(pilImg, pt, text, textWidth=None, color = (255,255,255), colorBackground = None, font = ImageFont.truetype(available_font, 16)):
    textY = pt[1]
    draw = ImageDraw.Draw(pilImg)
    if textWidth == None:
        lines = [text]
    else:
        lines = textwrap.wrap(text, width=textWidth)
    for line in lines:
        width, height = font.getsize(line)
        if colorBackground != None:
            draw.rectangle((pt[0], pt[1], pt[0] + width, pt[1] + height), fill=tuple(colorBackground[::-1]))
        draw.text(pt, line, fill = tuple(color), font = font)
        textY += height
    return pilImg

def getColorsPalette():
    colors = [[255,0,0], [0,255,0], [0,0,255], [255,255,0], [255,0,255]]
    for i in range(5):
        for dim in range(0,3):
            for s in (0.25, 0.5, 0.75):
                if colors[i][dim] != 0:
                    newColor = copy.deepcopy(colors[i])
                    newColor[dim] = int(round(newColor[dim] * s))
                    colors.append(newColor)
    return colors

def imconvertPil2Cv(pilImg):
    rgb = pilImg.convert('RGB')
    return np.array(rgb).copy()[:, :, ::-1]

def imconvertCv2Pil(img):
    cv2_im = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    return Image.fromarray(cv2_im)

def ToIntegers(list1D):
    return [int(float(x)) for x in list1D]

def getDictionary(keys, values, boConvertValueToInt = True):
    dictionary = {}
    for key,value in zip(keys, values):
        if (boConvertValueToInt):
            value = int(value)
        dictionary[key] = value
    return dictionary

class Bbox:
    MAX_VALID_DIM = 100000
    left = top = right = bottom = None

    def __init__(self, left, top, right, bottom):
        self.left   = int(round(float(left)))
        self.top    = int(round(float(top)))
        self.right  = int(round(float(right)))
        self.bottom = int(round(float(bottom)))
        self.standardize()

    def __str__(self):
        return ("Bbox object: left = {0}, top = {1}, right = {2}, bottom = {3}".format(self.left, self.top, self.right, self.bottom))

    def __repr__(self):
        return str(self)

    def rect(self):
        return [self.left, self.top, self.right, self.bottom]

    def max(self):
        return max([self.left, self.top, self.right, self.bottom])

    def min(self):
        return min([self.left, self.top, self.right, self.bottom])

    def width(self):
        width  = self.right - self.left + 1
        assert(width>=0)
        return width

    def height(self):
        height = self.bottom - self.top + 1
        assert(height>=0)
        return height

    def surfaceArea(self):
        return self.width() * self.height()

max_stars_repo_name: meghnalge/Azure-python-web-app

max_issues_repo_name: meghnalge/Azure-python-web-app

max_forks_repo_name: meghnalge/Azure-python-web-app


text:
from torch.utils.data import Dataset, DataLoader, RandomSampler, SequentialSampler
import torch
import albumentations as A
import numpy as np
from torch.utils.data import Dataset
import torch
import cv2
import pandas as pd
from tqdm import tqdm

def batch_to_device(batch,device):
    batch_dict = {key:batch[key].to(device) for key in batch}
    return batch_dict

tr_collate_fn = None
val_collate_fn = None

def fix_row(row):
    if len(str(row).split()) > 1:
        row = int(str(row).split()[0])
    return row

class CustomDataset(Dataset):

    def __init__(self, df, cfg, aug, mode='train', allowed_targets=None):

        self.cfg = cfg
        self.df = df.copy()

        if mode == 'val':
            self.df['landmark_id'] = self.df['landmarks'].apply(lambda x:fix_row(x))
            self.df['landmark_id'] = self.df['landmark_id'].fillna(-1)
            self.df['landmark_id'] = self.df['landmark_id'].astype(int)
            self.df['landmarks'].fillna('',inplace=True)

        self.landmark_id2class_id = dict(zip(cfg.landmark_id2class_id['landmark_id'].astype(int),
                                             cfg.landmark_id2class_id['class_id'].astype(int)))
        self.landmark_id2class_id[-1] = self.cfg.n_classes

        if mode == 'index':
            pass

        if mode == 'query':
            pass

        if mode in ['test','query','index']:
            self.df['target'] = 0
        else:
            self.df['target'] = self.df['landmark_id'].map(self.landmark_id2class_id)
            self.df['target'] = self.df['target'].fillna(-1).astype(int)
        if allowed_targets is not None:
            self.df = self.df[self.df['target'].isin(allowed_targets)]
        self.labels = self.df['target'].values

        self.image_ids = self.df['id'].values
        self.mode = mode
        self.aug = aug
        self.normalization = cfg.normalization
        tmp = np.sqrt(1 / np.sqrt(self.df['target'].value_counts().sort_index().values))
        self.margins = (tmp - tmp.min()) / (tmp.max() - tmp.min()) * cfg.arcface_m_x + cfg.arcface_m_y

        if mode == 'test':
            self.data_folder = cfg.test_data_folder
        elif mode == 'val':
            self.data_folder = cfg.val_data_folder
        elif mode == 'query':
            self.data_folder = cfg.query_data_folder
        elif mode == 'index':
            self.data_folder = cfg.index_data_folder
        else:
            self.data_folder = cfg.data_folder

    def __getitem__(self, idx):

        image_id = self.image_ids[idx]
        label = self.labels[idx]

        img = self.load_one(image_id)

        if self.aug:
            img = self.augment(img)

        img = img.astype(np.float32)
        if self.normalization:
            img = self.normalize_img(img)

        img = self.to_torch_tensor(img)
        feature_dict = {'input':img,
                       'target':torch.tensor(label).float(),
                        'image_idx':torch.tensor(idx)
                       }
        return feature_dict

    def __len__(self):
        return len(self.image_ids)

    def load_one(self, id_):
        id_ = str(id_)
        fp = f'{self.data_folder}{id_[0]}/{id_[1]}/{id_[2]}/{id_}{self.cfg.suffix}'
        try:
            img = cv2.imread(fp)[:,:,::-1]
        except:
            print("FAIL READING img", id_)
        return img

    def augment(self,img):
        img_aug = self.aug(image=img)['image']
        return img_aug

    def normalize_img(self,img):

        if self.normalization == 'channel':
            #print(img.shape)
            pixel_mean = img.mean((0,1))
            pixel_std = img.std((0,1)) + 1e-4
            img = (img - pixel_mean[None,None,:]) / pixel_std[None,None,:]
            img = img.clip(-20,20)

        elif self.normalization == 'image':
            img = (img - img.mean()) / (img.std() + 1e-4)
            img = img.clip(-20,20)

        elif self.normalization == 'simple':
            img = img/255

        elif self.normalization == 'inception':
            mean = np.array([0.5, 0.5 , 0.5], dtype=np.float32)
            std = np.array([0.5, 0.5 , 0.5], dtype=np.float32)
            img = img.astype(np.float32)
            img = img/255.
            img -= mean
            img *= np.reciprocal(std, dtype=np.float32)

        elif self.normalization == 'imagenet':
            mean = np.array([123.675, 116.28 , 103.53 ], dtype=np.float32)
            std = np.array([58.395   , 57.120, 57.375   ], dtype=np.float32)
            img = img.astype(np.float32)
            img -= mean
            img *= np.reciprocal(std, dtype=np.float32)

        elif self.normalization == 'min_max':
            img = img - np.min(img)
            img = img / np.max(img)
            return img

        else:
            pass

        return img

    def to_torch_tensor(self,img):
        return torch.from_numpy(img.transpose((2, 0, 1)))


text:
import numpy as np
import gurobipy as gp

# min 0.5 * ||A * x - b||_2^2 + mu * ||x||_{1,2}

def gl_gurobi(x0: np.ndarray, A: np.ndarray, b: np.ndarray, mu, opts):
    _, n = A.shape
    m, l = b.shape
    with gp.Env(empty=True) as env:
        env.setParam('OutputFlag', 0)
        env.start( )
        # Build model m here
        with gp.Model('Gurobi', env=env) as M:
            # The default lower bound is 0.
            x = M.addMVar(shape=(n, l), name='x', lb=-gp.GRB.INFINITY)
            t2 = M.addMVar(shape=(n,), name='t2')
            z = M.addMVar(shape=b.shape, name='z', lb=-gp.GRB.INFINITY)

            cost = mu * t2.sum( )
            for i in range(m):
                cost += z[ i, : ] @ z[ i, : ] * 0.5
            M.setObjective(cost)

            M.addConstrs(z[ :, i ] + b[ :, i ] == A @ x[ :, i ] for i in range(l))
            M.addConstrs(t2[ i ] @ t2[ i ] >= x[ i, : ] @ x[ i, : ] for i in range(n))

            M.optimize( )

            solve_time = M.Runtime
            fval = M.objVal
            num_iters = M.BarIterCount  # Number of barrier iterations performed
            rv = x.X

            out = {
                "fval": fval,
                "tt": solve_time
            }
            return rv, num_iters, out


text:
from turtle import st
from random import randint
from math import ceil
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
from numpy.core.records import array

LEVEL_SIZE = 28
GENERATED_LEVELS = 2
START_LEVEL = 1
DRAWIT= False

PIG_RANGE = [1,6]
NUMBER_OF_PIGS = 0

# blocks number and size
# Dictionary represents [RealWorld Size (width, height), actual size, block name key, material]

           # ICE            #STONE          #WOOD                       RealWorld Size
blocks = {'1':[0.22,0.22,1,1,1], '2':[0.22,0.22,1,1,2], '3':[0.22,0.22,1,1,3],    # [1,1]
          '5':[0.43,0.22,2,2,1], '6':[0.43,0.22,2,2,2], '7':[0.43,0.22,2,2,3],    # [2,1]
          '8':[0.22,0.43,2,3,1], '9':[0.22,0.43,2,3,2], '10':[0.22,0.43,2,3,3],   # [1,2]
          '11':[0.85,0.22,4,4,1], '12':[0.85,0.22,4,4,2], '13':[0.85,0.22,4,4,3], # [4,1]
          '14':[0.22,0.85,4,5,1], '15':[0.22,0.85,4,5,2], '16':[0.22,0.85,4,5,3], # [1,4]
          '17':[1.68,0.22,8,6,1], '18':[1.68,0.22,8,6,2], '19':[1.68,0.22,8,6,3], # [8,1]
          '20':[0.22,1.68,8,7,1], '21':[0.22,1.68,8,7,2], '22':[0.22,1.68,8,7,3], # [1,8]
          '23':[2.06,0.22,9,8,1], '24':[2.06,0.22,9,8,2], '25':[2.06,0.22,9,8,3], # [9,1]
          '26':[0.22,2.06,9,9,1], '27':[0.22,2.06,9,9,2], '28':[0.22,2.06,9,9,3]} # [1,9]

blocks_sizes = {'1': [1,1], '2': [2,1], '3': [1,2], '4': [4,1],
          '5': [1,4], '6':[8,1], '7':[1,8],
          '8': [9,1], '9':[1,9]}

blocks_strings = {"2ice": "11", "2stone": "22", "2wood": "33",
                 "4ice": "1111", "4stone": "2222", "4wood": "3333",
                 "8ice": "11111111", "8stone": "22222222", "8wood": "33333333",
                 "9ice": "111111111", "9stone": "222222222", "9wood": "333333333"}

pig_size = [0.47,0.45]

tiny_block_size = [0.22,0.22]
tiny_block_name = "SquareTiny"

# blocks number and name
# (blocks 3, 5, 7, 9) are their respective block names rotated 90 derees clockwise
block_names = {'1':"SquareTiny", '2':"RectTiny", '3':"RectTiny", '4':"RectSmall",
               '5':"RectSmall", '6':"RectMedium", '7':"RectMedium",
               '8':"RectBig", '9':"RectBig"}

# materials number and name
materials = {'1':"ice", '2':"stone", '3':"wood"}

bird_types_index = {'0':"BirdYellow", '1':"BirdBlue", '2':"BirdRed", '3':"BirdBlack", '4':"BirdWhite"}

absolute_ground_y = -3.5
absolute_ground_x = 2.0

data = []

def remove_values_from_list(the_list, val):
   return [value for value in the_list if value != val]

def clearSquareBracketsIfNeeded(line: str):
    return line.replace("[", "").replace("]","")

def clearLine(line: str):
    line = line.replace("\n", "")
    line = clearSquareBracketsIfNeeded(line)
    splittedLine = line.split(" ")
    splittedLine = remove_values_from_list(splittedLine, "")
    return splittedLine

def draw(twoDArray):
    # plot
    fig, ax = plt.subplots()

    ca = []
    ca.append([0,255,255,255])

    iceIncluded = False
    stoneIncluded = False
    woodIncluded = False
    pigIncluded = False

    for i in range(0, len(twoDArray)):
        for j in range(0, len(twoDArray[i])):
            if twoDArray[i][j] == 1 and iceIncluded == False :
                ca.append([1,0,228,255])
                iceIncluded = True
            elif twoDArray[i][j] == 2 and stoneIncluded == False:
                ca.append([2,144,148,151])
                stoneIncluded = True
            elif twoDArray[i][j] == 3 and woodIncluded == False:
                ca.append([3,214,137,16])
                woodIncluded = True
            elif twoDArray[i][j] == 4 and pigIncluded == False:
                ca.append([4,40,180,99])
                pigIncluded = True

    ca = np.array(ca)

    colors = ca[ca[:,0].argsort()][:,1:]/255.
    cmap = matplotlib.colors.ListedColormap(colors)

    ax.imshow(twoDArray, cmap)

    plt.show()

def readData():
    for j in range(START_LEVEL,GENERATED_LEVELS+1):
        FILE = open("generated_level_{size}x{size}_{number}.txt".format(size = LEVEL_SIZE, number = j), 'r')
        levelArray = []
        for line in FILE:
            if line == "\n":
                data.append(np.array(levelArray))
                levelArray = []
                continue
            else:
                levelRow = clearLine(line)
                charArrayLength = len(levelRow)
                castedLevelRow = []
                for i in range(0, charArrayLength):
                    if levelRow[i] == '1':  #Ice Material
                        castedLevelRow.append(1)
                    elif levelRow[i] == '2': #Stone Material
                        castedLevelRow.append(2)
                    elif levelRow[i] == '3': #Wood Material
                        castedLevelRow.append(3)
                    elif levelRow[i] == '4': #Pig
                        castedLevelRow.append(4)
                    else:
                        castedLevelRow.append(0)
                levelArray.append(castedLevelRow)
        if len(levelArray) != 0:
            data.append(np.array(levelArray))
        FILE.close()
    for i in range(len(data)):
        if data[i].size != LEVEL_SIZE * LEVEL_SIZE:
            print(i)

def checkFourDirection(level, i, j):
    if j-1 >= 0 and j-1 < LEVEL_SIZE:
        if level[i][j-1] != 0:
            return False
    if j+1 >= 0 and j+1 < LEVEL_SIZE:
        if level[i][j+1] != 0:
            return False
    if i-1 >= 0 and i-1 < LEVEL_SIZE:
        if level[i-1][j] != 0:
            return False
    if i+1 >= 0 and i+1 < LEVEL_SIZE:
        if level[i+1][j] != 0:
            return False
    return True

def levelIslandCleaner(level):
    for i in range(LEVEL_SIZE-1): #Cells at the bottom should not be cleared
        for j in range(LEVEL_SIZE):
            if level[i][j] != 0:
                if checkFourDirection(level, i, j):
                    level[i][j] = 0

def generateString(character, count):
    generated = ""
    for i in range(count):
        generated += character
    return generated

def findAll(sentence, word):
    index = sentence.find(word)
    while index != -1:
        yield index
        index = sentence.find(word, index+1)

def findStructures(level):
    levelText = []

    #Looking for horizontal structures
    for i in range(LEVEL_SIZE-1, -1, -1):
        levelSentence = ""
        for j in range(LEVEL_SIZE):
            levelSentence += str(level[i][j])

        ####### Horizontal RectBig #######
        hRectBigSize = blocks_sizes['8'][0]

        for is9ice in findAll(levelSentence, blocks_strings["9ice"]): #Horizontal Ice RectBig
            for j in range(is9ice, is9ice+hRectBigSize, 1):
                level[i][j] = 23
            levelSentence = levelSentence.replace(blocks_strings["9ice"], generateString('s', hRectBigSize), 1)

        for is9stone in findAll(levelSentence, blocks_strings["9stone"]): #Horizontal Stone RectBig
            for j in range(is9stone, is9stone+hRectBigSize, 1):
                level[i][j] = 24
            levelSentence = levelSentence.replace(blocks_strings["9stone"], generateString('u', hRectBigSize), 1)

        for is9wood in findAll(levelSentence, blocks_strings["9wood"]): #Horizontal Wood RectBig
            for j in range(is9wood, is9wood+hRectBigSize, 1):
                level[i][j] = 25
            levelSentence = levelSentence.replace(blocks_strings["9wood"], generateString('w', hRectBigSize), 1)

        ####### Horizontal RectMedium #######
        hRectMediumSize = blocks_sizes['6'][0]

        for is8ice in findAll(levelSentence, blocks_strings["8ice"]): #Horizontal Ice RectMedium
            for j in range(is8ice, is8ice+hRectMediumSize, 1):
                level[i][j] = 17
            levelSentence = levelSentence.replace(blocks_strings["8ice"], generateString('m', hRectMediumSize), 1)

        for is8stone in findAll(levelSentence, blocks_strings["8stone"]): #Horizontal Stone RectMedium
            for j in range(is8stone, is8stone+hRectMediumSize, 1):
                level[i][j] = 18
            levelSentence = levelSentence.replace(blocks_strings["8stone"], generateString('o', hRectMediumSize), 1)

        for is8wood in findAll(levelSentence, blocks_strings["8wood"]): #Horizontal Wood RectMedium
            for j in range(is8wood, is8wood+hRectMediumSize, 1):
                level[i][j] = 19
            levelSentence = levelSentence.replace(blocks_strings["8wood"], generateString('q', hRectMediumSize), 1)

        ####### Horizontal RectSmall #######
        hRectSmallSize = blocks_sizes['4'][0]

        for is4ice in findAll(levelSentence, blocks_strings["4ice"]): #Horizontal Ice RectSmall
            for j in range(is4ice, is4ice+hRectSmallSize, 1):
                level[i][j] = 11
            levelSentence = levelSentence.replace(blocks_strings["4ice"], generateString('g', hRectSmallSize), 1)

        for is4stone in findAll(levelSentence, blocks_strings["4stone"]): #Horizontal Stone RectSmall
            for j in range(is4stone, is4stone+hRectSmallSize, 1):
                level[i][j] = 12
            levelSentence = levelSentence.replace(blocks_strings["4stone"], generateString('i', hRectSmallSize), 1)

        for is4wood in findAll(levelSentence, blocks_strings["4wood"]): #Horizontal Wood RectSmall
            for j in range(is4wood, is4wood+hRectSmallSize, 1):
                level[i][j] = 13
            levelSentence = levelSentence.replace(blocks_strings["4wood"], generateString('k', hRectSmallSize), 1)

        ####### Horizontal RectTiny #######
        hRectTinySize = blocks_sizes['2'][0]

        for is2ice in findAll(levelSentence, blocks_strings["2ice"]): #Horizontal Ice RectSmall
            for j in range(is2ice, is2ice+hRectTinySize, 1):
                level[i][j] = 5
            levelSentence = levelSentence.replace(blocks_strings["2ice"], generateString('a', hRectTinySize), 1)

        for is2stone in findAll(levelSentence, blocks_strings["2stone"]): #Horizontal Stone RectSmall
            for j in range(is2stone, is2stone+hRectTinySize, 1):
                level[i][j] = 6
            levelSentence = levelSentence.replace(blocks_strings["2stone"], generateString('c', hRectTinySize), 1)

        for is2wood in findAll(levelSentence, blocks_strings["2wood"]): #Horizontal Wood RectSmall
            for j in range(is2wood, is2wood+hRectTinySize, 1):
                level[i][j] = 7
            levelSentence = levelSentence.replace(blocks_strings["2wood"], generateString('e', hRectTinySize), 1)

        levelText.insert(0, levelSentence)

    newLevelText = []
    #Looking for vertical structures
    for j in range(LEVEL_SIZE):
        levelSentence = ""
        for i in range(LEVEL_SIZE):
            levelSentence += levelText[i][j]

        ####### Vertical RectBig #######
        vRectBigSize = blocks_sizes['9'][1]

        for is9ice in findAll(levelSentence, blocks_strings["9ice"]): #Vertical Ice RectBig
            for i in range(is9ice, is9ice+vRectBigSize, 1):
                level[i][j] = 26
            levelSentence = levelSentence.replace(blocks_strings["9ice"], generateString('t', vRectBigSize), 1)

        for is9stone in findAll(levelSentence, blocks_strings["9stone"]): #Vertical Stone RectBig
            for i in range(is9stone, is9stone+vRectBigSize, 1):
                level[i][j] = 27
            levelSentence = levelSentence.replace(blocks_strings["9stone"], generateString('v', vRectBigSize), 1)

        for is9wood in findAll(levelSentence, blocks_strings["9wood"]): #Vertical Wood RectBig
            for i in range(is9wood, is9wood+vRectBigSize, 1):
                level[i][j] = 28
            levelSentence = levelSentence.replace(blocks_strings["9wood"], generateString('x', vRectBigSize), 1)

        ####### Vertical RectMedium #######
        vRectMediumSize = blocks_sizes['7'][1]

        for is8ice in findAll(levelSentence, blocks_strings["8ice"]): #Vertical Ice RectMedium
            for i in range(is8ice, is8ice+vRectMediumSize, 1):
                level[i][j] = 20
            levelSentence = levelSentence.replace(blocks_strings["8ice"], generateString('n', vRectMediumSize), 1)

        for is8stone in findAll(levelSentence, blocks_strings["8stone"]): #Vertical Stone RectMedium
            for i in range(is8stone, is8stone+vRectMediumSize, 1):
                level[i][j] = 21
            levelSentence = levelSentence.replace(blocks_strings["8stone"], generateString('p', vRectMediumSize), 1)

        for is8wood in findAll(levelSentence, blocks_strings["8wood"]): #Vertical Wood RectMedium
            for i in range(is8wood, is8wood+vRectMediumSize, 1):
                level[i][j] = 22
            levelSentence = levelSentence.replace(blocks_strings["8wood"], generateString('r', vRectMediumSize), 1)

        ####### Vertical RectSmall #######
        vRectSmallSize = blocks_sizes['5'][1]

        for is4ice in findAll(levelSentence, blocks_strings["4ice"]): #Vertical Ice RectSmall
            for i in range(is4ice, is4ice+vRectSmallSize, 1):
                level[i][j] = 14
            levelSentence = levelSentence.replace(blocks_strings["4ice"], generateString('h', vRectSmallSize), 1)

        for is4stone in findAll(levelSentence, blocks_strings["4stone"]): #Vertical Stone RectSmall
            for i in range(is4stone, is4stone+vRectSmallSize, 1):
                level[i][j] = 15
            levelSentence = levelSentence.replace(blocks_strings["4stone"], generateString('j', vRectSmallSize), 1)

        for is4wood in findAll(levelSentence, blocks_strings["4wood"]): #Vertical Wood RectSmall
            for i in range(is4wood, is4wood+vRectSmallSize, 1):
                level[i][j] = 16
            levelSentence = levelSentence.replace(blocks_strings["4wood"], generateString('l', vRectSmallSize), 1)

        ####### Vertical RectTiny #######
        vRectTinySize = blocks_sizes['3'][1]

        for is2ice in findAll(levelSentence, blocks_strings["2ice"]): #Vertical Ice RectSmall
            for i in range(is2ice, is2ice+vRectTinySize, 1):
                level[i][j] = 8
            levelSentence = levelSentence.replace(blocks_strings["2ice"], generateString('b', vRectTinySize), 1)

        for is2stone in findAll(levelSentence, blocks_strings["2stone"]): #Vertical Stone RectSmall
            for i in range(is2stone, is2stone+vRectTinySize, 1):
                level[i][j] = 9
            levelSentence = levelSentence.replace(blocks_strings["2stone"], generateString('d', vRectTinySize), 1)

        for is2wood in findAll(levelSentence, blocks_strings["2wood"]): #Vertical Wood RectSmall
            for i in range(is2wood, is2wood+vRectTinySize, 1):
                level[i][j] = 10
            levelSentence = levelSentence.replace(blocks_strings["2wood"], generateString('f', vRectTinySize), 1)

        newLevelText.insert(0,levelSentence)

def findPigLocations(level):
    pig_locations = []
    for i in range(LEVEL_SIZE):
        for j in range(LEVEL_SIZE):
            if level[i][j] == 4:
                pig_locations.append([i, j])

    if len(pig_locations) != 0:
        return pig_locations
    else:
        #Control floor gaps between structures firstly
        NUMBER_OF_PIGS = randint(int(PIG_RANGE[0]),int(PIG_RANGE[1]))
        emptyIndex = 0
        while emptyIndex < LEVEL_SIZE-2:
            if level[LEVEL_SIZE-1][emptyIndex] == 0 and level[LEVEL_SIZE-2][emptyIndex] == 0 and level[LEVEL_SIZE-3][emptyIndex] == 0 and level[LEVEL_SIZE-1][emptyIndex+1] and level[LEVEL_SIZE-2][emptyIndex+1] == 0 and level[LEVEL_SIZE-3][emptyIndex+1] == 0 and level[LEVEL_SIZE-1][emptyIndex+2] and level[LEVEL_SIZE-2][emptyIndex+2] == 0 and level[LEVEL_SIZE-3][emptyIndex+2] == 0:
                if len(pig_locations) <= NUMBER_OF_PIGS:
                    pig_locations.append([LEVEL_SIZE-2, emptyIndex+1])
                    level[LEVEL_SIZE-2][emptyIndex+1] = 4
            emptyIndex += 1

        #Control pig sized gaps in structures or gaps on top of structure
        i = 3
        while i < LEVEL_SIZE - 1:
            j = 0
            while j < LEVEL_SIZE-2:
                if level[i][j+1] != 0:
                    if level[i-1][j] == 0 and level[i-1][j+1] == 0 and level[i-1][j+2] == 0 and level[i-2][j] == 0 and level[i-2][j+1] == 0 and level[i-2][j+2] == 0 and level[i-3][j] == 0 and level[i-3][j+1] == 0 and level[i-3][j+2] == 0:
                        if len(pig_locations) <= NUMBER_OF_PIGS:
                            pig_locations.append([i-2, j+1])
                            level[i-2][j+1] = 4
                j += 1
            i += 1
        return pig_locations

def chooseBirds(level, bird_count):

    iceCount = 0 #1
    stoneCount = 0 #2
    woodCount = 0 #3
    totalMaterial = 0

    for i in range(LEVEL_SIZE):
        for j in range(LEVEL_SIZE):
            cell = level[i][j]
            if cell != 0 and cell != 4:
                structure = blocks[str(cell)]
                material_key = structure[4]
                if material_key == 1:
                    iceCount += 1
                elif material_key == 2:
                    stoneCount += 1
                elif material_key == 3:
                    woodCount += 1

    totalMaterial = iceCount + stoneCount + woodCount

    iceRatio = iceCount / totalMaterial
    stoneRatio = stoneCount / totalMaterial
    woodRatio = woodCount / totalMaterial

    bird_list = []

    #bird_types_index = {'0':"BirdYellow", '1':"BirdBlue", '2':"BirdRed", '3':"BirdBlack", '4':"BirdWhite"}

    while len(bird_list) < bird_count:
        if iceRatio >= woodRatio and iceRatio >= stoneRatio:
            bird_list.append(1)
        elif stoneRatio >= 0.75:
            bird_list.append(3)
        elif woodRatio >= stoneRatio and woodRatio >= iceRatio:
            bird_list.append(0)
        else:
            bird_list.append(2)

        if len(bird_list) < bird_count - 1:
             bird_list.append(randint(0,2))

    return bird_list

def writeLevelXML(levels):
    XML_LEVEL_COUNTER = 1
    for index in range(len(levels)):
        f = open("level-{:02d}.xml".format(XML_LEVEL_COUNTER), "w")

        level = levels[index]
        levelIslandCleaner(level)
        findStructures(level)
        pig_locations = findPigLocations(level)

        bird_count =  ceil(len(pig_locations)/2)

        f.write('<?xml version="1.0" encoding="utf-16"?>\n')
        f.write('<Level width ="2">\n')
        f.write('<Camera x="0" y="2" minWidth="20" maxWidth="30">\n')
        f.write('<Birds>\n')

        bird_list = chooseBirds(level, bird_count)
        for bird in bird_list:
            f.write('<Bird type="%s"/>\n' % bird_types_index[str(bird)])

        f.write('</Birds>\n')
        f.write('<Slingshot x="-8" y="-2.5">\n')
        f.write('<GameObjects>\n')

        for i in range(LEVEL_SIZE-1, -1, -1):
            j = 0
            while j < LEVEL_SIZE:
                cell = level[i][j]
                if cell != 0 and cell != 4:
                    structure = blocks[str(cell)]
                    structure_size = structure[2]
                    block_name_key = structure[3]
                    material_key = structure[4]

                    if block_name_key in (3,5,7,9):
                        j += 1
                    else:
                        rotation = 0
                        x_location = absolute_ground_x + (structure_size * tiny_block_size[0])/2 + j * tiny_block_size[0]
                        y_location = absolute_ground_y + tiny_block_size[1]/2 + (LEVEL_SIZE-1-i) * tiny_block_size[1]

                        f.write('<Block type="%s" material="%s" x="%s" y="%s" rotation="%s" />\n' % (block_names[str(block_name_key)], materials[str(material_key)], str(x_location), str(y_location), str(rotation)))
                        j += structure_size
                else:
                    j += 1

        for j in range(LEVEL_SIZE):
            i = LEVEL_SIZE - 1
            while i >= 0:
                cell = level[i][j]
                if cell != 0 and cell != 4:
                    structure = blocks[str(cell)]
                    structure_size = structure[2]
                    block_name_key = structure[3]
                    material_key = structure[4]

                    if block_name_key in (3,5,7,9):
                        rotation = 90
                        x_location = absolute_ground_x + tiny_block_size[0]/2 + j * tiny_block_size[0]
                        y_location = absolute_ground_y + (structure_size * tiny_block_size[1])/2 + (LEVEL_SIZE-1-i) * tiny_block_size[1]

                        f.write('<Block type="%s" material="%s" x="%s" y="%s" rotation="%s" />\n' % (block_names[str(block_name_key)], materials[str(material_key)], str(x_location), str(y_location), str(rotation)))
                        i -= structure_size
                    else:
                        i -= 1
                else:
                    i -= 1

        for i in range(LEVEL_SIZE-1, -1, -1):
            for j in range(LEVEL_SIZE):
                if level[i][j] == 4:
                    pig_x_location = absolute_ground_x + tiny_block_size[0]/2 + j * tiny_block_size[0]
                    pig_y_locations = absolute_ground_y + tiny_block_size[1]/2 + (LEVEL_SIZE-1-i) * tiny_block_size[1]
                    f.write('<Pig type="BasicSmall" material="" x="%s" y="%s" rotation="0" />\n' % (str(pig_x_location),str(pig_y_locations)))

        f.write('</GameObjects>\n')
        f.write('</Level>\n')

        f.close()

        XML_LEVEL_COUNTER = XML_LEVEL_COUNTER + 1

readData()
levels = np.array(data)

if DRAWIT == True:
    for index in range(len(levels)):
        level = levels[index]
        draw(level)

writeLevelXML(levels)

max_stars_repo_name: yilmazburk/AngryBirdsLevelGeneration

max_issues_repo_name: yilmazburk/AngryBirdsLevelGeneration

max_forks_repo_name: yilmazburk/AngryBirdsLevelGeneration

reason: import numpy,from numpy


text:
import numpy as np
from numba import njit, jit

#NOTE be careful when using the Beam2D with the Truss2D because currently the
#     Truss2D is derived with only 2 DOFs per node, while the Beam2D is defined
#     with 3 DOFs per node
DOF = 2

class Truss2D(object):
    __slots__ = ['n1', 'n2', 'E', 'A', 'le', 'rho']
    def __init__(self):
        self.n1 = None
        self.n2 = None
        # Material Lastrobe Lescalloy
        self.E = None
        self.A = None
        self.le = None
        self.rho = None

@njit
def update_K_M(i, A, E, rho,
        pos1, pos2,
        ncoords, rowK, colK, valK, rowM, colM, valM,
        lumped=False):
    """Update a global stiffness matrix K and mass matrix M
    """
    x1, y1 = ncoords[pos1]
    x2, y2 = ncoords[pos2]
    theta = np.arctan2(y2-y1, x2-x1)
    le = ((x2 - x1)**2 + (y2 - y1)**2)**0.5
    c = np.cos(theta)
    s = np.sin(theta)

    # positions the global matrices
    c1 = DOF*pos1
    c2 = DOF*pos2

    rowK[16*i+0] = 0+c1
    colK[16*i+0] = 0+c1
    valK[16*i+0] = A*E*c**2/le
    rowK[16*i+1] = 0+c1
    colK[16*i+1] = 1+c1
    valK[16*i+1] = A*E*c*s/le
    rowK[16*i+2] = 0+c1
    colK[16*i+2] = 0+c2
    valK[16*i+2] = -A*E*c**2/le
    rowK[16*i+3] = 0+c1
    colK[16*i+3] = 1+c2
    valK[16*i+3] = -A*E*c*s/le
    rowK[16*i+4] = 1+c1
    colK[16*i+4] = 0+c1
    valK[16*i+4] = A*E*c*s/le
    rowK[16*i+5] = 1+c1
    colK[16*i+5] = 1+c1
    valK[16*i+5] = A*E*s**2/le
    rowK[16*i+6] = 1+c1
    colK[16*i+6] = 0+c2
    valK[16*i+6] = -A*E*c*s/le
    rowK[16*i+7] = 1+c1
    colK[16*i+7] = 1+c2
    valK[16*i+7] = -A*E*s**2/le
    rowK[16*i+8] = 0+c2
    colK[16*i+8] = 0+c1
    valK[16*i+8] = -A*E*c**2/le
    rowK[16*i+9] = 0+c2
    colK[16*i+9] = 1+c1
    valK[16*i+9] = -A*E*c*s/le
    rowK[16*i+10] = 0+c2
    colK[16*i+10] = 0+c2
    valK[16*i+10] = A*E*c**2/le
    rowK[16*i+11] = 0+c2
    colK[16*i+11] = 1+c2
    valK[16*i+11] = A*E*c*s/le
    rowK[16*i+12] = 1+c2
    colK[16*i+12] = 0+c1
    valK[16*i+12] = -A*E*c*s/le
    rowK[16*i+13] = 1+c2
    colK[16*i+13] = 1+c1
    valK[16*i+13] = -A*E*s**2/le
    rowK[16*i+14] = 1+c2
    colK[16*i+14] = 0+c2
    valK[16*i+14] = A*E*c*s/le
    rowK[16*i+15] = 1+c2
    colK[16*i+15] = 1+c2
    valK[16*i+15] = A*E*s**2/le

    if not lumped:
        rowM[8*i+0] = 0+c1
        colM[8*i+0] = 0+c1
        valM[8*i+0] = A*le*c**2*rho/3. + A*le*rho*s**2/3.
        rowM[8*i+1] = 0+c1
        colM[8*i+1] = 0+c2
        valM[8*i+1] = A*le*c**2*rho/6. + A*le*rho*s**2/6.
        rowM[8*i+2] = 1+c1
        colM[8*i+2] = 1+c1
        valM[8*i+2] = A*le*c**2*rho/3. + A*le*rho*s**2/3.
        rowM[8*i+3] = 1+c1
        colM[8*i+3] = 1+c2
        valM[8*i+3] = A*le*c**2*rho/6. + A*le*rho*s**2/6.
        rowM[8*i+4] = 0+c2
        colM[8*i+4] = 0+c1
        valM[8*i+4] = A*le*c**2*rho/6. + A*le*rho*s**2/6.
        rowM[8*i+5] = 0+c2
        colM[8*i+5] = 0+c2
        valM[8*i+5] = A*le*c**2*rho/3. + A*le*rho*s**2/3.
        rowM[8*i+6] = 1+c2
        colM[8*i+6] = 1+c1
        valM[8*i+6] = A*le*c**2*rho/6. + A*le*rho*s**2/6.
        rowM[8*i+7] = 1+c2
        colM[8*i+7] = 1+c2
        valM[8*i+7] = A*le*c**2*rho/3. + A*le*rho*s**2/3.

    if lumped:
        rowM[4*i+0] = 0+c1
        colM[4*i+0] = 0+c1
        valM[4*i+0] = A*le*c**2*rho/2. + A*le*rho*s**2/2.
        rowM[4*i+1] = 1+c1
        colM[4*i+1] = 1+c1
        valM[4*i+1] = A*le*c**2*rho/2. + A*le*rho*s**2/2.
        rowM[4*i+2] = 0+c2
        colM[4*i+2] = 0+c2
        valM[4*i+2] = A*le*c**2*rho/2. + A*le*rho*s**2/2.
        rowM[4*i+3] = 1+c2
        colM[4*i+3] = 1+c2
        valM[4*i+3] = A*le*c**2*rho/2. + A*le*rho*s**2/2.

reason: import numpy,from numba