text:
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.

import itertools
import logging
from typing import Dict, NamedTuple, Optional, Tuple, Union

import numpy as np
import torch
import xgboost as xgb
from ml.rl.evaluation.cpe import CpeEstimate, bootstrapped_std_error_of_mean
from ml.rl.evaluation.evaluation_data_page import EvaluationDataPage
from torch import Tensor

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

DEFAULT_FRAC_TRAIN = 0.4
DEFAULT_FRAC_VALID = 0.1

class TrainValidEvalData(NamedTuple):
    contexts_dict: Dict[str, Tensor]
    model_propensities_dict: Dict[str, Tensor]
    actions_logged_dict: Dict[str, Tensor]
    action_mask_dict: Dict[str, Tensor]
    logged_rewards_dict: Dict[str, Tensor]
    model_rewards_dict: Dict[str, Tensor]
    model_rewards_for_logged_action_dict: Dict[str, Tensor]
    logged_propensities_dict: Dict[str, Tensor]
    num_examples_dict: Dict[str, int]

class EstimationData(NamedTuple):
    contexts_actions_train: Optional[Tensor]
    policy_indicators_train: Optional[Tensor]
    weights_train: Optional[Tensor]
    contexts_actions_valid: Optional[Tensor]
    policy_indicators_valid: Optional[Tensor]
    weights_valid: Optional[Tensor]
    contexts_actions_eval: Optional[Tensor]
    contexts_train: Optional[Tensor]
    actions_logged_train: Optional[Tensor]
    contexts_valid: Optional[Tensor]
    actions_logged_valid: Optional[Tensor]
    contexts_eval: Optional[Tensor]
    actions_logged_eval: Optional[Tensor]
    model_propensities_eval: Tensor
    model_rewards_eval: Tensor
    action_mask_eval: Tensor
    logged_rewards_eval: Tensor
    model_rewards_for_logged_action_eval: Tensor
    logged_propensities_eval: Tensor

class ImportanceSamplingData(NamedTuple):
    importance_weight: Tensor
    logged_rewards: Tensor
    model_rewards: Tensor
    model_rewards_for_logged_action: Tensor
    model_propensities: Tensor

class DoublyRobustEstimator:
    """
    For details, visit https://arxiv.org/pdf/1612.01205.pdf
    """

    def _split_data(
        self,
        edp: EvaluationDataPage,
        frac_train: float = DEFAULT_FRAC_TRAIN,
        frac_valid: float = DEFAULT_FRAC_VALID,
    ) -> TrainValidEvalData:
        """
        Split the data into training, validation and evaluation parts.
        Training and validation and used for model training to estimate
        the importance weights.
        Only evaluation data is used for policy estimation.

        This function is used for BOP-E and Estimated Propensity Score methods,
        but not for the standard Doubly Robust estimator.
        """
        num_examples = edp.model_propensities.shape[0]
        # split data into training, validation and eval
        indices = np.random.permutation(num_examples)
        idx_train = indices[0 : int(frac_train * num_examples)]
        idx_valid = indices[
            int(frac_train * num_examples) : int(
                (frac_train + frac_valid) * num_examples
            )
        ]
        idx_eval = indices[int((frac_train + frac_valid) * num_examples) :]
        if edp.contexts is None:
            raise ValueError("contexts not provided in input")
        contexts_dict = {
            "train": edp.contexts[idx_train],
            "valid": edp.contexts[idx_valid],
            "eval": edp.contexts[idx_eval],
        }
        model_propensities_dict = {
            "train": edp.model_propensities[idx_train],
            "valid": edp.model_propensities[idx_valid],
            "eval": edp.model_propensities[idx_eval],
        }
        # edp.action_mask is N*N_actions tensor of indicators of which actions
        # were actually taken by the logged algo
        actions_logged = torch.max(edp.action_mask, dim=1, keepdim=True)[1].float()
        actions_logged_dict = {
            "train": actions_logged[idx_train],
            "valid": actions_logged[idx_valid],
            "eval": actions_logged[idx_eval],
        }
        action_mask_dict = {
            "train": edp.action_mask[idx_train],
            "valid": edp.action_mask[idx_valid],
            "eval": edp.action_mask[idx_eval],
        }
        logged_rewards_dict = {
            "train": edp.logged_rewards[idx_train],
            "valid": edp.logged_rewards[idx_valid],
            "eval": edp.logged_rewards[idx_eval],
        }
        model_rewards_dict = {
            "train": edp.model_rewards[idx_train],
            "valid": edp.model_rewards[idx_valid],
            "eval": edp.model_rewards[idx_eval],
        }
        model_rewards_for_logged_action_dict = {
            "train": edp.model_rewards_for_logged_action[idx_train],
            "valid": edp.model_rewards_for_logged_action[idx_valid],
            "eval": edp.model_rewards_for_logged_action[idx_eval],
        }
        logged_propensities_dict = {
            "train": edp.logged_propensities[idx_train],
            "valid": edp.logged_propensities[idx_valid],
            "eval": edp.logged_propensities[idx_eval],
        }
        num_examples_dict = {
            "train": int(frac_train * num_examples),
            "valid": int((frac_train + frac_valid) * num_examples)
            - int(frac_train * num_examples),
            "eval": num_examples - int((frac_train + frac_valid) * num_examples),
        }
        return TrainValidEvalData(
            contexts_dict=contexts_dict,
            model_propensities_dict=model_propensities_dict,
            actions_logged_dict=actions_logged_dict,
            action_mask_dict=action_mask_dict,
            logged_rewards_dict=logged_rewards_dict,
            model_rewards_dict=model_rewards_dict,
            model_rewards_for_logged_action_dict=model_rewards_for_logged_action_dict,
            logged_propensities_dict=logged_propensities_dict,
            num_examples_dict=num_examples_dict,
        )

    def _prepare_data(self, edp: EvaluationDataPage) -> EstimationData:
        return EstimationData(
            contexts_actions_train=None,
            policy_indicators_train=None,
            weights_train=None,
            contexts_actions_valid=None,
            policy_indicators_valid=None,
            weights_valid=None,
            contexts_actions_eval=None,
            contexts_train=None,
            actions_logged_train=None,
            contexts_valid=None,
            actions_logged_valid=None,
            contexts_eval=edp.contexts,
            actions_logged_eval=None,
            model_propensities_eval=edp.model_propensities,
            model_rewards_eval=edp.model_rewards,
            action_mask_eval=edp.action_mask,
            logged_rewards_eval=edp.logged_rewards,
            model_rewards_for_logged_action_eval=edp.model_rewards_for_logged_action,
            logged_propensities_eval=edp.logged_propensities,
        )

    def _get_importance_sampling_inputs(
        self, ed: EstimationData
    ) -> ImportanceSamplingData:
        target_propensity_for_action = torch.sum(
            ed.model_propensities_eval * ed.action_mask_eval, dim=1, keepdim=True
        )
        # target_propensity_for_action is N*1 tensor of target algo propensities
        # for historical actions
        # logged_propensities_eval is N*1 tensor of propensity scores for historical
        # actions by the prod algorithm at each context
        importance_weights = (
            target_propensity_for_action / ed.logged_propensities_eval
        ).float()
        return ImportanceSamplingData(
            importance_weight=importance_weights,
            logged_rewards=ed.logged_rewards_eval,
            model_rewards=ed.model_rewards_eval,
            model_rewards_for_logged_action=ed.model_rewards_for_logged_action_eval,
            model_propensities=ed.model_propensities_eval,
        )

    def _get_importance_sampling_estimates(
        self, isd: ImportanceSamplingData
    ) -> Tuple[CpeEstimate, CpeEstimate, CpeEstimate]:

        # The score we would get if we evaluate the logged policy against itself
        logged_policy_score = float(
            torch.mean(isd.logged_rewards)
        )  # logged_rewards is N*1 tensor of historical rewards
        if logged_policy_score < 1e-6:
            logger.warning(
                "Can't normalize DR-CPE because of small or negative "
                + "logged_policy_score"
            )
            normalizer = 0.0
        else:
            normalizer = 1.0 / logged_policy_score

        if isd.model_rewards is None:
            # Fill with zero, equivalent to just doing IPS
            direct_method_values = torch.zeros(
                [isd.model_propensities.shape[0], 1], dtype=torch.float32
            )
        else:
            # model rewards is (N_samples)*N*N_actions tensor of predicted
            # counterfactual rewards for each possible action at each
            # historical context
            direct_method_values = torch.sum(
                isd.model_propensities * isd.model_rewards, dim=1, keepdim=True
            )

        direct_method_score = float(torch.mean(direct_method_values))
        direct_method_std_error = bootstrapped_std_error_of_mean(
            direct_method_values.squeeze()
        )
        direct_method_estimate = CpeEstimate(
            raw=direct_method_score,
            normalized=direct_method_score * normalizer,
            raw_std_error=direct_method_std_error,
            normalized_std_error=direct_method_std_error * normalizer,
        )

        ips = isd.importance_weight * isd.logged_rewards  # N*1

        doubly_robust = (
            isd.importance_weight
            * (isd.logged_rewards - isd.model_rewards_for_logged_action)
        ) + direct_method_values
        # model_rewards_for_logged_action is N*1 of estimated rewards for target
        # policy

        ips_score = float(torch.mean(ips))
        ips_score_std_error = bootstrapped_std_error_of_mean(ips.squeeze())
        inverse_propensity_estimate = CpeEstimate(
            raw=ips_score,
            normalized=ips_score * normalizer,
            raw_std_error=ips_score_std_error,
            normalized_std_error=ips_score_std_error * normalizer,
        )

        dr_score = float(torch.mean(doubly_robust))
        dr_score_std_error = bootstrapped_std_error_of_mean(doubly_robust.squeeze())
        doubly_robust_estimate = CpeEstimate(
            raw=dr_score,
            normalized=dr_score * normalizer,
            raw_std_error=dr_score_std_error,
            normalized_std_error=dr_score_std_error * normalizer,
        )

        return (
            direct_method_estimate,
            inverse_propensity_estimate,
            doubly_robust_estimate,
        )

    def estimate(
        self, edp: EvaluationDataPage, hp: Optional[dict] = None
    ) -> Tuple[CpeEstimate, CpeEstimate, CpeEstimate]:
        ed = self._prepare_data(edp)
        isd = self._get_importance_sampling_inputs(ed)
        return self._get_importance_sampling_estimates(isd)

