text:
import numpy as np
import tensorflow as tf
import functools

from baselines.common.tf_util import get_session, save_variables, load_variables
from baselines.common.tf_util import initialize

try:
    import baselines.common.mpi_adam_optimizer as mpi_adam
    from mpi4py import MPI
    from baselines.common.mpi_util import sync_from_root
except ImportError:
    MPI = None

from .utils import reduce_std

# Disable sync check because each worker has different rand conv weights
mpi_adam.check_synced = lambda: None

class NetRandModel:
    """
    We use this object to :
    __init__:
    - Creates the step_model
    - Creates the train_model

    train():
    - Make the training part (feedforward and retropropagation of gradients)

    save/load():
    - Save load the model
    """
    def __init__(self, *, policy, ob_space, ac_space, nbatch_act, nbatch_train,
                nsteps, ent_coef, vf_coef, max_grad_norm, mpi_rank_weight=1, comm=None, microbatch_size=None,
                fm_coeff=0.002):
        self.sess = sess = get_session()

        if MPI is not None and comm is None:
            comm = MPI.COMM_WORLD

        with tf.variable_scope('ppo2_model', reuse=tf.AUTO_REUSE):
            # CREATE OUR TWO MODELS
            # act_model that is used for sampling
            act_model = policy(nbatch_act, 1, sess)
            act_model_clean = policy(nbatch_act, 1, sess, randomization=False)

            # Train model for training
            if microbatch_size is None:
                train_model = policy(nbatch_train, nsteps, sess)
                train_model_clean = policy(nbatch_train, nsteps, sess, randomization=False)
            else:
                train_model = policy(microbatch_size, nsteps, sess)
                train_model_clean = policy(microbatch_size, nsteps, sess, randomization=False)

        # CREATE THE PLACEHOLDERS
        self.A = A = train_model.pdtype.sample_placeholder([None])
        self.ADV = ADV = tf.placeholder(tf.float32, [None])
        self.R = R = tf.placeholder(tf.float32, [None])
        # Keep track of old actor
        self.OLDNEGLOGPAC = OLDNEGLOGPAC = tf.placeholder(tf.float32, [None])
        # Keep track of old critic
        self.OLDVPRED = OLDVPRED = tf.placeholder(tf.float32, [None])
        self.LR = LR = tf.placeholder(tf.float32, [])
        # Cliprange
        self.CLIPRANGE = CLIPRANGE = tf.placeholder(tf.float32, [])
        # Normalizing advantage
        ADV = (ADV - tf.reduce_mean(ADV)) / (reduce_std(ADV) + 1e-8)

        ############ Training with Randomized Obs ############
        # Total loss = Policy gradient loss - entropy * entropy coefficient + Value coefficient * value loss

        # Calculate the entropy
        entropy = tf.reduce_mean(train_model.pd.entropy())

        # Calculate value loss
        vpred = train_model.vf
        vpredclipped = OLDVPRED + tf.clip_by_value(train_model.vf - OLDVPRED, - CLIPRANGE, CLIPRANGE)
        vf_losses1 = tf.square(vpred - R)
        vf_losses2 = tf.square(vpredclipped - R)
        vf_loss = .5 * tf.reduce_mean(tf.maximum(vf_losses1, vf_losses2))

        # Calculate policy gradient loss
        neglogpac = train_model.pd.neglogp(A)
        ratio = tf.exp(OLDNEGLOGPAC - neglogpac)
        pg_losses = -ADV * ratio
        pg_losses2 = -ADV * tf.clip_by_value(ratio, 1.0 - CLIPRANGE, 1.0 + CLIPRANGE)
        pg_loss = tf.reduce_mean(tf.maximum(pg_losses, pg_losses2))

        # Record some information
        approxkl = .5 * tf.reduce_mean(tf.square(neglogpac - OLDNEGLOGPAC))
        clipfrac = tf.reduce_mean(tf.to_float(tf.greater(tf.abs(ratio - 1.0), CLIPRANGE)))
        ############################################

        ############ Training with Clean Obs ############
        # Total loss = Policy gradient loss - entropy * entropy coefficient + Value coefficient * value loss

        # Calculate the entropy
        entropy_clean = tf.reduce_mean(train_model_clean.pd.entropy())

        # Calculate value loss
        vpred_clean = train_model_clean.vf
        vpredclipped_clean = OLDVPRED + tf.clip_by_value(train_model_clean.vf - OLDVPRED, - CLIPRANGE, CLIPRANGE)
        vf_losses1_clean = tf.square(vpred_clean - R)
        vf_losses2_clean = tf.square(vpredclipped_clean - R)
        vf_loss_clean = .5 * tf.reduce_mean(tf.maximum(vf_losses1_clean, vf_losses2_clean))

        # Calculate policy gradient loss
        neglogpac_clean = train_model_clean.pd.neglogp(A)
        ratio_clean = tf.exp(OLDNEGLOGPAC - neglogpac_clean)
        pg_losses_clean = -ADV * ratio_clean
        pg_losses2_clean = -ADV * tf.clip_by_value(ratio_clean, 1.0 - CLIPRANGE, 1.0 + CLIPRANGE)
        pg_loss_clean = tf.reduce_mean(tf.maximum(pg_losses_clean, pg_losses2_clean))

        # Record some information
        approxkl_clean = .5 * tf.reduce_mean(tf.square(neglogpac_clean - self.OLDNEGLOGPAC))
        clipfrac_clean = tf.reduce_mean(tf.to_float(tf.greater(tf.abs(ratio_clean - 1.0), self.CLIPRANGE)))
        ############################################

        ############ Calculate the total loss ############
        fm_loss = tf.losses.mean_squared_error(labels=tf.stop_gradient(train_model_clean.latent_fts), predictions=train_model.latent_fts)
        loss = pg_loss - entropy * ent_coef + vf_loss * vf_coef + fm_loss * fm_coeff
        loss_clean = pg_loss_clean - entropy_clean * ent_coef + vf_loss_clean * vf_coef + fm_loss * fm_coeff
        self.stats_list = [loss, fm_loss, pg_loss, vf_loss, entropy, approxkl, clipfrac]
        self.stats_list_clean = [loss_clean, fm_loss, pg_loss_clean, vf_loss_clean, entropy_clean, approxkl_clean, clipfrac_clean]
        ##################################################

        ############ UPDATE THE PARAMETERS ############
        # 1. Get the model parameters
        params = tf.trainable_variables('ppo2_model')
        # 2. Build our trainer
        if comm is not None and comm.Get_size() > 1:
            self.trainer = mpi_adam.MpiAdamOptimizer(comm, learning_rate=LR, mpi_rank_weight=mpi_rank_weight, epsilon=1e-5)
        else:
            self.trainer = tf.train.AdamOptimizer(learning_rate=LR, epsilon=1e-5)
        # 3. Calculate the gradients
        grads_and_var = self.trainer.compute_gradients(loss, params)
        grads_and_var_clean = self.trainer.compute_gradients(loss_clean, params)
        grads, var = zip(*grads_and_var)
        grads_clean, var_clean = zip(*grads_and_var_clean)
        # 4. Clip the gradient if required
        if max_grad_norm is not None:
            grads, _grad_norm = tf.clip_by_global_norm(grads, max_grad_norm)
            grads_clean, _grad_norm = tf.clip_by_global_norm(grads_clean, max_grad_norm)
        grads_and_var = list(zip(grads, var))
        grads_and_var_clean = list(zip(grads_clean, var_clean))
        ###############################################

        self.loss_names = ['total_loss', 'fm_loss', 'policy_loss', 'value_loss', 'policy_entropy', 'approxkl', 'clipfrac']
        self.grads = grads
        self.var = var
        self._train_op = self.trainer.apply_gradients(grads_and_var)
        self._train_clean_op = self.trainer.apply_gradients(grads_and_var_clean)
        self.fm_coeff = fm_coeff
        self.clean_flag = False
        self._init_randcnn = tf.variables_initializer(act_model.randcnn_param)

        self.train_model = train_model
        self.train_model_clean = train_model_clean
        self.act_model = act_model
        self.act_model_clean = act_model_clean
        self.initial_state = act_model.initial_state

        self.save = functools.partial(save_variables, sess=sess)
        self.load = functools.partial(load_variables, sess=sess)

        initialize()
        global_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="")
        if MPI is not None:
            sync_from_root(sess, global_variables, comm=comm) #pylint: disable=E1101

    def step(self, *args, **kwargs):
        if self.clean_flag:
            return self.act_model_clean.step(*args, **kwargs)
        else:
            return self.act_model.step(*args, **kwargs)

    def value(self, *args, **kwargs):
        if self.clean_flag:
            return self.act_model_clean.value(*args, **kwargs)
        else:
            return self.act_model.value(*args, **kwargs)

    def train(self, lr, cliprange, obs, returns, masks, actions, values, neglogpacs, states=None):
        # Here we calculate advantage A(s,a) = R + yV(s') - V(s)
        # Returns = R + yV(s')
        advs = returns - values

        td_map = {
            self.train_model.X : obs,
            self.train_model_clean.X: obs,
            self.A : actions,
            self.ADV : advs,
            self.R : returns,
            self.LR : lr,
            self.CLIPRANGE : cliprange,
            self.OLDNEGLOGPAC : neglogpacs,
            self.OLDVPRED : values
        }

        if self.clean_flag:
            return self.sess.run(self.stats_list_clean + [self._train_clean_op], td_map)[:-1]
        else:
            return self.sess.run(self.stats_list + [self._train_op], td_map)[:-1]

max_stars_repo_name: tangandrew/mixreg

max_issues_repo_name: tangandrew/mixreg

max_forks_repo_name: tangandrew/mixreg


text:
# coding=utf-8
# Copyright (c) DIRECT Contributors
import argparse
import functools
import logging
import os
import pathlib
import sys
import urllib.parse
from collections import defaultdict
from typing import Callable, Dict, List, Optional, Union

import numpy as np
import torch
from omegaconf import DictConfig

from direct.cli.utils import check_train_val
from direct.common.subsample import build_masking_function
from direct.data.datasets import build_dataset_from_input
from direct.data.lr_scheduler import WarmupMultiStepLR
from direct.data.mri_transforms import build_mri_transforms
from direct.environment import setup_training_environment
from direct.launch import launch
from direct.types import PathOrString
from direct.utils import remove_keys, set_all_seeds, str_to_class
from direct.utils.dataset import get_filenames_for_datasets_from_config
from direct.utils.io import check_is_valid_url, read_json

