text:
#! /usr/bin/env python3

import sys
import numpy as np

from Functions import *
from trexi.TREXI import *
from cirexi.CIREXI import *
from brexi.BREXI import *
from elrexi.ELREXI import *

for rexi_method in [
        "trexi",
        "cirexi",
        "elrexi",
        "brexi_tanhsinh",
        "brexi_gauss",
        "brexi_radau",
        "brexi_chebyshev",
    ]:

    function = Functions(
        function_name = "phi3",
        efloat_mode = "mpfloat"
    )

    """
    x = 1e-16j
    x = 0.6j+0.5
    for i in range(6):
        val = function.phiNRec(i, x)
        print(str(float(val.real))+"\t"+str(float(val.imag)))
        val = function.phiNSeries(i, x)
        print(str(float(val.real))+"\t"+str(float(val.imag)))
        print("")

    sys.exit(1)
    """

    for function_name in ["phi0", "phi1", "phi2", "phi3"]: #, "ups1", "ups2", "ups3"]:
    #for function_name in ["phi0"]:

        # T-REXI: Number of Gaussian basis functions
        M = 256

        # T-REXI: Spacing between Gaussian basis functions
        h = 0.2

        # CI-REXI: Number of quadrature poles
        #N = M
        N = 256

        # CI-REXI: Radius
        #R = M*h

        # CI-REXI: Value on imaginary axis to be included
        #same used for ELREXI as max imag of ellipse
        lambda_include_imag = 20

        # CI-REXI: Maximum value of quadrature pole
        #same used for ELREXI as max real for ellipse
        lambda_max_real = 10

        # Butcher-REXI
        butcherOrder = 10

        # Testing: number of samples
        num_test_samples = 12345

        # Testing: Range (start, end)
        test_range_real = [None, None]
        test_range_imag = [None, None]

        # Error to test for
        error_eps = 1e-8

        # verbose
        verbosity = 10
        verbosity = 0

        # efloat_mode
        efloat_mode = "float"
        #efloat_mode = "mpfloat"

        coeffs = None

        if rexi_method == "trexi":

            if function_name != "phi0":
                continue

            trexi = TREXI(efloat_mode=efloat_mode)
            coeffs = trexi.setup(
                function_name = function_name,
                M = N,
                h = h
            )

            # Convert to floating point
            coeffs = coeffs.toFloat()
            unique_id_string = trexi.getUniqueId()

            test_range_real = None
            test_range_imag = [-N*h*0.95, N*h*0.95]

        elif rexi_method == "cirexi":

            cirexi = CIREXI(efloat_mode=efloat_mode)

            coeffs = cirexi.setup(
                function_name = function_name,
                N = N,
                lambda_max_real = lambda_max_real,
                lambda_include_imag = lambda_include_imag
            )

            # Convert to floating point
            coeffs = coeffs.toFloat()

            unique_id_string = cirexi.getUniqueId()

            #test_range = [-lambda_include_imag*0.5, lambda_include_imag*0.5]
            test_range_real = [-1.0, 1.0]
            test_range_imag = [-1.0, 1.0]

        elif rexi_method == "elrexi":

            elrexi = ELREXI(efloat_mode=efloat_mode)

            coeffs = elrexi.setup(
                function_name = function_name,
                N = N,
                lambda_max_real = lambda_max_real,
                lambda_max_imag = lambda_include_imag
            )

            # Convert to floating point
            coeffs = coeffs.toFloat()

            unique_id_string = elrexi.getUniqueId()

            #test_range = [-lambda_include_imag*0.5, lambda_include_imag*0.5]
            test_range_real = [-1.0, 1.0]
            test_range_imag = [-1.0, 1.0]

        elif rexi_method == "brexi_gauss":

            if function_name != "phi0":
                continue

            brexi = BREXI(efloat_mode=efloat_mode)
            coeffs = brexi.setup(N=butcherOrder, quadrature_method="gauss")

            # Convert to floating point
            coeffs = coeffs.toFloat()
            unique_id_string = brexi.getUniqueId()
            test_range_real = [butcherOrder*0.25, butcherOrder*0.25]
            test_range_imag = [butcherOrder*0.5, butcherOrder*0.5]

        elif rexi_method == "brexi_radau":

            if function_name != "phi0":
                continue

            brexi = BREXI(efloat_mode=efloat_mode)
            coeffs = brexi.setup(N=butcherOrder, quadrature_method="radau")

            # Convert to floating point
            coeffs = coeffs.toFloat()
            unique_id_string = brexi.getUniqueId()
            test_range_real = [butcherOrder*0.25, butcherOrder*0.25]
            test_range_imag = [butcherOrder*0.5, butcherOrder*0.5]

        elif rexi_method == "brexi_chebyshev":

            if function_name != "phi0":
                continue

            brexi = BREXI(efloat_mode=efloat_mode)
            coeffs = brexi.setup(N=butcherOrder, quadrature_method="chebyshev")

            # Convert to floating point
            coeffs = coeffs.toFloat()
            unique_id_string = brexi.getUniqueId()
            test_range_real = [butcherOrder*0.15, butcherOrder*0.15]
            test_range_imag = [butcherOrder*0.25, butcherOrder*0.25]

        elif rexi_method == "brexi_tanhsinh":

            if function_name != "phi0":
                continue

            brexi = BREXI(efloat_mode=efloat_mode)
            coeffs = brexi.setup(N=butcherOrder, quadrature_method="tanhsinh")

            # Convert to floating point
            coeffs = coeffs.toFloat()
            unique_id_string = brexi.getUniqueId()
            test_range_real = [butcherOrder*0.25, butcherOrder*0.25]
            test_range_imag = [butcherOrder*0.25, butcherOrder*0.25]

        else:
            raise Exception("Unsupported REXI method")

        function = Functions(
            function_name = function_name,
            efloat_mode = "float"
        )

        print("")
        print(unique_id_string)
        print(" + function_name: "+function_name)

        if test_range_real != None:
            max_error = 0
            for x in np.linspace(test_range_real[0], test_range_real[1], num_test_samples):
                lam = x

                y = function.eval(lam)
                yn = coeffs.eval(lam)

                err = np.abs(y-yn)

                if verbosity > 0:

                    #if True:
                    if False:
                        print("x="+str(lam)+"\t\terror="+str(err))
                    else:
                        print("Lambda: "+str(lam))
                        print(" +  exact: "+str(y))
                        print(" + approx: "+str(yn))
                        print(" + Error: "+str(err))
                        print("")

                max_error = max(max_error, err)

            if verbosity == 0:
                print(" + test_range_real: ["+str(test_range_real[0])+", "+str(test_range_real[1])+"]")
                print(" + Error: "+str(max_error))

            if max_error > error_eps:
                raise Exception("Error threshold "+str(error_eps)+" exceeded")

        if test_range_imag != None:
            max_error = 0
            for x in np.linspace(test_range_imag[0], test_range_imag[1], num_test_samples):
                lam = 1j*x

                y = function.eval(lam)
                yn = coeffs.eval(lam)

                err = np.abs(y-yn)

                if verbosity > 0:

                    #if True:
                    if False:
                        print("x="+str(lam)+"\t\terror="+str(err))
                    else:
                        print("Lambda: "+str(lam))
                        print(" +  exact: "+str(y))
                        print(" + approx: "+str(yn))
                        print(" + Error: "+str(err))
                        print("")

                max_error = max(max_error, err)

            if verbosity == 0:
                print(" + test_range_imag: ["+str(test_range_imag[0])+", "+str(test_range_imag[1])+"]")
                print(" + Error: "+str(max_error))

            if max_error > error_eps:
                raise Exception("Error threshold "+str(error_eps)+" exceeded")

        #coeffs.write_file("/tmp/REXI_"+rexi_method+"_"+unique_id_string+"_txt.txt", False)
        #coeffs.write_file("/tmp/REXI_"+rexi_method+"_"+unique_id_string+"_bin.txt", True)

max_stars_repo_name: raaphy/sweet

max_issues_repo_name: raaphy/sweet

max_forks_repo_name: raaphy/sweet


text:
__author__ = 'Jihyun Park'
__email__ = 'jihyunp@uci.edu'

import cPickle as cp
import numpy as np
import os

from mhddata import MHDTrainData, MHDTestData
from evaluate import get_binary_classification_scores, get_accuracy, get_weighted_avg
from utils import get_marginals_from_y_seq, flatten_nested_labels, get_lab_arr
from hmm import convert_class_prob_to_log_emission_prob, viterbi, viterbi_with_multiple_transitions,\
    get_transition_mat, get_spkr_transition_mat, get_start_end_prob

from sklearn.linear_model import LogisticRegression
import sklearn
if int(sklearn.__version__.split(".")[1]) > 17:
    from sklearn.model_selection import GridSearchCV
else:
    from sklearn.grid_search import GridSearchCV

class DialogModel():
    """
    Base class for training & testing topic classification on MHD dialog data.
    The base class is mainly used when loading the results of the model
    that was trained outside this package. (method load_results())
    Other models inherits this model.
    """

    def __init__(self):
        self.tr_data = None  # training data (MHDTrainData)
        self.te_data = None  # test data (MHDTestData)
        self.vectorizer = None  # CountVectorizer for creating BOW/TFIDF
        self.model = None  # Model (e.g. an object of LogisticRegression classifier)
        self.result = None  # Result object, of class DialogResult

    def load_model(self, model_file):
        pass

    def fit_model(self, data_file, model_file):
        pass

    def predict(self, te_data_file):
        pass

    def load_results(self, test_data, model_info='dialog_model', marginals=None,
                     predictions='./model123_pred.pkl', output_probs='./model123_prob.pkl',
                     verbose=1, output_filename='./utter_level_result.txt'):
        """
        This is used when you are not running predictions but just want to load the results
        to calculate scores or to plug the results into HMM.

        Parameters
        ----------
        test_data : MHDTestData
            An object of MHDTestData
        model_info : str
            Model information. e.g. "HMM_LR_OVR"
        marginals : np.array or None
            Marginal probabilities for labels. If not given, weighted average scores will not provided.
        predictions : str
            Path to the pickle file that has nested list of predictions
        output_probs : str
            Path to the pickle file that has nested list of output probabilities
        verbose : int
            The level of verbosity in range [0,3]
        output_filename : str
            Utterance-level results will be saved to this txt file.

        Returns
        -------
        DialogResult

        """

        self.te_data = test_data
        self.n_labels = self.te_data.n_labels
        self.model_info = model_info

        n_labs = self.te_data.n_labels
        te_data_nested = self.te_data.get_utter_level_data_from_sids(sorted(self.te_data.sstt2uid.keys()))
        ulists, docs, labs = te_data_nested

        if os.path.exists(output_probs):
            with open(output_probs, 'rb') as f:
                prob = cp.load(f)
        else:
            prob = None
        if os.path.exists(predictions):
            with open(predictions, 'rb') as f:
                pred = cp.load(f)
        else:
            pred = None

        self.result = DialogResult(n_labs, pred, prob, marginals, model_info)

        if self.te_data.has_label:
            if verbose > 0:
                print("Calculate score")
            self.result.get_scores(labs)
            if verbose > 0:
                print("Printing utterance-level results to file "+output_filename)
            self.result.print_utter_level_results(ulists, docs, labs, self.te_data.lid2name,
                                                  filename=output_filename)
        return self.result