class DoublyRobustEstimatorBOPE(DoublyRobustEstimator):
    """
    This class implements a doubly-robust Balanced Off-Policy Evaluation (BOP-E)
    method.
    For details about BOP-E see https://arxiv.org/abs/1906.03694
    For analysis of BOP-E performance see https://fburl.com/bope_eval_nb

    Note that when using BOP-E the data gets split into training, validation
    and evaluation parts and only the evaluation part is used directly for policy
    evaluation, while training and validation datasets are used for model training.

    supported modes (all doubly robust):
    1. bope_weights. Use BOP-E (ignoring logged porpensities) to estimate the
        importance weights. Propensities of the target policy are used as
        observation weights when training BOP-E classifier.
    2. bope_weighted_targets. Use BOP-E (ignoring logged porpensities) to
        estimate the importance weights. Propensities of the target policy
        are used as soft targets to train BOP-E regressor. With this method
        BOP-E trains a regressor instead of a classifier.
    3. bope_sampling. Use BOP-E (ignoring logged porpensities)
        to estimate the importance weights. Propensities of the target policy
        are used to sample the actions for the classifier training data.
    """

    def _prepare_data(self, edp: EvaluationDataPage) -> EstimationData:
        """
        Prepare the datasets for BOP-E classifier estimation
        """
        assert (
            edp.contexts is not None
        ), "edp.contexts have to be specified when using the estimation-based methods"
        num_actions = edp.model_propensities.shape[1]
        tved = self._split_data(edp, self.frac_train, self.frac_valid)

        actions_target_dict = {}
        contexts_actions_target_dict = {}
        weights_target_dict = {}
        policy_indicators_target_dict = {}
        if self.mode == "bope_sampling":
            for d in ["train", "valid"]:
                # model_propensities is N*N_actions tensor of propensity scores
                # for each possible action by the target algorithm at each context
                actions_target_dict[d] = (
                    torch.multinomial(
                        tved.model_propensities_dict[d],
                        self.num_samples,
                        replacement=True,
                    )
                    .float()
                    .transpose(0, 1)
                    .contiguous()
                    .view(-1, 1)
                )
                # transpose and reshape so that the contexts (rows) are arranged
                # like [C1,...,CN,C1,...,CN,.....,C1,...,CN]

                # TODO: add context-action interaction here
                contexts_actions_target_dict[d] = torch.cat(
                    [
                        torch.cat([tved.contexts_dict[d]] * self.num_samples, dim=0),
                        actions_target_dict[d],
                    ],
                    dim=1,
                )
                weights_target_dict[d] = torch.ones(
                    tved.num_examples_dict[d] * self.num_samples, 1
                )
                policy_indicators_target_dict[d] = torch.ones(
                    tved.num_examples_dict[d] * self.num_samples, 1, dtype=torch.float32
                )
        elif self.mode == "bope_weights":
            # rows are outer products of actions and contexts, ordered first by
            # context and then by action
            # [[C0,A0], [C0,A1], [C0,A2], [C1,A0], [C1,A1], [C1,A2],...]
            for d in ["train", "valid"]:
                actions_target_dict[d] = torch.tensor(
                    list(
                        itertools.chain.from_iterable(
                            [
                                [x] * tved.num_examples_dict[d]
                                for x in range(num_actions)
                            ]
                        )
                    ),
                    dtype=torch.float32,
                ).view(-1, 1)
                weights_target_dict[d] = (
                    tved.model_propensities_dict[d]
                    .transpose(0, 1)
                    .contiguous()
                    .view(-1, 1)
                )
                policy_indicators_target_dict[d] = torch.ones(
                    tved.num_examples_dict[d] * num_actions, 1, dtype=torch.float32
                )
                # TODO: add context-action interaction here
                contexts_actions_target_dict[d] = torch.cat(
                    [
                        torch.cat([tved.contexts_dict[d]] * num_actions, dim=0),  # 1498
                        actions_target_dict[d],  # 1496
                    ],
                    dim=1,
                )
        elif self.mode == "bope_weighted_targets":
            # rows are outer products of actions and contexts, ordered first by
            # context and then by action
            # [[C0,A0], [C0,A1], [C0,A2], [C1,A0], [C1,A1], [C1,A2],...]
            for d in ["train", "valid"]:
                actions_target_dict[d] = torch.tensor(
                    list(
                        itertools.chain.from_iterable(
                            [
                                [x] * tved.num_examples_dict[d]
                                for x in range(num_actions)
                            ]
                        )
                    ),
                    dtype=torch.float32,
                ).view(-1, 1)
                weights_target_dict[d] = torch.ones(
                    tved.num_examples_dict[d] * num_actions, 1
                )
                policy_indicators_target_dict[d] = (
                    tved.model_propensities_dict[d]
                    .transpose(0, 1)
                    .contiguous()
                    .view(-1, 1)
                )
                # TODO: add context-action interaction here
                contexts_actions_target_dict[d] = torch.cat(
                    [
                        torch.cat([tved.contexts_dict[d]] * num_actions, dim=0),
                        actions_target_dict[d],
                    ],
                    dim=1,
                )
        else:
            raise ValueError("BOP-E mode '{}'' not supported".format(self.mode))
        contexts_actions_logged_dict = {}
        weights_logged_dict = {}
        policy_indicators_logged_dict = {}
        contexts_actions_all_dict = {}
        policy_indicators_all_dict = {}
        weights_all_dict = {}
        for d in ["train", "valid"]:
            contexts_actions_logged_dict[d] = torch.cat(
                (tved.contexts_dict[d], tved.actions_logged_dict[d]), dim=1
            )  # N*(d+1)
            weights_logged_dict[d] = torch.ones(
                tved.num_examples_dict[d], 1, dtype=torch.float32
            )
            policy_indicators_logged_dict[d] = torch.zeros(
                tved.num_examples_dict[d], 1, dtype=torch.float32
            )
            contexts_actions_all_dict[d] = torch.cat(
                [contexts_actions_logged_dict[d], contexts_actions_target_dict[d]],
                dim=0,
            ).numpy()
            policy_indicators_all_dict[d] = torch.cat(
                [policy_indicators_logged_dict[d], policy_indicators_target_dict[d]],
                dim=0,
            ).numpy()
            weights_all_dict[d] = (
                torch.cat([weights_logged_dict[d], weights_target_dict[d]], dim=0)
                .flatten()
                .numpy()
            )
            if (
                contexts_actions_all_dict[d].shape[0]
                != policy_indicators_all_dict[d].shape[0]
            ):
                raise ValueError(
                    "number of rows in {} contexts_actions({}) and policy_"
                    "indicators({}) has to be equal".format(
                        d,
                        contexts_actions_all_dict[d].shape[0],
                        policy_indicators_all_dict[d].shape[0],
                    )
                )
            if contexts_actions_all_dict[d].shape[0] != weights_all_dict[d].shape[0]:
                raise ValueError(
                    "number of rows in {} contexts_actions({}) and weights({})"
                    " has to be equal".format(
                        d,
                        contexts_actions_all_dict[d].shape[0],
                        weights_all_dict[d].shape[0],
                    )
                )
        contexts_actions_logged_dict["eval"] = torch.cat(
            (tved.contexts_dict["eval"], tved.actions_logged_dict["eval"]), dim=1
        )  # N*(d+1)

        return EstimationData(
            contexts_actions_train=contexts_actions_all_dict["train"],
            policy_indicators_train=policy_indicators_all_dict["train"],
            weights_train=weights_all_dict["train"],
            contexts_actions_valid=contexts_actions_all_dict["valid"],
            policy_indicators_valid=policy_indicators_all_dict["valid"],
            weights_valid=weights_all_dict["valid"],
            contexts_actions_eval=contexts_actions_logged_dict["eval"],
            contexts_train=None,
            actions_logged_train=None,
            contexts_valid=None,
            actions_logged_valid=None,
            contexts_eval=None,
            actions_logged_eval=None,
            model_propensities_eval=tved.model_propensities_dict["eval"],
            model_rewards_eval=tved.model_rewards_dict["eval"],
            action_mask_eval=tved.action_mask_dict["eval"],
            logged_rewards_eval=tved.logged_rewards_dict["eval"],
            model_rewards_for_logged_action_eval=tved.model_rewards_for_logged_action_dict[
                "eval"
            ],
            logged_propensities_eval=tved.logged_propensities_dict["eval"],
        )

    def _estimate_xgboost_model(
        self,
        ed: EstimationData,
        xgb_params: Dict[str, Union[str, float, int]] = None,
        nthread: int = 8,
    ) -> xgb.Booster:
        if xgb_params is None:
            xgb_params = {}
        dmatrix_train = xgb.DMatrix(
            ed.contexts_actions_train,
            ed.policy_indicators_train,
            nthread=nthread,
            weight=ed.weights_train,
        )
        dmatrix_valid = xgb.DMatrix(
            ed.contexts_actions_valid,
            ed.policy_indicators_valid,
            nthread=nthread,
            weight=ed.weights_valid,
        )
        if xgb_params is not None:  # check for None to satisfy a test
            xgb_params.update({"objective": "binary:logistic"})
        classifier: xgb.Booster = xgb.train(
            xgb_params,
            dmatrix_train,
            evals=[(dmatrix_valid, "validation_set")],
            verbose_eval=False,
        )
        return classifier

    def _get_importance_sampling_inputs(
        self, ed: EstimationData, xgb_params: Dict[str, Union[str, float, int]] = None
    ) -> ImportanceSamplingData:
        classifier = self._estimate_xgboost_model(ed, xgb_params)

        # predictions are made only for the eval set to prevent classifier
        # overfitting
        predictions = classifier.predict(xgb.DMatrix(ed.contexts_actions_eval))

        if self.mode == "bope_sampling":
            bope_weight_normalization_factor = 1.0 / self.num_samples
        else:
            bope_weight_normalization_factor = 1.0

        importance_weights = (
            torch.tensor(predictions / (1.0 - predictions), dtype=torch.float32).view(
                -1, 1
            )
            * bope_weight_normalization_factor
        )
        return ImportanceSamplingData(
            importance_weight=importance_weights,
            logged_rewards=ed.logged_rewards_eval,
            model_rewards=ed.model_rewards_eval,
            model_rewards_for_logged_action=ed.model_rewards_for_logged_action_eval,
            model_propensities=ed.model_propensities_eval,
        )

    def estimate(
        self, edp: EvaluationDataPage, hp: Optional[dict] = None
    ) -> Tuple[CpeEstimate, CpeEstimate, CpeEstimate]:
        if hp is None:
            raise ValueError("Hyperparameters have to be provided for BOP-E")
        if "mode" not in hp:
            raise ValueError("Mode has to be specified in hyperparameters")
        self.mode = hp["mode"]
        if (self.mode == "bope_sampling") and ("num_samples" not in hp):
            raise ValueError(
                "Number of samples has to be specified for mode 'bope_sampling'"
            )
        self.num_samples = hp.get("num_samples", 0)
        self.frac_train = hp.get("frac_train", DEFAULT_FRAC_TRAIN)
        self.frac_valid = hp.get("frac_valid", DEFAULT_FRAC_VALID)
        xgb_params: Dict[str, Union[str, float, int]] = hp.get("xgb_params", {})
        ed = self._prepare_data(edp)
        isd = self._get_importance_sampling_inputs(ed, xgb_params)
        return self._get_importance_sampling_estimates(isd)