logger = logging.getLogger(__name__)

def parse_noise_dict(noise_dict: dict, percentile: float = 1.0, multiplier: float = 1.0):
    logger.info("Parsing noise dictionary...")
    output: Dict = defaultdict(dict)
    for filename in noise_dict:
        data_per_volume = noise_dict[filename]
        for slice_no in data_per_volume:
            curr_data = data_per_volume[slice_no]
            if percentile != 1.0:
                lower_clip = np.percentile(curr_data, 100 * (1 - percentile))
                upper_clip = np.percentile(curr_data, 100 * percentile)
                curr_data = np.clip(curr_data, lower_clip, upper_clip)

            output[filename][int(slice_no)] = (
                curr_data * multiplier
            ) ** 2  # np.asarray(curr_data) * multiplier# (np.clip(curr_data, lower_clip, upper_clip) * multiplier) ** 2

    return output

def get_root_of_file(filename: PathOrString):
    """Get the root directory of the file or URL to file.

    Examples
    --------
    >>> get_root_of_file('/mnt/archive/data.txt')
    >>> /mnt/archive
    >>> get_root_of_file('https://aiforoncology.nl/people')
    >>> https://aiforoncology.nl/

    Parameters
    ----------
    filename: pathlib.Path or str

    Returns
    -------
    pathlib.Path or str
    """
    if check_is_valid_url(str(filename)):
        filename = urllib.parse.urljoin(str(filename), ".")
    else:
        filename = pathlib.Path(filename).parents[0]

    return filename

def build_transforms_from_environment(env, dataset_config: DictConfig) -> Callable:
    mri_transforms_func = functools.partial(
        build_mri_transforms,
        forward_operator=env.engine.forward_operator,
        backward_operator=env.engine.backward_operator,
        mask_func=build_masking_function(**dataset_config.transforms.masking),
    )
    return mri_transforms_func(**remove_keys(dataset_config.transforms, "masking"))  # type: ignore

def build_training_datasets_from_environment(
    env,
    datasets_config: List[DictConfig],
    lists_root: Optional[PathOrString] = None,
    data_root: Optional[PathOrString] = None,
    initial_images: Optional[Union[List[pathlib.Path], None]] = None,
    initial_kspaces: Optional[Union[List[pathlib.Path], None]] = None,
    pass_text_description: bool = True,
    pass_dictionaries: Optional[Dict[str, Dict]] = None,
):

    datasets = []
    for idx, dataset_config in enumerate(datasets_config):
        if pass_text_description:
            if not "text_description" in dataset_config:
                dataset_config.text_description = f"ds{idx}" if len(datasets_config) > 1 else None
        else:
            dataset_config.text_description = None
        transforms = build_transforms_from_environment(env, dataset_config)
        dataset_args = {"transforms": transforms, "dataset_config": dataset_config}
        if initial_images is not None:
            dataset_args.update({"initial_images": initial_images})
        if initial_kspaces is not None:
            dataset_args.update({"initial_kspaces": initial_kspaces})
        if data_root is not None:
            dataset_args.update({"data_root": data_root})
            filenames_filter = get_filenames_for_datasets_from_config(dataset_config, lists_root, data_root)
            dataset_args.update({"filenames_filter": filenames_filter})
        if pass_dictionaries is not None:
            dataset_args.update({"pass_dictionaries": pass_dictionaries})
        dataset = build_dataset_from_input(**dataset_args)

        logger.debug("Transforms %s / %s :\n%s", idx + 1, len(datasets_config), transforms)
        datasets.append(dataset)
        logger.info(
            "Data size for %s (%s/%s): %s.",
            dataset_config.text_description,  # type: ignore
            idx + 1,
            len(datasets_config),
            len(dataset),
        )

    return datasets

def setup_train(
    run_name: str,
    training_root: Union[pathlib.Path, None],
    validation_root: Union[pathlib.Path, None],
    base_directory: pathlib.Path,
    cfg_filename: PathOrString,
    force_validation: bool,
    initialization_checkpoint: PathOrString,
    initial_images: Optional[Union[List[pathlib.Path], None]],
    initial_kspace: Optional[Union[List[pathlib.Path], None]],
    noise: Optional[Union[List[pathlib.Path], None]],
    device: str,
    num_workers: int,
    resume: bool,
    machine_rank: int,
    mixed_precision: bool,
    debug: bool,
):

    env = setup_training_environment(
        run_name,
        base_directory,
        cfg_filename,
        device,
        machine_rank,
        mixed_precision,
        debug=debug,
    )

    # Trigger cudnn benchmark and remove the associated cache
    torch.backends.cudnn.benchmark = True
    torch.cuda.empty_cache()

    if initial_kspace is not None and initial_images is not None:
        raise ValueError("Cannot both provide initial kspace or initial images.")
    # Create training data
    training_dataset_args = {"env": env, "datasets_config": env.cfg.training.datasets, "pass_text_description": True}
    pass_dictionaries = {}
    if noise is not None:
        if not env.cfg.physics.use_noise_matrix:
            raise ValueError("cfg.physics.use_noise_matrix is null, yet command line passed noise files.")

        noise = [read_json(fn) for fn in noise]
        pass_dictionaries["loglikelihood_scaling"] = [
            parse_noise_dict(_, percentile=0.999, multiplier=env.cfg.physics.noise_matrix_scaling) for _ in noise
        ]
        training_dataset_args.update({"pass_dictionaries": pass_dictionaries})

    if training_root is not None:
        training_dataset_args.update({"data_root": training_root})
        # Get the lists_root. Assume now the given path is with respect to the config file.
        lists_root = get_root_of_file(cfg_filename)
        if lists_root is not None:
            training_dataset_args.update({"lists_root": lists_root})
    if initial_images is not None:
        training_dataset_args.update({"initial_images": initial_images[0]})
    if initial_kspace is not None:
        training_dataset_args.update({"initial_kspaces": initial_kspace[0]})

    # Build training datasets
    training_datasets = build_training_datasets_from_environment(**training_dataset_args)
    training_data_sizes = [len(_) for _ in training_datasets]
    logger.info("Training data sizes: %s (sum=%s).", training_data_sizes, sum(training_data_sizes))

    # Create validation data
    if "validation" in env.cfg:
        validation_dataset_args = {
            "env": env,
            "datasets_config": env.cfg.validation.datasets,
            "pass_text_description": True,
        }
        if validation_root is not None:
            validation_dataset_args.update({"data_root": validation_root})
            lists_root = get_root_of_file(cfg_filename)
            if lists_root is not None:
                validation_dataset_args.update({"lists_root": lists_root})
        if initial_images is not None:
            validation_dataset_args.update({"initial_images": initial_images[1]})
        if initial_kspace is not None:
            validation_dataset_args.update({"initial_kspaces": initial_kspace[1]})

        # Build validation datasets
        validation_data = build_training_datasets_from_environment(**validation_dataset_args)
    else:
        logger.info("No validation data.")
        validation_data = None

    # Create the optimizers
    logger.info("Building optimizers.")
    optimizer_params = [{"params": env.engine.model.parameters()}]
    for curr_model_name in env.engine.models:
        # TODO(jt): Can get learning rate from the config per additional model too.
        curr_learning_rate = env.cfg.training.lr
        logger.info("Adding model parameters of %s with learning rate %s.", curr_model_name, curr_learning_rate)
        optimizer_params.append(
            {
                "params": env.engine.models[curr_model_name].parameters(),
                "lr": curr_learning_rate,
            }
        )

    optimizer: torch.optim.Optimizer = str_to_class("torch.optim", env.cfg.training.optimizer)(  # noqa
        optimizer_params,
        lr=env.cfg.training.lr,
        weight_decay=env.cfg.training.weight_decay,
    )  # noqa

    # Build the LR scheduler, we use a fixed LR schedule step size, no adaptive training schedule.
    solver_steps = list(
        range(
            env.cfg.training.lr_step_size,
            env.cfg.training.num_iterations,
            env.cfg.training.lr_step_size,
        )
    )
    lr_scheduler = WarmupMultiStepLR(
        optimizer,
        solver_steps,
        env.cfg.training.lr_gamma,
        warmup_factor=1 / 3.0,
        warmup_iterations=env.cfg.training.lr_warmup_iter,
        warmup_method="linear",
    )

    # Just to make sure.
    torch.cuda.empty_cache()

    # Check the initialization checkpoint
    if env.cfg.training.model_checkpoint:
        if initialization_checkpoint:
            logger.warning(
                "`--initialization-checkpoint is set, and config has a set `training.model_checkpoint`: %s. "
                "Will overwrite config variable with the command line: %s.",
                env.cfg.training.model_checkpoint,
                initialization_checkpoint,
            )
            # Now overwrite this in the configuration, so the correct value is dumped.
            env.cfg.training.model_checkpoint = str(initialization_checkpoint)
        else:
            initialization_checkpoint = env.cfg.training.model_checkpoint

    env.engine.train(
        optimizer,
        lr_scheduler,
        training_datasets,
        env.experiment_dir,
        validation_datasets=validation_data,
        resume=resume,
        initialization=initialization_checkpoint,
        start_with_validation=force_validation,
        num_workers=num_workers,
    )

def train_from_argparse(args: argparse.Namespace):
    # This sets MKL threads to 1.
    # DataLoader can otherwise bring a lot of difficulties when computing CPU FFTs in the transforms.
    torch.set_num_threads(1)
    os.environ["OMP_NUM_THREADS"] = "1"
    # Disable Tensorboard warnings.
    os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

    if args.initialization_images is not None and args.initialization_kspace is not None:
        sys.exit("--initialization-images and --initialization-kspace are mutually exclusive.")
    check_train_val(args.initialization_images, "initialization-images")
    check_train_val(args.initialization_kspace, "initialization-kspace")
    check_train_val(args.noise, "noise")

    set_all_seeds(args.seed)

    run_name = args.name if args.name is not None else os.path.basename(args.cfg_file)[:-5]

    # TODO(jt): Duplicate params
    launch(
        setup_train,
        args.num_machines,
        args.num_gpus,
        args.machine_rank,
        args.dist_url,
        run_name,
        args.training_root,
        args.validation_root,
        args.experiment_dir,
        args.cfg_file,
        args.force_validation,
        args.initialization_checkpoint,
        args.initialization_images,
        args.initialization_kspace,
        args.noise,
        args.device,
        args.num_workers,
        args.resume,
        args.machine_rank,
        args.mixed_precision,
        args.debug,
    )