class LogRegDialogModel(DialogModel):
    """
    Logistic Regression model for running topic classification on MHD dialog data.
    """

    def __init__(self, lr_type="ovr"):
        DialogModel.__init__(self)
        self.lr_type = lr_type
        self.trainX, self.trainy = None, None

    def grid_search_parameter(self, train_data, C_values=None,
                              penalty_type="l2", solver='lbfgs',
                              n_fold=3, verbose=1):
        """
        A method that does grid search over the training data and finds the best
        regularization constant and returns the value.
        It creates self.grid_search, which is an obj of sklearn's GridSearchCV.

        Parameters
        ----------
        train_data : MHDTrainData
            An object of MHDTrainData for training data.
        C_values : iterable or None
            Values to be searched to find the best C value.
            If None (default), C value is searched over the values np.arange(0.6,3,0.1)
        penalty_type : str
            Penalty type of logistic regression. "l2" is default.
        solver : str
            "lbfgs" is set to default
        n_fold : int
            The number of cross validation folds.
            Default = 3. Large number is not recommended since there are labels that are quite rare.
        verbose : int
            The level of verbosity in range [0,3]

        Returns
        -------
        float
            Best parameter (regularization constant)
        """
        if self.tr_data is None:
            self.tr_data = train_data
            self.marginals = get_marginals_from_y_seq(self.tr_data.uid2lid, self.tr_data.n_labels)

        if self.trainX is None or self.trainy is None:
            trainX, self.vectorizer = self.tr_data.fit_bow(self.tr_data.corpus_txt, tfidf=True)
            trainy = self.tr_data.uid2lid
            self.trainX = trainX
            self.trainy = trainy
        else:
            trainX = self.trainX
            trainy = self.trainy

        # Just do 3-fold cross-validation
        grid = GridSearchCV(LogisticRegression(penalty=penalty_type, solver=solver, multi_class=self.lr_type),
                            {'C': C_values}, cv=n_fold, verbose=verbose)
        grid.fit(trainX, trainy)
        self.grid_search = grid
        print("Best regularization constant: %.2f" % grid.best_params_['C'])
        return grid.best_params_

    def fit_model(self, train_data, penalty_type="l2", reg_const=1.0,
                  solver='lbfgs', model_file='./lrdialog.pkl', verbose=1):
        """
        Loads training data from `data_file`, processes the data,
        fits the LR model, and then saves the model.
        Updates self.model.

        Parameters
        ----------
        train_data : MHDTrainData
            An object of MHDTrainData for training data
        penalty_type : str
            Penalty type of logistic regression. "l2" is default.
        reg_const : float
            Regularization constant. Inverse of regularization strength.
            Smaller values specify larger regularization.
            (You can plug in the number after running the method 'grid_search_parameter')
            Default is set to 1.0.
        solver : str
            "lbfgs" is set to default.
        model_file : str
            File path to the pickle file of trained model.
        verbose : int
            The level of verbosity.

        Returns
        -------
        LogisticRegression

        """
        if self.tr_data is None:
            self.tr_data = train_data
            self.marginals = get_marginals_from_y_seq(self.tr_data.uid2lid, self.tr_data.n_labels)

        if self.trainX is None or self.trainy is None:
            trainX, self.vectorizer = self.tr_data.fit_bow(self.tr_data.corpus_txt, tfidf=True)
            trainy = self.tr_data.uid2lid
            self.trainX = trainX
            self.trainy = trainy
        else:
            trainX = self.trainX
            trainy = self.trainy

        self.model = LogisticRegression(penalty=penalty_type, C=reg_const,
                                        multi_class=self.lr_type, solver=solver)
        if verbose > 0:
            print("Fitting LogReg Model")
        self.model = self.model.fit(trainX, trainy)
        # Saves model into pkl
        with open(model_file, 'wb') as f:
            if verbose > 0:
                print("Saving Logistic regression model to "+ model_file)
            cp.dump((self.model, self.vectorizer, self.marginals), f, protocol=cp.HIGHEST_PROTOCOL)
        return self.model

    def load_model(self, model_file='./lrdialog.pkl'):
        """
        Loads trained model from a pickle file path.
        self.model, self.vectorizer, self.marginals are updated.
        """
        with open(model_file, 'rb') as f:
            print("Loading Logistic regression model from "+ model_file)
            self.model, self.vectorizer, self.marginals = cp.load(f)

    def predict(self, test_data, verbose=1, output_filename='./utter_level_results.txt'):
        """
        Loads test data from 'te_data_file' and processes the data.
        Run prediction using the trained model.
        Also calculate the scores if the test data has labels.

        Parameters
        ----------
        test_data : MHDTestData
            An object of MHDTestData for test dataset.
        verbose : int
            The level of verbosity in range [0,3]
        output_filename : str
            Path to the file where the utter-level result will be saved.

        Returns
        -------

        """
        if not (self.model and self.vectorizer):
            print ("ERROR: Train or load the model first")
            return

        self.te_data = test_data
        self.n_labels = self.te_data.n_labels

        n_labs = self.te_data.n_labels
        self.model_info = "_".join(["LogReg", self.lr_type, self.model.penalty, str(self.model.C)])

        te_data_nested = self.te_data.get_utter_level_data_from_sids(sorted(self.te_data.sstt2uid.keys()))
        ulists, docs, labs = te_data_nested

        outputprobs = []
        yhats = []
        for sidx in range(len(docs)):
            testX_s = MHDTestData.transform_bow(docs[sidx], self.vectorizer)

            outputprob = self.model.predict_proba(testX_s)
            outputprobs.append(outputprob)
            yhat = self.model.predict(testX_s)
            yhats.append(yhat)

        self.result = DialogResult(n_labs, yhats, outputprobs, self.marginals, self.model_info)
        if self.te_data.has_label:
            if verbose > 0:
                print("Calculate score")
            self.result.get_scores(labs)
            if verbose > 0:
                print("Printing utterance-level results to file "+output_filename)
            self.result.print_utter_level_results(ulists, docs, labs, self.te_data.lid2name,
                                                  filename=output_filename)
        else:
            if verbose > 0:
                print("Printing utterance-level results to file "+output_filename)
            self.result.print_utter_level_results_without_true_lab(ulists, docs, self.te_data.lid2name,
                                                                   filename=output_filename)
        return self.result

class HMMDialogModel(DialogModel):
    """
    Hidden Markov Model that is run on top of other model or a set of probabilities.
    HMM is not jointly trained with the other model that generates output probabilities.
    Pre-trained model with output probabilities are given as an input.
    """

    def __init__(self, base_model):
        """
        Parameters
        ----------
        base_model : DialogModel
            A model whose output probabilities are used for emission probabilities in HMM.
        """

        DialogModel.__init__(self)
        if base_model.result is None:
            print("ERROR: Base model for HMMDialogModel should have .result in it.")
            print("       .predict() method should be applied before running HMM")
            return
        self.base_model = base_model

        # The following variables will be updated at fit_model()
        self.log_transitions, self.marginals = None, None
        self.start_prob, self.end_prob = None, None
        self.log_start_prob, self.log_end_prob = None, None

    def fit_model(self, train_data, model_file='./hmmdialog.pkl', verbose=1):
        """
        Loads training data from 'data_file', processes the data,
        gets transition matrix and other probabilities and saves the model.
        self.start_prob, self.end_prob, self.log_transition

        fit
        Parameters
        ----------
        train_data : MHDTrainData
            An object of MHDTrainData for training data.
        model_file : str
            Path to the pickle file where the Hidden Markov Model related data will be saved.
        verbose : int
            The level of verbosity in range [0,3]

        Returns
        -------

        """
        self.tr_data = train_data
        self.marginals = get_marginals_from_y_seq(self.tr_data.uid2lid, self.tr_data.n_labels)

        self.n_labels = self.tr_data.n_labels
        tr_data_nested = self.tr_data.get_utter_level_data_from_sids(self.tr_data.sstt2uid.keys())
        ulists_tr, docs_tr, labs_tr = tr_data_nested

        self.log_transitions = get_transition_mat(labs_tr, self.n_labels,
                                                  proportional_prior=True, prior_sum=0.5, log=True)

        self.start_prob, self.end_prob = get_start_end_prob(labs_tr, self.n_labels)
        self.log_start_prob = np.log(self.start_prob)
        self.log_end_prob = np.log(self.end_prob)
        self.marginals = get_marginals_from_y_seq(self.tr_data.uid2lid, self.n_labels)

        with open(model_file, 'wb') as f:
            print("Saving model to "+ model_file)
            data_to_save = (self.log_transitions, self.start_prob, self.end_prob, self.marginals)
            cp.dump(data_to_save, f, protocol=cp.HIGHEST_PROTOCOL)

    def load_model(self, model_file='./hmmdialog.pkl'):
        """
        Loads model (trained probabilities) from a pickle
        """
        print("Loading model from "+ model_file)
        with open(model_file, 'rb') as f:
            self.log_transitions, self.start_prob, self.end_prob, self.marginals = cp.load(f)
            self.log_start_prob = np.log(self.start_prob)
            self.log_end_prob = np.log(self.end_prob)

    def predict_viterbi(self, test_data, verbose=1, output_filename="./utter_level_result.txt"):
        """
        Viterbi decoding using output probabilites from the base model,
        marginal probabilities, and transition probabilities.

        Parameters
        ----------
        test_data : MHDTestData
            An object of MHDTestData for test data.
        verbose : int
            The level of verbosity in range [0,3]
        output_filename : str
            Path to the utterance-level result file.
        """
        if self.log_transitions is None:
            print ("ERROR: Train or load the model first")
            return

        self.te_data = test_data
        self.n_labels = self.te_data.n_labels

        self.model_info = "_".join(["HMM", self.base_model.model_info])

        te_data_nested = self.te_data.get_utter_level_data_from_sids(sorted(self.te_data.sstt2uid.keys()))
        ulists, docs, labs = te_data_nested

        vit_res = []
        for sidx in range(len(ulists)):
            output_prob_s = self.base_model.result.output_prob[sidx]
            log_emissions = convert_class_prob_to_log_emission_prob(output_prob_s, self.marginals)

            vit_res.append(viterbi(log_emissions, self.log_transitions,
                                   self.log_start_prob, self.log_end_prob))

        yhats = [s[1] for s in vit_res]
        output_scores = [s[0] for s in vit_res]
        self.result = DialogResult(self.n_labels, yhats, None, self.marginals,
                                   self.model_info, output_scores)
        if self.te_data.has_label:
            if verbose > 0:
                print("Calculate score")
            self.result.get_scores(labs)
            if verbose > 0:
                print("Printing utterance-level results to file "+output_filename)
            self.result.print_utter_level_results(ulists, docs, labs, self.te_data.lid2name,
                                                  filename=output_filename)
        else:
            if verbose > 0:
                print("Printing utterance-level results to file "+output_filename)
            self.result.print_utter_level_results_without_true_lab(ulists, docs, self.te_data.lid2name,
                                                                   filename=output_filename)
        return self.result