class DoublyRobustEstimatorEstProp(DoublyRobustEstimator):
    def _prepare_data(self, edp: EvaluationDataPage) -> EstimationData:
        assert (
            edp.contexts is not None
        ), "edp.contexts have to be specified when using the estimation-based methods"
        self.num_actions = edp.model_propensities.shape[1]
        tved = self._split_data(edp, self.frac_train, self.frac_valid)

        return EstimationData(
            contexts_actions_train=None,
            policy_indicators_train=None,
            weights_train=None,
            contexts_actions_valid=None,
            policy_indicators_valid=None,
            weights_valid=None,
            contexts_actions_eval=None,
            contexts_train=tved.contexts_dict["train"],
            actions_logged_train=tved.actions_logged_dict["train"],
            contexts_valid=tved.contexts_dict["valid"],
            actions_logged_valid=tved.actions_logged_dict["valid"],
            contexts_eval=tved.contexts_dict["eval"],
            actions_logged_eval=tved.actions_logged_dict["eval"],
            model_propensities_eval=tved.model_propensities_dict["eval"],
            model_rewards_eval=tved.model_rewards_dict["eval"],
            action_mask_eval=tved.action_mask_dict["eval"],
            logged_rewards_eval=tved.logged_rewards_dict["eval"],
            model_rewards_for_logged_action_eval=tved.model_rewards_for_logged_action_dict[
                "eval"
            ],
            logged_propensities_eval=tved.logged_propensities_dict["eval"],
        )

    def _estimate_xgboost_model(
        self,
        ed: EstimationData,
        num_classes: int,
        xgb_params: Dict[str, Union[str, float, int]] = None,
        nthread: int = 8,
    ) -> xgb.Booster:
        if xgb_params is None:
            xgb_params = {}
        dmatrix_train = xgb.DMatrix(
            ed.contexts_train, ed.actions_logged_train, nthread=nthread
        )
        dmatrix_valid = xgb.DMatrix(
            ed.contexts_valid, ed.actions_logged_valid, nthread=nthread
        )
        xgb_params = xgb_params.copy()
        xgb_params.update(
            {"objective": "multi:softprob", "num_class": num_classes, "n_gpus": 0}
        )
        classifier: xgb.Booster = xgb.train(
            xgb_params,
            dmatrix_train,
            evals=[(dmatrix_valid, "validation_set")],
            verbose_eval=False,
        )
        return classifier

    def _get_importance_sampling_inputs(
        self, ed: EstimationData, xgb_params: Dict[str, Union[str, float, int]] = None
    ):
        classifier = self._estimate_xgboost_model(ed, self.num_actions, xgb_params)
        # predictions are made only for the eval set to prevent classifier
        # overfitting
        predicted_logged_propensities_all_actions = torch.tensor(
            classifier.predict(xgb.DMatrix(ed.contexts_eval)), dtype=torch.float32
        )
        if ed.actions_logged_eval is None:
            raise ValueError("ed.actions_logged_eval has to be non-None")
        ret = predicted_logged_propensities_all_actions.gather(
            1, ed.actions_logged_eval.long()
        )
        predicted_logged_policy_propensities_logged_actions = ret

        target_propensity_for_action = torch.sum(
            ed.model_propensities_eval * ed.action_mask_eval, dim=1, keepdim=True
        )

        importance_weights = (
            target_propensity_for_action
            / predicted_logged_policy_propensities_logged_actions
        ).float()
        return ImportanceSamplingData(
            importance_weight=importance_weights,
            logged_rewards=ed.logged_rewards_eval,
            model_rewards=ed.model_rewards_eval,
            model_rewards_for_logged_action=ed.model_rewards_for_logged_action_eval,
            model_propensities=ed.model_propensities_eval,
        )

    def estimate(
        self, edp: EvaluationDataPage, hp: Optional[dict] = None
    ) -> Tuple[CpeEstimate, CpeEstimate, CpeEstimate]:
        if hp is None:
            self.frac_train = DEFAULT_FRAC_TRAIN
            self.frac_valid = DEFAULT_FRAC_VALID
            xgb_params: Dict[str, Union[str, float, int]] = {}
        else:
            self.frac_train = hp.get("frac_train", DEFAULT_FRAC_TRAIN)
            self.frac_valid = hp.get("frac_valid", DEFAULT_FRAC_VALID)
            xgb_params = hp.get("xgb_params", {})
        ed = self._prepare_data(edp)
        isd = self._get_importance_sampling_inputs(ed, xgb_params)
        return self._get_importance_sampling_estimates(isd)

max_stars_repo_name: brettkoonce/ReAgent

max_issues_repo_name: brettkoonce/ReAgent

max_forks_repo_name: brettkoonce/ReAgent


text:
import numpy as np

def apply(g_matrix, resistances, r_i):
    """Fills matrix `g` used in equation `gv = i`.

    Values are filled by applying Kirchhoff's current law at the nodes on the
    word and bit lines.

    Parameters
    ----------
    g_matrix : lil_matrix
        Matrix `g` used in equation `gv = i`.
    resistances : ndarray
        Resistances of crossbar devices.
    r_i : named tuple of (int or float)
        Interconnect resistances along the word and bit line segments.

    Returns
    -------
    lil_matrix
        Filled matrix `g`.
    """
    with np.errstate(divide='ignore'):
        conductances = 1./resistances
    if r_i.word_line > 0:
        g_matrix = word_line_nodes(g_matrix, conductances, r_i)
    if r_i.bit_line > 0:
        g_matrix = bit_line_nodes(g_matrix, conductances, r_i)
    return g_matrix

def word_line_nodes(g_matrix, conductances, r_i):
    """Fills matrix `g` with values corresponding to nodes on the word lines.

    Parameters
    ----------
    g_matrix : lil_matrix
        Matrix `g` used in equation `gv = i`.
    conductances : ndarray
        Conductances of crossbar devices.
    r_i : named tuple of (int or float)
        Interconnect resistances along the word and bit line segments.

    Returns
    -------
    lil_matrix
        Partially filled matrix `g`.
    """
    (num_word_lines, num_bit_lines) = conductances.shape
    g_i = 1/r_i.word_line
    # if `r_i.bit_line == 0`, we are solving only for a half of the matrix
    # `v` and so `bit_line_nodes()` will not even be called.

    if num_bit_lines != 1:
        # first column
        idx_word_lines = np.arange(num_word_lines)
        idx_bit_lines = np.repeat(0, num_word_lines)
        idx = np.ravel_multi_index(
            (idx_word_lines, idx_bit_lines), conductances.shape)
        g_matrix[idx, idx] = np.ones(
            (num_word_lines,))*2*g_i + conductances[:, 0]
        g_matrix[idx, idx + 1] = -np.ones((num_word_lines,))*g_i
        if r_i.bit_line > 0:
            g_matrix[idx, idx + conductances.size] = -conductances[:, 0]

        # middle columns
        for i in range(1, num_bit_lines - 1):
            idx_word_lines = np.arange(num_word_lines)
            idx_bit_lines = np.repeat(i, num_word_lines)
            idx = np.ravel_multi_index(
                (idx_word_lines, idx_bit_lines), conductances.shape)
            g_matrix[idx, idx] = np.ones(
                (num_word_lines,))*2*g_i + conductances[:, i]
            g_matrix[idx, idx - 1] = -np.ones((num_word_lines,))*g_i
            g_matrix[idx, idx + 1] = -np.ones((num_word_lines,))*g_i
            if r_i.bit_line > 0:
                g_matrix[idx, idx + conductances.size] = -conductances[:, i]

        # last column
        idx_word_lines = np.arange(num_word_lines)
        idx_bit_lines = np.repeat(num_bit_lines - 1, num_word_lines)
        idx = np.ravel_multi_index(
            (idx_word_lines, idx_bit_lines), conductances.shape)
        g_matrix[idx, idx] = np.ones(
            (num_word_lines,))*g_i + conductances[:, -1]
        g_matrix[idx, idx - 1] = -np.ones((num_word_lines,))*g_i
        if r_i.bit_line > 0:
            g_matrix[idx, idx + conductances.size] = -conductances[:, -1]
    else:
        # the only column
        idx_word_lines = np.arange(num_word_lines)
        idx_bit_lines = np.repeat(0, num_word_lines)
        idx = np.ravel_multi_index(
            (idx_word_lines, idx_bit_lines), conductances.shape)
        g_matrix[idx, idx] = np.ones(
            (num_word_lines,))*g_i + conductances[:, 0]
        if r_i.bit_line > 0:
            g_matrix[idx, idx + conductances.size] = -conductances[:, 0]

    return g_matrix

def bit_line_nodes(g_matrix, conductances, r_i):
    """Fills matrix g with values corresponding to nodes on the bit lines.

    Parameters
    ----------
    g_matrix : lil_matrix
        Matrix `g` used in equation `gv = i`.
    conductances : ndarray
        Conductances of crossbar devices.
    r_i : named tuple of (int or float)
        Interconnect resistances along the word and bit line segments.

    Returns
    -------
    lil_matrix
        Filled matrix `g`.
    """
    (num_word_lines, num_bit_lines) = conductances.shape
    g_bl = 1/r_i.bit_line
    # if `r_i.word_line == 0`, we are solving only for a half of the matrix
    # `v` and so `word_line_nodes()` will not even be called.
    if r_i.word_line > 0:
        offset = conductances.size
    else:
        offset = 0

    if num_word_lines != 1:
        # first row
        idx_word_lines = np.repeat(0, num_bit_lines)
        idx_bit_lines = np.arange(num_bit_lines)
        idx = offset + np.ravel_multi_index(
            (idx_word_lines, idx_bit_lines), conductances.shape)
        g_matrix[idx, idx] = np.ones((num_bit_lines,))*g_bl + conductances[0, :]
        g_matrix[idx, idx + num_bit_lines] = -np.ones((num_bit_lines,))*g_bl
        if r_i.word_line > 0:
            g_matrix[idx, idx - conductances.size] = -conductances[0, :]

        # middle rows
        for i in range(1, num_word_lines - 1):
            idx_word_lines = np.repeat(i, num_bit_lines)
            idx_bit_lines = np.arange(num_bit_lines)
            idx = offset + np.ravel_multi_index(
                (idx_word_lines, idx_bit_lines), conductances.shape)
            g_matrix[idx, idx] = np.ones(
                (num_bit_lines,))*2*g_bl + conductances[i, :]
            g_matrix[idx, idx + num_bit_lines] = -np.ones((num_bit_lines,))*g_bl
            g_matrix[idx, idx - num_bit_lines] = -np.ones((num_bit_lines,))*g_bl
            if r_i.word_line > 0:
                g_matrix[idx, idx - conductances.size] = -conductances[i, :]

        # last row
        idx_word_lines = np.repeat(num_word_lines - 1, num_bit_lines)
        idx_bit_lines = np.arange(num_bit_lines)
        idx = offset + np.ravel_multi_index(
            (idx_word_lines, idx_bit_lines), conductances.shape)
        g_matrix[idx, idx] = np.ones(
            (num_bit_lines,))*2*g_bl + conductances[-1, :]
        g_matrix[idx, idx - num_bit_lines] = -np.ones((num_bit_lines,))*g_bl
        if r_i.word_line > 0:
            g_matrix[idx, idx - conductances.size] = -conductances[-1, :]
    else:
        # the only row
        idx_word_lines = np.repeat(0, num_bit_lines)
        idx_bit_lines = np.arange(num_bit_lines)
        idx = offset + np.ravel_multi_index(
            (idx_word_lines, idx_bit_lines), conductances.shape)
        g_matrix[idx, idx] = np.ones((num_bit_lines,))*g_bl + conductances[0, :]
        if r_i.word_line > 0:
            g_matrix[idx, idx - conductances.size] = -conductances[0, :]

    return g_matrix

ext: py

max_stars_repo_name: nikhil-garg-rram/badcrossbar

max_issues_repo_name: nikhil-garg-rram/badcrossbar

max_forks_repo_name: nikhil-garg-rram/badcrossbar

reason: import numpy


text:
"""
A collection of NMR processing functions for filtering, smoothing, and
correcting spectral baselines.

"""

import numpy as np
import scipy
import scipy.ndimage

# Linear (First order) baseline correction

def base(data,nl,nw=0):
    """
    Linear (first-order) Baseline Correction based on node list.

    Parameters:

    * data  Array of spectral data.
    * nl    List of baseline nodes.
    * nw    Node half-width in points.

    """

    if data.ndim == 1:
        data = data-calc_bl_linear(data,nl,nw)
    else:   # for 2D array loop over traces
        for i,vec in enumerate(data):
            data[i] = data[i]-calc_bl_linear(vec,nl,nw)
    return data

def calc_bl_linear(x,nl,nw=0):
    """
    Calculate a baseline using linear approximation between nodes

    Parameters:

    * x     1D data
    * nl    List of baseline nodes
    * nw    Node half-width in points

    """
    bl = np.zeros_like(x)
    for i in range(len(nl)-1):
        # minimum and maximum index
        min = nl[i]
        max = nl[i+1]

        # linspace s1 and s2
        s1 = x[min-nw:min+nw+1].mean()
        s2 = x[max-nw:max+nw+1].mean()
        bl[min:max+1] = np.linspace(s1,s2,max-min+1)
    return bl