ext: py

max_stars_repo_name: NKI-AI/direct

max_issues_repo_name: NKI-AI/direct

max_forks_repo_name: NKI-AI/direct

reason: import numpy


text:
import numpy as np
import sympy
from zquantum.core.circuit import Circuit, Qubit, Gate, create_layer_of_gates
from zquantum.core.interfaces.ansatz import Ansatz
from zquantum.core.interfaces.ansatz_utils import (
    ansatz_property,
    invalidates_parametrized_circuit,
)
from typing import Optional, List
from .ansatz_utils import get_entangling_layer

from overrides import overrides

class QCBMAnsatz(Ansatz):

    supports_parametrized_circuits = True
    number_of_qubits = ansatz_property("number_of_qubits")
    topology = ansatz_property("topology")

    def __init__(
        self, number_of_layers: int, number_of_qubits: int, topology: str = "all",
    ):
        """
        An ansatz implementation used for running the Quantum Circuit Born Machine.

        Args:
            number_of_layers (int): number of entangling layers in the circuit.
            number_of_qubits (int): number of qubits in the circuit.
            topology (str): the topology representing the connectivity of the qubits.

        Attributes:
            number_of_qubits (int): See Args
            number_of_layers (int): See Args
            topology (str): See Args
            number_of_params: number of the parameters that need to be set for the ansatz circuit.
        """
        super().__init__(number_of_layers)
        self._number_of_qubits = number_of_qubits
        self._topology = topology
        if number_of_layers == 0:
            raise ValueError("QCBMAnsatz is only defined for number_of_layers > 0.")

    @property
    def number_of_params(self) -> int:
        """
        Returns number of parameters in the ansatz.
        """
        return np.sum(self.get_number_of_parameters_by_layer())

    @property
    def n_params_per_ent_layer(self) -> int:
        if self.topology == "all":
            return int((self.number_of_qubits * (self.number_of_qubits - 1)) / 2)
        elif self.topology == "line":
            return self.number_of_qubits - 1
        else:
            raise RuntimeError("Topology {} is not supported".format(self.topology))

    @overrides
    def _generate_circuit(self, params: Optional[np.ndarray] = None) -> Circuit:
        """Builds a qcbm ansatz circuit, using the ansatz in https://advances.sciencemag.org/content/5/10/eaaw9918/tab-pdf (Fig.2 - top).

        Args:
            params (numpy.array): input parameters of the circuit (1d array).

        Returns:
            Circuit
        """
        if params is None:
            params = np.asarray(
                [sympy.Symbol("theta_{}".format(i)) for i in range(self.number_of_params)]
            )

        assert len(params) == self.number_of_params

        if self.number_of_layers == 1:
            # Only one layer, should be a single layer of rotations with Rx
            return create_layer_of_gates(self.number_of_qubits, "Rx", params)

        circuit = Circuit()
        parameter_index = 0
        for layer_index in range(self.number_of_layers):
            if layer_index == 0:
                # First layer is always 2 single qubit rotations on Rx Rz
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rx",
                    params[parameter_index : parameter_index + self.number_of_qubits],
                )
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rz",
                    params[
                        parameter_index
                        + self.number_of_qubits : parameter_index
                        + 2 * self.number_of_qubits
                    ],
                )
                parameter_index += 2 * self.number_of_qubits
            elif (
                self.number_of_layers % 2 == 1
                and layer_index == self.number_of_layers - 1
            ):
                # Last layer for odd number of layers is rotations on Rx Rz
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rz",
                    params[parameter_index : parameter_index + self.number_of_qubits],
                )
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rx",
                    params[
                        parameter_index
                        + self.number_of_qubits : parameter_index
                        + 2 * self.number_of_qubits
                    ],
                )
                parameter_index += 2 * self.number_of_qubits
            elif (
                self.number_of_layers % 2 == 0
                and layer_index == self.number_of_layers - 2
            ):
                # Even number of layers, second to last layer is 3 rotation layer with Rx Rz Rx
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rx",
                    params[parameter_index : parameter_index + self.number_of_qubits],
                )
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rz",
                    params[
                        parameter_index
                        + self.number_of_qubits : parameter_index
                        + 2 * self.number_of_qubits
                    ],
                )
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rx",
                    params[
                        parameter_index
                        + 2 * self.number_of_qubits : parameter_index
                        + 3 * self.number_of_qubits
                    ],
                )
                parameter_index += 3 * self.number_of_qubits
            elif (
                self.number_of_layers % 2 == 1
                and layer_index == self.number_of_layers - 3
            ):
                # Odd number of layers, third to last layer is 3 rotation layer with Rx Rz Rx
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rx",
                    params[parameter_index : parameter_index + self.number_of_qubits],
                )
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rz",
                    params[
                        parameter_index
                        + self.number_of_qubits : parameter_index
                        + 2 * self.number_of_qubits
                    ],
                )
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rx",
                    params[
                        parameter_index
                        + 2 * self.number_of_qubits : parameter_index
                        + 3 * self.number_of_qubits
                    ],
                )
                parameter_index += 3 * self.number_of_qubits
            elif layer_index % 2 == 1:
                # Currently on an entangling layer
                circuit += get_entangling_layer(
                    params[
                        parameter_index : parameter_index + self.n_params_per_ent_layer
                    ],
                    self.number_of_qubits,
                    "XX",
                    self.topology,
                )
                parameter_index += self.n_params_per_ent_layer
            else:
                # A normal single qubit rotation layer of Rx Rz
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rx",
                    params[parameter_index : parameter_index + self.number_of_qubits],
                )
                circuit += create_layer_of_gates(
                    self.number_of_qubits,
                    "Rz",
                    params[
                        parameter_index
                        + self.number_of_qubits : parameter_index
                        + 2 * self.number_of_qubits
                    ],
                )
                parameter_index += 2 * self.number_of_qubits

        return circuit

    def get_number_of_parameters_by_layer(self) -> np.ndarray:
        """Determine the number of parameters needed for each layer in the ansatz

        Returns:
            A 1D array of integers
        """
        if self.number_of_layers == 1:
            # If only one layer, then only need parameters for a single layer of Rx gates
            return np.asarray([self.number_of_qubits])

        num_params_by_layer = []
        for layer_index in range(self.number_of_layers):
            if layer_index == 0:
                # First layer is always 2 parameters per qubit for 2 single qubit rotations
                num_params_by_layer.append(self.number_of_qubits * 2)
            elif (
                self.number_of_layers % 2 == 1
                and layer_index == self.number_of_layers - 1
            ):
                # Last layer for odd number of layers is 2 layer rotations
                num_params_by_layer.append(self.number_of_qubits * 2)
            elif (
                self.number_of_layers % 2 == 0
                and layer_index == self.number_of_layers - 2
            ):
                # Even number of layers, second to last layer is 3 rotation layer
                num_params_by_layer.append(self.number_of_qubits * 3)
            elif (
                self.number_of_layers % 2 == 1
                and layer_index == self.number_of_layers - 3
            ):
                # Odd number of layers, third to last layer is 3 rotation layer
                num_params_by_layer.append(self.number_of_qubits * 3)
            elif layer_index % 2 == 1:
                # Currently on an entangling layer
                num_params_by_layer.append(self.n_params_per_ent_layer)
            else:
                # A normal single qubit rotation layer
                num_params_by_layer.append(self.number_of_qubits * 2)

        return np.asarray(num_params_by_layer)

max_stars_repo_name: Ben-Foxman/z-quantum-qcbm

max_issues_repo_name: Ben-Foxman/z-quantum-qcbm

max_forks_repo_name: Ben-Foxman/z-quantum-qcbm

reason: import numpy,import sympy


text:
"""
    Support functionalities
"""

import numpy as np
from .carfollow import U_I

np.random.seed(100)

def sigmoid(x, A: float = 1, a: float = 50, d: int = 250):
    """Sigmoid function"""
    return A * 1 / (1 + np.exp(-(x - d) / a))

def deriv_sigmoid(x, A: float = 1, a: float = 50, d: int = 250):
    """Sigmoid derivative function"""
    return (A / a) * sigmoid(x, 1, a, d) * (1 - sigmoid(x, 1, a, d))

def pulse_sigmoid(x, A: float = 1, d: int = 250, duration: float = 1000):
    """Sigmoid pulse minimum duration 1000 meters"""
    d = max(d, 250)  # Minimum effective delay
    duration = max(duration, 1000)  # Minimum effective duration
    effective_duration = 500 + duration - 1000  # Transforming total duration into effective
    return sigmoid(x, A, d=d) - sigmoid(x, A, d=d + effective_duration)

def deriv_pulse_sigmoid(x, A: float = 1, a: float = 50, d: int = 250, duration: float = 1000):
    """Sigmoid pulse derivative"""
    d = max(d, 250)  # Minimum effective delay
    duration = max(duration, 1000)  # Minimum effective duration
    effective_duration = 500 + duration - 1000  # Transforming total duration into effective
    return 20 * deriv_sigmoid(x=x, A=A, a=a, d=d) - 20 * deriv_sigmoid(
        x=x, A=A, a=a, d=d + effective_duration
    )

def speed_pulse(x, v0=U_I, drop: float = 1, delay: int = 250, duration: float = 1000):
    """ Create a decreasing speed pulse"""
    return v0 - pulse_sigmoid(x=x, A=drop, d=delay, duration=duration)

def acceleration_pulse(x, v0=U_I, drop: float = 1, delay: int = 250, duration: float = 1000):
    """ Create a decreasing acceleration pulse"""
    return -deriv_pulse_sigmoid(x=x, A=drop, d=delay, duration=duration)

def speed_drop(x, v0=U_I, drop: float = 1, delay: int = 250):
    """ Create a decreasing jump on speed"""
    return v0 - sigmoid(x, A=drop, d=delay)


text:
import unittest

import numpy as np
import torch