class DialogResult():
    """
    Class that stores all the results and calculates different scores.

    * S : number of sessions
    * N_s: number of utterances in the session. different number for each session.
    * T : number of labels

    Variables
    ---------
    n_labels : int
        Number of labels
    predictions : list[list[int]]
        Nested list of size (S, N_s, 1). List of sessions, where session is a list of
        predictions of utterances in that session
    predictions_f : list[int]
        Flattened version of predictions. Length = S x N_s
    output_prob : list[np.array]
        List of output probabilities for each session.
        Length S list of np.array with size (N_s, T)
        Total size = (S, N_s, T)
    output_score : list[list[float]]
        Nested list of output scores of size (S, N_s, 1).
        Only used for HMM.
    marginals : np.array
        np.array, size (T,)
        Marginal probabilities calculated from the training data.
    model_info : str
        String that describes the model
    scores : dict[str, float]
        Dictionary that stores accuracy, and all the averaged and weighted averaged binary scores.
        '_w' means weighted. Weights are the marginal probabilities from training data.
        Keys are: 'accuracy', 'auc', 'auc_w', 'f1score', 'f1score_w', 'precision', 'precision_w',
                   'recall', 'recall_w', 'rprecision', 'rprecision_w'
    bin_scores : dict[str, list[float]]
        Dictionary that has different binary score lists.
        Keys are 'precision', 'recall', 'auc', 'rprecision', 'f1score'
        Value is a list of scores for each label. length=T.
    """

    def __init__(self, n_labels, predictions, output_prob=None, marginals=None,
                 model_info="", output_score=None):
        self.n_labels = n_labels
        self.predictions = predictions
        self.predictions_f = flatten_nested_labels(predictions)
        self.output_prob = output_prob
        if output_prob is not None:
            self.output_prob_f = flatten_nested_labels(output_prob)
        self.output_score = output_score
        self.marginals = marginals
        self.model_info = model_info
        self.scores = None
        self.bin_scores = None

    def get_scores(self, true_y):
        """
        Calculates scores given true labels.

        Parameters
        ----------
        true_y : list[list[int]]
            Nested list of size (S, Ns, 1)
            (A list list of labels in each session.)

        Returns
        -------

        """
        scores = {}

        true_y_f = flatten_nested_labels(true_y)
        acc = get_accuracy(true_y_f, self.predictions_f)
        scores["accuracy"] = acc

        # Only take averages using non-zero instance labels.
        nonzero_labs = np.where(np.sum(get_lab_arr(true_y_f, self.n_labels), axis=0))[0]
        bin_scores = get_binary_classification_scores(true_y_f, self.predictions_f, self.n_labels)
        for sc in sorted(bin_scores.keys()):
            if self.marginals is None:
                weighted = 0.0
            else:
                weighted = get_weighted_avg(bin_scores[sc][nonzero_labs], self.marginals[nonzero_labs])
            notweighted = np.mean(bin_scores[sc][nonzero_labs])
            scores[sc + "_w"] = weighted
            scores[sc] = notweighted

        self.scores = scores
        self.bin_scores = bin_scores
        return self.scores

    def print_scores(self, true_y=None, filename='./result_in_diff_metrics.csv'):
        """
        Prints and writes scores into a csv file.
        If self.scores does not exist and 'true_y' is given, it first runs get_scores().

        Parameters
        ----------
        true_y : list[list[int]]
            Nested list of size (S, N_s, 1)
            (A list list of labels in each session.)
            If .scores have already been calculated, it can be None (Default).
        filename : str
            File path to the output.
        """
        if self.scores is None:
            if true_y is None:
                print("ERROR: First run get_scores() or input true labels (true_y)")
                return
            else:
                self.get_scores(true_y)

        bin_metrics = ["precision", "recall", "auc", "rprecision", "f1score"]
        orders = ["model", "accuracy"] + [met + "_w" for met in bin_metrics] + bin_metrics
        print(",".join(orders))
        with open(filename, 'w') as f:
            f.write(",".join(orders) + "\n")
        self.print_row_of_diff_metrics(self.model_info, self.scores, orders, filename)

    def print_row_of_diff_metrics(self, modelname, result_numbers, headers, fname):
        """
        Prints a row of scores.
        Used inside of print_scores()
        """
        with open(fname, 'a') as f:
            f.write(modelname)
            print(modelname),
            for met in headers[1:]:
                f.write(",%.4f" % result_numbers[met])
                print(",%.4f" % result_numbers[met]),
            f.write("\n")

    def print_utter_level_results(self, uids, docs, true_y, label_names, print_output=False,
                                  filename='./per_utter_result.txt'):
        """
        Prints the utterance level prediction results into a file.
        Result file will have columns 'SessionID', 'UtteranceID', 'Utterance',
        'True label name', 'Predicted Label name by the model'
        and each row is the utterance.

        Parameters
        ----------

        uids : list[list[int]]
            Nested list of utterance IDs with size (S, N_s, 1) for test data
        docs : list[list[str]]
            Nested list of utterances (text) with size (S, N_s, 1) for test data
        true_y : list[list[int]]
            Nested list of labels with size (S, N_s, 1) for test data
        label_names : list or dict[int, str]
            Mapping from a label index to the label name
        filename : str
            Path to the file name
        """
        if not os.path.isdir(os.path.dirname(filename)):
            os.makedirs(os.path.dirname(filename))
        with open(filename, 'w') as f:
            f.write("SessionIndex|UtteranceID|Utterance|TrueLab|"+self.model_info+"\n")
            if print_output:
                print("SessionIndex|UtteranceID|Utterance|TrueLab|"+self.model_info)

            for sidx in range(len(true_y)):
                truel = true_y[sidx]
                predl = self.predictions[sidx]

                for i in range(len(truel)):
                    f.write("%d|%d|%s|%s|%s\n" % (sidx, uids[sidx][i], docs[sidx][i],
                                                label_names[truel[i]], label_names[predl[i]]))
                    if print_output:
                        print("%d|%d|%s|%s|%s" % (sidx, uids[sidx][i], docs[sidx][i],
                                                      label_names[truel[i]], label_names[predl[i]]))

    def print_utter_level_results_without_true_lab(self, uids, docs, label_names, print_output=False,
                                                   filename='./per_utter_result.txt'):
        """
        Prints the utterance level prediction results into a file.
        Result file will have columns 'SessionID', 'UtteranceID', 'Utterance',
        'True label name', 'Predicted Label name by the model'
        and each row is the utterance.

        Parameters
        ----------

        uids : list[list[int]]
            Nested list of utterance IDs with size (S, N_s, 1) for test data
        docs : list[list[str]]
            Nested list of utterances (text) with size (S, N_s, 1) for test data
        label_names : list or dict[int, str]
            Mapping from a label index to the label name
        filename : str
            Path to the file name
        """
        if not os.path.isdir(os.path.dirname(filename)):
            os.makedirs(os.path.dirname(filename))

        with open(filename, 'w') as f:
            f.write("SessionIndex|UtteranceID|Utterance|"+self.model_info+"\n")
            if print_output:
                print("SessionIndex|UtteranceID|Utterance|"+self.model_info)

            for sidx in range(len(uids)):
                predl = self.predictions[sidx]

                for i in range(len(uids[sidx])):
                    f.write("%d|%d|%s|%s|\n" % (sidx, uids[sidx][i], docs[sidx][i],
                                                  label_names[predl[i]]))
                    if print_output:
                        print("%d|%d|%s|%s|" % (sidx, uids[sidx][i], docs[sidx][i],
                                                  label_names[predl[i]]))

ext: py

reason: import numpy


text:
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import pandas as pd
import numpy as np
import altair as alt
from py_scripts.plotter import make_plot_top, make_plot_bot
from py_scripts.wrangle_data import prep_data

alt.data_transformers.enable('default')
alt.data_transformers.disable_max_rows()

app = dash.Dash(__name__, assets_folder='assets', external_stylesheets=[dbc.themes.BOOTSTRAP])

# Boostrap CSS.
app.css.append_css({'external_url': 'https://codepen.io/amyoshino/pen/jzXypZ.css'})  # noqa: E501

server = app.server
app.title = 'Victorious Secret Crime Analyzer'

# get the top 4 crimes df
df_t4 = prep_data()

body = dbc.Container(
    [
        dbc.Row(
            [
                dbc.Col(
                    [
                        html.H2("San Francisco Top 4 Crimes"),
                        html.P(
                            """\
                            When looking for a place to live or visit, one important factor that people will consider
                            is the safety of the neighborhood. This app aims to help people make decisions when considering their next trip or move to San Francisco, California
                            via visually exploring a dataset of crime statistics. The app provides an overview of the crime rate across
                            neighborhoods and allows users to focus on more specific information through
                            filtering crime type or time of the crime.
                            """
                        ),
                        html.H5("Use the box below to choose crimes of interest:"),
                        dcc.Dropdown(
                                    id = 'drop_selection_crime',
                                    options=[{'label': i, 'value': i} for i in df_t4['Category'].unique()
                                    ],
                                    style={'height': '20px',
                                        'width': '400px'},
                                    value=df_t4['Category'].unique(),
                                    multi=True)
                    ],
                    md=5,
                ),
                dbc.Col(
                    [
                        dbc.Row(
                            [
                                html.Iframe(
                                    sandbox = "allow-scripts",
                                    id = "plot_top",
                                    height = "400",
                                    width = "650",
                                    style = {"border-width": "0px"},
                                    srcDoc = make_plot_top(df_t4).to_html()
                                    )
                            ]
                        )
                    ]
                ),
            ]
        ),
        dbc.Row(
            html.Iframe(
                sandbox='allow-scripts',
                id='plot_bot',
                height='500',
                width='1200',
                style={'border-width': '0px'},
                srcDoc= make_plot_bot(df_t4).to_html()
                )
        )
    ],
    className="mt-4",
)

app.layout = html.Div(body)

@app.callback([dash.dependencies.Output('plot_top', 'srcDoc'),
    dash.dependencies.Output('plot_bot', 'srcDoc')],
    [dash.dependencies.Input('drop_selection_crime', 'value')]
    )
def update_df(chosen):
    new_df = df_t4[(df_t4["Category"].isin(chosen))]
    updated_plot_top = make_plot_top(new_df).to_html()
    updated_plot_bottom = make_plot_bot(new_df).to_html()

    return updated_plot_top, updated_plot_bottom

if __name__ == '__main__':
    app.run_server(debug=False)


text:
# -*- coding: utf-8 -*-
import numpy as np