# Constant baseline correction

def cbf(data,last=10,apply=slice(None)):
    """
    Constant Baseline correction

    Parameters:

    * data  Array of spectral data.
    * last  Percent of -1 axis used to calculate correction.
    * apply Slice describing 0th-axis region(s) to apply correction to.
            Ignored in 1D data.

    """
    # calculate the correction
    n = data.shape[-1]*last/100.+1.
    corr = data[...,-n:].sum(axis=-1)/n

    # apply correction
    if data.ndim == 2:
        data[apply] =  data[apply] - np.array([corr]).transpose()[apply]
        return data
    else:
        return data-corr

def cbf_explicit(data,calc=slice(None),apply=slice(None)):
    """
    Constant Baseline - explicit region

    Parameters:

    * data  Array of spectral data.
    * calc  Slice describing region to use for calculating correction.
    * apply Slice describing 0th-axis region(s) to apply correction to.
            Ignored in 1D data.

    """
    # calculate correction
    n = len(range(data.shape[-1])[calc])
    corr = data[...,calc].sum(axis=-1)/n

    # apply correction
    if data.ndim == 2:
        data[apply] = data[apply] - np.array([corr]).transpose()[apply]
        return data
    else:
        return data-corr

# Median baseline correction

def med(data,mw=24,sf=16,sigma=5.0):
    """
    Median baseline correction

    Algorith described in:
    Friedrichs, M.S. JBNMR 1995 5 147-153.

    Parameters:

    * data  Array of spectral data.
    * mw    Median Window size in pts.
    * sf    Smooth window size in pts.
    * sigma Standard-deviation of Gaussian in convolution

    """
    if data.ndim == 1:
        data = data - calc_bl_med(data,mw,sf,sigma)
    else:
        for i,vec in enumerate(data):
            data[i] = vec - calc_bl_med(vec,mw,sf,sigma)
    return out

def calc_bl_med(x,mw,sf,sigma):
    """
    Calculate a baseline using median baseline correction.

    Algorithm described in:
    Friedrichs, M.S. JBNMR 1995 5 147-153

    Parameter:

    x       1D data
    mw      Median Window size in pts.
    sf      Smooth window size in pts.
    sigma   Standard-deviation of Gaussian in convolution.

    """

    # create extrema array (non extrema values are masked out)
    mask = x == scipy.ndimage.median_filter(x,size=3)
    mask[0] = False     # first pt always extrema
    mask[-1] = False    # last pt always extrema
    e = np.ma.masked_array(x,mask)

    # fill in the median vector
    half_mw = mw/2
    m = scipy.ndimage.median_filter(e,mw+1,mode="mirror")
    # using the median_filter might give slightly different results than
    # described algorithm but is MUCH faster

    # convolve with a gaussian
    g = scipy.signal.gaussian(sf,sigma)
    g = g/g.sum()

    return scipy.signal.convolve(m,g,mode='same')

# Solvent Filter

def sol_general(data,filter,w=16,mode='same'):
    """
    Solvent filter with generic filter.

    Algorithm described in:
    Marion et al. JMR 1989 84 425-430

    Parameters:

    * data   Array of spectral data.
    * filter filter to convolve with data
    * mode   mode for output ('valid','same', or 'full')

    """
    A = filter.sum()
    if data.ndim == 2:
        filter = np.atleast_2d(filter)
    return data-scipy.signal.convolve(data,filter,mode=mode)/A

def sol_boxcar(data,w=16,mode='same'):
    """
    Solvent filter with boxcar filter.

    Parameters:

    * data   Array of spectral data.
    * w      Width of convolution window.
    * mode   mode for output ('valid','same', or 'full')

    """
    filter = scipy.signal.boxcar(w)
    return sol_general(data,filter,w=w,mode=mode)

def sol_sine(data,w=16,mode='same'):
    """
    Solvent filter with sine-bell filter.

    Parameters:

    * data   Array of spectral data.
    * w      Width of convolution window.
    * mode   mode for output ('valid','same', or 'full')

    """
    filter = np.cos(np.pi*np.linspace(-0.5,0.5,w))
    return sol_general(data,filter,w=w,mode=mode)

def sol_sine2(data,w=16,mode='same'):
    """
    Solvent filter with square sine-bell filter.

    Parameters:

    * data   Array of spectral data.
    * w      Width of convolution window.
    * mode   mode for output ('valid','same', or 'full')

    """
    filter = np.cos(np.pi*np.linspace(-0.5,0.5,w))**2
    return sol_general(data,filter,w=w,mode=mode)

def sol_gaussian(data,w=16,mode='same'):
    """
    Solvent filter with square gaussian filter.

    Parameters:

    * data   Array of spectral data.
    * w      Width of convolution window.
    * mode   mode for output ('valid','same', or 'full')

    """
    filter = scipy.signal.gaussian(w,w/2.)
    return sol_general(data,filter,w=w,mode=mode)

# Polynomial Solvent Subtraction

def poly_td(data):
    """
    Polynomial time domain solvent subtraction

    From NMRPipe paper(appendix):

    when used with the argument -time, fits all data points to a polynomial,
    which is then subtracted from the original data.  It is intended to fit
    and subtract low-freqency solvent signal in the FID, a procedure that
    often causes less distortions than time-domain convolution methods.
    By default, a fourth-order polynomials is used.  For speed successive
    averages of regions are usually fit, rather than fitting all of the data.

    Alg:

    1. Calculate average of blocks
    2. Fit these averages to polynomial (block parameters)
    3. Back out "real" polynomial parameters from these block parameters
    4. Subtract off the polynomial from data

    """
    # XXX
    pass

def poly_fd(data):
    """
    Polynomial frequency domain baseline correction

    From NMRPipe paper (appendix):

    applies a polynomial baseline correction of the order specified by
    argument -ord via an automated base-line detection method when used
    with argument -auto.  The defauly is a forth-order polynomial. The
    automated base-line mode works as follows: a copy of a given vector is
    divided into a series of adjacent sections, typically eight points wide.
    The average value of each section is subtracted from all points in that
    section, to generate a 'centered' vector.  The intensities of the entire
    centered vector are sorted, and the standard deviation of the noise is
    estimated under the assumption that a given fraction (typically about
    30%) of the smallest intensities belong to the base-line, and that the
    noise is normally distributed.  This noise estimate is multiplied by a
    constant, typically about 1.5, to yield a classification threshold.
    Then, each section in the centered vector is classified as base line only
    if its standard deviation does not exceed the threshold.  These
    classifications are used to correct the original vector.

    Alg:

    1. Divide into 'blocks'
    2. Center each block and create centered vector
    3. Calculate intensity (abs) of each centered vector
    4. Sort intensities, lower 30% belong to baseline
    5. Fit base line intensities to Normal distribution, gives estimation
       of standard deviation (std) of noise
    6. Classification threshold set to 1.5*std
    7. Qualify each block in centered vector as baseline only
       (its std < thres) or not (std > thres)
    8. Fit baseline only points to polynomial and substract off

    """
    # XXX
    pass

max_stars_repo_name: thegooglecodearchive/nmrglue

max_issues_repo_name: thegooglecodearchive/nmrglue

max_forks_repo_name: thegooglecodearchive/nmrglue

reason: import numpy,import scipy


text:
import open3d as o3d
import numpy as np
from pclpy import pcl
from tqdm import tqdm
import os

def o3d_meshes(dataset_name: str, path_to_data_folder='../../data'):
    """
    Read in mesh (.ply, .stl, .off) files. The function assumes that each class of objects is in
    a separate folder and highly spefified for our needs, which is why a list of all objects of
    each data set are provided as a hard-coded array of strings.
    You can download the data sets referenced in [#1]_ and [#2]_ and use the path to them.

    References
    ----------
    .. [#1] http://modelnet.cs.princeton.edu/ Z. Wu, S. Song, A. Khosla, F. Yu, L. Zhang, X. Tang and J. Xiao. 3D ShapeNets: A Deep Representation for Volumetric Shapes. Proceedings of 28th IEEE Conference on Computer Vision and Pattern Recognition (CVPR2015)
    .. [#2] http://www.cim.mcgill.ca/~shape/benchMark/ K. Siddiqi, J. Zhang, D. Macrini, A. Shokoufandeh, S. Bouix & S. Dickinson. Retrieving Articulated 3D Models Using Medial Surfaces. Machine Vision and Applications, 19(4), 261--274, 2008.

    Parameters
    ----------
    dataset_name: Should correspond to the name of the folder with the data set
    path_to_data_folder

    Returns
    -------
    o3d_meshes : array-like, shape (number of objects)
    The Open3D mesh representations of the objects as a list.

    labels :  array-like, shape (number of objects)
    The labels of the objects as a list of integers starting from 0.

    """

    # http://modelnet.cs.princeton.edu/
    if dataset_name == 'modelnet10':
        objects = ['bathtub', 'bed', 'chair', 'desk', 'dresser', 'monitor', 'night_stand', 'sofa', 'table', 'toilet']

    elif dataset_name == 'tali_15':
        objects = ['Manching', 'Milet']

    elif dataset_name == 'mixed_bones':
        objects = ['capra', 'ovis_aries']

    # http://www.cim.mcgill.ca/~shape/benchMark/
    elif dataset_name == 'mc_gill':
        objects = ['airplanes_ply', 'dinosaurs_ply', 'fishes_ply']

    else:
        raise ValueError('Unknown dataset')

    o3d_meshes = []
    labels = []
    print('Read in %d classes of mesh files...' % len(objects))
    for i, obj in enumerate(tqdm(objects)):
        if dataset_name == 'modelnet10':

            objects_o3d = [o3d.io.read_triangle_mesh(file) for file in
                           list_files(path_to_data_folder + '/' + dataset_name + '/' + obj + '/test')]
        else:
            objects_o3d = [o3d.io.read_triangle_mesh(file) for file in
                           list_files(path_to_data_folder + '/' + dataset_name + '/' + obj)]
        # print('class ', i, ': ', len(objects_o3d), ' objects')
        o3d_meshes.extend(objects_o3d)
        labels.extend([i] * len(objects_o3d))

    return o3d_meshes, labels

def save_o3d_arrays(o3d_meshes, labels, data_set_name: str):
    os.makedirs('../generated/' + data_set_name, exist_ok=True)

    np.save('../generated/' + data_set_name + '/' + data_set_name + '_labels', labels)
    np.save('../generated/' + data_set_name + '/' + data_set_name + '_vertices',
            [np.asarray(obj.vertices_list, dtype=object) for obj in o3d_meshes])

def save_meshes_as_pcd_pointclouds(mesh_list, data_set_name: str):
    path = '../generated/' + data_set_name + '/pcd_pointclouds/'
    os.makedirs(path, exist_ok=True)

    for i, mesh in enumerate(mesh_list):
        pcd = o3d.geometry.PointCloud()
        pcd.points = mesh.vertices
        pcd.normals = mesh.vertex_normals

        o3d.io.write_point_cloud(path + data_set_name + '_' + str(i) + '.pcd', pcd)

def save_meshes_as_poisson_sampled_pointclouds(mesh_list, data_set_name: str, number_of_points, init_factor):
    path = '../../generated/' + data_set_name + '/pcd_pointclouds_poisson/' + str(number_of_points) + '/'
    os.makedirs(path, exist_ok=True)

    print('Save %d meshes as .pcd point clouds...' % len(mesh_list))
    for i, mesh in enumerate(tqdm(mesh_list)):
        file_path = path + data_set_name + '_' + str(i).zfill(2) + '.pcd'

        pcd = mesh.sample_points_poisson_disk(number_of_points=number_of_points, init_factor=init_factor)
        o3d.io.write_point_cloud(file_path, pcd)

