text:
import decimal

import numpy as np
import torch
from torch import nn
from torch.autograd import Function

from ...utils import logging

logger = logging.get_logger(__name__)

class QuantEmbedding(qc.Module):
    def __init__(
        self,
        num_embeddings,
        embedding_dim,
        padding_idx=None,
        max_norm=None,
        norm_type=2.0,
        scale_grad_by_freq=False,
        sparse=False,
        _weight=None,
        weight_bit=8,
        momentum=0.95,
        quant_mode=False,
    ):
        super().__init__()
        self.num_ = num_embeddings
        self.dim = embedding_dim
        self.padding_idx = padding_idx
        self.max_norm = max_norm
        self.norm_type = norm_type
        self.scale_grad_by_freq = scale_grad_by_freq
        self.sparse = sparse

        self.weight = nn.Parameter(torch.zeros([num_embeddings, embedding_dim]))
        self.register_buffer("weight_scaling_factor", torch.zeros(1))
        self.register_buffer("weight_integer", torch.zeros_like(self.weight))

        self.weight_bit = weight_bit
        self.momentum = momentum
        self.quant_mode = quant_mode
        self.percentile_mode = False
        self.weight_function = SymmetricQuantFunction.apply

    def forward(self, x, positions=None, incremental_state=None):
        if not self.quant_mode:
            return (
                F.embedding(
                    x,
                    self.weight,
                    self.padding_idx,
                    self.max_norm,
                    self.norm_type,
                    self.scale_grad_by_freq,
                    self.sparse,
                ),
                None,
            )

        w = self.weight
        w_transform = w.data.detach()
        w_min = w_transform.min().expand(1)
        w_max = w_transform.max().expand(1)

        self.weight_scaling_factor = symmetric_linear_quantization_params(
            self.weight_bit, w_min, w_max, False
        )
        self.weight_integer = self.weight_function(
            self.weight, self.weight_bit, self.percentile_mode, self.weight_scaling_factor
        )

        emb_int = F.embedding(
            x,
            self.weight_integer,
            self.padding_idx,
            self.max_norm,
            self.norm_type,
            self.scale_grad_by_freq,
            self.sparse,
        )
        return emb_int * self.weight_scaling_factor, self.weight_scaling_factor

class QuantAct(qc.Module):
    def __init__(
        self,
        activation_bit,
        act_range_momentum=0.95,
        per_channel=False,
        channel_len=None,
        quant_mode=False,
    ):
        super().__init__()

        self.activation_bit = activation_bit
        self.act_range_momentum = act_range_momentum
        self.quant_mode = quant_mode
        self.per_channel = per_channel
        self.percentile = False
        self.act_function = SymmetricQuantFunction.apply

        if not self.per_channel:
            self.register_buffer("x_min", torch.zeros(1))
            self.register_buffer("x_max", torch.zeros(1))
            self.register_buffer("act_scaling_factor", torch.zeros(1))
            self.x_min -= 1e-5
            self.x_max += 1e-5
        else:
            raise NotImplementedError("per-channel mode is not currently supported for activation.")

    def __repr__(self):
        return (
            f"{self.__class__.__name__}(activation_bit={self.activation_bit}, "
            f"quant_mode: {self.activation_bit}, Act_min: {self.x_min.item():.2f}, "
            f"Act_max: {self.x_max.item():.2f})"
        )

    def forward(
        self,
        x,
        pre_act_scaling_factor=None,
        identity=None,
        identity_scaling_factor=None,
        specified_min=None,
        specified_max=None,
    ):

        x_act = x if identity is None else identity + x
        # collect running stats if training
        if self.training:
            assert not self.percentile, "percentile mode is not currently supported for activation."
            assert (
                not self.per_channel
            ), "per-channel mode is not currently supported for activation."
            x_min = x_act.data.min()
            x_max = x_act.data.max()

            assert (
                x_max.isnan().sum() == 0 and x_min.isnan().sum() == 0
            ), "NaN detected when computing min/max of the activation"

            # Initialization
            if self.x_min.min() > -1.1e-5 and self.x_max.max() < 1.1e-5:
                self.x_min = self.x_min + x_min
                self.x_max = self.x_max + x_max

            # exponential moving average (EMA)
            # use momentum to prevent the quantized values change greatly every iteration
            elif self.act_range_momentum == -1:
                self.x_min = torch.min(self.x_min, x_min)
                self.x_max = torch.max(self.x_max, x_max)
            else:
                self.x_min = self.x_min * self.act_range_momentum + x_min * (
                    1 - self.act_range_momentum
                )
                self.x_max = self.x_max * self.act_range_momentum + x_max * (
                    1 - self.act_range_momentum
                )

        if not self.quant_mode:
            return x_act, None

        x_min = self.x_min if specified_min is None else specified_min
        x_max = self.x_max if specified_max is None else specified_max

        self.act_scaling_factor = symmetric_linear_quantization_params(
            self.activation_bit, x_min, x_max, per_channel=self.per_channel
        )

        if pre_act_scaling_factor is None:
            # this is for the input quantization
            quant_act_int = self.act_function(
                x, self.activation_bit, self.percentile, self.act_scaling_factor
            )
        else:
            quant_act_int = FixedPointMul.apply(
                x,
                pre_act_scaling_factor,
                self.activation_bit,
                self.act_scaling_factor,
                identity,
                identity_scaling_factor,
            )

        correct_output_scale = self.act_scaling_factor.view(-1)

        return quant_act_int * correct_output_scale, self.act_scaling_factor

class QuantLinear(qc.Module):
    def __init__(
        self,
        in_features,
        out_features,
        bias=True,
        weight_bit=8,
        bias_bit=32,
        per_channel=False,
        quant_mode=False,
    ):
        super().__init__()
        self.in_features = in_features
        self.out_features = out_features

        self.weight = nn.Parameter(torch.zeros([out_features, in_features]))
        self.register_buffer("weight_integer", torch.zeros_like(self.weight))
        self.register_buffer("fc_scaling_factor", torch.zeros(self.out_features))
        if bias:
            self.bias = nn.Parameter(torch.zeros(out_features))
            self.register_buffer("bias_integer", torch.zeros_like(self.bias))

        self.weight_bit = weight_bit
        self.quant_mode = quant_mode
        self.per_channel = per_channel
        self.bias_bit = bias_bit
        self.quant_mode = quant_mode
        self.percentile_mode = False
        self.weight_function = SymmetricQuantFunction.apply

    def __repr__(self):
        s = super().__repr__()
        s = f"({s} weight_bit={self.weight_bit}, quant_mode={self.quant_mode})"
        return s

    def forward(self, x, prev_act_scaling_factor=None):
        if not self.quant_mode:
            return F.linear(x, weight=self.weight, bias=self.bias), None

        # assert that prev_act_scaling_factor is a scalar tensor
        assert prev_act_scaling_factor is not None and prev_act_scaling_factor.shape == (1,), (
            "Input activation to the QuantLinear layer should be globally (non-channel-wise) quantized. "
            "Please add a QuantAct layer with `per_channel = True` before this QuantAct layer"
        )

        w = self.weight
        w_transform = w.data.detach()
        if self.per_channel:
            w_min, _ = torch.min(w_transform, dim=1, out=None)
            w_max, _ = torch.max(w_transform, dim=1, out=None)
        else:
            w_min = w_transform.min().expand(1)
            w_max = w_transform.max().expand(1)

        self.fc_scaling_factor = symmetric_linear_quantization_params(
            self.weight_bit, w_min, w_max, self.per_channel
        )
        self.weight_integer = self.weight_function(
            self.weight, self.weight_bit, self.percentile_mode, self.fc_scaling_factor
        )

        bias_scaling_factor = self.fc_scaling_factor * prev_act_scaling_factor

        if self.bias is not None:
            self.bias_integer = self.weight_function(
                self.bias, self.bias_bit, False, bias_scaling_factor
            )

        prev_act_scaling_factor = prev_act_scaling_factor.view(1, -1)
        x_int = x / prev_act_scaling_factor

        return (
            F.linear(x_int, weight=self.weight_integer, bias=self.bias_integer)
            * bias_scaling_factor,
            bias_scaling_factor,
        )

class IntGELU(qc.Module):
    def __init__(self, quant_mode=True, force_dequant="none"):
        super().__init__()
        self.quant_mode = quant_mode

        if force_dequant in ["nonlinear", "gelu"]:
            logger.info("Force dequantize gelu")
            self.quant_mode = False

        if not self.quant_mode:
            self.activation_fn = nn.GELU()

        self.k = 1.4142
        self.const = 14  # dummy integer constant
        self.coeff = [-0.2888, -1.769, 1]  # a(x+b)**2 + c
        self.coeff[2] /= self.coeff[0]

    def int_erf(self, x_int, scaling_factor):
        b_int = torch.floor(self.coeff[1] / scaling_factor)
        c_int = torch.floor(self.coeff[2] / scaling_factor**2)
        sign = torch.sign(x_int)

        abs_int = torch.min(torch.abs(x_int), -b_int)
        y_int = sign * ((abs_int + b_int) ** 2 + c_int)
        scaling_factor = scaling_factor**2 * self.coeff[0]

        # avoid overflow
        y_int = floor_ste.apply(y_int / 2**self.const)
        scaling_factor = scaling_factor * 2**self.const

        return y_int, scaling_factor

    def forward(self, x, scaling_factor=None):
        if not self.quant_mode:
            return self.activation_fn(x), None

        x_int = x / scaling_factor
        sigmoid_int, sigmoid_scaling_factor = self.int_erf(x_int, scaling_factor / self.k)

        shift_int = 1.0 // sigmoid_scaling_factor

        x_int = x_int * (sigmoid_int + shift_int)
        scaling_factor = scaling_factor * sigmoid_scaling_factor / 2

        return x_int * scaling_factor, scaling_factor