from torchfm.layer import AnovaKernel

class TestAnovaKernel(unittest.TestCase):

    def test_forward_order_2(self):
        batch_size, num_fields, embed_dim = 32, 16, 16
        kernel = AnovaKernel(order=2, reduce_sum=True).eval()
        with torch.no_grad():
            x = torch.FloatTensor(np.random.randn(batch_size, num_fields, embed_dim))
            y_true = 0
            for i in range(num_fields - 1):
                for j in range(i + 1, num_fields):
                    y_true = x[:, i, :] * x[:, j, :] + y_true
            y_true = torch.sum(y_true, dim=1, keepdim=True).numpy()
            y_pred = kernel(x).numpy()
        np.testing.assert_almost_equal(y_pred, y_true, 3)

    def test_forward_order_3(self):
        batch_size, num_fields, embed_dim = 32, 16, 16
        kernel = AnovaKernel(order=3, reduce_sum=True).eval()
        with torch.no_grad():
            x = torch.FloatTensor(np.random.randn(batch_size, num_fields, embed_dim))
            y_true = 0
            for i in range(num_fields - 2):
                for j in range(i + 1, num_fields - 1):
                    for k in range(j + 1, num_fields):
                        y_true = x[:, i, :] * x[:, j, :] * x[:, k, :] + y_true
            y_true = torch.sum(y_true, dim=1, keepdim=True).numpy()
            y_pred = kernel(x).numpy()
        np.testing.assert_almost_equal(y_pred, y_true, 3)

    def test_forward_order_4(self):
        batch_size, num_fields, embed_dim = 32, 16, 16
        kernel = AnovaKernel(order=4, reduce_sum=True).eval()
        with torch.no_grad():
            x = torch.FloatTensor(np.random.randn(batch_size, num_fields, embed_dim))
            y_true = 0
            for i in range(num_fields - 3):
                for j in range(i + 1, num_fields - 2):
                    for k in range(j + 1, num_fields - 1):
                        for l in range(k + 1, num_fields):
                            y_true = x[:, i, :] * x[:, j, :] * x[:, k, :] * x[:, l, :] + y_true
            y_true = torch.sum(y_true, dim=1, keepdim=True).numpy()
            y_pred = kernel(x).numpy()
        np.testing.assert_almost_equal(y_pred, y_true, 3)

if __name__ == '__main__':
    unittest.main(verbosity=2)

max_stars_repo_name: JazonJiao/pytorch-fm

max_issues_repo_name: JazonJiao/pytorch-fm

max_forks_repo_name: JazonJiao/pytorch-fm


text:
import glob
import xml.etree.ElementTree as ET

import numpy as np

from kmeans import kmeans, avg_iou

ANNOTATIONS_PATH = "Annotations"
CLUSTERS = 5

def load_dataset(path):
	dataset = []
	for xml_file in glob.glob("{}/*xml".format(path)):
		tree = ET.parse(xml_file)

		height = int(tree.findtext("./size/height"))
		width = int(tree.findtext("./size/width"))

		for obj in tree.iter("object"):
			xmin = int(obj.findtext("bndbox/xmin")) / width
			ymin = int(obj.findtext("bndbox/ymin")) / height
			xmax = int(obj.findtext("bndbox/xmax")) / width
			ymax = int(obj.findtext("bndbox/ymax")) / height

			dataset.append([xmax - xmin, ymax - ymin])

	return np.array(dataset)

data = load_dataset(ANNOTATIONS_PATH)
out = kmeans(data, k=CLUSTERS)
print("Accuracy: {:.2f}%".format(avg_iou(data, out) * 100))
print("Boxes:\n {}".format(out))

ratios = np.around(out[:, 0] / out[:, 1], decimals=2).tolist()
print("Ratios:\n {}".format(sorted(ratios)))

max_stars_repo_name: KevinNuNu/kmeans-anchor-boxes

max_issues_repo_name: lmhlll/kmeans-anchor-boxes

max_forks_repo_name: lmhlll/kmeans-anchor-boxes


text:
import numpy as np

# Map of color names to RGB values
COLORS = {
    'black': np.array([0, 0, 0]),
    'white': np.array([255, 255, 255])
}
# Used to map colors to integers
COLOR_TO_IDX = {
    'black': 0,
    'white': 1
}

COLOR_NAMES = []
COLOR_VALUES = []
IDX_TO_COLOR = {}

def update_global_color_variables():
    """Overwrite all global color variables when new colors are added
    to COLORS and COLOR_TO_IDX"""
    COLOR_NAMES.extend(sorted(list(COLORS.keys())))
    COLOR_VALUES.extend(list(COLORS.values()))
    IDX_TO_COLOR.update(dict(zip(COLOR_TO_IDX.values(), COLOR_TO_IDX.keys())))

def random_rgb_values():
    """pick random rgb values"""
    red = np.random.randint(0, 255)
    green = np.random.randint(0, 255)
    blue = np.random.randint(0, 255)
    return red, green, blue

def generate_colors(amount):
    """Generate Random unique colors based on set amount of agents"""
    for _ in range(amount):
        # check if the color already exists
        while True:
            red, green, blue = random_rgb_values()
            for color in COLORS:
                same_values = COLORS[color] == np.array([red, green, blue])
                if same_values.all():
                    continue
            break
        color_name = "{}.{}.{}".format(red, green, blue)
        # save values
        COLORS[color_name] = np.array([red, green, blue])
        # i+2 since black and white is already defined
        COLOR_TO_IDX[color_name] = len(COLOR_TO_IDX)
    update_global_color_variables()

max_stars_repo_name: zarahz/MARL-and-Markets

max_issues_repo_name: zarahz/MARL-and-Markets

max_forks_repo_name: zarahz/MARL-and-Markets


text:
import client
import load_data
import logging
import numpy as np
import pickle
import random
import sys
from threading import Thread
import torch
import utils.dists as dists  # pylint: disable=no-name-in-module
import math

def random_n(b1, b2, b3):
    rand_list = []
    out = [0, 0, 0, 0]
    for i in range(20):
        rand_list.append(random.randint(1, 100))
    for rand in rand_list:
        if rand <= b1:
            out[0] += 1
        elif b1 < rand <= b2:
            out[1] += 1
        elif b2 < rand <= b3:
            out[2] += 1
        else:
            out[3] += 1
    return out

def random_d(d, k=20):
    rand_list = []
    out = [0, 0, 0, 0]
    for i in range(d):
        rand_list.append(random.randint(1, 100))
    for rand in rand_list:
        if rand <= 25:
            out[0] += 1
        elif 25 < rand <= 50:
            out[1] += 1
        elif 50 < rand <= 75:
            out[2] += 1
        else:
            out[3] += 1
    pick = k
    for i in range(4):
        if pick == 0:
            out[i] = 0
        elif pick < out[i]:
            out[i] = pick
            pick = 0
        else:
            pick -= out[i]
    return out

def random_sim(sample_clients):
    ans = []
    out = random_n(25, 50, 75)
    pick = np.random.binomial(size=out[0], n=1, p=0.1)
    pick = np.append(pick, np.random.binomial(size=out[1], n=1, p=0.3))
    pick = np.append(pick, np.random.binomial(size=out[2], n=1, p=0.6))
    pick = np.append(pick, np.random.binomial(size=out[3], n=1, p=0.9))
    for i in range(len(pick)):
        if pick[i] == 1:
            ans.append(sample_clients[i])
    return ans

def fedcs_sim(sample_clients):
    ans = []
    pick = np.random.binomial(size=20, n=1, p=0.9)
    for i in range(len(pick)):
        if pick[i] == 1:
            ans.append(sample_clients[i])
    return ans

def pow_d_sim(clients_per_round, class_a, class_b, class_c, class_d):
    out = random_d(d=70)
    pick_a = np.random.binomial(size=out[0], n=1, p=0.1)
    pick_a = sum(pick_a)
    ans_a = [client for client in random.sample(class_a, pick_a)]
    pick_b = np.random.binomial(size=out[1], n=1, p=0.3)
    pick_b = sum(pick_b)
    ans_b = [client for client in random.sample(class_b, pick_b)]
    pick_c = np.random.binomial(size=out[2], n=1, p=0.6)
    pick_c = sum(pick_c)
    ans_c = [client for client in random.sample(class_c, pick_c)]
    pick_d = np.random.binomial(size=out[3], n=1, p=0.9)
    pick_d = sum(pick_d)
    ans_d = [client for client in random.sample(class_d, pick_d)]
    ans = ans_a + ans_b + ans_c + ans_d
    return ans

def _make_class(size, p):
    '''
    :param size: size og returned group
    :param p: probability of binomial distribution
    :return: array of size 'size', with 0/1 values according to p
    '''
    return np.random.binomial(size=size, n=1, p=p)

def _create_clients_group(K=100, groups=4):
    '''
    to simulate the 'heterogeneous volatility' the autors of the article divide the
    whole set of clients into 4 classes, with the success rate respectively
    set as 0.1, 0.3, 0.6, and 0.9.
    :param K: number of total active clients
    :param groups: number of different groups (with different binomial distribution of success rate
    :return: group of size K, with equally divided clients into 4 classes
    '''
    Xt = []
    group_size = int(K / groups)
    Xt = np.concatenate((_make_class(group_size, 0.1), _make_class(group_size, 0.3)))
    Xt = np.concatenate((Xt, _make_class(group_size, 0.6)))
    Xt = np.concatenate((Xt, _make_class(group_size, 0.9)))
    return Xt

def _num_sigma(T, s_type, num=1, k=20, K=100):
    def _sigma_t(t):
        return (num * k / K)

    def _inc_sigma_t(t):
        if t < (T / 4):
            return 0
        else:
            return k / K

    if s_type == "num":
        return _sigma_t
    else:
        return _inc_sigma_t

def ProbAlloc(k, sigma_t, W_t, K=100):
    '''
    :param k: the number of involved clients in each round
    :param sigma_t: fairness quota of round t
    :param W_t: exponential weights for round (vector of size K)
    :param K: total num of activate clients
    :return: - Pt: probability allocation vector for round t
             - St: overflowed set for round t
    '''
    St = []
    P_t = np.zeros(len(W_t))
    for i in range(0, len(W_t)):
        P_t[i] = sigma_t + (((k - (K * sigma_t)) * W_t[i]) / sum(W_t))
        if P_t[i] > 1:
            P_t[i] = 1
            St.append(i)

    return P_t, St