def save_meshes_as_ply_files(mesh_list, data_set_name: str):
    path = '../generated/' + data_set_name + '/ply_files/'
    os.makedirs(path, exist_ok=True)

    for i, mesh in enumerate(mesh_list):
        o3d.io.write_triangle_mesh(path + data_set_name + '_' + str(i) + '.ply', mesh)

def read_pcd_pointclouds(data_set_name: str, max_point_cloud_size=50000, path_to_generated_folder='../generated'):
    pcd_files = list_files(path_to_generated_folder + '/' + data_set_name + '/pcd_pointclouds/')
    print('Start reading %d pcd pointclouds.' % len(pcd_files))

    reader = pcl.io.PCDReader()

    pcl_point_clouds = []
    normals = []
    ignored = []
    for i, file in enumerate(pcd_files):
        pc = pcl.PointCloud.PointXYZ()
        reader.read(file, pc)
        p3d_pc = o3d.io.read_point_cloud(file)

        if len(pc.points) < max_point_cloud_size:
            pcl_point_clouds.append(pc)
            normals.append(p3d_pc.normals)
        else:
            ignored.append(i)

    print('%d files were ignored because they are too large.' % len(ignored))
    return pcl_point_clouds, normals, ignored

def read_pcd_pointclouds_poisson(data_set_name: str, number_of_points: int, path_to_generated_folder='../../generated'):
    pcd_files = list_files(path_to_generated_folder + '/' + data_set_name + '/pcd_pointclouds_poisson/'
                           + str(number_of_points) + '/')
    reader = pcl.io.PCDReader()
    pcl_point_clouds = []
    open3d_point_clouds = []
    print('Start reading %d poisson sampled pcd pointclouds...' % len(pcd_files))
    for i, file in enumerate(tqdm(pcd_files)):
        pc = pcl.PointCloud.PointXYZ()
        reader.read(file, pc)
        pcl_point_clouds.append(pc)

        open3d_point_clouds.append(o3d.io.read_point_cloud(file))
        # print(file)

    return pcl_point_clouds, open3d_point_clouds

def read_ply_files_with_pcl(data_set_name: str, max_point_cloud_size=50000):
    pcd_files = list_files('../generated/' + data_set_name + '/ply_files/')
    print('Start reading %d pcd pointclouds.' % len(pcd_files))

    reader = pcl.io.PLYReader()

    pcl_point_clouds = []
    normals = []
    ignored = []
    for i, file in enumerate(pcd_files):
        pc = pcl.PointCloud.PointXYZ()
        mesh = pcl.PolygonMesh()
        ply = pcl.io.loadPLYFile(file, mesh)
        reader.read(file, pc)
        p3d_pc = o3d.io.read_point_cloud(file)

        if len(pc.points) < max_point_cloud_size:
            pcl_point_clouds.append(pc)
            normals.append(p3d_pc.normals)
        else:
            ignored.append(i)

    print('%d files were ignored because they are too large.' % len(ignored))
    return pcl_point_clouds, normals, ignored

def list_files(directory):
    r = []
    for root, dirs, files in os.walk(directory):
        for name in files:
            r.append(os.path.join(root, name))
    return r


text:
import networkx as nx
import pandas as pd
import matplotlib.patches as mpatches

red_patch = mpatches.Patch(color='red', label='2Mbps')
blue_patch = mpatches.Patch(color='blue', label='10Mbps')
gray_patch = mpatches.Patch(color='gray', label='1200kbps')
yellow_patch = mpatches.Patch(color='yellow', label='4Mbps')

import matplotlib.pyplot as plt
edgelist = pd.read_csv('/home/home/Documents/nbp-doktorat/omrezje.csv')
nodelist = pd.read_csv('/home/home/Documents/nbp-doktorat/vozlisce.csv')
g = nx.Graph()
for i, elrow in edgelist.iterrows():
    g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
for i, nlrow in nodelist.iterrows():
    g.node[nlrow['id']] = nlrow[1:].to_dict()
#node_positions = {node[0]: (node[1]['x'], -node[1]['y']) for node in g.nodes(data=True)}
#print(node_positions)
edge_colors = [e[2]['color'] for e in g.edges(data=True)]
plt.figure(figsize=(8, 6))
path = dict(nx.all_pairs_shortest_path(g))
print(path)
print(edge_colors)
#nx.draw(g, edge_color=edge_colors, node_size=5, node_color='black',with_labels=True, distance=True, weight=True,pos=nx.spring_layout(g),prog='neato')
nx.draw(g, edge_color=edge_colors, node_size=5, node_color='black',with_labels=True, distance=True, weight=True)
plt.legend(handles=[red_patch,blue_patch,gray_patch,yellow_patch])
plt.title('NBP omrezje', size=20)
plt.show()

reason: import networkx


text:
import numpy as np
from initializers import he_normal

class Layer(object):
    """
    An abstract class that represents a neural network layer.
    """

    def __init__(self, input_shape=None):
        self.input_shape = input_shape
        self.cache = {'X': None}
        self.optimizer_params = {}

    def build(self):
        """
        Initializes the layer's parameters that depend on the input shape
        """

        pass

    def __call__(self, X, *args, **kwargs):
        return self.forward(X, *args, **kwargs)

    @staticmethod
    def forward(f):
        def forward(self, X, training=False, *args, **kwargs):
            """
            Computes the layer's output (forward pass)

            If training is True, the input and output of each layer is stored so that it
            can be used later during backprop. Also, some layers such as Dropout behave
            differently if training is set to True.
            """

            Z = f(self, X, training=training, *args, **kwargs)

            if training:
                self.cache['X'] = X
                self.cache['Z'] = Z

            return Z
        return forward

    @staticmethod
    def backward(f):
        def backward(self, dJ_dZ, training=False, *args, **kwargs):
            """
            Computes the gradients (backward pass) of the loss function with respect to the
            layer's parameters (if any) and the layer's input

            dJ_dZ is the tensor of gradients of the loss function with respect to the layer's
            outputs

            When implementing this function for a layer with parameters, you must return dJ_dX
            (the gradients with respect to the input) first in a tuple, then the rest of the
            tuple should contain the gradients with respect to the layer's parameters in the
            same order of the arguments in update_parameters()
            """

            return f(self, dJ_dZ, training=training, *args, **kwargs)
        return backward

    def update_parameters(self, optimizer):
        """
        Updates the layer's parameters
        """

        pass