class IntSoftmax(qc.Module):
    def __init__(self, output_bit, quant_mode=False, force_dequant="none"):
        super().__init__()
        self.output_bit = output_bit
        self.max_bit = 32
        self.quant_mode = quant_mode

        if force_dequant in ["nonlinear", "softmax"]:
            logger.info("Force dequantize softmax")
            self.quant_mode = False

        self.act = QuantAct(16, quant_mode=self.quant_mode)
        self.x0 = -0.6931  # -ln2
        self.const = 30  # dummy integer constant
        self.coef = [0.35815147, 0.96963238, 1.0]  # ax**2 + bx + c
        self.coef[1] /= self.coef[0]
        self.coef[2] /= self.coef[0]

    def int_polynomial(self, x_int, scaling_factor):
        with torch.no_grad():
            b_int = torch.floor(self.coef[1] / scaling_factor)
            c_int = torch.floor(self.coef[2] / scaling_factor**2)
        z = (x_int + b_int) * x_int + c_int
        scaling_factor = self.coef[0] * scaling_factor**2
        return z, scaling_factor

    def int_exp(self, x_int, scaling_factor):
        with torch.no_grad():
            x0_int = torch.floor(self.x0 / scaling_factor)
        x_int = torch.max(x_int, self.const * x0_int)

        q = floor_ste.apply(x_int / x0_int)
        r = x_int - x0_int * q
        exp_int, exp_scaling_factor = self.int_polynomial(r, scaling_factor)
        exp_int = torch.clamp(floor_ste.apply(exp_int * 2 ** (self.const - q)), min=0)
        scaling_factor = exp_scaling_factor / 2**self.const
        return exp_int, scaling_factor

    def forward(self, x, scaling_factor):
        if not self.quant_mode:
            return F.softmax(x, dim=-1), None

        x_int = x / scaling_factor

        x_int_max, _ = x_int.max(dim=-1, keepdim=True)
        x_int = x_int - x_int_max
        exp_int, exp_scaling_factor = self.int_exp(x_int, scaling_factor)

        # Avoid overflow
        exp, exp_scaling_factor = self.act(exp_int, exp_scaling_factor)
        exp_int = exp / exp_scaling_factor

        exp_int_sum = exp_int.sum(dim=-1, keepdim=True)
        factor = floor_ste.apply(2**self.max_bit / exp_int_sum)
        exp_int = floor_ste.apply(exp_int * factor / 2 ** (self.max_bit - self.output_bit))
        scaling_factor = 1 / 2**self.output_bit
        return exp_int * scaling_factor, scaling_factor

class IntLayerNorm(qc.Module):
    def __init__(self, normalized_shape, eps, output_bit=8, quant_mode=False, force_dequant="none"):
        super().__init__()
        self.normalized_shape = normalized_shape
        self.eps = eps

        self.weight = nn.Parameter(torch.zeros(normalized_shape))
        self.bias = nn.Parameter(torch.zeros(normalized_shape))

        self.quant_mode = quant_mode
        if force_dequant in ["nonlinear", "layernorm"]:
            logger.info("Force dequantize layernorm")
            self.quant_mode = False

        self.register_buffer("shift", torch.zeros(1))
        self.output_bit = output_bit
        self.max_bit = 32
        self.dim_sqrt = None
        self.activation = QuantAct(self.output_bit, quant_mode=self.quant_mode)

    def set_shift(self, y_int):
        with torch.no_grad():
            y_sq_int = y_int**2
            var_int = torch.sum(y_sq_int, axis=2, keepdim=True)
            shift = (torch.log2(torch.sqrt(var_int / 2**self.max_bit)).ceil()).max()
            shift_old = self.shift
            self.shift = torch.max(self.shift, shift)
            logger.info(f"Dynamic shift adjustment: {int(shift_old)} to {int(self.shift)}")

    def overflow_fallback(self, y_int):
        self.set_shift(y_int)  # adjusts `self.shift`
        y_int_shifted = floor_ste.apply(y_int / 2**self.shift)
        y_sq_int = y_int_shifted**2
        var_int = torch.sum(y_sq_int, axis=2, keepdim=True)
        return var_int

    def forward(self, x, scaling_factor=None):
        if not self.quant_mode:
            mean = x.mean(axis=2, keepdim=True)
            y = x - mean
            var = torch.mean(y**2, axis=2, keepdim=True)
            x = y / torch.sqrt(self.eps + var)
            x = x * self.weight + self.bias
            return x, None

        # compute sqrt of the feature dimension if it is the first run
        if self.dim_sqrt is None:
            n = torch.tensor(x.shape[2], dtype=torch.float)
            self.dim_sqrt = torch.sqrt(n).to(x.device)

        # Normalization: computes mean and variance(std)
        x_int = x / scaling_factor
        mean_int = round_ste.apply(x_int.mean(axis=2, keepdim=True))
        y_int = x_int - mean_int
        y_int_shifted = floor_ste.apply(y_int / 2**self.shift)
        y_sq_int = y_int_shifted**2
        var_int = torch.sum(y_sq_int, axis=2, keepdim=True)

        # overflow handling in training time
        if self.training:
            # if overflow is detected
            if var_int.max() >= 2**self.max_bit:
                var_int = self.overflow_fallback(y_int)
                assert var_int.max() < 2**self.max_bit + 0.1, (
                    "Error detected in overflow handling: "
                    "`var_int` exceeds `self.max_bit` (the maximum possible bit width)"
                )

        # To be replaced with integer-sqrt kernel that produces the same output
        std_int = floor_ste.apply(torch.sqrt(var_int)) * 2**self.shift
        factor = floor_ste.apply(2**31 / std_int)
        y_int = floor_ste.apply(y_int * factor / 2)
        scaling_factor = self.dim_sqrt / 2**30

        # scaling and shifting
        bias = self.bias.data.detach() / (self.weight.data.detach())
        bias_int = floor_ste.apply(bias / scaling_factor)

        y_int = y_int + bias_int
        scaling_factor = scaling_factor * self.weight
        x = y_int * scaling_factor

        return x, scaling_factor

def get_percentile_min_max(input, lower_percentile, upper_percentile, output_tensor=False):
    input_length = input.shape[0]

    lower_index = round(input_length * (1 - lower_percentile * 0.01))
    upper_index = round(input_length * upper_percentile * 0.01)

    upper_bound = torch.kthvalue(input, k=upper_index).values

    if lower_percentile == 0:
        lower_bound = upper_bound * 0
        # lower_index += 1
    else:
        lower_bound = -torch.kthvalue(-input, k=lower_index).values

    if not output_tensor:
        lower_bound = lower_bound.item()
        upper_bound = upper_bound.item()
    return lower_bound, upper_bound

def linear_quantize(input, scale, zero_point, inplace=False):
    if len(input.shape) == 4:
        scale = scale.view(-1, 1, 1, 1)
        zero_point = zero_point.view(-1, 1, 1, 1)
    # reshape scale and zeropoint for linear weights
    elif len(input.shape) == 2:
        scale = scale.view(-1, 1)
        zero_point = zero_point.view(-1, 1)
    else:
        scale = scale.view(-1)
        zero_point = zero_point.view(-1)
    # quantized = float / scale + zero_point
    if inplace:
        input.mul_(1.0 / scale).add_(zero_point).round_()
        return input
    return torch.round(1.0 / scale * input + zero_point)

def symmetric_linear_quantization_params(
    num_bits, saturation_min, saturation_max, per_channel=False
):
    with torch.no_grad():
        n = 2 ** (num_bits - 1) - 1

        if per_channel:
            scale, _ = torch.max(
                torch.stack([saturation_min.abs(), saturation_max.abs()], dim=1), dim=1
            )
            scale = torch.clamp(scale, min=1e-8) / n

        else:
            scale = max(saturation_min.abs(), saturation_max.abs())
            scale = torch.clamp(scale, min=1e-8) / n

    return scale

class SymmetricQuantFunction(Function):
    @staticmethod
    def forward(ctx, x, k, percentile_mode, scale):
        zero_point = torch.tensor(0.0).to(scale.device)

        n = 2 ** (k - 1) - 1
        new_quant_x = linear_quantize(x, scale, zero_point, inplace=False)
        new_quant_x = torch.clamp(new_quant_x, -n, n - 1)

        ctx.scale = scale
        return new_quant_x

    @staticmethod
    def backward(ctx, grad_output):

        scale = ctx.scale
        if len(grad_output.shape) == 4:
            scale = scale.view(-1, 1, 1, 1)
        # reshape scale and zeropoint for linear weights
        elif len(grad_output.shape) == 2:
            scale = scale.view(-1, 1)
        else:
            scale = scale.view(-1)

        return grad_output.clone() / scale, None, None, None, None

class floor_ste(Function):
    @staticmethod
    def forward(ctx, x):
        return torch.floor(x)

    @staticmethod
    def backward(ctx, grad_output):
        return grad_output.clone()

class round_ste(Function):
    @staticmethod
    def forward(ctx, x):
        return torch.round(x)

    @staticmethod
    def backward(ctx, grad_output):
        return grad_output.clone()

def batch_frexp(inputs, max_bit=31):
    shape_of_input = inputs.size()

    # trans the input to be a 1-d tensor
    inputs = inputs.view(-1)

    output_m, output_e = np.frexp(inputs.cpu().numpy())
    tmp_m = []
    for m in output_m:
        int_m_shifted = int(
            decimal.Decimal(m * (2**max_bit)).quantize(
                decimal.Decimal("1"), rounding=decimal.ROUND_HALF_UP
            )
        )
        tmp_m.append(int_m_shifted)
    output_m = np.array(tmp_m)

    output_e = float(max_bit) - output_e

    return (
        torch.from_numpy(output_m).to(inputs.device).view(shape_of_input),
        torch.from_numpy(output_e).to(inputs.device).view(shape_of_input),
    )