class ActionRule:

    def __init__(self):
        self.action_message = None
        self.rule_condition = {0: self.nothing, 1: self.adjacent}
        self.rule_direction = {0: self.nothing, 1: self.width, 2: self.height, 3: self.cross, 4: self.diagonal, 5: self.eight_dir}
        self.rule_method = {0: self.nothing, 1: self.reverse, 2: self.remove}

        self.game_data = None
        self.board = None

        self.curr_x = None
        self.curr_y = None
        self.next_x = None
        self.next_y = None
        self.obj_number = None

        self.condition = None
        self.dir = None
        self.method = None

        self.rule_list = []
        self.dir_list = []
        self.stone_list = []

    def nothing(self):
        pass

    def apply_action_rule(self, game_data, placement_data):
        """
        Function for apply action rule

        :param game_data: game data(rule)
        :type game_data: object
        :param placement_data: placement & current board
        :type placement_data: object
        :return: result, update board
        :rtype: str, 2d list of int
        """

        self.setting(game_data, placement_data)

        self.method()

        return 'OK', self.board

    def setting(self, game_data, placement_data):
        """
        Set data for apply action

        """
        self.game_data = game_data
        self.board = placement_data.board

        self.curr_x = placement_data.curr_x
        self.curr_y = placement_data.curr_y
        self.next_x = placement_data.next_x
        self.next_y = placement_data.next_y
        self.obj_number = placement_data.obj_number

        self.condition = self.rule_condition[int(game_data.rule[int(placement_data.obj_number)-1]['actionCondition'])]
        self.dir = self.rule_direction[int(game_data.rule[int(placement_data.obj_number)-1]['actionDir'])]
        self.method = self.rule_method[int(game_data.rule[int(placement_data.obj_number)-1]['actionMethod'])]

    # condition
    def surround(self):
        pass

    def adjacent(self):
        """
        get adjacent stones

        :return: adjacent stones
        :rtype: list of tuple
        """

        self.dir()
        poses = []
        for d in self.dir_list:
            x = self.next_x + d[0]
            y = self.next_y + d[1]
            if self.check_range(x, y):
                continue
            poses.append((x, y))
        return poses

    # direction
    def width(self):
        self.dir_list = [(0, 1), (0, -1)]

    def height(self):
        self.dir_list = [(1, 0), (-1, 0)]

    def cross(self):
        self.dir_list = [(0, 1), (1, 0), (0, -1), (-1, 0)]

    def diagonal(self):
        self.dir_list = [(-1, 1), (1, 1), (1, -1), (-1, -1)]

    def eight_dir(self):
        self.dir_list = [(0, 1), (1, 0), (0, -1), (-1, 0), (-1, 1), (1, 1), (1, -1), (-1, -1)]

    def reverse(self):
        """
        Reverse adjacent stones
        """
        poses = self.adjacent()
        for pos in poses:
            if self.board[pos[0]][pos[1]] < 0:
                self.board[pos[0]][pos[1]] *= -1

    def remove(self):
        pass

    def check_range(self, x, y):
        return not (0 <= x < len(self.board)) and (0 <= y < len(self.board))


text:
import numpy
from vtk import vtkStructuredPointsReader
from vtk.util import numpy_support as VN

reader = vtkStructuredPointsReader()
reader.SetFileName(filename)
reader.ReadAllVectorsOn()
reader.ReadAllScalarsOn()
reader.Update()

data = reader.GetOutput()

dim = data.GetDimensions()
vec = list(dim)
vec = [i - 1 for i in dim]
vec.append(3)

u = VN.vtk_to_numpy(data.GetCellData().GetArray("velocity"))
b = VN.vtk_to_numpy(data.GetCellData().GetArray("cell_centered_B"))

u = u.reshape(vec, order="F")
b = b.reshape(vec, order="F")

x = zeros(data.GetNumberOfPoints())
y = zeros(data.GetNumberOfPoints())
z = zeros(data.GetNumberOfPoints())

for i in range(data.GetNumberOfPoints()):
    x[i], y[i], z[i] = data.GetPoint(i)

x = x.reshape(dim, order="F")
y = y.reshape(dim, order="F")
z = z.reshape(dim, order="F")

max_stars_repo_name: djeada/VTK

max_issues_repo_name: djeada/VTK

max_forks_repo_name: djeada/VTK


text:
# Copyright 2021 Google LLC
#
# 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
#
#     https://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.

"""Bisection algorithm in JAX."""

from typing import Any
from typing import Callable
from typing import NamedTuple

from dataclasses import dataclass

import jax.numpy as jnp

from jaxopt._src import base
from jaxopt._src import implicit_diff as idf
from jaxopt._src import linear_solve
from jaxopt._src import loop

class BisectionState(NamedTuple):
  """Named tuple containing state information."""
  iter_num: int
  value: float
  error: float
  low: float
  high: float

@dataclass
class Bisection:
  """One-dimensional root finding using bisection.

  Attributes:
    optimality_fun: a function ``optimality_fun(x, *args, **kwargs)``
      where ``x`` is a 1d variable. The function should be increasing w.r.t.
      ``x`` on ``[lower, upper]`` if ``increasing=True`` and decreasing
      otherwise.
    lower: the lower end of the bracketing interval.
    upper: the upper end of the bracketing interval.
    increasing: whether ``optimality_fun(x, *args, **kwargs)`` is an
      increasing function of ``x``. Default: True.
    maxiter: maximum number of iterations.
    tol: tolerance.
    check_bracket: whether to check correctness of the bracketing interval.
      If True, the method ``run`` cannot be jitted.
    implicit_diff: if True, enable implicit differentiation.
    verbose: whether to print error on every iteration or not.
      Warning: verbose=True will automatically disable jit.
  """
  optimality_fun: Callable
  lower: float
  upper: float
  increasing: bool = True
  maxiter: int = 30
  tol: float = 1e-5
  check_bracket: bool = True
  implicit_diff: bool = True
  verbose: bool = False

  def _check_bracket(self, *args, **kwargs):
    if self._fun(self.lower, *args, **kwargs) > 0:
      raise ValueError("optimality_fun(lower, *args, **kwargs) should be < 0 "
                       "if increasing=True and > 0 if increasing=False.")

    if self._fun(self.upper, *args, **kwargs) < 0:
      raise ValueError("optimality_fun(upper, *args, **kwargs) should be > 0 "
                       "if increasing=True and < 0 if increasing=False.")

  def init(self,
           init_params,
           *args,
           **kwargs) -> base.OptStep:
    """Initialize the ``(params, state)`` pair.

    Args:
    Return type:
      base.OptStep
    Returns:
      (params, state)
    """
    if self.check_bracket:
      self._check_bracket(*args, **kwargs)

    state = BisectionState(iter_num=0,
                           value=jnp.inf,
                           error=jnp.inf,
                           low=self.lower,
                           high=self.upper)

    if init_params is None:
      init_params = 0.5 * (state.high + state.low)

    return base.OptStep(params=init_params, state=state)

  def update(self,
             params: Any,
             state: NamedTuple,
             *args,
             **kwargs) -> base.OptStep:
    """Performs one iteration of the bisection solver.

    Args:
      params: pytree containing the parameters.
      state: named tuple containing the solver state.
    Return type:
      base.OptStep
    Returns:
      (params, state)
    """
    value = self._fun(params, *args, **kwargs)
    too_large = value > 0
    # When `value` is too large, `params` becomes the next `high`,
    # and `low` remains the same. Otherwise, it is the opposite.
    high = jnp.where(too_large, params, state.high)
    low = jnp.where(too_large, state.low, params)

    state = BisectionState(iter_num=state.iter_num + 1,
                           value=value,
                           error=jnp.sqrt(value ** 2),
                           low=low,
                           high=high)

    # We return `midpoint` as the next guess.
    # Users can also inspect state.low and state.high.
    midpoint = 0.5 * (low + high)
    return base.OptStep(params=midpoint, state=state)

  def run(self,
          init_params,
          *args,
          **kwargs) -> base.OptStep:
    """Runs the bisection algorithm.

    Args:
    Return type:
      base.OptStep
    Returns:
      (params, state)
    """
    def cond_fun(pair):
      _, state = pair
      if self.verbose:
        print(state.error)
      return state.error > self.tol

    def body_fun(pair):
      params, state = pair
      return self.update(params, state, *args, **kwargs)

    return loop.while_loop(cond_fun=cond_fun, body_fun=body_fun,
                           init_val=self.init(init_params, *args, **kwargs),
                           maxiter=self.maxiter, jit=self._jit,
                           unroll=self._unroll)

  def l2_optimality_error(self, params, *args, **kwargs):
    """Computes the L2 optimality error."""
    return jnp.sqrt(self.optimality_fun(params, *args, **kwargs) ** 2)

  def __post_init__(self):
    # We prepare the function below so that we don't have to worry about
    # the sign during the algorithm.
    if self.increasing:
      self._fun = self.optimality_fun
    else:
      self._fun = lambda x, *a, **kw: -self.optimality_fun(x, *a, **kw)

    # We always jit unless verbose mode is enabled.
    self._jit = not self.verbose
    # We unroll when implicit diff is disabled or when jit is disabled.
    self._unroll = not self.implicit_diff or not self._jit

    if self.implicit_diff:
      decorator = idf.custom_root(self.optimality_fun,
                                  has_aux=True,
                                  solve=linear_solve.solve_lu)
      # pylint: disable=g-missing-from-attributes
      self.run = decorator(self.run)

max_stars_repo_name: mblondel/jaxopt

max_issues_repo_name: mblondel/jaxopt

max_forks_repo_name: mblondel/jaxopt

reason: import jax,from jax


text:
import numpy as np

class Map:
    '''
    Classe mapa. Definimos 'graph' como a matriz do mapa, onde:
    0 -> Parede
    1 -> Livre
    2 -> Posição Inicial
    3 -> Posição Final
    '''

    def __init__(self,size,start,finish):
        self.size = size
        self.start = start
        self.finish = finish
        self.graph = np.ones(shape=size, dtype=np.int)
        self.graph[start] = 2
        self.graph[finish] = 3

    def get_neighbors(self,position):
        neighbors = list()
        for i,j in ([0,1],[-1,0],[0,-1],[1,0], [1,1], [1,-1], [-1,1], [-1,-1]):
            if (position[0]+i >= 0 and position[0]+i < self.graph.shape[0] and
                position[1]+j >= 0 and position[1]+j < self.graph.shape[1] and
                self.graph[position[0]+i][position[1]+j] != 0):
                neighbors.append((position[0]+i,position[1]+j))
        return neighbors

    def make_wall(self,position):
        for pos in position:
            self.graph[pos] = 0

max_stars_repo_name: skkywalker/tcc

max_issues_repo_name: skkywalker/tcc

max_forks_repo_name: skkywalker/tcc


text:
""" Wrappers for building piecewise linear nets
    (generally we'll only worry about ReLU nets here)
"""
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from collections import OrderedDict
import matplotlib.pyplot as plt
import utilities as utils
import gurobipy as gb
import copy