class Linear(Layer):
    """
    Just your regular fully connected layer
    """

    def __init__(self, units, kernel_regularizer=None, kernel_initializer=he_normal, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.units = units
        self.kernel_regularizer = kernel_regularizer
        self.kernel_initializer = kernel_initializer

    def build(self, weights=None, biases=None, *args, **kwargs):
        super().build(*args, **kwargs)

        self.output_shape = (self.units,)

        if weights is not None:
            self.weights = weights
        else:
            self.weights = self.kernel_initializer((self.input_shape[0], self.units))

        if biases is not None:
            self.biases = biases
        else:
            self.biases = np.zeros((1, self.units))

        self.optimizer_params['weights'] = None
        self.optimizer_params['biases'] = None

    @Layer.forward
    def forward(self, X, training=False):
        Z = X @ self.weights + self.biases
        return Z

    @Layer.backward
    def backward(self, dJ_dZ, training=False):
        # compute the gradients with respect to the weights
        X = self.cache['X']
        dJ_dW = X.T @ dJ_dZ
        # add regularization gradients
        if self.kernel_regularizer is not None:
            m = dJ_dZ.shape[0]
            dJ_dW += self.kernel_regularizer.backward(self.weights) / m

        # compute the gradients with respect to the biases
        dJ_db = np.sum(dJ_dZ, axis=0, keepdims=True)

        # compute the gradients with respect to the input
        dJ_dX = dJ_dZ @ self.weights.T

        return dJ_dX, dJ_dW, dJ_db

    def update_parameters(self, dJ_dW, dJ_db, optimizer):
        self.weights, self.optimizer_params['weights'] = optimizer.optimize(self.weights, dJ_dW,
                                                                            self.optimizer_params['weights'])
        self.biases, self.optimizer_params['biases'] = optimizer.optimize(self.biases, dJ_db,
                                                                          self.optimizer_params['biases'])

class ReLU(Layer):
    def build(self, *args, **kwargs):
        super().build(*args, **kwargs)

        self.output_shape = self.input_shape

    @Layer.forward
    def forward(self, X, training=False):
        Z = np.maximum(0, X)
        return Z

    @Layer.backward
    def backward(self, dJ_dZ, training=False):
        X = self.cache['X']
        dJ_dX = dJ_dZ * np.heaviside(X, 0)
        return dJ_dX

class Tanh(Layer):
    def build(self, *args, **kwargs):
        super().build(*args, **kwargs)

        self.output_shape = self.input_shape

    @Layer.forward
    def forward(self, X, training=False):
        Z = np.tanh(X)
        return Z

    @Layer.backward
    def backward(self, dJ_dZ, training=False):
        Z = self.cache['Z']
        dJ_dX = dJ_dZ * (1 - np.square(Z))
        return dJ_dX

class Sigmoid(Layer):
    def build(self, *args, **kwargs):
        super().build(*args, **kwargs)

        self.output_shape = self.input_shape

    @Layer.forward
    def forward(self, X, training=False):
        Z = 1 / (1 + np.exp(-X))
        return Z

    @Layer.backward
    def backward(self, dJ_dZ, training=False):
        Z = self.cache['Z']
        dJ_dX = dJ_dZ * Z * (1 - Z)
        return dJ_dX

class Softmax(Layer):
    def build(self, *args, **kwargs):
        super().build(*args, **kwargs)

        self.output_shape = self.input_shape

    @Layer.forward
    def forward(self, X, training=False):
        X = X - np.max(X, axis=-1, keepdims=True)
        exp_X = np.exp(X)
        Z = exp_X / exp_X.sum(axis=-1, keepdims=True)
        return Z

    @Layer.backward
    def backward(self, dJ_dZ, training=False):
        # dJ_dX = dJ_dZ * Z - (dJ_dZ @ Z.T * np.eye(m)) @ Z  # -> needs a lot (m^2) of memory

        Z = self.cache['Z']
        dJ_dX = (dJ_dZ - np.sum(dJ_dZ * Z, axis=-1, keepdims=True)) * Z
        return dJ_dX

class Dropout(Layer):
    def __init__(self, drop_rate, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.drop_rate = drop_rate

    def build(self, *args, **kwargs):
        super().build(*args, **kwargs)

        self.output_shape = self.input_shape

    @Layer.forward
    def forward(self, X, training=False):
        if training:
            mask = (np.random.rand(*X.shape) >= self.drop_rate).astype(int)
            self.cache['mask'] = mask
            Z = X * mask / (1 - self.drop_rate)
        else:
            Z = X
        return Z

    @Layer.backward
    def backward(self, dJ_dZ, training=False):
        if training:
            mask = self.cache['mask']
            dJ_dX = dJ_dZ * mask / (1 - self.drop_rate)
        else:
            dJ_dX = dJ_dZ
        return dJ_dX

class BatchNormalization(Layer):
    def __init__(self, epsilon=1e-8, *args, **kwargs):
        super().__init__(self, *args, **kwargs)

        self.epsilon = epsilon

    def build(self, gamma=None, beta=None, *args, **kwargs):
        super().build(*args, **kwargs)

        self.output_shape = self.input_shape

        if beta is not None:
            self.beta = beta
        else:
            self.beta = np.zeros((1,) + self.input_shape)

        if gamma is not None:
            self.gamma = gamma
        else:
            self.gamma = np.ones((1,) + self.input_shape)

        self.optimizer_params['gamma'] = None
        self.optimizer_params['beta'] = None

    @Layer.forward
    def forward(self, X, training=False):
        X_mean = np.mean(X, axis=0, keepdims=True)
        X_var = np.mean(np.square(X - X_mean), axis=0, keepdims=True)
        X_hat = (X - X_mean) / (np.sqrt(X_var) + self.epsilon)
        Z = self.gamma * X_hat + self.beta

        if training:
            self.cache['X_hat'] = X_hat
            self.cache['X_mean'] = X_mean
            self.cache['X_var'] = X_var
        return Z

    @Layer.backward
    def backward(self, dJ_dZ, training=False):
        X = self.cache['X']
        X_hat = self.cache['X_hat']
        X_mean = self.cache['X_mean']
        X_var = self.cache['X_var']

        m = X.shape[0]

        dJ_dgamma = np.sum(dJ_dZ * X_hat, axis=0, keepdims=True)
        dJ_dbeta = np.sum(dJ_dZ, axis=0, keepdims=True)

        dJ_dX_hat = dJ_dZ * self.gamma

        minus = X - X_mean
        denom = (np.sqrt(X_var) + self.epsilon)

        dminus_dX = 1 - X / m
        ddenom_dX = (2 / m) * (X - X_mean) * dminus_dX / denom
        dX_hat_dX = (denom * dminus_dX - minus * ddenom_dX) / np.square(denom)
        dJ_dX = dJ_dX_hat * dX_hat_dX

        return dJ_dX, dJ_dgamma, dJ_dbeta

    def update_parameters(self, dJ_dgamma, dJ_dbeta, optimizer):
        self.gamma, self.optimizer_params['gamma'] = optimizer.optimize(self.gamma, dJ_dgamma,
                                                                            self.optimizer_params['gamma'])
        self.beta, self.optimizer_params['beta'] = optimizer.optimize(self.beta, dJ_dbeta,
                                                                          self.optimizer_params['beta'])

class Conv2D(Layer):
    """
    2D convolution layer

    Specify the number of output channels using the filters argument.
    For each of kernel_size, stride and padding, if an integer is provided,
    the both dimensions will be set to that integer.
    """

    def __init__(self, filters, kernel_size, stride=(1,1), kernel_regularizer=None, kernel_initializer=he_normal, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # convolutional layer hyperparameters
        self.filters = filters
        if isinstance(kernel_size, int):
            kernel_size = (kernel_size, kernel_size)
        if isinstance(stride, int):
            stride = (stride, stride)
        self.kernel_size = kernel_size
        self.stride = stride

        # other hyperparameters
        self.kernel_regularizer = kernel_regularizer
        self.kernel_initializer = kernel_initializer

    def build(self, kernels=None, biases=None, *args, **kwargs):
        super().build(*args, **kwargs)

        h_in, w_in, c_in = self.input_shape
        h_out = int((h_in - self.kernel_size[0]) / self.stride[0] + 1)
        w_out = int((w_in - self.kernel_size[1]) / self.stride[1] + 1)
        c_out = self.filters
        self.output_shape = (h_out, w_out, c_out)

        if kernels is not None:
            self.kernels = kernels
        else:
            self.kernels = self.kernel_initializer((c_out, self.kernel_size[0], self.kernel_size[1], c_in))

        if biases is not None:
            self.biases = biases
        else:
            self.biases = np.zeros((c_out,))

        self.optimizer_params['kernels'] = None
        self.optimizer_params['biases'] = None

    @Layer.forward
    def forward(self, X, training=False):
        """
        X has the shape (m, h_in, w_in, c_in)
        """

        m = X.shape[0]
        h_out, w_out, c_out = self.output_shape
        k_y, k_x = self.kernel_size
        s_y, s_x = self.stride

        Z = np.zeros((m,) + self.output_shape)
        for i in range(m):
            for y_out in range(h_out):
                y_in_start = y_out * s_y
                y_in_end = y_in_start + k_y
                for x_out in range(w_out):
                    x_in_start = x_out * s_x
                    x_in_end = x_in_start + k_x

                    X_slice = X[i,y_in_start:y_in_end,x_in_start:x_in_end,:]
                    for f in range(c_out):
                        Z[i,y_out,x_out,f] = np.sum(self.kernels[f] * X_slice) + self.biases[f]

        return Z

    @Layer.backward
    def backward(self, dJ_dZ, training=False):
        # TODO
        return dJ_dZ

    def update_parameters(self, dJ_dW, dJ_db, optimizer):
        self.kernels, self.optimizer_params['kernels'] = optimizer.optimize(self.kernels, dJ_dW,
                                                                            self.optimizer_params['kernels'])
        self.biases, self.optimizer_params['biases'] = optimizer.optimize(self.biases, dJ_db,
                                                                          self.optimizer_params['biases'])

max_stars_repo_name: MoazAshraf/Deep-Learning-From-Scratch

max_issues_repo_name: MoazAshraf/Deep-Learning-From-Scratch

max_forks_repo_name: MoazAshraf/Deep-Learning-From-Scratch


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

'''postprocess'''
import argparse
import collections
import glob
import json
import math
import os
import pickle
import re
import string
import sys
import unicodedata

import six
import numpy as np

def parse_args():
    """set and check parameters."""
    parser = argparse.ArgumentParser(description="bert process")
    parser.add_argument("--data_dir", type=str, default="",
                        help="Dataset contain input_ids, input_mask, segment_ids, label_ids")
    parser.add_argument("--eval_json_path", type=str, default="", help="label ids to name")
    args_opt = parser.parse_args()
    return args_opt

def f1_score(prediction, ground_truth):
    """calculate f1 score"""
    prediction_tokens = normalize_answer(prediction).split()
    ground_truth_tokens = normalize_answer(ground_truth).split()
    common = collections.Counter(prediction_tokens) & collections.Counter(ground_truth_tokens)
    num_same = sum(common.values())
    if num_same == 0:
        return 0
    precision = 1.0 * num_same / len(prediction_tokens)
    recall = 1.0 * num_same / len(ground_truth_tokens)
    f1 = (2 * precision * recall) / (precision + recall)
    return f1

def post_process(dataset_file, all_predictions, output_metrics="output.json"):
    """
    process the result of infer tensor to Visualization results.
    Args:
        args: param of config.
        file_name: label file name.
        infer_result: get logit from infer result
        max_seq_length: sentence input length default is 128.
    """
    # print the infer result
    with open(dataset_file) as ds:
        print('==========')
        dataset_json = json.load(ds)
        dataset = dataset_json['data']
        print(dataset)
    print('success')
    re_json = evaluate(dataset, all_predictions)
    print(json.dumps(re_json))
    with open(output_metrics, 'w') as wr:
        wr.write(json.dumps(re_json))

def normalize_answer(s):
    """Lower text and remove punctuation, articles and extra whitespace."""
    def remove_articles(text):
        return re.sub(r'\b(a|an|the)\b', ' ', text)

    def white_space_fix(text):
        return ' '.join(text.split())

    def remove_punc(text):
        exclude = set(string.punctuation)
        return ''.join(ch for ch in text if ch not in exclude)

    def lower(text):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(s))))

def exact_match_score(prediction, ground_truth):
    return normalize_answer(prediction) == normalize_answer(ground_truth)

def metric_max_over_ground_truths(metric_fn, prediction, ground_truths):
    scores_for_ground_truths = []
    for ground_truth in ground_truths:
        score = metric_fn(prediction, ground_truth)
        scores_for_ground_truths.append(score)
    return max(scores_for_ground_truths)

def evaluate(dataset, predictions):
    """do evaluation"""
    f1 = exact_match = total = 0
    for article in dataset:
        for paragraph in article['paragraphs']:
            for qa in paragraph['qas']:
                total += 1
                if qa['id'] not in predictions:
                    message = 'Unanswered question ' + qa['id'] + \
                              ' will receive score 0.'
                    print(message, file=sys.stderr)
                    continue
                ground_truths = list(map(lambda x: x['text'], qa['answers']))
                if not ground_truths:
                    continue
                prediction = predictions[qa['id']]
                exact_match += metric_max_over_ground_truths(
                    exact_match_score, prediction, ground_truths)
                f1 += metric_max_over_ground_truths(
                    f1_score, prediction, ground_truths)

    exact_match = 100.0 * exact_match / total
    f1 = 100.0 * f1 / total
    print(exact_match)
    print(f1)
    return {'exact_match': exact_match, 'f1': f1}

def get_infer_logits(args, file_name):
    """
    get the result of model output.
    Args:
        infer_result: get logit from infer result
        max_seq_length: sentence input length default is 384.
    """
    infer_logits_path = os.path.realpath(os.path.join(args.data_dir, "10_data", file_name))
    data_0 = []
    data_1 = []
    with open(infer_logits_path, "r") as f:
        for line in f:
            data_0.append(float(line.strip('\n')))

    for i in range(384):
        data_1.append([data_0[i], data_0[384 + i]])
    res = np.array(data_1)
    #print("output tensor is: ", res.shape)
    start_logits = [float(x) for x in res[:, 0].flat]
    end_logits = [float(x) for x in res[:, 1].flat]

    return start_logits, end_logits

def _get_best_indexes(logits, n_best_size):
    """Get the n-best logits from a list."""
    index_and_score = sorted(enumerate(logits), key=lambda x: x[1], reverse=True)

    best_indexes = []
    for (i, score) in enumerate(index_and_score):
        if i >= n_best_size:
            break
        best_indexes.append(score[0])
    return best_indexes

def get_prelim_predictions(args, file_name, start_logits, end_logits, n_best_size, max_answer_length):
    """
    process the logits of infer tensor to Visualization results based on n_best_size and max_answer_length
    Args:
        args: param of config.
        file_name: used feature file name.
        start_logits: get start logit from infer result
        end_logits: get end logit from infer result
        n_best_size: best of n answer (start point and end point).
        max_answer_length: maximum answer length
    """
    feature_tokens_file = os.path.realpath(os.path.join(args.data_dir, "04_data", file_name))
    feature_token_to_orig_map_file = os.path.realpath(os.path.join(args.data_dir, "05_data", file_name))
    feature_token_is_max_context_file = os.path.realpath(os.path.join(args.data_dir, "06_data", file_name))
    example_index_file = os.path.realpath(os.path.join(args.data_dir, "09_data", file_name))
    tokens = pickle.load(open(feature_tokens_file, 'rb+'))
    token_to_orig_map = pickle.load(open(feature_token_to_orig_map_file, 'rb+'))
    token_is_max_context = pickle.load(open(feature_token_is_max_context_file, 'rb+'))
    example_index = np.fromfile(example_index_file, np.int32)[0]
    _PrelimPrediction = collections.namedtuple(
        "PrelimPrediction", ["start_index", "end_index", "start_logit", "end_logit"])
    prelim_predictions = []
    # keep track of the minimum score of null start+end of position 0
    start_indexes = _get_best_indexes(start_logits, n_best_size)
    end_indexes = _get_best_indexes(end_logits, n_best_size)
    # if we could have irrelevant answers, get the min score of irrelevant
    for start_index in start_indexes:
        for end_index in end_indexes:
            # We could hypothetically create invalid predictions, e.g., predict
            # that the start of the span is in the question. We throw out all
            # invalid predictions.
            if start_index >= len(tokens):
                continue
            if end_index >= len(tokens):
                continue
            if start_index not in token_to_orig_map:
                continue
            if end_index not in token_to_orig_map:
                continue
            if not token_is_max_context.get(start_index, False):
                continue
            if end_index < start_index:
                continue
            length = end_index - start_index + 1
            if length > max_answer_length:
                continue
            prelim_predictions.append(
                _PrelimPrediction(
                    start_index=start_index,
                    end_index=end_index,
                    start_logit=start_logits[start_index],
                    end_logit=end_logits[end_index]))

    prelim_predictions = sorted(
        prelim_predictions,
        key=lambda x: (x.start_logit + x.end_logit),
        reverse=True)
    return prelim_predictions, (tokens, token_to_orig_map, token_is_max_context, example_index)