class FixedPointMul(Function):
    @staticmethod
    def forward(
        ctx,
        pre_act,
        pre_act_scaling_factor,
        bit_num,
        z_scaling_factor,
        identity=None,
        identity_scaling_factor=None,
    ):

        if len(pre_act_scaling_factor.shape) == 3:
            reshape = lambda x: x  # noqa: E731
        else:
            reshape = lambda x: x.view(1, 1, -1)  # noqa: E731
        ctx.identity = identity

        n = 2 ** (bit_num - 1) - 1

        with torch.no_grad():
            pre_act_scaling_factor = reshape(pre_act_scaling_factor)
            if identity is not None:
                identity_scaling_factor = reshape(identity_scaling_factor)

            ctx.z_scaling_factor = z_scaling_factor

            z_int = torch.round(pre_act / pre_act_scaling_factor)
            _A = pre_act_scaling_factor.type(torch.double)
            _B = (z_scaling_factor.type(torch.float)).type(torch.double)
            new_scale = _A / _B
            new_scale = reshape(new_scale)

            m, e = batch_frexp(new_scale)

            output = z_int.type(torch.double) * m.type(torch.double)
            output = torch.round(output / (2.0**e))

            if identity is not None:
                # needs addition of identity activation
                wx_int = torch.round(identity / identity_scaling_factor)

                _A = identity_scaling_factor.type(torch.double)
                _B = (z_scaling_factor.type(torch.float)).type(torch.double)
                new_scale = _A / _B
                new_scale = reshape(new_scale)

                m1, e1 = batch_frexp(new_scale)
                output1 = wx_int.type(torch.double) * m1.type(torch.double)
                output1 = torch.round(output1 / (2.0**e1))

                output = output1 + output

            return torch.clamp(output.type(torch.float), -n - 1, n)

    @staticmethod
    def backward(ctx, grad_output):
        identity_grad = None
        if ctx.identity is not None:
            identity_grad = grad_output.clone() / ctx.z_scaling_factor
        return (
            grad_output.clone() / ctx.z_scaling_factor,
            None,
            None,
            None,
            None,
            identity_grad,
            None,
        )


text:
from numpy import arange, meshgrid
from matplotlib import pyplot

#funzione obiettivo
def objective(x, y):
    '''funzione obiettivo z = x**2 + y**2 '''
    return x ** 2.0 + y ** 2.0

if __name__ == '__main__':
    #range dell'input
    r_min, r_max = -1.0, 1.0

    #campiono il range in modo uniforme
    xaxis = arange(r_min, r_max, 0.1)
    yaxis = arange(r_min, r_max, 0.1)

    #creo un mesh dall'asse
    x, y = meshgrid(xaxis, yaxis, sparse=False)
    print(x,'\n',y)

    #calcolo i target
    results = objective(x, y)

    #creo il plot della superficie, con il color scheme jet
    figure = pyplot.figure()
    axis = figure.gca(projection='3d')
    axis.plot_surface(x, y, results, cmap="jet")
    pyplot.show()

ext: py

reason: from numpy


text:
import numpy as np

def __normalize(data: np.ndarray) -> np.ndarray:
    totals = data.sum(axis=-1)
    totals = np.expand_dims(totals, axis=-1)
    return data.astype('float') / totals

def __unfairness_common(votes, seats):
    assert isinstance(votes, np.ndarray)
    assert isinstance(seats, np.ndarray)
    assert votes.shape == seats.shape
    votes = __normalize(votes)
    seats = __normalize(seats)
    return votes, seats

def loosemorehanby_index(votes: np.ndarray, seats: np.ndarray) -> np.ndarray:
    votes, seats = __unfairness_common(votes, seats)
    deltas = votes - seats
    return np.abs(deltas).sum(axis=-1) / 2.0

def gallagher_index(votes: np.ndarray, seats: np.ndarray) -> np.ndarray:
    votes, seats = __unfairness_common(votes, seats)
    deltas = votes - seats
    return np.sqrt((deltas * deltas).sum(axis=-1) * 0.5)

max_stars_repo_name: mfow/polisci

max_issues_repo_name: mfow/polisci

max_forks_repo_name: mfow/polisci

reason: import numpy


text:
import numpy as np
import os
import yaml
import torch
from utils.dataloader import load_data
from mod_shift.ModShiftModel_keops import ModShiftModel
from mod_shift.implemented_functions import w_clipped_linear, rho_clipped_linear

datasets = ["CREMI_A", "CREMI_B", "CREMI_C", "ISBI"]
repeats = 5
downsample = 4

# get weights as LazyTensor
def compute_weights_from_points(data):
    dists, _, _ = ModShiftModel.compute_distances(data)
    return w_clipped_linear(dists, clustering_config[dataset+'_ModShift']['betas'][0])

# get rhos as LazyTensor
def compute_rho_from_points(data):

    dists, _, _ = ModShiftModel.compute_distances(data)

    return rho_clipped_linear(dists, 2 * clustering_config[dataset+'_ModShift']['betas'][0])

# compute MC vectors  from labels using pykeops
def compute_mc_vector_from_seg(seg):
    seg = torch.tensor(seg, device="cuda")[:, None, ...]

    dists, _, _ = ModShiftModel.compute_distances(seg)

    # mc vector in matrix form, so every non-diagonal entry appears twice
    mc_vector = dists.sign()  # 0 for dist = 0, 1 for dist > 0
    return mc_vector

# compute energies
def mc_energy(mc_vector, weights):
   return 0.5*(mc_vector * weights).sum(1).sum(1)  # divide by 2 as every edge is considered twice

for dataset in datasets:
    print(f"{dataset} #############################################")
    # load data config
    with open(f"config_{dataset}.yml", "r") as config_file:
        data_config = yaml.load(config_file, Loader=yaml.FullLoader)

    # load weights config
    with open(f"config_{dataset}_best.yml", "r") as config_file:
        clustering_config = yaml.load(config_file, Loader=yaml.FullLoader)

    # set target_dir
    target_dir = os.path.join(data_config["root_path"], data_config["dataset"], "results")
    if data_config["dataset"] =="CREMI":
        target_dir = os.path.join(target_dir, data_config["set"])

    # load gt
    data = load_data(data_config)[:, ::downsample, ::downsample, :]
    data = torch.tensor(np.moveaxis(data, -1, 1), device="cuda", dtype=torch.float64)
    gt = load_data(data_config, gt = True)[:, ::downsample, ::downsample].astype(np.float64)

    # load labels
    # load Mod Shift shifted points and labels
    mod_shifted = []
    mod_labels = []
    for i in range(repeats):
        mod_labels.append(np.load(os.path.join(target_dir,
                                               f"mod_shift_labels_beta_{clustering_config[dataset+'_ModShift']['betas'][0]}_"+ \
                                               f"threshold_{clustering_config[dataset+'_ModShift']['thresholds'][0]}_"+ \
                                               f"run_{i}_downsample_{downsample}.npy")))
        mod_shifted.append(np.load(os.path.join(target_dir,
                                               f"mod_shift_trajectories_beta_{clustering_config[dataset+'_ModShift']['betas'][0]}_"+ \
                                               f"run_{i}_downsample_{downsample}.npy"))[-1])
    mod_labels = np.stack(mod_labels, axis=0).astype(np.float64)  # runs slices H W
    mod_shifted = np.stack(mod_shifted, axis=0).astype(np.float64)
    mod_shifted = torch.tensor(mod_shifted, device="cuda", dtype=torch.float64)

    # load KL labels
    kl_labels = np.load(os.path.join(target_dir, f"KL_complete_labels_{repeats}.npy")).astype(np.float64)

    # load MWS labels
    mws_labels = np.load(os.path.join(target_dir, f"MWS_complete_labels_{repeats}.npy")).astype(np.float64)

    # compute energies
    weights = compute_weights_from_points(data)
    initial_energy = mc_energy(compute_rho_from_points(data), weights).mean(0)
    gt_energy = mc_energy(compute_mc_vector_from_seg(gt), weights).mean(0)
    mod_shifted_energy = []
    mod_energy = []
    kl_energy = []
    mws_energy = []
    for i in range(repeats):
        mod_shifted_energy.append(mc_energy(compute_rho_from_points(mod_shifted[i]), weights))
        mod_energy.append(mc_energy(compute_mc_vector_from_seg(mod_labels[i]), weights))
        kl_energy.append(mc_energy(compute_mc_vector_from_seg(kl_labels[i]), weights))
        mws_energy.append(mc_energy(compute_mc_vector_from_seg(mws_labels[i]), weights))

    mod_shifted_energy = torch.stack(mod_shifted_energy, dim=0).mean(1)
    mod_energy = torch.stack(mod_energy, dim=0).mean(1)
    kl_energy = torch.stack(kl_energy, dim=0).mean(1)
    mws_energy = torch.stack(mws_energy, dim=0).mean(1)

    mean_mod_shifted_energy = mod_shifted_energy.mean(0)
    std_mod_shifted_energy = mod_shifted_energy.std(0)

    mean_mod_energy = mod_energy.mean(0)
    std_mod_energy = mod_energy.std(0)

    mean_kl_energy = kl_energy.mean(0)
    std_kl_energy = kl_energy.std(0)

    mean_mws_energy = mws_energy.mean(0)
    std_mws_energy = mws_energy.std(0)

    # compare energies
    min_energy = min(initial_energy, mean_mod_energy, mean_kl_energy, mean_mws_energy, gt_energy, mean_mod_shifted_energy)

    deviation_inital = (initial_energy - min_energy)
    deviation_gt = (gt_energy - min_energy)
    deviation_mod_shifted = (mean_mod_shifted_energy - min_energy)
    deviation_mod = (mean_mod_energy - min_energy)
    deviation_kl = (mean_kl_energy - min_energy)
    deviation_mws = (mean_mws_energy - min_energy)

    # print
    print(f"Initial energy: {float(initial_energy)}")
    print(f"deviation initial energy: {float(deviation_inital)}\n")

    print(f"GT energy: {float(gt_energy)}")
    print(f"deviation gt energy: {float(deviation_gt)}\n")

    print(f"Mean Mod energy: {float(mean_mod_energy)}")
    print(f"Std Mod energy: {float(std_mod_energy)}")
    print(f"deviation Mod energy: {float(deviation_mod)}\n")

    print(f"Mean Mod shifted energy: {float(mean_mod_shifted_energy)}")
    print(f"Std Mod shifted energy: {float(std_mod_shifted_energy)}")
    print(f"deviation Mod shifted energy: {float(deviation_mod_shifted)}\n")

    print(f"Mean KL energy: {float(mean_kl_energy)}")
    print(f"Std KL energy: {float(std_kl_energy)}")
    print(f"deviation KL energy: {float(deviation_kl)}\n")

    print(f"Mean MWS energy: {float(mean_mws_energy)}")
    print(f"Std MWS energy: {float(std_mws_energy)}")
    print(f"deviation MWS energy {float(deviation_mws)}\n")