class ReLUNet(nn.Module):
    def __init__(self, layer_sizes=None, bias=True, dtype=torch.float32,
                 manual_net=None):
        super(ReLUNet, self).__init__()
        self.layer_sizes = layer_sizes
        self.dtype = dtype
        self.fcs = []
        self.num_relus = len(layer_sizes) - 2
        self.bias = bias
        if manual_net is None:
            self.net = self.build_network(layer_sizes)
        else:
            self.net = manual_net
            self._adjust_fcs()

    @classmethod
    def from_sequential(cls, sequential):
        fcs = [_ for _ in sequential if isinstance(_, (nn.Linear, nn.Identity))]
        layer_sizes = [fcs[0].in_features]
        for fc in fcs:
            layer_sizes.append(fc.out_features)
        bias = any(fc.bias is not None for fc in fcs)
        dtype = fcs[0].weight.dtype

        new_net = cls(layer_sizes=layer_sizes, bias=bias,
                      dtype=dtype, manual_net=sequential)
        new_net.fcs = fcs
        return new_net

    def _adjust_fcs(self):
        """ Collects FC's from self.net and modifies self.fcs """
        fcs = [_ for _ in self.net if isinstance(_, (nn.Linear, nn.Identity))]
        self.fcs = fcs

    def re_init_weights(self):
        self.net = self.build_network(self.layer_sizes)

    def clone(self):
        """ Returns a deepcopy of this object """
        return self.from_sequential(copy.deepcopy(self.net))

    def tensorfy_clone(self, x, requires_grad=False):
        """ Clones whatever x is into a tensor with self's datatype """
        if isinstance(x, torch.Tensor):
            return x.clone().detach()\
                    .type(self.dtype).requires_grad_(requires_grad)
        else:
            return torch.tensor(x, dtype=self.dtype,
                                requires_grad=requires_grad)

    def build_network(self, layer_sizes):
        layers = OrderedDict()
        self.fcs = []
        num = 1
        for size_pair in zip(layer_sizes, layer_sizes[1:]):
            size, next_size = size_pair
            layer = nn.Linear(size, next_size, bias=self.bias).type(self.dtype)
            layers[str(num)] = layer
            self.fcs.append(layer)
            num = num + 1
            layers[str(num)] = nn.ReLU()
            num = num + 1
        del layers[str(num-1)]      # No ReLU for the last layer
        net = nn.Sequential(layers).type(self.dtype)
        return net

    def get_parameters(self):
        params = []
        for fc in self.fcs:
            fc_params = [elem for elem in fc.parameters()]
            for param in fc_params:
                params.append(param)
        return params

    def forward(self, x, return_preacts=False):
        """ Standard forward method for ReLU net
            If return_preacts is True, then we collect and return the
            values after applying each linear layer
        """
        x = x.view(-1, self.fcs[0].in_features)
        preacts = []
        for i, fc in enumerate(self.fcs):
            if i == len(self.fcs) - 1:
                x = fc(x)
                if return_preacts:
                    preacts.append(x)
                    return preacts
                return x
            x = fc(x)
            preacts.append(torch.clone(x))
            x = F.relu(x)

    def classify_np(self, x):
        tens_x = torch.Tensor(x)
        return self.forward(tens_x).max(1)[1].item()

    def get_grad_at_point(self, x, c_vector):
        """ Simple helper method to get the gradient of a single input point
            (specifically, gets the gradient of <c_vector, f(x)> wrt x)
        ARGS:
            x: tensor [1xINPUT-SHAPE]
            c_vector: tensor [OUTPUT_DIM]
        """
        c_vector = self.tensorfy_clone(c_vector)
        x = self.tensorfy_clone(x, requires_grad=True)
        output = self.forward(x).mv(c_vector).sum()
        output.backward()
        return x.grad

    def random_max_grad(self, hyperbox, c_vector, num_random, pnorm=1):
        """ Takes a bunch of random points within the hyperbox and
            computes the gradient magnitude at each. Records the
            maximum normed gradient (and point)
        ARGS:
            hyperbox: HyperBox object
            c_vector: tensor [OUTPUT_DIM]
            num_random : int - number of random points
            pnorm : int, float - Lp norm to take of gradients
        RETURNS:
            {norm, point, grad} that maximizes grad norm
        """

        max_norm, max_point, max_grad = -1, None, None
        c_vector = self.tensorfy_clone(c_vector)
        for i in range(num_random):
            random_input = hyperbox.random_point(tensor_or_np='tensor')
            grad = self.get_grad_at_point(random_input, c_vector)
            grad_norm = grad.norm(p=pnorm)
            if grad_norm > max_norm:
                max_norm = grad_norm
                max_point = random_input
                max_grad = grad
        return {'norm': max_norm,
                'point': max_point,
                'grad': max_grad}

    def display_decision_bounds(self, x_range, y_range, density, figsize=(8,8)):
        """ For 2d-input networks, will use EricWong-esque code to
            build an axes object and plot decision boundaries
        ARGS:
            x_range  : pair of floats (lo, hi) - denotes x range of the grid
            y_range  : pair of floats (lo, hi) - denotes y range of the grid
            density : int - number of x,y coords to check
            figsize : tuple - for custom figure sizes
        RETURNS:
            ax object
        """
        # Right now only works for functions mapping R2->R2
        assert self.layer_sizes[0] == 2# and self.layer_sizes[-1] == 2

        # Compute the grid points
        x_lo, x_hi = x_range
        y_lo, y_hi = y_range
        XX, YY = np.meshgrid(np.linspace(x_lo, x_hi, density),
                             np.linspace(y_lo, y_hi, density))
        X0 = torch.Tensor(np.stack([np.ravel(XX), np.ravel(YY)]).T)
        y0 = self(X0)
        ZZ = (y0[:,0] - y0[:,1]).view(density, density).data.numpy()

        # Build plot and plot gridpoints
        fig, ax = plt.subplots(figsize=figsize)
        ax.contourf(XX, YY, -ZZ, cmap='coolwarm',
                    levels=np.linspace(-1000, 1000, 3))
        ax.axis([x_lo, x_hi, y_lo, y_hi])
        return ax

    def get_ith_hidden_unit(self, i):
        """ Returns the i^th hidden unit, which, is a pair of
            (nn.Linear, nn.ReLU) objects, starting at index 0.
        """
        seq_start = 2 * i
        seq_out = self.net[seq_start: seq_start + 2]
        return (seq_out[0], seq_out[1])

    def get_sign_configs(self, x):
        preacts = [utils.as_numpy(_.squeeze()) for _ in
                   self(x.view(-1), return_preacts=True)[:-1]]
        return [_ > 0 for _ in preacts]

    def polytope_from_signs(self, signs):
        """ Returns a 'polytope' of the form (A, b) where
            A is an (m,n)-numpy array and (b) is a numpy vector
            and this is the set of points
            {x | Ax <= b} defined by the sign config provided
        ARGS:
            signs: list of numpy boolean arrays corresponding to
                   which ReLU's are on
        RETURNS:
        """
        configs = [torch.tensor(sign).type(self.dtype) for sign in signs]
        lambdas = [torch.diag(config) for config in configs]
        js = [torch.diag(-2 * config + 1) for config in configs]
        # Compute Z_k = W_k * x + b_k for each layer
        wks = [self.fcs[0].weight]
        bks = [self.fcs[0].bias]
        for (i, fc) in enumerate(self.fcs[1:]):
            current_wk = wks[-1]
            current_bk = bks[-1]
            current_lambda = lambdas[i]
            precompute = fc.weight.matmul(current_lambda)
            wks.append(precompute.matmul(current_wk))
            bks.append(precompute.matmul(current_bk) + fc.bias)

        a_stack = []
        b_stack = []
        for j, wk, bk in zip(js, wks, bks):
            a_stack.append(j.matmul(wk))
            b_stack.append(-j.matmul(bk))

        polytope_A = utils.as_numpy(torch.cat(a_stack, dim=0))
        polytope_b = utils.as_numpy(torch.cat(b_stack, dim=0))
        return utils.Polytope(polytope_A, polytope_b)

    def find_feasible_from_signs(self, sign_configs, input_hbox=None):
        """ Finds a feasible differentiable point that has the given
            ReLU configs.
        """
        # First check shapes are okay:
        assert len(sign_configs) == self.num_relus
        assert all([len(sign_configs[i]) == self.layer_sizes[i + 1]
                    for i in range(self.num_relus)])
        # Then build a gurobi model and add constraints for each layer
        with utils.silent():
            model = gb.Model()

        # Add input keys:
        input_key = 'input'
        input_namer = utils.build_var_namer(input_key)
        input_vars = []
        for i in range(self.layer_sizes[0]):
            if input_hbox is not None:
                lb, ub = input_hbox[i]
            else:
                lb, ub = -gb.GRB.INFINITY, gb.GRB.INFINITY
            input_vars.append(model.addVar(lb=lb, ub=ub, name=input_namer(i)))

        slack_var = model.addVar(lb=0, name='slack')

        # And then iteratively add layers
        lin_vars = input_vars
        for i in range(self.num_relus):
            lin_vars = self._add_layer_to_gurobi_model(i, model, lin_vars,
                                                       slack_var, sign_configs[i])

        # Add the objective to maximize and then solve
        model.setObjective(slack_var, gb.GRB.MAXIMIZE)
        model.update()
        model.optimize()

        # And handle the outputs
        if model.Status in [3, 4]:
            return None
        else:
            return {'slack': model.getObjective().getValue(),
                    'x': np.array([v.X for v in input_vars]),
                    'model': model}

    def _add_layer_to_gurobi_model(self, layer_num, model, lin_vars, slack_var,
                                   layer_signs):
        """ Adds new variables and new constraints """

        weight = utils.as_numpy(self.fcs[layer_num].weight)
        bias = utils.as_numpy(self.fcs[layer_num].bias)
        output_vars = []
        for i, row in enumerate(weight):
            if layer_signs[i] == True:
                var = model.addVar(lb=-gb.GRB.INFINITY, ub=gb.GRB.INFINITY)
                model.addConstr(var == gb.LinExpr(row, lin_vars) + bias[i])
                model.addConstr(gb.LinExpr(row, lin_vars) + bias[i] - slack_var >= 0.0)
            else:
                var = model.addVar(lb=0.0, ub=0.0)
                model.addConstr(gb.LinExpr(row, lin_vars) + bias[i] + slack_var <= 0.0)
            output_vars.append(var)
        return output_vars