def get_final_text(pred_text, orig_text, do_lower_case):
    """Project the tokenized prediction back to the original text."""
    def _strip_spaces(text):
        ns_chars = []
        ns_to_s_map = collections.OrderedDict()
        for (i, c) in enumerate(text):
            if c == " ":
                continue
            ns_to_s_map[len(ns_chars)] = i
            ns_chars.append(c)
        ns_text = "".join(ns_chars)
        return (ns_text, ns_to_s_map)

    tokenizer = BasicTokenizer(do_lower_case=do_lower_case)
    tok_text = " ".join(tokenizer.tokenize(orig_text))

    start_position = tok_text.find(pred_text)
    if start_position == -1:
        return orig_text
    end_position = start_position + len(pred_text) - 1

    (orig_ns_text, orig_ns_to_s_map) = _strip_spaces(orig_text)
    (tok_ns_text, tok_ns_to_s_map) = _strip_spaces(tok_text)

    if len(orig_ns_text) != len(tok_ns_text):
        return orig_text

    tok_s_to_ns_map = {}
    for (i, tok_index) in six.iteritems(tok_ns_to_s_map):
        tok_s_to_ns_map[tok_index] = i

    orig_start_position = None
    if start_position in tok_s_to_ns_map:
        ns_start_position = tok_s_to_ns_map[start_position]
        if ns_start_position in orig_ns_to_s_map:
            orig_start_position = orig_ns_to_s_map[ns_start_position]

    if orig_start_position is None:
        return orig_text

    orig_end_position = None
    if end_position in tok_s_to_ns_map:
        ns_end_position = tok_s_to_ns_map[end_position]
        if ns_end_position in orig_ns_to_s_map:
            orig_end_position = orig_ns_to_s_map[ns_end_position]

    if orig_end_position is None:
        return orig_text

    output_text = orig_text[orig_start_position:(orig_end_position + 1)]
    return output_text

def get_nbest(args, prelim_predictions, features, n_best_size, do_lower_case):
    """get nbest predictions"""
    _NbestPrediction = collections.namedtuple("NbestPrediction", ["text", "start_logit", "end_logit"])
    seen_predictions = {}
    nbest = []
    (tokens, token_to_orig_map, token_is_max_context, example_index) = features
    token_is_max_context = token_is_max_context
    doc_tokens_file = os.path.realpath(os.path.join(args.data_dir, "07_data", 'squad_bs_'+str(example_index)+'.bin'))
    qas_id_file = os.path.realpath(os.path.join(args.data_dir, "08_data", 'squad_bs_'+str(example_index)+'.bin'))
    doc_tokens = pickle.load(open(doc_tokens_file, 'rb+'))
    qas_id = pickle.load(open(qas_id_file, 'rb+'))
    for pred in prelim_predictions:
        if len(nbest) >= n_best_size:
            break
        if pred.start_index > 0:  # this is a non-null prediction
            tok_tokens = tokens[pred.start_index:(pred.end_index + 1)]
            orig_doc_start = token_to_orig_map[pred.start_index]
            orig_doc_end = token_to_orig_map[pred.end_index]
            orig_tokens = doc_tokens[orig_doc_start:(orig_doc_end + 1)]
            tok_text = " ".join(tok_tokens)

            # De-tokenize WordPieces that have been split off.
            tok_text = tok_text.replace(" ##", "")
            tok_text = tok_text.replace("##", "")

            # Clean whitespace
            tok_text = tok_text.strip()
            tok_text = " ".join(tok_text.split())
            orig_text = " ".join(orig_tokens)
            final_text = get_final_text(tok_text, orig_text, do_lower_case)
            if final_text in seen_predictions:
                continue

            seen_predictions[final_text] = True
        else:
            final_text = ""
            seen_predictions[final_text] = True

        nbest.append(
            _NbestPrediction(
                text=final_text,
                start_logit=pred.start_logit,
                end_logit=pred.end_logit))

    # In very rare edge cases we could have no valid predictions. So we
    # just create a nonce prediction in this case to avoid failure.
    if not nbest:
        nbest.append(_NbestPrediction(text="empty", start_logit=0.0, end_logit=0.0))

    assert len(nbest) >= 1
    return nbest, qas_id

def _compute_softmax(scores):
    """Compute softmax probability over raw logits."""
    if not scores:
        return []

    max_score = None
    for score in scores:
        if max_score is None or score > max_score:
            max_score = score

    exp_scores = []
    total_sum = 0.0
    for score in scores:
        x = math.exp(score - max_score)
        exp_scores.append(x)
        total_sum += x

    probs = []
    for score in exp_scores:
        probs.append(score / total_sum)
    return probs

def get_one_prediction(nbest):
    '''get one prediction'''
    total_scores = []
    best_non_null_entry = None
    for entry in nbest:
        total_scores.append(entry.start_logit + entry.end_logit)
        if not best_non_null_entry:
            if entry.text:
                best_non_null_entry = entry

    probs = _compute_softmax(total_scores)

    nbest_json = []
    for (i, entry) in enumerate(nbest):
        output = collections.OrderedDict()
        output["text"] = entry.text
        output["probability"] = probs[i]
        output["start_logit"] = entry.start_logit
        output["end_logit"] = entry.end_logit
        nbest_json.append(output)

    assert len(nbest_json) >= 1
    return nbest_json

def whitespace_tokenize(text):
    """Runs basic whitespace cleaning and splitting on a piece of text."""
    text = text.strip()
    if not text:
        return []
    tokens = text.split()
    return tokens

def _is_punctuation(char):
    """Checks whether `chars` is a punctuation character."""
    cp = ord(char)
    # We treat all non-letter/number ASCII as punctuation.
    # Characters such as "^", "$", and "`" are not in the Unicode
    # Punctuation class but we treat them as punctuation anyways, for
    # consistency.
    if ((33 <= cp <= 47) or (58 <= cp <= 64) or
            (91 <= cp <= 96) or (123 <= cp <= 126)):
        return True
    cat = unicodedata.category(char)
    if cat.startswith("P"):
        return True
    return False

def _is_control(char):
    """Checks whether `chars` is a control character."""
    # These are technically control characters but we count them as whitespace
    # characters.
    control_char = ["\t", "\n", "\r"]
    if char in control_char:
        return False
    cat = unicodedata.category(char)
    if cat in ("Cc", "Cf"):
        return True
    return False

def _is_whitespace(char):
    """Checks whether `chars` is a whitespace character."""
    # \t, \n, and \r are technically control characters but we treat them
    # as whitespace since they are generally considered as such.
    whitespace_char = [" ", "\t", "\n", "\r"]
    if char in whitespace_char:
        return True
    cat = unicodedata.category(char)
    if cat == "Zs":
        return True
    return False

class BasicTokenizer():
    """
    Basic tokenizer
    """
    def __init__(self, do_lower_case=True):
        self.do_lower_case = do_lower_case

    def tokenize(self, text):
        """
        Do basic tokenization.
        Args:
            text: text in unicode.

        Returns:
            a list of tokens split from text
        """
        text = self._clean_text(text)
        text = self._tokenize_chinese_chars(text)

        orig_tokens = whitespace_tokenize(text)
        split_tokens = []
        for token in orig_tokens:
            if self.do_lower_case:
                token = token.lower()
                token = self._run_strip_accents(token)
            aaa = self._run_split_on_punc(token)
            split_tokens.extend(aaa)

        output_tokens = whitespace_tokenize(" ".join(split_tokens))
        return output_tokens

    def _run_strip_accents(self, text):
        """Strips accents from a piece of text."""
        text = unicodedata.normalize("NFD", text)
        output = []
        for char in text:
            cat = unicodedata.category(char)
            if cat == "Mn":
                continue
            output.append(char)
        return "".join(output)

    def _run_split_on_punc(self, text):
        """Splits punctuation on a piece of text."""
        i = 0
        start_new_word = True
        output = []
        for char in text:
            if _is_punctuation(char):
                output.append([char])
                start_new_word = True
            else:
                if start_new_word:
                    output.append([])
                start_new_word = False
                output[-1].append(char)
            i += 1
        return ["".join(x) for x in output]

    def _clean_text(self, text):
        """Performs invalid character removal and whitespace cleanup on text."""
        output = []
        for char in text:
            cp = ord(char)
            if cp == 0 or cp == 0xfffd or _is_control(char):
                continue
            if _is_whitespace(char):
                output.append(" ")
            else:
                output.append(char)
        return "".join(output)

    def _tokenize_chinese_chars(self, text):
        """Adds whitespace around any CJK character."""
        output = []
        for char in text:
            cp = ord(char)
            if self._is_chinese_char(cp):
                output.append(" ")
                output.append(char)
                output.append(" ")
            else:
                output.append(char)
        return "".join(output)

    def _is_chinese_char(self, cp):
        """Checks whether CP is the codepoint of a CJK character."""
        # This defines a "chinese character" as anything in the CJK Unicode block:
        #   https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
        #
        # Note that the CJK Unicode block is NOT all Japanese and Korean characters,
        # despite its name. The modern Korean Hangul alphabet is a different block,
        # as is Japanese Hiragana and Katakana. Those alphabets are used to write
        # space-separated words, so they are not treated specially and handled
        # like the all of the other languages.
        if ((0x4E00 <= cp <= 0x9FFF) or
                (0x3400 <= cp <= 0x4DBF) or
                (0x20000 <= cp <= 0x2A6DF) or
                (0x2A700 <= cp <= 0x2B73F) or
                (0x2B740 <= cp <= 0x2B81F) or
                (0x2B820 <= cp <= 0x2CEAF) or
                (0xF900 <= cp <= 0xFAFF) or
                (0x2F800 <= cp <= 0x2FA1F)):
            return True

        return False

def run():
    """
    read pipeline and do infer
    """
    args = parse_args()
    # input_ids file list, every file content a tensor[1,128]
    file_list = glob.glob(os.path.join(os.path.realpath(args.data_dir), "10_data", "*.txt"))
    cwq_lists = []
    for i in range(len(file_list)):
        b = os.path.split(file_list[i])
        cwq_lists.append(b)

    def take_second(elem):
        return elem[1]

    cwq_lists.sort(key=take_second)
    yms_lists = []
    for i in range(len(cwq_lists)):
        c = cwq_lists[i][0] + '/' + cwq_lists[i][1]
        yms_lists.append(c)
    file_list = yms_lists
    all_predictions = collections.OrderedDict()
    for input_ids in file_list:
        file_name = input_ids.split('/')[-1].split('.')[0] + '.bin'
        start_logits, end_logits = get_infer_logits(args, input_ids.split('/')[-1])
        prelim_predictions, features = get_prelim_predictions(args, file_name, start_logits=start_logits,
                                                              end_logits=end_logits, n_best_size=20,
                                                              max_answer_length=30)
        nbest, qas_id = get_nbest(args, prelim_predictions, features, n_best_size=20, do_lower_case=True)
        nbest_json = get_one_prediction(nbest)
        all_predictions[qas_id] = nbest_json[0]["text"]
    file = open('infer_result.txt', 'w')
    file.write(str(all_predictions))
    file.close()
    print('done')
    post_process(args.eval_json_path, all_predictions, output_metrics="output.json")

if __name__ == '__main__':
    run()


text:
# exhaustive search
import math
import sympy as sym