max_stars_repo_name: ModShift/ModShift

max_issues_repo_name: ModShift/ModShift

max_forks_repo_name: ModShift/ModShift


text:
"""
The scripts in this file help improve the localization scores for a sequence.

The main function takes in a sequence, the target localization (cytoplasmic/
transmembrane/secreted), a target organism and a set of positions to mutate.

The function mutates each position for all possible amino acids and evaluates
the probability that the mutated sequence is localized at the target location.
Other functions help representing the results and getting a list of the
best mutations.
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as colors

from .check_inputs import check_input
from .predict import predict_loc_simple

amino_acids = [
    "G",
    "A",
    "L",
    "M",
    "F",
    "W",
    "K",
    "Q",
    "E",
    "S",
    "P",
    "V",
    "I",
    "C",
    "Y",
    "H",
    "R",
    "N",
    "D",
    "T",
]

def get_residue_positions(positions):
    """Converts a list of integers or ranges to individual residue numbers.

    This function is used to process the input string for the positions to
    mutate. It takes in a string of integers representing residue positions.
    This string must be a list of comma-separated integers. Ranges can also
    be defined using "-" between two integers.
    Note1: The ranges are INCLUSIVE.
    Note2: The positions in the sequence are 1-based (not zero-based), so
        the first residue of the protein is residue number 1.

    Args:
        positions: string with a list of protein residues.
            Example: "1,3-5,8,10-12"

    Returns:
        res_posit: list of individual residues.
            Example: [1,3,4,5,8,10,11,12]
    """

    # initialize list
    res_posit = np.array([])

    # preprocess string to get list of integers or ranges
    positions = positions.split(",")

    for item in positions:
        # split if element contains a "-" and is a range
        try:
            item = np.array(str(item).split("-"), dtype="int")
        # if it can't be converted to integer, raise ValueError
        except Exception as e:
            raise ValueError(
                'The position should be a list of integers or ranges '
                + 'defined as: "integer-integer", error ' + str(e)
            )

        if len(item) == 2:
            # if the element had a "-" the length will be 2 (initial-final pos)
            # create a range of integers from lower to higher value
            res_posit = np.append(res_posit, list(range(item[0], item[1] + 1)))
        elif len(item) == 1:
            # if it only had an integer, append this integer to the list
            res_posit = np.append(res_posit, item)

    # convert to numpy array and make sure all are integers
    # no need to raise ValueError because that was previously checked
    res_posit = np.array(np.unique(np.sort(res_posit)), dtype="int")

    return res_posit

def optimize_sequence(
    seq, organism, target_class, include_dg=False, positions=None
):
    """Predicts the effect on localization of mutations at selected positions.

    This function takes in an amino acid sequence, a target localization and
    organism, a True/False value on whether to include extra features (deltaG)
    and (optionally) a list of positions to mutate. All positions are
    considered if no list of positions is passed in.
    The function mutates all positions of the sequence and uses the ML model
    to predict the probability that the protein is localized at the target
    localization for the given organism.
    Note that the number of sequences tested increases rapidly with the number
    of positions, as 20 amino acids have to be tested for each position
    (20*num_positions). It only looks at single-site mutations, if not the
    amount of testing would increase even further (20^numb_positions).
    With the current implementation there is a limit at 10 total positions to
    mutate to avoid spending too much time computing the results. However,
    users could run the function in parallel to get results for all positions.

    Args:
        seq: string with the amino acid sequence
        organism: either human, yeast or ecoli
        target_class: either cytoplasm, membrane or secreted
        include_dg: whether to include delta G to the model (True/False)
        positions: a string indicating the positions to mutate e.g "1,3-5,8"

    Returns:
        mutated_scores: a pd.DataFrame with columns indicating the position and
            original amino acid and rows for all different amino acids tested
            at that position. Cells contain the probability of the mutated
            sequence of being localized at the target localization.
        initial_score: the probability of being secreted to the target
            localization for the original sequence
    """
    # First, check if there are any ambiguous amino acids or invalid symbols
    # and convert to appropiate format if it was provided in 3 letter code
    sequence = check_input(seq)

    # Then, find the predicted localization and initial score for this local.
    initial_score = predict_loc_simple(
        sequence, organism, target_class, include_dg
    )

    if positions is None:
        # if no position was specified, mutate all
        res_poses = np.array(list(range(0, len(sequence))))
    else:
        # if positions were specified, convert to list with previous function
        res_poses = get_residue_positions(positions)
        # convert positions to 0 based positions for ease of working in python
        res_poses = res_poses - 1

        # check if the largest position value is within the protein length
        if np.max(res_poses) + 1 > len(sequence):
            raise ValueError(
                "You passed residue position "
                + str(np.max(res_poses) + 1)
                + ", which is larger than the protein length of "
                + str(len(sequence))
                + " residues."
            )
        else:
            pass

    # converting to unirep can be slow for long sequences
    # since this function requires evaluating 20*(numb. positions) sequences
    # we will limit the number of positions to mutate to 10
    # Future developments to remove this limit could be:
    #    - use numba to speed up unirep (easy because it is numpy implemented)
    #    - estimate time it takes depending on protein lenght, inform the user
    #      and let the user decide if the process should be done or not
    if len(res_poses) > 10:
        raise ValueError(
            "The maximum number of residues to mutate is currently set at 10. "
            + "Going over this maximum would take too much time with the "
            + "current implementation, sorry."
        )

    mutated_scores = pd.DataFrame()

    # for each positon to mutate
    for resid in res_poses:
        mut_score_pos = []
        # generate all possible mutations
        for mutation in amino_acids:
            mutated_seq = (
                sequence[:resid] + str(mutation) + sequence[resid + 1:]
            )
            # evaluate each of them with the machine learning model
            pred_score = predict_loc_simple(
                mutated_seq, organism, target_class, include_dg
            )
            mut_score_pos.append(pred_score)

        mut_score_pos = pd.Series(mut_score_pos, index=amino_acids)

        # get the name for the mutated position
        # +1 to correct 0 based sequence handled by function internally
        original_name = str(sequence[resid]) + "_" + str(resid + 1)
        # keep results in data frame
        mutated_scores[original_name] = mut_score_pos

    return mutated_scores, initial_score

def plot_optimization(
    mutated_scores, initial_score, plot_inplace=True, dpi=100
):
    """Represents a heatmap indicating how mutations affect localization.

    This function takes in a pd.DataFrame and an intial value, substracts the
    initial value to each value in the pd.DataFrame and represents a heatmap
    showing the differences between the values in the pd.DataFrame and the
    initial value. Higher (positive) values are shown in red and lower
    (negative) values in blue. The color scale is symmetric and adjusted to the
    maximum absolute value of differences.

    In this context, the heatmap takes in the pd.DataFrame of scores for each
    mutation and the probability of being secreted at the target location for
    the initial sequence without mutations. The heatmap then represents how
    much each mutation at each position increases (red) or decreases (blue) the
    probability of the protein to be localized at the target localization.

    The size of the heatmap is adjusted depending on the number of positions,
    but it is only clear if the number of positions is below 40.

    Args:
        mutated_scores: a pd.DataFrame with the probability predicted by the
            model for each mutation (rows) at each position (columns).
        initial_score: a float representing the probability predicted by the
            model for the initial sequence.
        plot_inplace: use True (default) if using the function on a Jupyter
            Notebook, so that the plot is shown and not returned. If using
            False in a Jupyter Notebook the plot will be represented twice.
            The False option returns the plot so that it can be stored in a
            variable, which was useful for the web app.
        dpi: dots per inch value of the generated figure. Defaults to 100,
            use 300 for better resolution.

    Returns:
        heatmap representation of how each mutation improves or decreases the
            probability of being localized at the target localization.
    """
    # check if input is pd.DataFrame
    if not isinstance(mutated_scores, pd.DataFrame):
        raise TypeError(
            "The mutate_scores variable should be a pd.DataFrame object "
            + "but received a "+str(type(mutated_scores))
        )
    else:
        pass

    # check if all values are numeric by attempting to substract initial_score
    try:
        relative_sc = mutated_scores - initial_score
    except Exception as e:
        raise TypeError(
            "The values of the mutated scores dataframe and initial_score"
            + "variable should be numeric. But got error: "
            + str(e)
        )

    # check if dpi is an integer value
    try:
        dpi = int(dpi)
    except Exception as e:
        raise TypeError(
            "The value of dpi should be an integer, but got "
            + str(type(dpi))
            + " instead, so the error is:" + str(e)
        )

    # set width between 8 and 18 depending on number of positions
    # if the maximum is passed, the x axis labels will overlap
    width = np.minimum(np.maximum(int(8 * mutated_scores.shape[1] / 28), 8),
                       18)
    # to have a centered color bar (white=0) get maximum value in dataset
    max_sc = np.max(np.abs(relative_sc.values))

    # determine figure shape
    fig, ax = plt.subplots(1, 1, figsize=(width, 6), dpi=dpi)

    # create color representation according to the maximum value in the data
    pcm = ax.pcolormesh(
        mutated_scores.columns,
        mutated_scores.index,
        relative_sc.values,
        shading="nearest",
        norm=colors.Normalize(vmin=-max_sc, vmax=max_sc),
        cmap="bwr",
    )
    # insert colorbar
    cb = fig.colorbar(pcm, ax=ax, extend="both")
    # label
    cb.set_label("Change in probability for target class", fontsize=12)
    ax.set_xticklabels(mutated_scores.columns, rotation=90)

    # to try to remove warning about xticklabels
    #     ticks_loc = mutated_scores.columns.tolist()
    #     ax.xaxis.set_major_locator(mticker.FixedLocator(ticks_loc))
    #     ax.set_xticklabels([x for x in ticks_loc])
    ax.set_xlabel("Position and original amino acid", fontsize=12)
    ax.set_ylabel("Mutation", fontsize=12)

    # if on a notebook, plot and return nothing or it would be plot twice
    if plot_inplace:
        plt.show()
        return None
    # for other applications select plot_inplace=False and return the figure
    else:
        return fig

def top_mutations(mutated_scores, initial_score, top_results=10):
    """Generate list of n mutations that improve localization probability

    Takes in the pd.DataFrame of predictions for mutated sequences and the
    probability of the initial sequence. After substracting the initial value
    from the values of the mutations, it generates a list of the mutations
    that increase the probability that the protein is localized at the target
    localization. The number of mutations returned is determined with the
    top_results variable, which defaults to 10. Note that if there are not so
    many beneficial mutations as indicated in top_results, the returned list is
    shorter to avoid returning mutations that would decrease the probability of
    being localized at the target localization. This means that if all
    mutations are detrimental, the function returns an empty pd.DataFrame.

    The returned mutations are sorted from larger increase to smaller increase
    and include information about the amino acid position, the original
    residue at that position, the mutation, the improvement with respect to
    initial_score and the final probability of the sequence with that mutation.

    Args:
        mutated_scores: a pd.DataFrame with the probability predicted by the
            model for each mutation (rows) at each position (columns).
        initial_score: a float representing the probability predicted by the
            model for the initial sequence.
        top_results: an integer indicating the number of mutations to return.

    Returns:
        top_res: a pd.DataFrame with the mutations that improve the
            probability that a protein is localized at the target localization,
            showing position, mutation and improvement with respect to the
            original score.
    """
    # check if top_results is an integer
    if type(top_results) != int:
        raise TypeError("top results should be an integer")
    else:
        pass

    # get the increase or decrease in probability of mutations compared to the
    # initial_score of the original sequence
    prob_change = mutated_scores - initial_score

    # prepare data frame for results
    top_res = pd.DataFrame(
        columns=["Position", "Mutation", "Prob_increase", "Target_probability"]
    )

    i = 0
    # initialize at max value so that it enters the loop
    pred_increase = 1

    # get best mutations until reaching top_results or mutations that do
    # not improve the probability
    while i < top_results and pred_increase > 0:
        # get column with maximum value
        position_mut = prob_change.max().idxmax()
        # get row with maximum value
        mutation = prob_change.idxmax()[position_mut]
        # get increase and localization probability of the sequence with the
        # mutation of interest
        pred_increase = prob_change.loc[mutation, position_mut]
        prob_value = mutated_scores.loc[mutation, position_mut]
        # change it for nan so that we can look for next worse mutation at the
        # next iteration
        prob_change.loc[mutation, position_mut] = np.nan
        # append to results
        mut_series = pd.Series(
            {
                "Position": position_mut,
                "Mutation": mutation,
                "Prob_increase": pred_increase,
                "Target_probability": prob_value,
            }
        )
        top_res = top_res.append(mut_series, ignore_index=True)
        i += 1

    return top_res

max_stars_repo_name: Lean-Mean-Protein-Machine-Learning/LMPM

max_issues_repo_name: Lean-Mean-Protein-Machine-Learning/LMPM

max_forks_repo_name: Lean-Mean-Protein-Machine-Learning/LMPM


text:
import itertools

from keras import backend as K, optimizers
from keras import layers
from keras import models

import tensorflow as tf
import numpy as np

from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def

np.random.seed(1)
tf.set_random_seed(1)

def model_fn(input_dim=3,
             labels_dim=1,
             hidden_units=(3, ),
             **args):
    """Create a Keras Sequential model with layers.

    Args:
    input_dim: (int) Input dimensions for input layer.
    labels_dim: (int) Label dimensions for input layer.
    hidden_units: [int] the layer sizes of the DNN (input layer first)
    learning_rate: (float) the learning rate for the optimizer.

    Returns:
        A Keras model.
    """

    # "set_learning_phase" to False to avoid:
    # AbortionError(code=StatusCode.INVALID_ARGUMENT during online prediction.
    K.set_learning_phase(0)

    learning_rate = float(args.get('learning_rate', 0.7))
    model = models.Sequential()
    # Input - Layer
    model.add(layers.Dense(3, input_dim=input_dim))
    # Hidden - Layer
    for units in hidden_units:
        model.add(layers.Dense(units=units, activation="sigmoid"))
    # Output- Layer
    model.add(layers.Dense(labels_dim, activation="sigmoid"))
    model.summary()
    print("Set {} as learning rate on model".format(learning_rate))
    compile_model(model, learning_rate)
    return model

def compile_model(model, learning_rate):
    model.compile(optimizer=optimizers.SGD(lr=learning_rate), loss='binary_crossentropy', metrics=['accuracy'])
    return model

def to_saved_model(model, export_path):
    """Convert the Keras HDF5 model into TensorFlow SavedModel."""

    builder = saved_model_builder.SavedModelBuilder(export_path)

    signature = predict_signature_def(
        inputs={'input': model.inputs[0]}, outputs={'income': model.outputs[0]})

    with K.get_session() as sess:
        builder.add_meta_graph_and_variables(
            sess=sess,
            tags=[tag_constants.SERVING],
            signature_def_map={
                signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
            })
        builder.save()

    print("Model saved in GCS")

def generator_input(batch_size=2):
    def operation(a, b, c):
        return 1 if (a != b and (a or c)) else 0
    while True:
        x_input = np.array(list(map(list, itertools.product([0, 1], repeat=3))))
        y_label = np.array(list(map(lambda f: operation(f[0], f[1], f[2]), x_input)))
        idx_len = x_input.shape[0]
        for index in range((0 + batch_size), (idx_len + 1), batch_size):
            yield (x_input[index-batch_size: index],
                   y_label[index-batch_size: index])


text:
#!/usr/bin/env python

from __future__ import division
import cv2
import numpy as np
import socket
import struct

MAX_DGRAM = 2**16

def dump_buffer(s):
    """ Emptying buffer frame """
    while True:
        seg, addr = s.recvfrom(MAX_DGRAM)
        print(seg[0])
        if struct.unpack("B", seg[0:1])[0] == 1:
            print("finish emptying buffer")
            break

def main():
    """ Getting image udp frame &
    concate before decode and output image """

    # Set up socket
    s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    s.bind(('192.168.54.10',49152))
    dat = b''
    #dump_buffer(s)
    i = 0
    while True:
        dat, addr = s.recvfrom(MAX_DGRAM)
        with open("./frames/frame%0.4d"%(i), "wb+") as frame:
            frame.write(dat)

        #dat = seg[1:]
        #img = cv2.imdecode(np.fromstring(dat, dtype=np.uint8), 1)
        #cv2.imshow('frame', img)
        #if not cv2.imwrite("./frames/coolframe"+str(i)+".jpg", img):
        #    print("not saved")
        #if cv2.waitKey(1) & 0xFF == ord('q'):
        #    break
        dat = b''
        i+=1
    # cap.release()
    cv2.destroyAllWindows()
    s.close()

if __name__ == "__main__":
    main()

max_stars_repo_name: LVGDDesman/PanasonicControll

max_issues_repo_name: LVGDDesman/PanasonicControl

max_forks_repo_name: LVGDDesman/PanasonicControl


text:
from abc import ABC

import numpy as np

class Sampler(ABC):
    """
    Abstraction of the sampler for ANN-DT algorithm
    """
    def get_default_params(self, x):
        """
        :return: default params for given sampler
        """
        pass

    def generate_x(self, train_x, number, restrictions):
        """
        Generates new samples
        :param train_x: available train_samples for given node
        :param number: the number of samples that should be generated
        :param restrictions: restriction to the generated samples (limit for each attribute)
        :return: randomly generated samples
        """
        pass

    def apply_restrictions(self, sample, restrictions):
        """
        :param sample: a sample
        :param restrictions: restriction to the generated samples (limit for each attribute)
        :return: True if the sample fits the restrictions
        """
        return np.all(sample >= restrictions[:, 0]) and np.all(sample <= restrictions[:, 1])

class NormalSampler(Sampler):
    """
    Representation of the normal sampler.
    It uses normal kernel distribution to generate new samples form the training samples
    """
    def __init__(self, x, additional_samples=0.5):
        """
        :param x: training samples from which a covariance matrix is computed
        :param additional_samples: a fraction that is additionaly generated at each step
            - it helps if the restrictions are less often fulfilled because more samples are generated and tested
        """
        self.cov = np.cov(np.swapaxes(x, 0, 1))
        self.additional_samples = additional_samples
        self.attr_num = len(x[0])

    def generate_x(self, train_x, count, restrictions):
        try_count = round(count*(1 + self.additional_samples))
        out = np.empty((0, self.attr_num))
        i = 0
        while len(out) < count:
            ri = np.random.randint(0, len(train_x), try_count)
            gens = np.random.multivariate_normal(np.zeros(self.attr_num), self.cov, try_count)
            gens = gens + train_x[ri]
            gens = gens[np.apply_along_axis(self.apply_restrictions, 1, gens, restrictions)]
            out = np.append(out, gens, 0)
            i += 1
            if i > 20:
                print("[anndt]: Generated only {} samples maximal count of the generationg cycle exceeds the threshold 20".format(len(out)))
                break
        return out[:count]

class BerNormalSampler(Sampler):
    """
    Representation of the bernuli*normal distribution with parameters p, mu, sigma
    """
    def __init__(self, x, additional_samples=0.5, always_positive=False, sigma=None):
        """
        :param x: training samples from which is taken p as a mean(x==0) and sigma = std(x) if not defined
        :param additional_samples: a fraction that is additionaly generated at each step
            - it helps if the restrictions are less often fulfilled because more samples are generated and tested
        :param always_positive: a flag that defines weather samples should be always positive
        :param sigma: a parameter of the kernel distribution
        """
        self.p = np.mean(x==0, 0)
        if sigma is None:
            self.sigma = np.std(x, 0)
        else:
            self.sigma = sigma
        self.additional_samples = additional_samples
        self.attr_num = len(x[0])
        self.always_positive = always_positive

    def generate_x(self, train_x, count, restrictions):
        try_count = round(count * (1 + self.additional_samples))
        out = np.empty((0, self.attr_num))
        i = 0
        while len(out) < count:
            ri = np.random.randint(0, len(train_x), try_count)
            zeros = np.apply_along_axis(np.less, 1, np.random.rand(try_count, self.attr_num), self.p)
            gens = self.sigma * np.random.randn(try_count, self.attr_num) * zeros
            gens = gens + train_x[ri]
            gens[train_x[ri]==0] = 0
            if self.always_positive:
                gens = np.abs(gens)
            gens = gens[np.apply_along_axis(self.apply_restrictions, 1, gens, restrictions)]
            out = np.append(out, gens, 0)
            i += 1
            if i > 20:
                print("[anndt]: Generated only {} samples maximal count of the generationg cycle exceeds the threshold 20".format(len(out)))
                break
        return out[:count]


text:
# Built-in imports
import sys
import os
import argparse
import random
from typing import Tuple, Dict, List, Any

# Libraries
import numpy as np

import torch
import torch.optim as optim

import matplotlib.pyplot as plt

import nltk
from nltk.corpus import stopwords

from allennlp.data.dataset_readers.dataset_reader import AllennlpDataset
from allennlp.data import Instance, DataLoader
from allennlp.data.vocabulary import Vocabulary
from allennlp.models import Model, BasicClassifier
from allennlp.modules.seq2vec_encoders import PytorchSeq2VecWrapper, CnnEncoder
from allennlp.modules.text_field_embedders import BasicTextFieldEmbedder
from allennlp.modules.token_embedders.embedding import _read_pretrained_embeddings_file
from allennlp.modules.token_embedders import Embedding, PretrainedTransformerEmbedder
from allennlp.nn.util import get_text_field_mask
from allennlp.training.metrics import CategoricalAccuracy
from allennlp.training.trainer import Trainer
from allennlp.common.util import lazy_groups_of
from allennlp.data.token_indexers import SingleIdTokenIndexer, PretrainedTransformerMismatchedIndexer
from allennlp.interpret.saliency_interpreters import SaliencyInterpreter, SimpleGradient
from allennlp.predictors import Predictor
from allennlp.data.batch import Batch
from allennlp.data.samplers import BucketBatchSampler
import torch.nn.functional as F
from allennlp.data.tokenizers import PretrainedTransformerTokenizer

# Custom imports
from facade.util.misc import compute_rank, get_stop_ids, create_labeled_instances
from facade.util.model_data_helpers import get_model, get_snli_reader
from facade.finetuners.nli_finetuner import NLI_FineTuner

def main():
    args = argument_parsing()
    print(args)

    reader = get_snli_reader(args.model_name)
    train_data = reader.read('https://s3-us-west-2.amazonaws.com/allennlp/datasets/snli/snli_1.0_train.jsonl')
    dev_data = reader.read('https://s3-us-west-2.amazonaws.com/allennlp/datasets/snli/snli_1.0_dev.jsonl')

    np.random.seed(42)
    sub_dev_data = AllennlpDataset([dev_data[idx] for idx in np.random.choice(len(dev_data), 1000, replace=False)])

    vocab = Vocabulary.from_instances(train_data)
    train_data.index_with(vocab)
    sub_dev_data.index_with(vocab)

    model = get_model(args.model_name, vocab, args.cuda, transformer_dim=256)

    fine_tuner = NLI_FineTuner(model, reader, train_data, sub_dev_data, vocab, args)
    fine_tuner.finetune()

def argument_parsing():
    parser = argparse.ArgumentParser(description='One argparser')
    parser.add_argument('--model_name', default='BERT', type=str, choices=['LSTM', 'BERT'], help='Which model to use')
    parser.add_argument('--batch_size', default=12, type=int, help='Batch size')
    parser.add_argument('--learning_rate', default=1e-5, type=float, help='Learning rate')
    parser.add_argument('--lmbda', default=0.1, type=float, help='Lambda of regularized loss')
    parser.add_argument('--exp_num', default=1, type=int, help='The experiment number')
    parser.add_argument('--loss', default='MSE', type=str, help='Loss function')
    parser.add_argument('--embedding_op', default='dot', type=str, choices=['dot', 'l2'], help='Dot product or l2 norm')
    parser.add_argument('--normalization', default='l1', type=str, choices=['l1', 'l2', 'none'], help='L1 norm or l2 norm')
    parser.add_argument('--normalization2', default=None, type=str, choices=['l1', 'l2', 'none'], help='L1 norm or l2 norm')
    parser.add_argument('--cuda', dest='cuda', action='store_true', help='Cuda enabled')
    parser.add_argument('--no-cuda', dest='cuda', action='store_false', help='Cuda disabled')
    parser.add_argument('--importance', default='first_token', type=str, choices=['first_token', 'stop_token'], help='Where the gradients should be high')
    parser.add_argument('--attack_target', type=str, choices=['premise', 'hypothesis'], help='Whether you want to attack the premise or hypothesis. This is only relevant if importance is set to stop token.')
    args = parser.parse_args()
    return args

if __name__ == '__main__':
    main()

max_stars_repo_name: ucinlp/facade

max_issues_repo_name: ucinlp/facade

max_forks_repo_name: ucinlp/facade


text:
import os
import numpy as np
import torch

def save_checkpoint(model, optimizer, save_path, epoch):
    state_dict = {}
    state_dict['model_state_dict'] = model.state_dict()
    state_dict['optimizer_state_dict'] = optimizer.state_dict()
    state_dict['epoch'] = epoch
    torch.save(state_dict, save_path)

def load_checkpoint(model, optimizer, load_path):
    checkpoint = torch.load(load_path)
    model.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    epoch = checkpoint['epoch']
    return model, optimizer, epoch

class ModelCheckpoint:
    def __init__(self, output_directory, ats_model, optimizer, save_best=True, save_frequency=False, lower_better=True):
        self.output_directory = output_directory
        os.makedirs(output_directory, exist_ok=True)
        self.save_frequency = save_frequency
        self.save_best = save_best
        self.lower_better = lower_better

        self.ats_model = ats_model
        self.optimizer = optimizer

        self.best_metric = np.finfo(np.float64).max if lower_better else np.finfo(np.float64).min

    def __call__(self, epoch, metric):
        if metric < self.best_metric:
            self.best_metric = metric
            if self.save_best:
                self._save_model("model_best.pth", epoch)

        if self.save_frequency and epoch % self.save_frequency == 0:
            self._save_model(f"model_{epoch}.pth", epoch)

    def _save_model(self, name, epoch):
        save_checkpoint(self.ats_model, self.optimizer, os.path.join(self.output_directory, name), epoch)


text:
#!/usr/bin/env python
# SAC RL Agent with Trainer
# Chapter 8, TensorFlow 2 Reinforcement Learning Cookbook | Praveen Palanisamy

import functools
import random
from collections import deque

import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
from tensorflow.keras.layers import Concatenate, Dense, Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam

tf.keras.backend.set_floatx("float64")

def actor(state_shape, action_shape, units=(512, 256, 64)):
    state_shape_flattened = functools.reduce(lambda x, y: x * y, state_shape)
    state = Input(shape=state_shape_flattened)
    x = Dense(units[0], name="L0", activation="relu")(state)
    for index in range(1, len(units)):
        x = Dense(units[index], name="L{}".format(index), activation="relu")(x)

    actions_mean = Dense(action_shape[0], name="Out_mean")(x)
    actions_std = Dense(action_shape[0], name="Out_std")(x)

    model = Model(inputs=state, outputs=[actions_mean, actions_std], name="Actor")

    return model

def critic(state_shape, action_shape, units=(512, 256, 64)):
    state_shape_flattened = functools.reduce(lambda x, y: x * y, state_shape)
    inputs = [Input(shape=state_shape_flattened), Input(shape=action_shape)]
    concat = Concatenate(axis=-1)(inputs)
    x = Dense(units[0], name="Hidden0", activation="relu")(concat)
    for index in range(1, len(units)):
        x = Dense(units[index], name="Hidden{}".format(index), activation="relu")(x)

    output = Dense(1, name="Out_QVal")(x)
    model = Model(inputs=inputs, outputs=output, name="Critic")

    return model

def update_target_weights(model, target_model, tau=0.005):
    weights = model.get_weights()
    target_weights = target_model.get_weights()
    for i in range(len(target_weights)):  # set tau% of target model to be new weights
        target_weights[i] = weights[i] * tau + target_weights[i] * (1 - tau)
    target_model.set_weights(target_weights)

class SAC(object):
    def __init__(
        self,
        observation_shape,
        action_space,
        lr_actor=3e-5,
        lr_critic=3e-4,
        actor_units=(64, 64),
        critic_units=(64, 64),
        auto_alpha=True,
        alpha=0.2,
        tau=0.005,
        gamma=0.99,
        batch_size=128,
        memory_cap=100000,
    ):
        self.state_shape = observation_shape  # shape of observations
        self.action_shape = action_space.shape  # number of actions
        self.action_bound = (action_space.high - action_space.low) / 2
        self.action_shift = (action_space.high + action_space.low) / 2
        self.memory = deque(maxlen=int(memory_cap))

        # Define and initialize actor network
        self.actor = actor(self.state_shape, self.action_shape, actor_units)
        self.actor_optimizer = Adam(learning_rate=lr_actor)
        self.log_std_min = -20
        self.log_std_max = 2
        print(self.actor.summary())

        # Define and initialize critic networks
        self.critic_1 = critic(self.state_shape, self.action_shape, critic_units)
        self.critic_target_1 = critic(self.state_shape, self.action_shape, critic_units)
        self.critic_optimizer_1 = Adam(learning_rate=lr_critic)
        update_target_weights(self.critic_1, self.critic_target_1, tau=1.0)

        self.critic_2 = critic(self.state_shape, self.action_shape, critic_units)
        self.critic_target_2 = critic(self.state_shape, self.action_shape, critic_units)
        self.critic_optimizer_2 = Adam(learning_rate=lr_critic)
        update_target_weights(self.critic_2, self.critic_target_2, tau=1.0)

        print(self.critic_1.summary())

        # Define and initialize temperature alpha and target entropy
        self.auto_alpha = auto_alpha
        if auto_alpha:
            self.target_entropy = -np.prod(self.action_shape)
            self.log_alpha = tf.Variable(0.0, dtype=tf.float64)
            self.alpha = tf.Variable(0.0, dtype=tf.float64)
            self.alpha.assign(tf.exp(self.log_alpha))
            self.alpha_optimizer = Adam(learning_rate=lr_actor)
        else:
            self.alpha = tf.Variable(alpha, dtype=tf.float64)

        # Set hyperparameters
        self.gamma = gamma  # discount factor
        self.tau = tau  # target model update
        self.batch_size = batch_size

        # Tensorboard
        self.summaries = {}

    def process_actions(self, mean, log_std, test=False, eps=1e-6):
        std = tf.math.exp(log_std)
        raw_actions = mean

        if not test:
            raw_actions += tf.random.normal(shape=mean.shape, dtype=tf.float64) * std

        log_prob_u = tfp.distributions.Normal(loc=mean, scale=std).log_prob(raw_actions)
        actions = tf.math.tanh(raw_actions)

        log_prob = tf.reduce_sum(log_prob_u - tf.math.log(1 - actions ** 2 + eps))

        actions = actions * self.action_bound + self.action_shift

        return actions, log_prob

    def act(self, state, test=False, use_random=False):
        state = state.reshape(-1)  # Flatten state
        state = np.expand_dims(state, axis=0).astype(np.float64)

        if use_random and len(self.memory) > self.batch_size:
            a = tf.random.uniform(
                shape=(1, self.action_shape[0]), minval=-1, maxval=1, dtype=tf.float64
            )
        else:
            means, log_stds = self.actor.predict(state)
            log_stds = tf.clip_by_value(log_stds, self.log_std_min, self.log_std_max)

            a, log_prob = self.process_actions(means, log_stds, test=test)

        q1 = self.critic_1.predict([state, a])[0][0]
        q2 = self.critic_2.predict([state, a])[0][0]
        self.summaries["q_min"] = tf.math.minimum(q1, q2)
        self.summaries["q_mean"] = np.mean([q1, q2])

        return a

    def save_model(self, a_fn, c_fn):
        self.actor.save(a_fn)
        self.critic_1.save(c_fn)

    def load_actor(self, a_fn):
        self.actor.load_weights(a_fn)
        print(self.actor.summary())

    def load_critic(self, c_fn):
        self.critic_1.load_weights(c_fn)
        self.critic_target_1.load_weights(c_fn)
        self.critic_2.load_weights(c_fn)
        self.critic_target_2.load_weights(c_fn)
        print(self.critic_1.summary())

    def remember(self, state, action, reward, next_state, done):
        state = state.reshape(-1)  # Flatten state
        state = np.expand_dims(state, axis=0)
        next_state = next_state.reshape(-1)  # Flatten next-state
        next_state = np.expand_dims(next_state, axis=0)
        self.memory.append([state, action, reward, next_state, done])

    def replay(self):
        if len(self.memory) < self.batch_size:
            return

        samples = random.sample(self.memory, self.batch_size)
        s = np.array(samples).T
        states, actions, rewards, next_states, dones = [
            np.vstack(s[i, :]).astype(np.float) for i in range(5)
        ]

        with tf.GradientTape(persistent=True) as tape:
            # next state action log probs
            means, log_stds = self.actor(next_states)
            log_stds = tf.clip_by_value(log_stds, self.log_std_min, self.log_std_max)
            next_actions, log_probs = self.process_actions(means, log_stds)

            # critics loss
            current_q_1 = self.critic_1([states, actions])
            current_q_2 = self.critic_2([states, actions])
            next_q_1 = self.critic_target_1([next_states, next_actions])
            next_q_2 = self.critic_target_2([next_states, next_actions])
            next_q_min = tf.math.minimum(next_q_1, next_q_2)
            state_values = next_q_min - self.alpha * log_probs
            target_qs = tf.stop_gradient(
                rewards + state_values * self.gamma * (1.0 - dones)
            )
            critic_loss_1 = tf.reduce_mean(
                0.5 * tf.math.square(current_q_1 - target_qs)
            )
            critic_loss_2 = tf.reduce_mean(
                0.5 * tf.math.square(current_q_2 - target_qs)
            )

            # current state action log probs
            means, log_stds = self.actor(states)
            log_stds = tf.clip_by_value(log_stds, self.log_std_min, self.log_std_max)
            actions, log_probs = self.process_actions(means, log_stds)

            # actor loss
            current_q_1 = self.critic_1([states, actions])
            current_q_2 = self.critic_2([states, actions])
            current_q_min = tf.math.minimum(current_q_1, current_q_2)
            actor_loss = tf.reduce_mean(self.alpha * log_probs - current_q_min)

            # temperature loss
            if self.auto_alpha:
                alpha_loss = -tf.reduce_mean(
                    (self.log_alpha * tf.stop_gradient(log_probs + self.target_entropy))
                )

        critic_grad = tape.gradient(
            critic_loss_1, self.critic_1.trainable_variables
        )  # compute actor gradient
        self.critic_optimizer_1.apply_gradients(
            zip(critic_grad, self.critic_1.trainable_variables)
        )

        critic_grad = tape.gradient(
            critic_loss_2, self.critic_2.trainable_variables
        )  # compute actor gradient
        self.critic_optimizer_2.apply_gradients(
            zip(critic_grad, self.critic_2.trainable_variables)
        )

        actor_grad = tape.gradient(
            actor_loss, self.actor.trainable_variables
        )  # compute actor gradient
        self.actor_optimizer.apply_gradients(
            zip(actor_grad, self.actor.trainable_variables)
        )

        # tensorboard info
        self.summaries["q1_loss"] = critic_loss_1
        self.summaries["q2_loss"] = critic_loss_2
        self.summaries["actor_loss"] = actor_loss

        if self.auto_alpha:
            # optimize temperature
            alpha_grad = tape.gradient(alpha_loss, [self.log_alpha])
            self.alpha_optimizer.apply_gradients(zip(alpha_grad, [self.log_alpha]))
            self.alpha.assign(tf.exp(self.log_alpha))
            # tensorboard info
            self.summaries["alpha_loss"] = alpha_loss

    def train(self, cur_state, action, reward, next_state, done):
        self.remember(cur_state, action, reward, next_state, done)  # add to memory
        self.replay()  # train models through memory replay
        update_target_weights(
            self.critic_1, self.critic_target_1, tau=self.tau
        )  # iterates target model
        update_target_weights(self.critic_2, self.critic_target_2, tau=self.tau)

    def update_memory(self, xp_store):
        for (cur_state, action, reward, next_state, done) in zip(
            xp_store["cur_states"],
            xp_store["actions"],
            xp_store["rewards"],
            xp_store["next_states"],
            xp_store["dones"],
        ):
            self.remember(cur_state, action, reward, next_state, done)  # add to memory

    def train_with_distributed_replay_memory(self, new_experiences):
        self.update_memory(new_experiences)
        self.replay()  # train models through memory replay
        update_target_weights(
            self.critic_1, self.critic_target_1, tau=self.tau
        )  # iterates target model
        update_target_weights(self.critic_2, self.critic_target_2, tau=self.tau)

    def log_status(self, summary_writer, episode_num, reward):
        """Write training stats using TF `summary_writer`"""
        with summary_writer.as_default():
            if len(self.memory) > self.batch_size:
                tf.summary.scalar(
                    "Loss/actor_loss", self.summaries["actor_loss"], step=episode_num
                )
                tf.summary.scalar(
                    "Loss/q1_loss", self.summaries["q1_loss"], step=episode_num
                )
                tf.summary.scalar(
                    "Loss/q2_loss", self.summaries["q2_loss"], step=episode_num
                )
                if self.auto_alpha:
                    tf.summary.scalar(
                        "Loss/alpha_loss",
                        self.summaries["alpha_loss"],
                        step=episode_num,
                    )

            tf.summary.scalar("Stats/alpha", self.alpha, step=episode_num)
            if self.auto_alpha:
                tf.summary.scalar("Stats/log_alpha", self.log_alpha, step=episode_num)
            tf.summary.scalar("Stats/q_min", self.summaries["q_min"], step=episode_num)
            tf.summary.scalar(
                "Stats/q_mean", self.summaries["q_mean"], step=episode_num
            )
            tf.summary.scalar("Main/step_reward", reward, step=episode_num)


text:
# -*- coding: utf-8 -*-
"""
Created on Tue May  4 12:22:13 2021