class SubReLUNet(ReLUNet):
    """ Expansion pack for ReLUNet which has some extra methods and
        attributes to aid with construction of split/partitioned subnetworks
    """

    def __init__(self, layer_sizes=None, bias=True, dtype=torch.float32,
                 manual_net=None, target_units=None, parent_units=None,
                 parent_net=None):
        super(SubReLUNet, self).__init__(layer_sizes=layer_sizes, bias=bias,
                                         dtype=dtype, manual_net=manual_net)

        self.target_units = target_units
        self.parent_units = parent_units
        self.parent_net = parent_net

    @classmethod
    def _check_valid_split_parameters(self, split_parameters):
        if split_parameters.manual_splits is None:
            return True
        else:
            return sum(split_parameters.manual_splits) == len(self.fcs) - 1

    @classmethod
    def split_network(cls, network, split_parameters):
        """ Takes in a ReLUNet, SplitParameters object and generates
            multiple ReLUNets in a list. The idea is that if a neural net is
            denoted as {L: linear layer, R: relu layer}, a ReluNet looks like
            LRLRLRLRLRL
            So if there are N hidden layers (R's in the above), then we split
            into chunks like
            - [LR, LR, LR, LR, LRL]
            (where the last one has a linear layer at the output)

        ARGS:
            split_parameters: SplitParameters object which describes how to
                              split this neural net
        RETURNS:
            list of ReLUNets, where all except the last one are of the form
            regex: /(LR)+I/  where {L: linear, R: Relu, I: Identity}
            and the last one is of form /(LR)+L/
        """
        # setup
        assert cls._check_valid_split_parameters(split_parameters)
        split_size_list = []
        num_hidden_units = len(network.fcs) - 1

        # Build the split size list and hidden unit indices
        if split_parameters.num_splits is not None:
            num_splits = split_parameters.num_splits
            split_size = math.ceil(num_hidden_units / float(num_splits))
            split_size_list = [split_size for _ in range(num_splits -1)]
            split_size_list.append(num_hidden_units - sum(split_size_list))

        elif split_pararameters.every_x is not None:
            every_x = int(split_pararameters.every_x)
            split_size_list = [every_x for _ in
                               range(num_hidden_units // every_x)]
            if num_hidden_units % every_x != 0:
                split_size_list.append(num_hidden_units % every_x)
        else:
            split_size_list = split_parameters.manual_splits

        # And then collect the "hidden units": the (LR) blocks
        is_relu = lambda lay: isinstance(lay, nn.ReLU)
        hidden_units = utils.partition_by_suffix(network.net, is_relu)

        # Now collect the subnetworks and 'target units'
        start_idx = 0
        target_idxs = []
        parent_idxs = []
        for subnet_no, size in enumerate(split_size_list):
            end_idx = start_idx + size
            target = [start_idx, end_idx]
            if split_parameters.lookback > 0:
                if target_start < 0:
                    target_start = 0
                start_idx = max([0, start_idx - split_pararameters.lookback])
            if split_parameters.lookahead > 0:
                end_idx = min([len(hidden_units),
                               end_idx + split_parameters.lookahead])
            target = [abs(_ - start_idx) for _ in target]
            target_idxs.append(target)
            parent_idxs.append((start_idx, end_idx))

        # And append one unit to each of these suffixes
        last_subseq_idx = len(subseqs) - 1
        output = []
        for i, (parent, target) in enumerate(zip(parent_idxs, target_idxs)):
            # Make the sequential
            subseq = network.net[parent[0]:parent[1]]
            if i == last_subseq_idx:
                suffix_unit = network.net[-1]
            else:
                suffix_unit = nn.Identity()
            seq = utils.flatten_list(subseq) + [suffix_unit]

            # Resolve the 'target_units':
            target_units = split_pararameters.cast_targets(target)
            new_net = SubReLUNet.from_sequential(seq)
            new_net.target_units = target_units
            new_net.parent_units = parent
            new_net.parent_net = network
            output.append(new_net)

        return output

max_stars_repo_name: revbucket/lipMIP

max_issues_repo_name: revbucket/lipMIP

max_forks_repo_name: revbucket/lipMIP


text:
# Class to generate Autoencoder Meta Embeddings (AEME)
# File: AEME.py
# Author: Atharva Kulkarni

import torch
import torch.nn as nn
from torch.utils.data import TensorDataset, RandomSampler, DataLoader
from sklearn.preprocessing import LabelEncoder
import time
import numpy as np
import gc
from DAE import DAE
from CAE import CAE
from AAE import AAE

class AEME():
    """ Class to implement Autoencoder for generating Meta-Embeddings """

    def __init__(self, mode="CAE", input_dim=300, latent_dim=100, activation="leaky_relu", lambda1=1, lambda2=1, lambda3=1, lambda4=1, lambda5=1, lambda6=1):
        """ Constructor to initialize autoencoder parameters
        @param mode (string): type of Autoencoder to build: Decoupled Autoencoder (DAE), Concatenated Autoencoder (CAE), Averaged Autoencoder (AAE).
        @param latent_dim (int): latent_dimension for each autoencoder. Default: 300.
        @ activation (string): type of activation: leaky_relu, paramaterized_leaky_relu, relu, tanh, and sigmoid. Default: leaky_relu.
        @param lambda1 (int): Multiplicaiton factor for computing loss for part1. Default: 1.
        @param lambda2 (int): Multiplicaiton factor for computing loss for part2. Default: 1.
        @param lambda3 (int): Multiplicaiton factor for computing loss for part3. Default: 1.
        @param lambda4 (int): Multiplicaiton factor for computing loss for part4 (Only for DAE). Default: 1.
        @param lambda5 (int): Multiplicaiton factor for computing loss for part5 ((Only for DAE). Default: 1.
        @param lambda6 (int): Multiplicaiton factor for computing loss for part6 ((Only for DAE). Default: 1.
        """
        self.label_encoder = LabelEncoder()
        if torch.cuda.is_available():
            self.device = torch.device("cuda:0")
            print("GPU : ", torch.cuda.get_device_name(0))
        else:
          self.device = torch.device("cpu")
          print("CPU on")

        self.mode = mode
        self.encoder = None

        if activation == "leaky_relu":
            activation = nn.LeakyReLU()
        elif activation == "paramaterized_leaky_relu":
            activation = nn.PReLU()
        else:
            activation = nn.ReLU()

        if mode == "DAE":
            self.ae = DAE(input_dim, latent_dim, activation, lambda1, lambda2, lambda3, lambda4, lambda5, lambda6)
        elif mode == "CAE":
            self.ae = CAE(input_dim, latent_dim, activation, lambda1, lambda2, lambda3)
        elif mode == "AAE":
            self.ae = AAE(input_dim, latent_dim, activation, lambda1, lambda2, lambda3)

    def add_noise(self, data, masking_noise_factor):
        """Function to add mask noise to data.
        @param data (np.array): data to add noise to.
        @param masking_noise_factor (float): Percentage of noise to add to the data.
        @return data (np.array): noise added data.
        """
        data_size, feature_size = data.shape
        for i in range(data_size):
            mask_noise = np.random.randint(0, feature_size, int(feature_size * masking_noise_factor))
            for m in mask_noise:
                data[i][m] = 0
        return data

    def prepare_input(self, vocab, x_train1, x_train2, x_train3, batch_size=128, masking_noise_factor=0.05):
        """ Funciton to generate Tensor Dataset.
        @param vocab (list): list of intersection vocabulary.
        @param x_train1 (np.array): The input data1.
        @param x_train2 (np.array): The input data2.
        @param x_train3 (np.array): The input data3.
        @param batch_size (int): Number of batches to divide the training data into.
        @param masking_noise (bool): To add Masking Noise or not.
        @param masking_noise_factor (float): Percentage noise to be induced in the input data. Default: 0.05 or 5%.
        """
        vocab = torch.as_tensor(self.label_encoder.fit_transform(vocab), device=self.device)

        x_train1_noisy = self.add_noise(x_train1, masking_noise_factor)
        x_train2_noisy = self.add_noise(x_train2, masking_noise_factor)
        x_train3_noisy = self.add_noise(x_train3, masking_noise_factor)

        tensor_dataset = torch.utils.data.TensorDataset(x_train1_noisy,
                                                        x_train2_noisy,
                                                        x_train3_noisy,
                                                        x_train1,
                                                        x_train2,
                                                        x_train3,
                                                        vocab)
        del x_train1_noisy
        del x_train2_noisy
        del x_train3_noisy
        del x_train1
        del x_train2
        del x_train3
        del vocab
        gc.collect()
        torch.cuda.empty_cache()
        return torch.utils.data.DataLoader(dataset=tensor_dataset,
                                           sampler=torch.utils.data.RandomSampler(tensor_dataset),
                                           batch_size=batch_size)

    def train(self, tensor_dataset, epochs=500, checkpoint_path=""):
        """ Function to train the Autoencoder Model.
        @param tensor_dataset (torch.tensor): Batch-wise dataset.
        @@param epochs (int): Number of epochs for which the model is to be trained. Default: 10.
        """
        self.ae.train()
        self.ae.to(self.device)

        optimizer = torch.optim.Adam(self.ae.parameters(), lr=0.001)

        training_loss = []

        if self.mode == "DAE":
            for step in range(1, epochs+1):
                start = time.time()
                epoch_loss = 0.0
                for batch_data in tensor_dataset:
                    optimizer.zero_grad()
                    x_train1_noisy, x_train2_noisy, x_train3_noisy, x_train1, x_train2, x_train3, _ = tuple(t.to(self.device) for t in batch_data)
                    output, bottleneck = self.ae(x_train1_noisy, x_train2_noisy, x_train3_noisy)
                    loss = self.ae.loss([output, bottleneck], [x_train1, x_train2, x_train3])
                    loss.backward()
                    epoch_loss = epoch_loss + loss.item()
                    optimizer.step()
                epoch_loss = epoch_loss/len(tensor_dataset)
                training_loss.append(epoch_loss)
                end = time.time()
                print("\nEpoch: {} of {} ----> loss: {:.5f}\t ETA: {:.2f} s".format(step, epochs, epoch_loss, (end-start)))

                if len(training_loss) > 2:
                  if epoch_loss < training_loss[-2]:
                      model_checkpoint = checkpoint_path + "_epoch_{}_loss_{:.5f}.pt".format(step, epoch_loss)
                      torch.save(self.ae.state_dict(), model_checkpoint)

        else:
            for step in range(1, epochs+1):
                start = time.time()
                epoch_loss = 0.0
                for batch_data in tensor_dataset:
                    optimizer.zero_grad()
                    x_train1_noisy, x_train2_noisy, x_train3_noisy, x_train1, x_train2, x_train3, _ = tuple(t.to(self.device) for t in batch_data)
                    output, _ = self.ae(x_train1_noisy, x_train2_noisy, x_train3_noisy)
                    loss = self.ae.loss(output, [x_train1, x_train2, x_train3])
                    loss.backward()
                    epoch_loss = epoch_loss + loss.item()
                    optimizer.step()
                epoch_loss = epoch_loss/len(tensor_dataset)
                training_loss.append(epoch_loss)
                end = time.time()
                print("\nEpoch: {} of {} ----> loss: {:5f}\t ETA: {:.2f} s".format(step, epochs, epoch_loss, (end-start)))

                if len(training_loss) > 2:
                  if epoch_loss < training_loss[-2]:
                      model_checkpoint = checkpoint_path + "_epoch_{}_loss_{:.5f}.pt".format(step, epoch_loss)
                      torch.save(self.ae.state_dict(), model_checkpoint)

    def predict(self, tensor_dataset, model_checkpoint):
        """ Function to generate predictions of the autoencoder's encoder.
        @param x_test1 (np.array): test input 1.
        @param x_test2 (np.array): test input 2.
        @param x_test3 (np.array): test input 3.
        @param model_checkpoint (string): model weights.
        @return predictions (np.array): Autoencoder's encoder's predictions.
        """
        self.ae.load_state_dict(torch.load(model_checkpoint))
        self.ae.eval()
        self.ae.to(self.device)

        embedding_dict = dict()
        for batch_data in tensor_dataset:
            _, _, _, x_train1, x_train2, x_train3, words = tuple(t.to(self.device) for t in batch_data)
            words = self.label_encoder.inverse_transform(words.to('cpu')).tolist()

            with torch.no_grad():
                _, bottleneck = self.ae(x_train1, x_train2, x_train3)
                bottleneck = torch.split(bottleneck, 1, dim=0)
                for word, vec in list(zip(words, bottleneck)):
                    embedding_dict[word] = vec[0]

                del batch_data
                del x_train1
                del x_train2
                del x_train3
                del words
                del bottleneck
                gc.collect()
                torch.cuda.empty_cache()
        return embedding_dict


text:
# https://www.kaggle.com/joshi98kishan/foldtraining-pytorch-tpu-8-cores

import os
import gc
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from sklearn.model_selection import GroupKFold

from albumentations import *
from segmentation_models_pytorch.unet import Unet
from segmentation_models_pytorch.encoders import get_preprocessing_fn

import torch
from torch import optim
from torch.optim.lr_scheduler import CosineAnnealingWarmRestarts
from torch.utils.data import Dataset, DataLoader

from patho.utils.utils import set_all_seeds, check_lr
from patho.cfg.config import path_cfg,img_proc_cfg
from patho.model.model_torch import DiceLoss,HuBMAPModel, get_dice_coeff
from patho.datagen.datagen import HuBMAPDataset

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
ENCODER_NAME = 'se_resnext50_32x4d'

preprocessing_fn = Lambda(image = get_preprocessing_fn(encoder_name = ENCODER_NAME,
                                                       pretrained = 'imagenet'))

# https://www.kaggle.com/iafoss/hubmap-pytorch-fast-ai-starter
transforms = Compose([
                HorizontalFlip(),
                VerticalFlip(),
                RandomRotate90(),
                ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=15, p=0.9,
                                 border_mode=cv2.BORDER_REFLECT),
                OneOf([
                    OpticalDistortion(p=0.3),
                    GridDistortion(p=.1),
                    IAAPiecewiseAffine(p=0.3),
                ], p=0.3),
                OneOf([
                    HueSaturationValue(10,15,10),
                    CLAHE(clip_limit=2),
                    RandomBrightnessContrast(),
                ], p=0.3),
            ], p=1.0)

def demo_lr():
    demo_model = HuBMAPModel()

    # LR which we set here is the highest LR value.
    demo_optim = optim.SGD(demo_model.parameters(),
                           lr=0.3,
                           momentum=0.9)
    DEMO_EPOCHS = 60

    # Change the T_0 argument to get more number of cycles.
    demo_sched = CosineAnnealingWarmRestarts(demo_optim,
                                             T_0=DEMO_EPOCHS // 3,
                                             T_mult=1,
                                             eta_min=0,
                                             last_epoch=-1)

    check_lr(DEMO_EPOCHS, demo_optim, demo_sched)

def trainer(DATA_PATH, epoch=30, output_dir=''):
    loss_fn = DiceLoss()
    for fold, (t_idx, v_idx) in enumerate(group_kfold.split(fnames,
                                                            groups=groups)):
        print(f'Fold: {fold + 1}')
        print('-' * 40)

        t_fnames = fnames[t_idx]
        v_fnames = fnames[v_idx]

        train_ds = HuBMAPDataset(data_path=DATA_PATH,
                                 fnames=t_fnames,
                                 preprocess_input=preprocessing_fn,
                                 transforms=transforms)

        val_ds = HuBMAPDataset(data_path=DATA_PATH,
                               fnames=v_fnames,
                               preprocess_input=preprocessing_fn,
                               transforms=None)

        print(f'Length of training set: {len(train_ds)}')
        print(f'Length of validation set: {len(val_ds)}')

        model = HuBMAPModel()
        model.float()

        lr = 0.3

        print('\n')

        train_dl = DataLoader(dataset=train_ds,
                              batch_size=4,
                              num_workers=8)

        val_dl = DataLoader(dataset=val_ds,
                            batch_size=4,
                            num_workers=8)

        fold_model = model
        fold_model.to(device)

        optimizer = optim.SGD(model.parameters(),
                              lr=lr,
                              momentum=0.9)

        # scheduler = CosineAnnealingWarmRestarts(optimizer,
        #                                         T_0=epoch// 3,
        #                                         T_mult=1,
        #                                         eta_min=0,
        #                                         last_epoch=-1)

        for e_no, epoch in enumerate(range(epoch)):
            # Here comes our data loader for 8 cores.
            # It takes famous 'DataLoader()' object and list of
            # devices where data has to be sent.
            # Calling 'per_device_loader()' on it will
            # return the data loader for the particular device.
            train_one_epoch(e_no,
                            train_dl,
                            fold_model,
                            loss_fn,
                            optimizer,
                            device)

            # del train_dl
            gc.collect()

        print('\nValidating now...')

        loss, dice_coeff = eval_fn(val_dl,
                                   fold_model,
                                   loss_fn,
                                   device)
        # del val_dl
        gc.collect()

        # Saving the model, so that we can import it in the inference kernel.
        torch.save(fold_model.state_dict(), output_dir + "/8core_fold_model_{fold}.pth")

        del train_ds, val_ds, model

# Updated
def train_one_epoch(epoch_no, data_loader, model, loss_fn, optimizer, device, scheduler=None):
    '''
    Run one epoch on the 'model'.
    Args:
        epoch_no: Serial number of the given epoch
        data_loader: Data iterator like DataLoader
        model: Model which needs to be train for one epoch
        optmizer : Pytorch's optimizer
        device: Device(A particular core) on which to run this epoch
        Scheduler : Pytorch's lr scheduler

    Returns: Nothing
    '''
    model.train()
    losses = []
    dice_coeffs = []

    for i, batch in tqdm(enumerate(data_loader)):
        img_btch, mask_btch = batch

        # *** Putting the images and masks to the TPU device. ***
        img_btch = img_btch.to(device)
        mask_btch = mask_btch.to(device)

        optimizer.zero_grad()

        pred_mask_btch = model(img_btch.float())

        loss = loss_fn(pred_mask_btch, mask_btch.float())

        loss.backward()

        '''
        xm.optimizer_step():
        Consolidates the gradients between cores and issues the XLA device step computation.
        The `step()` function now not only propagates gradients, but uses the TPU context
        to synchronize gradient updates across each processes' copy of the network.
        This ensures that each processes' network copy stays "in sync" (they are all identical).
        This means that each process's network has the same weights after this is called.
        [Source:PyTorch XLA doc]
        '''
        # Note: barrier=True not needed when using ParallelLoader
        # xm.optimizer_step(optimizer)
        if scheduler is not None:
            scheduler.step()

        # 'mesh_reduce()' reduce the loss calculated on 8 cores.
        # The way it needs to be reduced is defined in 'reduce()' function
        # loss_reduced = xm.mesh_reduce('train_loss_reduce',
        #                               loss,
        #                               reduce)
        losses.append(loss.item())

        dice_coeff = get_dice_coeff(torch.squeeze(pred_mask_btch),
                                    mask_btch.float())
        dice_coeffs.append(dice_coeff.cpu().detach().numpy())

        del img_btch, pred_mask_btch, mask_btch
        gc.collect()

    print(f'{epoch_no + 1} - Loss : {np.mean(losses): .4f}, Dice Coeff : {np.mean(dice_coeffs): .4f}')

# New stuff
def eval_fn(data_loader, model, loss_fn, device):
    '''
    Calculates metrics on the validation data.

    Returns: returns calculated metrics
    '''
    model.eval()

    dice_coeffs = []
    losses = []
    with torch.no_grad():
        for i, batch in enumerate(data_loader):
            img_btch, mask_btch = batch

            img_btch = img_btch.to(device)
            mask_btch = mask_btch.to(device)
            pred_mask_btch = model(img_btch.float())

            loss = loss_fn(pred_mask_btch,
                           mask_btch.float())
            losses.append(loss.item())

            dice_coeff = get_dice_coeff(torch.squeeze(pred_mask_btch),
                                        mask_btch.float())
            dice_coeffs.append(dice_coeff.cpu().detach().numpy())

    total_dice_coeff = (dice_coeffs)
    total_loss = (losses)

    print(f'Val Loss : {np.mean(losses): .4f}, Val Dice : {np.mean(dice_coeffs): .4f}')

    return total_loss, total_dice_coeff

if __name__ == '__main__':
    set_all_seeds(21)
    tileProcCfg = img_proc_cfg()
    path_cfg    = path_cfg()

    print(f'No. of training images: {len(os.listdir(path_cfg.DATA_512_IMG_DIR))}')
    print(f'No. of masks: {len(os.listdir(path_cfg.DATA_512_MASK_DIR))}')

    # Images and its corresponding masks are saved with the same filename.
    fnames = np.array(os.listdir(path_cfg.DATA_512_IMG_DIR))

    groups = [fname[:9] for fname in fnames]

    group_kfold = GroupKFold(n_splits=4)

    # demo_lr(
    trainer(path_cfg.DATA_512_DIR, output_dir=path_cfg.RESULTS_DIR)


text:
# coding: utf-8

# In[4]:
import numpy as np
import operator

from math import log
def calcShannonEnt(dataSet):#计算香农熵
    numEntries=len(dataSet)#获取数据集的文件数目
    labelCounts={}#建立一个标签矩阵，以存放对应的标签值
    #依次在数据集当中將数据集的标签放到标签矩阵里
    for featVec in dataSet:
        currentLabels=featVec[-1]#依次取featVec里的最后一个元素，即标签元素
        if currentLabels not in labelCounts.keys():#如果得到的标签不在已有的标签集内
            labelCounts[currentLabels]=0#创建一个新的标签
        labelCounts[currentLabels]+=1#对对应的标签种类进行计数
    shannonEnt=0.0#熵清0
    #对所有不同类别计算熵
    for key in labelCounts:
        prob=float(labelCounts[key])/numEntries
        shannonEnt-=prob*log(prob,2)
    return shannonEnt

# In[9]:

def createDataSet():
    dataSet=([[1,1,'Yes'],[1,1,'Yes'],[1,0,'No'],[0,1,'No'],[0,1,'No']])#这里不能写array，因为不是纯数
    labels=['no surfacing','flippers']
    return dataSet,labels

def splitDataSet(dataSet,axis,value):#按照给定特征对数据集进行划分，三个参量依次是数据集，划分数据集的特征和特征返回值
    retDataSet=[]#构建新矩阵
    #对于数据集中的数据
    for featVec in dataSet:
        if featVec[axis]==value:#一旦发现特征与目标值一致
            reducedFeatVec=featVec[:axis]#將axis之前的列附到resucdFeatVec里
            reducedFeatVec.extend(featVec[axis+1:])#将axis以后的列附到reducedFeatVec里
            retDataSet.append(reducedFeatVec)#將去除了指定特征列的数据集放在retDataSet里
    return retDataSet#返回划分后数据集

def chooseBestFeatureToSplit(dataSet):#选择最好的数据分割点
    lens=len(dataSet[0])-1#对数据集第一行，即第一个例子的行数（特征个数），减1是为了方便数组计算
    baseShannonEnt=calcShannonEnt(dataSet)#算出原始香农熵
    bestGain=0.0#將最信息增益先置零
    bestFeature=-1#將最佳分割点值-1，不置0是因为0相当于第一个分割点，会引起误解
    for i in range(lens):#循环各个特征找使信息熵最大点处
        featList=[example[i] for example in dataSet]#对于数据集里的所有特征遍历
        uniqueVals=set(featList)#设置一个列表存放这些特征
        newEnt=0.0#信息熵清零
        for value in uniqueVals:#遍历所有特征，根据特征不同来实现分割从而获取不同信息熵
            subDataSet=splitDataSet(dataSet,i,value)#不同特征处分割
            prob=len(subDataSet)/float(len(dataSet))
            newEnt+=prob*calcShannonEnt(subDataSet)#获取此时的信息熵
        infoGain=baseShannonEnt-newEnt#获取信息增益
        if(infoGain>bestGain):#如果信息增益比现有最好增益还大
            bestGain=infoGain#则取代他
            bestFeature=i#并记下此时的分割位置
    return bestFeature#返回分割位置

#####################下述两个函数组合在一起就是ID3决策树算法#############################
#####################这个多数表决程序本质上与K近邻法的第一个分类器是一样##################

def majorityCnt(classList):#多数表决分类函数
    classCount={}#建立一个数据字典，里面存储所有的类别
    for vote in classList:
        if vote not in classCount.keys():classCount[vote]=0#如果有新的类别，则创立一个新的元素代表该种类
        classCount[vote]+=1#否则该元素加一
    sortedClassCount=sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)#对数据集进行排序，第二行作为排序依据，从高到低排
    return sortedClassCount[0][0]#把第一个元素返回，即返回出现次数最多的那个元素