def E3CS_FL_algorithm(k, sigma_t, W_t, x_t, K=100, eta=0.5):
    '''
    :param k: the number of involved clients in each round
    :param K: accessible clients in the system
    :param eta: the learning rate of weights update
    :return: At: the selected group in round t
    '''
    At = np.zeros(k)
    Pt, St = ([] for i in range(2))

    # x_t = _create_clients_group(K)  # the success status of a client in each round
    Pt, St = ProbAlloc(k, sigma_t, W_t, K)
    Pt_tensor = torch.tensor(Pt)
    At = torch.multinomial(Pt_tensor, k, replacement=False)
    x_estimator_t = np.zeros(K)
    for i in range(0, K):
        x_estimator_t[i] = x_t[i] / Pt[i] if Pt[i] > 0.014 else x_t[i] / 0.014
        W_t[i] = W_t[i] if (i in St) else W_t[i] * math.exp((k - (K * sigma_t)) * eta * x_estimator_t[i] / K)
    return At, W_t

class Server(object):
    """Basic federated learning server."""

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

    # Set up server
    def boot(self):
        logging.info('Booting {} server...'.format(self.config.server))

        model_path = self.config.paths.model
        total_clients = self.config.clients.total

        # Add fl_model to import path
        sys.path.append(model_path)

        # Set up simulated server
        self.load_data()
        self.load_model()
        self.make_clients(total_clients)

    def load_data(self):
        import fl_model  # pylint: disable=import-error

        # Extract config for loaders
        config = self.config

        # Set up data generator
        generator = fl_model.Generator()

        # Generate data
        data_path = self.config.paths.data
        data = generator.generate(data_path)
        labels = generator.labels

        logging.info('Dataset size: {}'.format(
            sum([len(x) for x in [data[label] for label in labels]])))
        logging.debug('Labels ({}): {}'.format(
            len(labels), labels))

        # Set up data loader
        self.loader = {
            'basic': load_data.Loader(config, generator),
            'bias': load_data.BiasLoader(config, generator),
            'shard': load_data.ShardLoader(config, generator)
        }[self.config.loader]

        logging.info('Loader: {}, IID: {}'.format(
            self.config.loader, self.config.data.IID))

    def load_model(self):
        import fl_model  # pylint: disable=import-error

        model_path = self.config.paths.model
        model_type = self.config.model

        logging.info('Model: {}'.format(model_type))

        # Set up global model
        self.model = fl_model.Net()
        self.save_model(self.model, model_path)

        # Extract flattened weights (if applicable)
        if self.config.paths.reports:
            self.saved_reports = {}
            self.save_reports(0, [])  # Save initial model

    def make_clients(self, num_clients):
        IID = self.config.data.IID
        labels = self.loader.labels
        loader = self.config.loader
        loading = self.config.data.loading

        if not IID:  # Create distribution for label preferences if non-IID
            dist = {
                "uniform": dists.uniform(num_clients, len(labels)),
                "normal": dists.normal(num_clients, len(labels))
            }[self.config.clients.label_distribution]
            random.shuffle(dist)  # Shuffle distribution

        # Make simulated clients
        clients = []
        for client_id in range(num_clients):

            # Create new client
            new_client = client.Client(client_id)

            if not IID:  # Configure clients for non-IID data
                if self.config.data.bias:
                    # Bias data partitions
                    bias = self.config.data.bias
                    # Choose weighted random preference
                    pref = random.choices(labels, dist)[0]

                    # Assign preference, bias config
                    new_client.set_bias(pref, bias)
                elif self.config.data.shard:
                    # Shard data partitions
                    shard = self.config.data.shard

                    # Assign shard config
                    new_client.set_shard(shard)

            clients.append(new_client)

        logging.info('Total clients: {}'.format(len(clients)))

        if loader == 'bias':
            logging.info('Label distribution: {}'.format(
                [[client.pref for client in clients].count(label) for label in labels]))

        if loading == 'static':
            if loader == 'shard':  # Create data shards
                self.loader.create_shards()

            # Send data partition to all clients
            [self.set_client_data(client) for client in clients]

        self.clients = clients

    # Run federated learning
    def run(self):
        name_file = "output_cifar_pow-d=70_iid_a.txt"
        rounds = self.config.fl.rounds
        target_accuracy = self.config.fl.target_accuracy
        reports_path = self.config.paths.reports
        f = open(name_file, 'w')
        f.write(self.config.model + "  " + self.config.method + "  \n")
        f.close()

        if target_accuracy:
            logging.info('Training: {} rounds or {}% accuracy\n'.format(
                rounds, 100 * target_accuracy))
        else:
            logging.info('Training: {} rounds\n'.format(rounds))

        # Perform rounds of federated learning
        for round in range(1, rounds + 1):
            logging.info('**** Round {}/{} ****'.format(round, rounds))

            # Run the federated learning round
            accuracy = self.round(round)
            f = open(name_file, 'a')
            f.write("round: " + str(round) + "    accuracy: " + str(accuracy * 100) + " \n")
            f.close()

            # Break loop when target accuracy is met
            if target_accuracy and (accuracy >= target_accuracy):
                logging.info('Target accuracy reached.')
                f.write("done this test\n")
                f.close()
                break

        if reports_path:
            with open(reports_path, 'wb') as f:
                pickle.dump(self.saved_reports, f)
            logging.info('Saved reports: {}'.format(reports_path))

    def round(self, round):
        import fl_model  # pylint: disable=import-error
        if self.config.method == "E3CS_0" or self.config.method == "E3CS_05" or self.config.method == "E3CS_08" or self.config.method == "E3CS_inc":
            if round == 1:
                W_t_old = np.ones(100)  # change according to weights of round t
            else:
                W_t_old = self.W_t
        # Select clients to participate in the round
        if self.config.method == "E3CS_0" or self.config.method == "E3CS_05" or self.config.method == "E3CS_08" or self.config.method == "E3CS_inc":
            sample_clients, W_t, A_t, sample_index = self.selection(round, W_t_old)
            self.W_t = W_t
        else:
            sample_clients = self.selection(round, w_t_up=0)

        # Configure sample clients
        self.configuration(sample_clients)

        # Run clients using multithreading for better parallelism
        threads = [Thread(target=client.run) for client in sample_clients]
        [t.start() for t in threads]
        [t.join() for t in threads]

        # Recieve client updates
        reports = self.reporting(sample_clients)

        # Perform weight aggregation
        logging.info('Aggregating updates')
        if self.config.method == "E3CS_0" or self.config.method == "E3CS_05" or self.config.method == "E3CS_08" or self.config.method == "E3CS_inc":
            updated_weights = self.aggregation(reports, W_t_old, A_t, sample_index)
        else:
            updated_weights = self.aggregation(reports, 0, A_t=0, sample_index=0)

        # Load updated weights
        fl_model.load_weights(self.model, updated_weights)

        # Extract flattened weights (if applicable)
        if self.config.paths.reports:
            self.save_reports(round, reports)

        # Save updated global model
        self.save_model(self.model, self.config.paths.model)

        # Test global model accuracy
        if self.config.clients.do_test:  # Get average accuracy from client reports
            accuracy = self.accuracy_averaging(reports)
        else:  # Test updated model on server
            testset = self.loader.get_testset()
            batch_size = self.config.fl.batch_size
            testloader = fl_model.get_testloader(testset, batch_size)
            accuracy = fl_model.test(self.model, testloader)

        logging.info('Average accuracy: {:.2f}%\n'.format(100 * accuracy))
        return accuracy

    # Federated learning phases

    def selection(self, t, w_t_up):
        clients_per_round = self.config.clients.per_round

        if self.config.method == "random":

            # Select devices to participate in round

            # Select clients randomly
            sample_clients = [client for client in random.sample(
                self.clients, clients_per_round)]

            return random_sim(sample_clients)

        elif self.config.method == "FedCS":
            class_d = []
            for i in range(25):
                class_d.append(self.clients[i])
            sample_clients = [client for client in random.sample(class_d, clients_per_round)]

            return fedcs_sim(sample_clients)

        elif self.config.method == "pow-d":
            class_a = []
            class_b = []
            class_c = []
            class_d = []
            for i in range(25):
                class_a.append(self.clients[i])
                class_b.append(self.clients[i + 25])
                class_c.append(self.clients[i + 50])
                class_d.append(self.clients[i + 75])
            return pow_d_sim(clients_per_round, class_a, class_b, class_c, class_d)

        elif self.config.method == "E3CS_0":
            k = 20  # num of selected clients in each round
            K = 100  # num of total activated clients
            T = 2500  # num of total rounds
            sample_client = []
            sample_index = []
            Xt = _create_clients_group(K)  # the success status of a client in each round
            sigma_t = (_num_sigma(T, s_type="num", num=0))(t)  # the minimum selection probability of each client
            At, Wt = E3CS_FL_algorithm(k=k, sigma_t=sigma_t, x_t=Xt, W_t=w_t_up, K=K, eta=0.5)
            At = At.detach().numpy()
            for i in range(k):
                if Xt[At[i]] == 1:
                    sample_client.append(self.clients[int(At[i])])
                    sample_index.append(int(At[i]))

            return sample_client, Wt, At, sample_index

        elif self.config.method == "E3CS_05":
            k = 20  # num of selected clients in each round
            K = 100  # num of total activated clients
            T = 2500  # num of total rounds
            sample_client = []
            sample_index = []
            Xt = _create_clients_group(K)  # the success status of a client in each round
            sigma_t = (_num_sigma(T, s_type="num", num=0.5))(t)  # the minimum selection probability of each client
            At, Wt = E3CS_FL_algorithm(k=k, sigma_t=sigma_t, x_t=Xt, W_t=w_t_up, K=K, eta=0.5)
            At = At.detach().numpy()
            for i in range(k):
                if Xt[At[i]] == 1:
                    sample_client.append(self.clients[int(At[i])])
                    sample_index.append(int(At[i]))

            return sample_client, Wt, At, sample_index

        elif self.config.method == "E3CS_08":
            k = 20  # num of selected clients in each round
            K = 100  # num of total activated clients
            T = 2500  # num of total rounds
            sample_client = []
            sample_index = []
            Xt = _create_clients_group(K)  # the success status of a client in each round
            sigma_t = (_num_sigma(T, s_type="num", num=0.8))(t)  # the minimum selection probability of each client
            At, Wt = E3CS_FL_algorithm(k=k, sigma_t=sigma_t, x_t=Xt, W_t=w_t_up, K=K, eta=0.5)
            At = At.detach().numpy()
            for i in range(k):
                if Xt[At[i]] == 1:
                    sample_client.append(self.clients[int(At[i])])
                    sample_index.append(int(At[i]))

            return sample_client, Wt, At, sample_index

        elif self.config.method == "E3CS_inc":
            k = 20  # num of selected clients in each round
            K = 100  # num of total activated clients
            T = 2500  # num of total rounds
            sample_client = []
            sample_index = []
            Xt = _create_clients_group(K)  # the success status of a client in each round
            sigma_t = (_num_sigma(T, s_type="inc", num=1))(t)  # the minimum selection probability of each client
            At, Wt = E3CS_FL_algorithm(k=k, sigma_t=sigma_t, x_t=Xt, W_t=w_t_up, K=K, eta=0.5)
            At = At.detach().numpy()
            for i in range(k):
                if Xt[At[i]] == 1:
                    sample_client.append(self.clients[int(At[i])])
                    sample_index.append(int(At[i]))

            return sample_client, Wt, At, sample_index

    def configuration(self, sample_clients):
        loader_type = self.config.loader
        loading = self.config.data.loading

        if loading == 'dynamic':
            # Create shards if applicable
            if loader_type == 'shard':
                self.loader.create_shards()

        # Configure selected clients for federated learning task
        for client in sample_clients:
            if loading == 'dynamic':
                self.set_client_data(client)  # Send data partition to client

            # Extract config for client
            config = self.config

            # Continue configuraion on client
            client.configure(config)

    def reporting(self, sample_clients):
        # Recieve reports from sample clients
        reports = [client.get_report() for client in sample_clients]

        logging.info('Reports recieved: {}'.format(len(reports)))
        assert len(reports) == len(sample_clients)

        return reports

    def aggregation(self, reports, updated_W, A_t, sample_index):
        return self.federated_averaging(reports, updated_W, A_t, sample_index)

    # Report aggregation
    def extract_client_updates(self, reports):
        import fl_model  # pylint: disable=import-error

        # Extract baseline model weights
        baseline_weights = fl_model.extract_weights(self.model)

        # Extract weights from reports
        weights = [report.weights for report in reports]

        # Calculate updates from weights
        updates = []
        for weight in weights:
            update = []
            for i, (name, weight) in enumerate(weight):
                bl_name, baseline = baseline_weights[i]

                # Ensure correct weight is being updated
                assert name == bl_name

                # Calculate update
                if self.config.method == "E3CS_0" or self.config.method == "E3CS_05" or self.config.method == "E3CS_08" or self.config.method == "E3CS_inc":
                    delta = weight
                else:
                    delta = weight - baseline
                update.append((name, delta))
            updates.append(update)

        return updates

    def federated_averaging(self, reports, W_t, A_t, sample_index):
        import fl_model  # pylint: disable=import-error

        # Extract updates from reports
        updates = self.extract_client_updates(reports)

        # Extract total number of samples
        if self.config.method == "E3CS_0" or self.config.method == "E3CS_05" or self.config.method == "E3CS_08" or self.config.method == "E3CS_inc":
            total_W = sum(W_t)
        else:
            total_samples = sum([report.num_samples for report in reports])

        if len(updates) != 0:
            # Perform weighted averaging
            avg_update = [torch.zeros(x.size())  # pylint: disable=no-member
                          for _, x in updates[0]]

            if self.config.method == "E3CS_0" or self.config.method == "E3CS_05" or self.config.method == "E3CS_08" or self.config.method == "E3CS_inc":
                for i, update in enumerate(updates):
                    num_samples = reports[i].num_samples
                    for j, (_, delta) in enumerate(update):
                        # Use weighted average by number of samples
                        avg_update[j] += delta * (W_t[sample_index[i]] / total_W)

            else:
                for i, update in enumerate(updates):
                    num_samples = reports[i].num_samples
                    for j, (_, delta) in enumerate(update):
                        # Use weighted average by number of samples
                        avg_update[j] += delta * (num_samples / total_samples)

        # Extract baseline model weights
        else:
            baseline_weights = fl_model.extract_weights(self.model)
            avg_update = []
            for i, _ in enumerate(baseline_weights):
                avg_update.append(0)

        baseline_weights = fl_model.extract_weights(self.model)

        # Load updated weights into model
        updated_weights = []
        if self.config.method == "E3CS_0" or self.config.method == "E3CS_05" or self.config.method == "E3CS_08" or self.config.method == "E3CS_inc":
            for i, (name, weight) in enumerate(baseline_weights):
                ne_weight = 0
                for j in range(100):
                    if not (j in sample_index):
                        ne_weight += weight * (W_t[j] / total_W)
                updated_weights.append((name, ne_weight + avg_update[i]))
        else:
            for i, (name, weight) in enumerate(baseline_weights):
                updated_weights.append((name, weight + avg_update[i]))

        return updated_weights

    def accuracy_averaging(self, reports):
        # Get total number of samples
        total_samples = sum([report.num_samples for report in reports])

        # Perform weighted averaging
        accuracy = 0
        for report in reports:
            accuracy += report.accuracy * (report.num_samples / total_samples)

        return accuracy

    # Server operations
    @staticmethod
    def flatten_weights(weights):
        # Flatten weights into vectors
        weight_vecs = []
        for _, weight in weights:
            weight_vecs.extend(weight.flatten().tolist())

        return np.array(weight_vecs)

    def set_client_data(self, client):
        loader = self.config.loader

        # Get data partition size
        if loader != 'shard':
            if self.config.data.partition.get('size'):
                partition_size = self.config.data.partition.get('size')
            elif self.config.data.partition.get('range'):
                start, stop = self.config.data.partition.get('range')
                partition_size = random.randint(start, stop)

        # Extract data partition for client
        if loader == 'basic':
            data = self.loader.get_partition(partition_size)
        elif loader == 'bias':
            data = self.loader.get_partition(partition_size, client.pref)
        elif loader == 'shard':
            data = self.loader.get_partition()
        else:
            logging.critical('Unknown data loader type')

        # Send data to client
        client.set_data(data, self.config)

    def save_model(self, model, path):
        path += '/global'
        torch.save(model.state_dict(), path)
        logging.info('Saved global model: {}'.format(path))

    def save_reports(self, round, reports):
        import fl_model  # pylint: disable=import-error

        if reports:
            self.saved_reports['round{}'.format(round)] = [(report.client_id, self.flatten_weights(
                report.weights)) for report in reports]

        # Extract global weights
        self.saved_reports['w{}'.format(round)] = self.flatten_weights(
            fl_model.extract_weights(self.model))