@author: hp
"""

from __future__ import division, print_function
# coding=utf-8
import sys
import os
import glob
import re
import numpy as np

# Keras
from tensorflow.keras.applications.imagenet_utils import preprocess_input, decode_predictions
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image

# Flask utils
from flask import Flask, redirect, url_for, request, render_template,session
import bcrypt
from werkzeug.utils import secure_filename
from flask_pymongo import PyMongo
app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'Malaria'
app.config['MONGO_URI'] = 'mongodb://127.0.0.1:27017/Malaria'
mongo = PyMongo(app)
app = Flask(__name__)
MODEL_PATH ='model_vgg19.h5'
model = load_model(MODEL_PATH)
def model_predict(file_path,model):
    img = image.load_img(file_path,target_size=(224, 224))
    x = image.img_to_array(img)
    x = x/255
    x = np.expand_dims(x, axis=0)
    preds = model.predict(x)
    preds=np.argmax(preds, axis=1)
    if preds==0:
        preds = "INFECTED"
    else:
        preds = "uninfected"
    return preds
@app.route('/',methods = ['POST','GET'])
def index():
    if 'email' in session:
        return render_template('sw_features.html')
    return render_template('index_sw.html')
@app.route('/predict', methods=['GET', 'POST'])
def predict():
    msg = ""
    if request.method == 'POST':
        f = request.files['file']
        basepath = os.path.dirname(f.filename)
        file_path = os.path.join(basepath, 'uploads', secure_filename(f.filename))
        f.save(file_path)
        msg = model_predict(file_path,model)
        session['status'] = msg
        return render_template("sw_features.html",msg = "Result: "+msg)
    return None
@app.route('/login', methods=['POST','GET'])
def login():
    msg = ""
    users = mongo.db.collection
    login_user = users.find_one({'email' : request.form['email']})

    if login_user:
        if bcrypt.hashpw(request.form['password'].encode('utf-8'), login_user['password']) == login_user['password']:
            session['email'] = request.form['email']
            session['Name'] = login_user['Name']
            session['status'] = 'unknown'
            #return redirect(url_for('sw_features.html'))
            return render_template('sw_features.html')
    msg = 'Invalid username/password combination'
    return render_template('index_sw.html',msg = msg)
@app.route('/register', methods=['POST', 'GET'])
def register():
    msg = ""
    if request.method == 'POST':
        users = mongo.db.collection
        existing_user = users.find_one({'email' : request.form['email']})

        if existing_user is None:
            hashpass = bcrypt.hashpw(request.form['password'].encode('utf-8'), bcrypt.gensalt())
            users.insert_one({'Name' : request.form['Name'],'email':request.form['email'], 'password' : hashpass,'Address':request.form['Address'],'status':'unknown'})
            session['email'] = request.form['email']
            session['status'] = 'unknown'
            msg = "You have successfully registered !!"
            #return redirect(url_for('index'))  #sending to index function
            return render_template('index_sw.html',msg = msg)
        msg = "That email already exists!"
        return render_template('index_sw.html',msg = msg)

    return render_template('register_sw.html',msg = msg)
@app.route('/logout')
def logout():
    msg=""
    session.pop('email', None)
    session.pop('Name', None)
    return render_template('index_sw.html',msg = "Successfully logged out")
if __name__ == '__main__':
    app.secret_key = 'mysecret'
    app.run()


text:
import numpy as np
import pandas as pd
import random
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Dropout, ActivityRegularization
from keras.wrappers.scikit_learn import KerasRegressor, KerasClassifier
from sklearn.model_selection import cross_val_predict
from sklearn.preprocessing import scale
from sklearn.metrics import r2_score, f1_score
from deap import algorithms
from deap import base
from deap import creator
from deap import tools

class Regressors:
    def __init__(self, individual):
        depths = [0, 0, 0, 1, 1, 1, 2, 2, 2]
        widths = [10, 15, 20, 25, 30, 35, 40, 50, 55]
        afs = ['relu', 'elu', 'selu', 'sigmoid', 'tanh', 'relu', 'elu', 'selu', 'sigmoid']
        opts = ['sgd', 'adam', 'adagrad', 'adamax', 'nadam', 'adam', 'adagrad', 'adamax', 'nadam']
        drops = [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4]
        l1s = [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4]
        l2s = [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4]
        self.depth = depths[individual[0]]
        self.width = widths[individual[1]]
        self.af = afs[individual[2]]
        self.opt = opts[individual[3]]
        self.drop = drops[individual[4]]
        self.l1 = l1s[individual[5]]
        self.l2 = l2s[individual[6]]

    def baseline_model(self):
        model = Sequential()
        model.add(Dense(self.width, input_dim=75, kernel_initializer='normal', activation=self.af)) #25
        for layer in range(self.depth):
            model.add(Dense(self.width, kernel_initializer='normal', activation=self.af))
            model.add(Dropout(self.drop, noise_shape=None, seed=None))
            model.add(ActivityRegularization(l1=self.l1, l2=self.l2))
        model.add(Dense(1, kernel_initializer='normal'))
        model.compile(loss='mse', optimizer=self.opt)
        return model

class Classifiers:
    def __init__(self, individual):
        depths = [0, 0, 0, 1, 1, 1, 2, 2, 2]
        widths = [10, 15, 20, 25, 30, 35, 40, 50, 55]
        afs = ['relu', 'elu', 'selu', 'sigmoid', 'tanh', 'relu', 'elu', 'selu', 'sigmoid']
        opts = ['sgd', 'adam', 'adagrad', 'adamax', 'nadam', 'adam', 'adagrad', 'adamax', 'nadam']
        drops = [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4]
        l1s = [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4]
        l2s = [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4]
        self.depth = depths[individual[0]]
        self.width = widths[individual[1]]
        self.af = afs[individual[2]]
        self.opt = opts[individual[3]]
        self.drop = drops[individual[4]]
        self.l1 = l1s[individual[5]]
        self.l2 = l2s[individual[6]]

    def baseline_model(self):
        model = Sequential()
        model.add(Dense(self.width, input_dim=75, kernel_initializer='normal', activation=self.af))  # 17
        for layer in range(self.depth):
            model.add(Dense(self.width, kernel_initializer='normal', activation=self.af))
            model.add(Dropout(self.drop, noise_shape=None, seed=None))
            model.add(ActivityRegularization(l1=self.l1, l2=self.l2))
        model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
        model.compile(loss='binary_crossentropy', optimizer=self.opt)
        return model

class BlindWatchmaker:
    def __init__(self, pop_size, generations, m_rate, c_rate, t_size, epochs, b_size, cv, type):
        self.pop_size = pop_size #population size
        self.generations = generations #number of generations
        self.m_rate = m_rate #mutation rate
        self.c_rate = c_rate #cross-over rate
        self.t_size = t_size #tournament size
        self.epochs = epochs #number of epochs
        self.b_size = b_size #batch size
        self.cv = cv #folds in cross validation
        self.type = type

    def get_data(self):
        if self.type == 'regressor':
            #df = pd.read_excel('simulation2_trainingset_full.xlsx')
            df = pd.read_excel('new_10_trainingset_full.xlsx')
            dataset = scale(df.values)
            X = dataset[:, :75]
            Y = dataset[:, 75]
        else:
            # df = pd.read_excel('simulation2_trainingset_class.xlsx')
            df = pd.read_excel('10_class.xlsx')
            dataset = df.values
            X = scale(dataset[:, :75])
            Y = dataset[:, 75]

        return X, Y

    def ev(self):
        X, Y = self.get_data()
        creator.create("FitnessMax", base.Fitness, weights=(1.0,))
        creator.create("Individual", list, typecode='b', fitness=creator.FitnessMax)

        toolbox = base.Toolbox()

        # Attribute generator
        toolbox.register("attr_bool", random.randint, 0, 8)

        # Structure initializers
        toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 7)
        toolbox.register("population", tools.initRepeat, list, toolbox.individual)

        def eval(individual):
            if self.type == 'regressor':
                r = Regressors(individual)
                model = r.baseline_model
                estimator = KerasRegressor(build_fn=model, epochs=self.epochs, batch_size=self.b_size, verbose=0)
                results = cross_val_predict(estimator, X, Y, cv=self.cv)
                r2 = r2_score(Y, results)
                return r2,
            else:
                c = Classifiers(individual)
                model = c.baseline_model
                estimator = KerasClassifier(build_fn=model, epochs=self.epochs, batch_size=self.b_size, verbose=0)
                results = cross_val_predict(estimator, X, Y, cv=self.cv)
                f1 = f1_score(Y, results)
                return f1,

        toolbox.register("evaluate", eval)
        toolbox.register("mate", tools.cxOnePoint)
        toolbox.register("mutate", tools.mutUniformInt, low=0, up=8, indpb=0.2)
        toolbox.register("select", tools.selTournament, tournsize=self.t_size)

        def evolve():
            random.seed(64)

            pop = toolbox.population(n=self.pop_size)
            hof = tools.HallOfFame(1)
            stats = tools.Statistics(lambda ind: ind.fitness.values)
            stats.register("avg", np.mean)
            stats.register("std", np.std)
            stats.register("max", np.max)
            stats.register("min", np.min)

            pop, log = algorithms.eaSimple(pop, toolbox, cxpb=self.c_rate, mutpb=self.m_rate, ngen=self.generations,
                                           stats=stats, halloffame=hof, verbose=True)

            return log, hof

        log, hof = evolve()
        print('Praise the fittest one!', hof)
        def plot():
            gen = log.select("gen")
            fits = log.select("avg")
            std = log.select("std")
            std = [i/2 for i in std]
            plt.grid()
            plt.errorbar([i+1 for i in gen], fits, std, uplims=True, fmt='-o', color='blue')
            plt.xlabel("Generation")
            plt.ylabel("F1 score")
            plt.show()
        plot()

evolver = BlindWatchmaker(15, 9, 0.1, 0.4, 3, 40, 4, 2, 'regressor')
evolver.ev()