def createTree(dataSet,labels):#创建一个决策树
    classList=[example[-1] for example in dataSet]#以数据集的最后一列作为新的一个列表
    if classList.count(classList[0])==len(classList):#如果分类列表完全相同
        return classList[0]#停止继续划分
    if len(dataSet[0])==1:#如果遍历完所有特征，仍不能划分为唯一门类
        return majorithCnt(classList)#返回出现出现次数最多的那个类标签
    bestFeat=chooseBestFeatureToSplit(dataSet)#否则选择最优特征
    bestFeatLabel=labels[bestFeat]#同时將最优特征的标签赋予bestFeatureLabel
    myTree = {bestFeatLabel:{}}#根据最优标签生成树
    del(labels[bestFeat])#將刚刚生成树所使用的标签去掉
    featValues=[example[bestFeat] for example in dataSet]#获取所有训练集中最优特征属性值
    uniqueVals=set(featValues)#把重复的属性去掉，并放到uniqueVals里
    for value in uniqueVals:#遍历特征遍历的所有属性值
        subLabels=labels[:]#先把原始标签数据完全复制，防止对原列表干扰
        myTree[bestFeatLabel][value]=createTree(splitDataSet(dataSet,bestFeat,value),subLabels)#再以该特征来划分决策树
    return myTree#返回决策树