text:
#!/usr/bin/python3
'''
Description:
------------
A script that retrieves financial data via the API of Alpha Vantage

Requirements:
------------
Install the alpha-vantage module via pip: "pip install alpha-vantage"
Make sure also that Pandas, pandas_datareader, numpy and xlrd are installed

You have to claim a free API key from https://www.alphavantage.co/documentation/

Usage:
-----
python ImportAlphaVantageAPI.py

Dr. Hussein Bakri
Enjoy!
'''

'''
Some famous companies stocks tikers to work with
------------------------------------------------
Apple                               AAPL
Procter & Gamble Co                 PG
Microsoft Corporation               MSFT
Exxon Mobil Corporation             XOM
BP plc                              BP
AT&T Inc.                           T
Ford Motor Company                  F
General Electric Company            GE
Alphabet Inc Class A (Google)       GOOGL
'''

API_KEY = "YOUR_API_KEY_HERE"
import numpy as np
import pandas as pd
from pandas_datareader import data as wb

from alpha_vantage.timeseries import TimeSeries

ts = TimeSeries(key = API_KEY, output_format='pandas')

# get_daily_adjusted() method obtains the adjusted closing prices for the designated stock.
PG_data, PG_metadata = ts.get_daily_adjusted('PG', outputsize='full')
# print(PG_data)

# Apple company stocks ticker is 'AAPL'
AAPL_data, AAPL_metadata = ts.get_daily_adjusted('AAPL', outputsize='full')
# print(AAPL_data)

# Apart from US stocks, you can retrieve data about foreign stocks (e.g. Beiersdorf, ticker: 'BEI.DE'),
# or market indices (e.g. Standard & Poor's, ticker: '^GSPC', or Dow Jones, ticker: '^DJI').
GSPC_data, GSPC_metadata = ts.get_daily_adjusted('^GSPC', outputsize='full')
# print(GSPC_data)
GSPC_data.info()

print(GSPC_data.head(15))

# To combine specific columns from various stocks, one would need to concatenate the columns of interest by using the .concat() function to specify which columns you want to extract.
portfolio = pd.concat([PG_data['5. adjusted close'], GSPC_data['5. adjusted close']], axis = 1)
portfolio.columns = ['PG Adjusted Close', 'GSPC Adjusted Close']
# print(portfolio)

############ Saving Apple Data ###########
############ Saving as CSV ################
AAPL_data.to_csv('AAPL_data.csv')

############ Saving as Excel #############
AAPL_data.to_excel('AAPL_data.xlsx')

# To read back from CSV
AAPL_data_CSV = pd.read_csv('AAPL_data.csv')
# print(AAPL_data_CSV.head())

# To read back from Excel
AAPL_data_EXCEL = pd.read_excel('AAPL_data.xlsx')
# print(AAPL_data_EXCEL.head())

max_stars_repo_name: HusseinBakri/PythonFinancialRepertoire

max_issues_repo_name: HusseinBakri/PythonFinancialRepertoire

max_forks_repo_name: HusseinBakri/PythonFinancialRepertoire


text:
import mahotas as mh
from sklearn import cross_validation
from sklearn.linear_model.logistic import LogisticRegression
import numpy as np
from glob import glob
from edginess import edginess_sobel

def features_for(im):
    im = mh.imread(im,as_grey=True).astype(np.uint8)
    #return mh.features.haralick(im).mean(0)
    return np.squeeze(mh.features.haralick(im)).reshape(-1)