print("EXHAUSTIVE SEARCH METHOD")
x = sym.Symbol('x')

### Function ###

f = x**3 + (54/x**2)

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

print("step : 1")
def substitute(k):
    return (f.subs(x,k))

a = float(input("Enter 'a' value "))
b = int(input("Enter 'b' value "))
n = int(input("Enter 'n' value "))
x1 = a
delta_x = (b-a)/n
print(delta_x)
x2 = delta_x + x1
x3 = x2 + delta_x
print("step : 2")
f1 = substitute(x1)
f2 = substitute(x2)
f3 = substitute(x3)
print("x1 = {}  x2 = {}  x3 = {}".format(x1,x2,x3))
print("f1 = {}  f2 = {}  f3 = {}".format(f1,f2,f3))

while (not ((f1 >= f2) and (f2 <= f3))):
    print("step : 2")
    x1=x2
    x2=x3
    x3=x3+delta_x
    print("x1 = {}  x2 = {}  x3 = {}".format(x1,x2,x3))
    f1 = substitute(x1)
    f2 = substitute(x2)
    f3 = substitute(x3)
    print("step : 3")
    if(x3 <= b):
        print("     x3 <= b ----- satisfied ")
        print("f1 = {}  f2 = {}  f3 = {}".format(f1,f2,f3))
        continue
    else:
        print("No minimum lies in {} {}".format(a,b))
        break

print("({} , {})".format(x1,x2+delta_x))

reason: import sympy


text:
import ncc
import mosse
import numpy as np
import trackers
from cvl.dataset import OnlineTrackingBenchmark
from multiprocessing import Pool
from functools import partial

DATASET_PATH = "../data/Mini-OTB"

def run_tracker_on_sequence(tracker, sequence, bbox_enlarge_factor):
    """
    Runs a tracker on a sequence and returns the resulting bounding boxes for each frame
    """
    bboxes = []
    for frame_idx, frame in enumerate(sequence):
        image_color = frame['image']
        if frame_idx == 0:
            bbox = frame['bounding_box']
            bbox = bbox.rescale(args.bbox_enlarge_factor)

            if bbox.width % 2 == 0:
                bbox.width += 1

            if bbox.height % 2 == 0:
                bbox.height += 1

            tracker.start(image_color, bbox)
        else:
            bbox = tracker.detect(image_color)
            tracker.update(image_color)
        bboxes.append(bbox.rescale(1/bbox_enlarge_factor))
    return bboxes

def evaluate(tracker, bbox_enlarge_factor):
    """
    Evaluates mean average IoU = Average IoU over all frames in a sequence, and mean over all sequences
    """
    otb = OnlineTrackingBenchmark(DATASET_PATH)
    per_seq_mean_iou = []

    for sequence_idx, sequence in enumerate(otb):
        bboxes = run_tracker_on_sequence(tracker, sequence, bbox_enlarge_factor)
        mean_iou = otb.calculate_mean_iou(sequence_idx, bboxes)
        per_seq_mean_iou.append(mean_iou)
        print(mean_iou)
    return np.mean(per_seq_mean_iou)

def _evaluate_sequence(seq_idx, args):
    tracker = trackers.get_tracker(args)
    otb = OnlineTrackingBenchmark(DATASET_PATH)
    bboxes = run_tracker_on_sequence(tracker, otb[seq_idx], args.bbox_enlarge_factor)
    mean_iou = otb.calculate_mean_iou(seq_idx, bboxes)
    return mean_iou

def evaluate_parallel(args):

    f = partial(_evaluate_sequence, args=args)

    num_sequences = len(OnlineTrackingBenchmark(DATASET_PATH))
    with Pool() as p:
        per_seq_mean_iou = p.map(f, list(range(num_sequences)))

    return np.mean(per_seq_mean_iou)

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(conflict_handler="resolve")
    parser.add_argument("--bbox-enlarge-factor", type=float, default=1.0)
    trackers.add_tracker_args(parser)
    args = parser.parse_args()

    mean_average_iou = evaluate_parallel(args)
    print(mean_average_iou)


text:
__all__ = [
    "BaseConnector",
    "BaseCommuter",
]

from contextlib import contextmanager
from typing import Any, Iterator, List, Mapping, Optional, Sequence, Tuple, Union

import abc

import numpy as np
import psycopg2
from psycopg2 import sql
from psycopg2.extensions import register_adapter, AsIs
from psycopg2.extras import execute_batch

from . import exc

QueryParams = Union[Sequence[Any], Mapping[str, Any]]
register_adapter(np.int64, AsIs)
register_adapter(np.float64, AsIs)

class BaseConnector(abc.ABC):
    """Base class for all connectors."""

    def __init__(self, **kwargs: str) -> None:
        self._kwargs = kwargs

        if "db_name" in self._kwargs:
            self._kwargs["dbname"] = self._kwargs.pop("db_name")

    def __del__(self) -> None:
        self.close_all()

    def __repr__(self) -> str:
        desc = "("
        for key in ["host", "user", "dbname"]:
            if key in self._kwargs.keys():
                desc += key + "=" + self._kwargs[key] + " "
        desc += ")"
        return desc

    @abc.abstractmethod
    @contextmanager
    def open_connection(self) -> Iterator[psycopg2.connect]:
        """Generates a new connection."""

        raise NotImplementedError

    @abc.abstractmethod
    def close_all(self) -> None:
        """Close all active connections."""

        raise NotImplementedError

class BaseCommuter:
    """Base class for all commuters.

    Args:
        connector:
            Instance of connection handler, any subclass
            inherited from :class:`~pgcom.base.BaseConnector`.
    """

    def __init__(self, connector: BaseConnector) -> None:
        self.connector = connector

    def __repr__(self) -> str:
        return repr(self.connector)

    def execute(
        self, cmd: Union[str, sql.Composed], values: Optional[QueryParams] = None
    ) -> None:
        """Execute a database operation (query or command).

        Args:
            cmd:
                SQL query to be executed.
            values:
                Query parameters.

        Returns:
            List of rows of a query result and list of column names.
            Two empty lists are returned if there is no records to fetch.

        Raises:
            QueryExecutionError: if execution fails.
        """

        self._execute(cmd=cmd, values=values)

    def _execute(
        self,
        cmd: Union[str, sql.Composed],
        values: Optional[QueryParams] = None,
        commit: Optional[bool] = True,
        batch: Optional[bool] = False,
    ) -> Tuple[List[Any], List[str]]:
        """Execute a database operation, query or command.

        Args:
            cmd:
                SQL command.
            values:
                Query parameters.
            commit:
                Commit the results if True.
            batch:
                Use execute_batch method if True.

        Returns:
            List of rows of a query result and list of column names.
            Two empty lists are returned if there is no records to fetch.

        Raises:
            QueryExecutionError: if execution fails.
        """

        fetched = []
        columns = []

        with self.connector.open_connection() as conn:
            try:
                with conn.cursor() as cur:
                    if batch:
                        execute_batch(cur, cmd, values)
                    else:
                        if values is None:
                            cur.execute(cmd)
                        else:
                            cur.execute(cmd, values)

                    if cur.description is not None:
                        fetched = cur.fetchall()
                        columns = [desc[0] for desc in cur.description]

                if commit:
                    conn.commit()
            except Exception as e:
                try:
                    conn.rollback()
                except Exception as ex:
                    exc.raise_with_traceback(
                        exc.QueryExecutionError(
                            f"Execution failed on sql: {cmd}\n{ex}\n "
                            f"unable to rollback"
                        )
                    )

                exc.raise_with_traceback(
                    exc.QueryExecutionError(f"Execution failed on sql: {cmd}\n{e}\n")
                )

        return fetched, columns

    def _get_schema(self, table_name: str) -> Tuple[str, str]:
        """Return schema and table name."""

        names = str.split(table_name, ".")

        if len(names) == 2:
            return names[0], names[1]
        else:
            return "public", table_name

max_stars_repo_name: viktorsapozhok/pgcom

max_issues_repo_name: viktorsapozhok/pgcom

max_forks_repo_name: viktorsapozhok/pgcom


text:
import pathlib, fdp, os, sys
import numpy as np

fdp_module_path = pathlib.Path(fdp.__path__[0])
mds_path = pathlib.Path(os.environ['MDSPLUS']) / 'mdsobjects' / 'python'
python_path = pathlib.Path(sys.base_prefix) / 'lib'
np_path = pathlib.Path(np.__path__[0])

def shorten_filename(fn):
    try:
        fn = fn.relative_to(fdp_module_path)
    except:
        try:
            fn = fn.relative_to(mds_path)
        except:
            try:
                fn = fn.relative_to(python_path)
            except:
                try:
                    fn = fn.relative_to(np_path)
                except:
                    pass
    return fn

def trace_lines(frame, event, arg):
    if event not in ['return']:
        return
    co = frame.f_code
    func_name = co.co_name
    line_no = frame.f_lineno
    filename = pathlib.Path(co.co_filename)
    filename = shorten_filename(filename)
    # if event == 'line':
    #     print('   Executing line  {}  in  {}  in  {}'.format(line_no, func_name, filename))
    if func_name[0] == '<':
        return
    if event == 'return':
        print('   Returning from  {}  in  {}'.format(func_name, filename))

def trace_calls(frame, event, arg):
    if event != 'call':
        # return for non-call events
        print('skipped event: {}'.format(event))
        return
    try:
        # caller details
        caller = frame.f_back
        caller_lineno = caller.f_lineno
        caller_code = caller.f_code
        caller_name = caller_code.co_name
        caller_filename = pathlib.Path(caller_code.co_filename)
        # callee details
        call_code = frame.f_code
        call_name = call_code.co_name
        call_filename = pathlib.Path(call_code.co_filename)
    except Exception as e:
        # print(e.args)
        return
    if call_name == 'write':
        # Ignore write calls from print statements
        return
    if (fdp_module_path.as_posix() not in call_filename.as_posix()) or \
        (fdp_module_path.as_posix() not in caller_filename.as_posix()):
        # ignore calls fully outside of the FDP package
        if caller_name == '<module>':
            print('Line {} in {}'.format(caller_lineno, caller_filename.name))
        else:
            return
    # adjust filenames relative to FDP directory
    caller_shortfn = shorten_filename(caller_filename)
    call_shortfn = shorten_filename(call_filename)
    if call_name[0] == '<':
        # 'parse' in call_name or \
        # 'parse' in caller_name or \
        # '__get' in call_name or \
        # '__get' in caller_name:
        return
    if 'MDSplus' in call_filename.as_posix():
        return
    print('   Calling  {}  in  {}  from line  {}  in  {}  in  {}'.format(
            call_name,
            call_shortfn.as_posix(),
            caller_lineno,
            caller_name,
            caller_shortfn.as_posix()))
    return trace_lines

max_stars_repo_name: Fusion-Data-Platform/fdp

max_issues_repo_name: Fusion-Data-Platform/fdp

max_forks_repo_name: Fusion-Data-Platform/fdp


text:
import thunder as td

from factorization import ICA
from numpy import arange
from scipy.ndimage.interpolation import shift
from scipy.ndimage import shift
from numpy import random
from scipy.ndimage.filters import gaussian_filter
from registration import CrossCorr

def main():

    data = td.images.fromtif(path='/home/yash/PycharmProjects/Thunder/neurofinder.00.00/images/*',npartitions=4)

    #print (data)
    reference = data.mean().toarray()
    algorithm = CrossCorr()
    model = algorithm.fit(data, reference)
    shifts = model.transformations
    model = algorithm.fit(shifts, reference=reference)
    registered = model.transform(data)
    print (registered)

if __name__ == "__main__":
    main()

reason: from numpy,from scipy