max_stars_repo_name: lwpyh/machinelearning

max_issues_repo_name: lwpyh/machinelearning

max_forks_repo_name: lwpyh/machinelearning


text:
from players_reinforcement.player import player
from auxiliar.aux_plot import *

import random
from collections import deque

import sys
sys.path.append('..')
import tensorblock as tb
import numpy as np

# PLAYER DQL BAYESIAN
class player_dql_bayesian_1(player):

    # __INIT__
    def __init__(self):

        player.__init__(self)

        self.num_stored_obsv = self.NUM_FRAMES
        self.random_prob     = self.START_RANDOM_PROB
        self.experiences     = deque()
        self.keep_prob       = self.FINAL_RANDOM_PROB

    # CHOOSE NEXT ACTION
    def act(self, state):

        return self.calculate(state)

    # CALCULATE NETWORK
    def calculate(self, state):

        output = self.brain.run( 'Output', [ [ 'Observation', [state] ] ], use_dropout=True )
        action = np.argmax(output)

        return self.create_action ( action )

    # PREPARE OPERATIONS
    def operations ( self ):

        # Action Placeholders

        self.brain.addInput( shape = [ None, self.num_actions ], name = 'Actions' )
        self.brain.addInput( shape = [ None                   ], name = 'Target'  )

        # Operations

        self.brain.addOperation( function = tb.ops.sum_mul,
                                 input    = ['Output', 'Actions'],
                                 name     = 'Readout' )

        self.brain.addOperation( function = tb.ops.mean_squared_error,
                                 input    = ['Target', 'Readout'],
                                 name     = 'Cost' )

        # Optimizer

        self.brain.addOperation( function      = tb.optims.adam,
                                 input         = 'Cost',
                                 learning_rate = self.LEARNING_RATE,
                                 name          = 'Optimizer' )

        # TensorBoard

        self.brain.addSummaryScalar( input = 'Cost' )
        self.brain.addSummaryHistogram( input = 'Target' )
        self.brain.addWriter(name = 'Writer' , dir = '../' )
        self.brain.addSummary( name = 'Summary' )
        self.brain.initialize()

    # TRAIN NETWORK
    def train( self, prev_state, curr_state, actn, rewd, done, episode ):

        # Store New Experience

        self.experiences.append( ( prev_state, curr_state, actn, rewd, done ) )

        if len(self.experiences) > self.EXPERIENCES_LEN:
            self.experiences.popleft()

        # Check for Train

        if len(self.experiences) > self.STEPS_BEFORE_TRAIN and self.BATCH_SIZE > 0:

            # Select Random Batch

            batch = random.sample(self.experiences, self.BATCH_SIZE)

            # Separate Batch Data

            prev_states = [d[0] for d in batch]
            curr_states = [d[1] for d in batch]
            actions     = [d[2] for d in batch]
            rewards     = [d[3] for d in batch]
            dones       = [d[4] for d in batch]

            # Calculate Rewards

            rewards_per_action = self.brain.run( 'Output', [['Observation', curr_states]],
                                                 use_dropout = True )

            # Calculate Expected Reward

            expected_reward = []
            for i in range(self.BATCH_SIZE):
                if dones[i]:
                    expected_reward.append( rewards[i] )
                else:
                    expected_reward.append( rewards[i] + \
                                            self.REWARD_DISCOUNT * np.max( rewards_per_action[i] ) )

            # Optimize Neural Network

            _, summary = self.brain.run( ['Optimizer','Summary'], [ [ 'Observation', prev_states ],
                                                                    [ 'Actions', actions         ],
                                                                    [ 'Target', expected_reward  ] ], use_dropout=True )

            # Update Random Probability

            if done and self.random_prob > self.FINAL_RANDOM_PROB:
                self.random_prob -= (self.START_RANDOM_PROB - self.FINAL_RANDOM_PROB) / \
                    self.NUM_EXPLORATION_EPISODES
                self.keep_prob += (self.START_RANDOM_PROB - self.FINAL_RANDOM_PROB) / \
                    self.NUM_EXPLORATION_EPISODES

            self.brain.setDropout( name = 'Drop', value = self.keep_prob )

            # TensorBoard

            self.brain.write( summary = summary, iter = episode )

max_stars_repo_name: bermeom/quadruped-robot

max_issues_repo_name: bermeom/quadruped-robot

max_forks_repo_name: bermeom/quadruped-robot


text:
import awkward as ak
import jax
import numpy as np
ak.jax.register()
jax.config.update("jax_platform_name", "cpu")

def sum_grad(array):
    def recurse(array, indices = np.zeros(len(array), dtype = np.int32)):
        if isinstance(array, ak.layout.NumpyArray):

            def segment_sum_wrapper(arr, indices = np.zeros(len(arr), dtype=np.int32)):
                # print(The indices)
                # indices = np.zeros(len(arr), dtype = np.int32)
                arr = jax.ops.segment_sum(arr, indices)
                return arr
            value, func = jax.vjp(segment_sum_wrapper, np.asarray(array), indices)
            return value, func

        elif isinstance(array, ak._util.listtypes):
            indices = array.offsets
            segment_sum_indices = []
            integer_tags = 0
            for i in range(len(indices) - 1):
                start = indices[i]
                stop = indices[i + 1]
                segment_sum_indices = segment_sum_indices + [integer_tags for _ in range(stop - start)]
                integer_tags = integer_tags + 1

            value, func = recurse(array.content, np.asarray(segment_sum_indices))
            _, aux_data = ak._connect._jax.jax_utils.special_flatten(ak.Array(array))
            children = []
            children.append(ak.from_jax(func(value)[0]))
            return ak._connect._jax.jax_utils.special_unflatten(aux_data, children)

    #    elif isinstance(array, ak._util.indexedtypes):
    #        return recurse(array.project())
    #    elif isinstance(array, ak._util.uniontypes):
    #        raise ValueError(
    #            "Can't differentiate an UnionArray type {0}".format(array)
    #        )
    #    elif isinstance(array, ak._util.recordtypes):
    #        indices = []
    #        children = []
    #        for content in array.contents:
    #            diff_arr = recurse(content)
    #            diff_children, _ = ak._connect._jax.jax_utils.special_flatten(ak.Array(diff_arr))
    #            children = children + diff_children

    #        return indices
    #    elif isinstance(outlayout, ak._util.indexedtypes):
    #        return fetch_indices_and_fieldloc_layout(outlayout.content)
    #    elif isinstance(outlayout, ak._util.indexedoptiontypes):
    #        return fetch_indices_and_fieldloc_layout(outlayout.content)
    #    elif isinstance(
    #        outlayout,
    #        (
    #            ak.layout.BitMaskedArray,
    #            ak.layout.ByteMaskedArray,
    #            ak.layout.UnmaskedArray,
    #        ),
    #    ):
    #        return fetch_indices_and_fieldloc_layout(outlayout.content)
    #    else:
    #        raise NotImplementedError

    return recurse(array.layout)

def sum_jax(arr):
    indices = np.zeros(len(arr), dtype = np.int32)
    arr1 = jax.ops.segment_sum(arr, indices)
    return arr1

arr = ak.Array(np.asarray([1., 2., 3., 4., 5.], dtype=np.float32))
arr1 = jax.numpy.array([1., 2., 3., 4., 5.])
arr2 = ak.Array([[1., 2., 3.], [4], [5., 6.]])

print(sum_grad(arr2))
# value, func = jax.vjp(, arr)

# print(value)
# print(func(value))

reason: import numpy,import jax