features = []

####################################################
print('SURFing ...')
tfeatures = features
from sklearn.cluster import KMeans
from mahotas.features import surf

basedir = 'small_train-dogs-cats'
images = glob('{}/*.jpg'.format(basedir))
alldescriptors = []
i = 0;
for im in images:
    im = mh.imread(im, as_grey=1)
    im = im.astype(np.uint8)
    alldescriptors.append(surf.surf(im, descriptor_only=True))
    i += 1
    print ('image:'+str(i))

print('Descriptors done')
k = 256
km = KMeans(k)

concatenated = np.concatenate(alldescriptors)
concatenated = concatenated[::64]
print('k-meaning...')
km.fit(concatenated)
features = []

basedir = 'test_dogs_vs_cats'
images = glob('{}/*.jpg'.format(basedir))
ims = len(images)
    #for im in images:
for i in range(1,ims+1):
    im = 'test_dogs_vs_cats/'+str(i)+'.jpg'
    print('processing ' +str(im) +' ...')
    im = mh.imread(im, as_grey=1)
    im = im.astype(np.uint8)
    d = surf.surf(im, descriptor_only=True)
    c = km.predict(d)
    features.append(
                    np.array([np.sum(c == i) for i in xrange(k)])
                    )

features = np.array(features)

np.savetxt("test_K-MEANS-ON_TRAINSET_featuresDogsCatsSURF.zat", features, delimiter=",")
np.savetxt("test_SURF_concatenated.zat", concatenated, delimiter=",")

max_stars_repo_name: gtesei/fast-furious

max_issues_repo_name: gtesei/fast-furious

max_forks_repo_name: gtesei/fast-furious


text:
import requests
import os
import json
from threading import Lock
import time
import os
from imageio import imread
import io
import uuid
import numpy as np
import base64
import cv2
import ast
from requests_toolbelt.multipart.encoder import MultipartEncoder
#from '/home/jetsonnano/Imagens/' import test.png

URL = "https://itdi-test-api.herokuapp.com/inference/"
#http://127.0.0.1:8000/

def addNewInference(body):
    # lock = Lock()
    # lock.acquire()

    #------------------ Save the images ------------------#

    # Convert the data to object
    # CASO USAR RABBIT MQ, DESCOMENTAR ABAIXO
    # body = ast.literal_eval(body.decode('utf-8'))

    # grab the variables
    # img_str = body['0']
    # cls_image = body['1']
    # confs = body['2']

    img = body[0]
    cls_image = body[1]
    confs = body[2]

    # build the image
    # img_b64 = imread(io.BytesIO(base64.b64decode(img_str)))
    # img = cv2.cvtColor(np.array(img_b64), cv2.COLOR_BGR2RGB)

    # Gerate an uuid for the image
    uuidImage = uuid.uuid4().hex

    # Variables configs
    directory = '/home/jetsonnano/captured_images/'
    img_name_withoutInference = ''
    img_name_withInference = ''
    txt_name = ''

    #create the directory to store the images
    try:
        if not os.path.exists(directory + 'Img' + str(uuidImage)):
            os.makedirs(directory + 'Img' + str(uuidImage))
    except OSError:
        print('Error: Creating directory. ' + directory)
        return

    # Grab the path
    path = directory + 'Img' + str(uuidImage) + '/'

    # Save the images without yolo-inference
    img_name = path + 'img' + str(uuidImage) + '.png'
    img_name_withoutInference = 'img' + str(uuidImage) + '.png'
    txt_name = 'img' + str(uuidImage)
    cv2.imwrite(img_name, img)

    # Write the .txt file with the classes
    f = open(path + 'img' + str(uuidImage) + ".txt", "x")
    i = 0
    for i in range (0, len(cls_image)):
        accuracy = '{:.2f}'.format(confs[i] * 100)
        f.write(str(cls_image[i]) + ' ' + str(accuracy) + '\n')
        i += 1

    f.close()

    # Write the image with the boxes
    img_name = path + 'imgYolo' + str(uuidImage) + '.png'
    img_name_withInference = 'imgYolo' + str(uuidImage) + '.png'
    cv2.imwrite(img_name, img)

    #----------------------------------------------------#

    #------------------ Send to the server ------------------#

    normalImage = img_name_withoutInference
    inferenceImage = img_name_withInference

    url = URL
    #f = open(path + txtName + '.txt', 'r')
    #inference = {f}
    #print(f.read())
    dict1 = {}

    with open(path + txt_name + '.txt', 'r') as f:
        for line in f:
            # reads each line and trims of extra the spaces
            # and gives only the valid words
            command, description = line.strip().split(None, 1)

            dict1[command] = description.strip()

    print(dict1)
    multipart_data = MultipartEncoder(
    fields={
            "normal_image": (
                normalImage,
                open(path + normalImage, 'rb'),
                "image/png"
            ),
            "inferred_image": (
                inferenceImage,
                open(path + inferenceImage, 'rb'),
                "image/png"
            ),
            "inference": json.dumps(dict1),
            "created_at": time.strftime('%Y/%m/%d %H:%M:%S', time.localtime())
        }
    )

    try:
        response = requests.post(
            url,
            headers={
                "Content-Type": multipart_data.content_type,
            },
            data=multipart_data,
        )

        print(response)

        # time.sleep(7)
        # lock.release()

        return
    except Exception as e:
            print(str(e), 'request error')

            # time.sleep(7)
            # lock.release()

            return

#addNewInference()

max_stars_repo_name: Jhow-Rambo/itdt-microservices

max_issues_repo_name: Jhow-Rambo/itdt-microservices

max_forks_repo_name: Jhow-Rambo/itdt-microservices


text:
from gym.spaces import Box, MultiDiscrete, Tuple as TupleSpace
import logging
import numpy as np
import random
import time
from typing import Callable, Optional, Tuple

from ray.rllib.env.multi_agent_env import MultiAgentEnv
from ray.rllib.policy.policy import PolicySpec
from ray.rllib.utils.typing import MultiAgentDict, PolicyID, AgentID

logger = logging.getLogger(__name__)

class Unity3DEnv(MultiAgentEnv):
    """A MultiAgentEnv representing a single Unity3D game instance.

    For an example on how to use this Env with a running Unity3D editor
    or with a compiled game, see:
    `rllib/examples/unity3d_env_local.py`
    For an example on how to use it inside a Unity game client, which
    connects to an RLlib Policy server, see:
    `rllib/examples/serving/unity3d_[client|server].py`

    Supports all Unity3D (MLAgents) examples, multi- or single-agent and
    gets converted automatically into an ExternalMultiAgentEnv, when used
    inside an RLlib PolicyClient for cloud/distributed training of Unity games.
    """

    # Default base port when connecting directly to the Editor
    _BASE_PORT_EDITOR = 5004
    # Default base port when connecting to a compiled environment
    _BASE_PORT_ENVIRONMENT = 5005
    # The worker_id for each environment instance
    _WORKER_ID = 0

    def __init__(self,
                 file_name: str = None,
                 port: Optional[int] = None,
                 seed: int = 0,
                 no_graphics: bool = False,
                 timeout_wait: int = 300,
                 episode_horizon: int = 1000):
        """Initializes a Unity3DEnv object.

        Args:
            file_name (Optional[str]): Name of the Unity game binary.
                If None, will assume a locally running Unity3D editor
                to be used, instead.
            port (Optional[int]): Port number to connect to Unity environment.
            seed (int): A random seed value to use for the Unity3D game.
            no_graphics (bool): Whether to run the Unity3D simulator in
                no-graphics mode. Default: False.
            timeout_wait (int): Time (in seconds) to wait for connection from
                the Unity3D instance.
            episode_horizon (int): A hard horizon to abide to. After at most
                this many steps (per-agent episode `step()` calls), the
                Unity3D game is reset and will start again (finishing the
                multi-agent episode that the game represents).
                Note: The game itself may contain its own episode length
                limits, which are always obeyed (on top of this value here).
        """

        super().__init__()

        if file_name is None:
            print(
                "No game binary provided, will use a running Unity editor "
                "instead.\nMake sure you are pressing the Play (|>) button in "
                "your editor to start.")

        import mlagents_envs
        from mlagents_envs.environment import UnityEnvironment

        # Try connecting to the Unity3D game instance. If a port is blocked
        port_ = None
        while True:
            # Sleep for random time to allow for concurrent startup of many
            # environments (num_workers >> 1). Otherwise, would lead to port
            # conflicts sometimes.
            if port_ is not None:
                time.sleep(random.randint(1, 10))
            port_ = port or (self._BASE_PORT_ENVIRONMENT
                             if file_name else self._BASE_PORT_EDITOR)
            # cache the worker_id and
            # increase it for the next environment
            worker_id_ = Unity3DEnv._WORKER_ID if file_name else 0
            Unity3DEnv._WORKER_ID += 1
            try:
                self.unity_env = UnityEnvironment(
                    file_name=file_name,
                    worker_id=worker_id_,
                    base_port=port_,
                    seed=seed,
                    no_graphics=no_graphics,
                    timeout_wait=timeout_wait,
                )
                print(
                    "Created UnityEnvironment for port {}".format(port_ +
                                                                  worker_id_))
            except mlagents_envs.exception.UnityWorkerInUseException:
                pass
            else:
                break

        # ML-Agents API version.
        self.api_version = self.unity_env.API_VERSION.split(".")
        self.api_version = [int(s) for s in self.api_version]

        # Reset entire env every this number of step calls.
        self.episode_horizon = episode_horizon
        # Keep track of how many times we have called `step` so far.
        self.episode_timesteps = 0

    def step(
            self, action_dict: MultiAgentDict
    ) -> Tuple[MultiAgentDict, MultiAgentDict, MultiAgentDict, MultiAgentDict]:
        """Performs one multi-agent step through the game.

        Args:
            action_dict (dict): Multi-agent action dict with:
                keys=agent identifier consisting of
                [MLagents behavior name, e.g. "Goalie?team=1"] + "_" +
                [Agent index, a unique MLAgent-assigned index per single agent]

        Returns:
            tuple:
                - obs: Multi-agent observation dict.
                    Only those observations for which to get new actions are
                    returned.
                - rewards: Rewards dict matching `obs`.
                - dones: Done dict with only an __all__ multi-agent entry in
                    it. __all__=True, if episode is done for all agents.
                - infos: An (empty) info dict.
        """
        from mlagents_envs.base_env import ActionTuple

        # Set only the required actions (from the DecisionSteps) in Unity3D.
        all_agents = []
        for behavior_name in self.unity_env.behavior_specs:
            # New ML-Agents API: Set all agents actions at the same time
            # via an ActionTuple. Since API v1.4.0.
            if self.api_version[0] > 1 or (self.api_version[0] == 1
                                           and self.api_version[1] >= 4):
                actions = []
                for agent_id in self.unity_env.get_steps(behavior_name)[
                        0].agent_id:
                    key = behavior_name + "_{}".format(agent_id)
                    all_agents.append(key)
                    actions.append(action_dict[key])
                if actions:
                    if actions[0].dtype == np.float32:
                        action_tuple = ActionTuple(
                            continuous=np.array(actions))
                    else:
                        action_tuple = ActionTuple(discrete=np.array(actions))
                    self.unity_env.set_actions(behavior_name, action_tuple)
            # Old behavior: Do not use an ActionTuple and set each agent's
            # action individually.
            else:
                for agent_id in self.unity_env.get_steps(behavior_name)[
                        0].agent_id_to_index.keys():
                    key = behavior_name + "_{}".format(agent_id)
                    all_agents.append(key)
                    self.unity_env.set_action_for_agent(
                        behavior_name, agent_id, action_dict[key])
        # Do the step.
        self.unity_env.step()

        obs, rewards, dones, infos = self._get_step_results()

        # Global horizon reached? -> Return __all__ done=True, so user
        # can reset. Set all agents' individual `done` to True as well.
        self.episode_timesteps += 1
        if self.episode_timesteps > self.episode_horizon:
            return obs, rewards, dict({
                "__all__": True
            }, **{agent_id: True
                  for agent_id in all_agents}), infos

        return obs, rewards, dones, infos

    def reset(self) -> MultiAgentDict:
        """Resets the entire Unity3D scene (a single multi-agent episode)."""
        self.episode_timesteps = 0
        self.unity_env.reset()
        obs, _, _, _ = self._get_step_results()
        return obs

    def _get_step_results(self):
        """Collects those agents' obs/rewards that have to act in next `step`.

        Returns:
            Tuple:
                obs: Multi-agent observation dict.
                    Only those observations for which to get new actions are
                    returned.
                rewards: Rewards dict matching `obs`.
                dones: Done dict with only an __all__ multi-agent entry in it.
                    __all__=True, if episode is done for all agents.
                infos: An (empty) info dict.
        """
        obs = {}
        rewards = {}
        infos = {}
        for behavior_name in self.unity_env.behavior_specs:
            decision_steps, terminal_steps = self.unity_env.get_steps(
                behavior_name)
            # Important: Only update those sub-envs that are currently
            # available within _env_state.
            # Loop through all envs ("agents") and fill in, whatever
            # information we have.
            for agent_id, idx in decision_steps.agent_id_to_index.items():
                key = behavior_name + "_{}".format(agent_id)
                os = tuple(o[idx] for o in decision_steps.obs)
                os = os[0] if len(os) == 1 else os
                obs[key] = os
                rewards[key] = decision_steps.reward[idx]  # rewards vector
            for agent_id, idx in terminal_steps.agent_id_to_index.items():
                key = behavior_name + "_{}".format(agent_id)
                # Only overwrite rewards (last reward in episode), b/c obs
                # here is the last obs (which doesn't matter anyways).
                # Unless key does not exist in obs.
                if key not in obs:
                    os = tuple(o[idx] for o in terminal_steps.obs)
                    obs[key] = os = os[0] if len(os) == 1 else os
                rewards[key] = terminal_steps.reward[idx]  # rewards vector

        # Only use dones if all agents are done, then we should do a reset.
        return obs, rewards, {"__all__": False}, infos

    @staticmethod
    def get_policy_configs_for_game(
            game_name: str) -> Tuple[dict, Callable[[AgentID], PolicyID]]:

        # The RLlib server must know about the Spaces that the Client will be
        # using inside Unity3D, up-front.
        obs_spaces = {
            # 3DBall.
            "3DBall": Box(float("-inf"), float("inf"), (8, )),
            # 3DBallHard.
            "3DBallHard": Box(float("-inf"), float("inf"), (45, )),
            # GridFoodCollector
            "GridFoodCollector": Box(float("-inf"), float("inf"), (40, 40, 6)),
            # Pyramids.
            "Pyramids": TupleSpace([
                Box(float("-inf"), float("inf"), (56, )),
                Box(float("-inf"), float("inf"), (56, )),
                Box(float("-inf"), float("inf"), (56, )),
                Box(float("-inf"), float("inf"), (4, )),
            ]),
            # SoccerStrikersVsGoalie.
            "Goalie": Box(float("-inf"), float("inf"), (738, )),
            "Striker": TupleSpace([
                Box(float("-inf"), float("inf"), (231, )),
                Box(float("-inf"), float("inf"), (63, )),
            ]),
            # Sorter.
            "Sorter": TupleSpace([
                Box(float("-inf"), float("inf"), (
                    20,
                    23,
                )),
                Box(float("-inf"), float("inf"), (10, )),
                Box(float("-inf"), float("inf"), (8, )),
            ]),
            # Tennis.
            "Tennis": Box(float("-inf"), float("inf"), (27, )),
            # VisualHallway.
            "VisualHallway": Box(float("-inf"), float("inf"), (84, 84, 3)),
            # Walker.
            "Walker": Box(float("-inf"), float("inf"), (307, )),
            # FoodCollector.
            "FoodCollector": TupleSpace([
                Box(float("-inf"), float("inf"), (49, )),
                Box(float("-inf"), float("inf"), (4, )),
            ]),
        }
        action_spaces = {
            # 3DBall.
            "3DBall": Box(
                float("-inf"), float("inf"), (2, ), dtype=np.float32),
            # 3DBallHard.
            "3DBallHard": Box(
                float("-inf"), float("inf"), (2, ), dtype=np.float32),
            # GridFoodCollector.
            "GridFoodCollector": MultiDiscrete([3, 3, 3, 2]),
            # Pyramids.
            "Pyramids": MultiDiscrete([5]),
            # SoccerStrikersVsGoalie.
            "Goalie": MultiDiscrete([3, 3, 3]),
            "Striker": MultiDiscrete([3, 3, 3]),
            # Sorter.
            "Sorter": MultiDiscrete([3, 3, 3]),
            # Tennis.
            "Tennis": Box(float("-inf"), float("inf"), (3, )),
            # VisualHallway.
            "VisualHallway": MultiDiscrete([5]),
            # Walker.
            "Walker": Box(float("-inf"), float("inf"), (46, )),
            # FoodCollector.
            "FoodCollector": MultiDiscrete([3, 3, 3, 2]),
        }

        # Policies (Unity: "behaviors") and agent-to-policy mapping fns.
        if game_name == "SoccerStrikersVsGoalie":
            policies = {
                "Goalie": PolicySpec(
                    observation_space=obs_spaces["Goalie"],
                    action_space=action_spaces["Goalie"]),
                "Striker": PolicySpec(
                    observation_space=obs_spaces["Striker"],
                    action_space=action_spaces["Striker"]),
            }

            def policy_mapping_fn(agent_id, episode, worker, **kwargs):
                return "Striker" if "Striker" in agent_id else "Goalie"

        else:
            policies = {
                game_name: PolicySpec(
                    observation_space=obs_spaces[game_name],
                    action_space=action_spaces[game_name]),
            }

            def policy_mapping_fn(agent_id, episode, worker, **kwargs):
                return game_name

        return policies, policy_mapping_fn

max_stars_repo_name: cseelhoff/ray

max_issues_repo_name: cseelhoff/ray

max_forks_repo_name: cseelhoff/ray


text:
import pickle
import numpy as np
import rasterio as rio

class IO:
    def __init__(self, rf_truck_detector):
        self.detector = rf_truck_detector

    def read_bands(self, file_path):
        try:
            with rio.open(file_path, "r") as src:
                self.detector.meta = src.meta
                if src.count < 4:
                    raise TypeError("Need 4 bands but %s given" % src.count)
                band_stack = np.zeros((src.count, src.height, src.width), dtype=np.float32)
                for band_idx in range(src.count):
                    band_stack[band_idx] = src.read(band_idx + 1)
        except rio.errors.RasterioIOError as e:
            print("Could not read from %s" % file_path)
            raise e
        else:
            print("Read %s bands from %s" % (self.detector.meta["count"], file_path))
        return band_stack

    def prediction_boxes_to_gpkg(self, prediction_boxes, file_path):
        self._write_boxes(file_path, prediction_boxes, ".gpkg")

    def prediction_boxes_to_geojson(self, prediction_boxes, file_path):
        self._write_boxes(file_path, prediction_boxes, ".geojson")

    def prediction_raster_to_gtiff(self, prediction_raster, file_path):
        if not any([file_path.endswith(suffix) for suffix in [".tif", ".tiff"]]):
            file_path += ".tiff"
        meta_copy = self.detector.meta.copy()
        meta_copy["count"] = 1
        meta_copy["dtype"] = prediction_raster.dtype
        try:
            with rio.open(file_path, "w", **meta_copy) as tgt:
                tgt.write(prediction_raster, 1)
        except rio.errors.RasterioIOError as e:
            print("Could not write to %s" % file_path)
            raise e
        else:
            print("Wrote to: %s" % file_path)

    @staticmethod
    def read_model(path):
        try:
            model = pickle.load(open(path, "rb"))
        except FileNotFoundError as e:
            raise FileNotFoundError("Model file not found at: %s" % path)
        return model

    @staticmethod
    def write_model(model, path):
        pickle.dump(model, open(path, "wb"))

    @staticmethod
    def _write_boxes(file_path, prediction_boxes, suffix):
        if not file_path.endswith(suffix):
            file_path += suffix
        drivers = {".geojson": "GeoJSON", ".gpkg": "GPKG"}
        try:
            prediction_boxes.to_file(file_path, driver=drivers[suffix])
        except TypeError as e:
            raise e
        else:
            print("Wrote to: %s" % file_path)