text:
.. _ensemble:

================
Ensemble methods
================

.. currentmodule:: sklearn.ensemble

The goal of **ensemble methods** is to combine the predictions of several
base estimators built with a given learning algorithm in order to improve
generalizability / robustness over a single estimator.

Two families of ensemble methods are usually distinguished:

- In **averaging methods**, the driving principle is to build several
  estimators independently and then to average their predictions. On average,
  the combined estimator is usually better than any of the single base
  estimator because its variance is reduced.

  **Examples:** :ref:`Bagging methods <bagging>`, :ref:`Forests of randomized trees <forest>`, ...

- By contrast, in **boosting methods**, base estimators are built sequentially
  and one tries to reduce the bias of the combined estimator. The motivation is
  to combine several weak models to produce a powerful ensemble.

  **Examples:** :ref:`AdaBoost <adaboost>`, :ref:`Gradient Tree Boosting <gradient_boosting>`, ...

.. _bagging:

Bagging meta-estimator
======================

In ensemble algorithms, bagging methods form a class of algorithms which build
several instances of a black-box estimator on random subsets of the original
training set and then aggregate their individual predictions to form a final
prediction. These methods are used as a way to reduce the variance of a base
estimator (e.g., a decision tree), by introducing randomization into its
construction procedure and then making an ensemble out of it. In many cases,
bagging methods constitute a very simple way to improve with respect to a
single model, without making it necessary to adapt the underlying base
algorithm. As they provide a way to reduce overfitting, bagging methods work
best with strong and complex models (e.g., fully developed decision trees), in
contrast with boosting methods which usually work best with weak models (e.g.,
shallow decision trees).

Bagging methods come in many flavours but mostly differ from each other by the
way they draw random subsets of the training set:

  * When random subsets of the dataset are drawn as random subsets of the
    samples, then this algorithm is known as Pasting [B1999]_.

  * When samples are drawn with replacement, then the method is known as
    Bagging [B1996]_.

  * When random subsets of the dataset are drawn as random subsets of
    the features, then the method is known as Random Subspaces [H1998]_.

  * Finally, when base estimators are built on subsets of both samples and
    features, then the method is known as Random Patches [LG2012]_.

In scikit-learn, bagging methods are offered as a unified
:class:`BaggingClassifier` meta-estimator  (resp. :class:`BaggingRegressor`),
taking as input a user-specified base estimator along with parameters
specifying the strategy to draw random subsets. In particular, ``max_samples``
and ``max_features`` control the size of the subsets (in terms of samples and
features), while ``bootstrap`` and ``bootstrap_features`` control whether
samples and features are drawn with or without replacement. As an example, the
snippet below illustrates how to instantiate a bagging ensemble of
:class:`KNeighborsClassifier` base estimators, each built on random subsets of
50% of the samples and 50% of the features.

    >>> from sklearn.ensemble import BaggingClassifier
    >>> from sklearn.neighbors import KNeighborsClassifier
    >>> bagging = BaggingClassifier(KNeighborsClassifier(),
    ...                             max_samples=0.5, max_features=0.5)

.. topic:: Examples:

 * :ref:`example_ensemble_plot_bias_variance.py`

.. topic:: References

  .. [B1999] L. Breiman, "Pasting small votes for classification in large
         databases and on-line", Machine Learning, 36(1), 85-103, 1999.

  .. [B1996] L. Breiman, "Bagging predictors", Machine Learning, 24(2),
         123-140, 1996.

  .. [H1998] T. Ho, "The random subspace method for constructing decision
         forests", Pattern Analysis and Machine Intelligence, 20(8), 832-844,
         1998.

  .. [LG2012] G. Louppe and P. Geurts, "Ensembles on Random Patches",
         Machine Learning and Knowledge Discovery in Databases, 346-361, 2012.

.. _forest:

Forests of randomized trees
===========================

The :mod:`sklearn.ensemble` module includes two averaging algorithms based
on randomized :ref:`decision trees <tree>`: the RandomForest algorithm
and the Extra-Trees method. Both algorithms are perturb-and-combine
techniques [B1998]_ specifically designed for trees. This means a diverse
set of classifiers is created by introducing randomness in the classifier
construction.  The prediction of the ensemble is given as the averaged
prediction of the individual classifiers.

As other classifiers, forest classifiers have to be fitted with two
arrays: an array X of size ``[n_samples, n_features]`` holding the
training samples, and an array Y of size ``[n_samples]`` holding the
target values (class labels) for the training samples::

    >>> from sklearn.ensemble import RandomForestClassifier
    >>> X = [[0, 0], [1, 1]]
    >>> Y = [0, 1]
    >>> clf = RandomForestClassifier(n_estimators=10)
    >>> clf = clf.fit(X, Y)

Like :ref:`decision trees <tree>`, forests of trees also extend
to :ref:`multi-output problems <tree_multioutput>`  (if Y is an array of size
``[n_samples, n_outputs]``).

Random Forests
--------------

In random forests (see :class:`RandomForestClassifier` and
:class:`RandomForestRegressor` classes), each tree in the ensemble is
built from a sample drawn with replacement (i.e., a bootstrap sample)
from the training set. In addition, when splitting a node during the
construction of the tree, the split that is chosen is no longer the
best split among all features. Instead, the split that is picked is the
best split among a random subset of the features. As a result of this
randomness, the bias of the forest usually slightly increases (with
respect to the bias of a single non-random tree) but, due to averaging,
its variance also decreases, usually more than compensating for the
increase in bias, hence yielding an overall better model.

In contrast to the original publication [B2001]_, the scikit-learn
implementation combines classifiers by averaging their probabilistic
prediction, instead of letting each classifier vote for a single class.

Extremely Randomized Trees
--------------------------

In extremely randomized trees (see :class:`ExtraTreesClassifier`
and :class:`ExtraTreesRegressor` classes), randomness goes one step
further in the way splits are computed. As in random forests, a random
subset of candidate features is used, but instead of looking for the
most discriminative thresholds, thresholds are drawn at random for each
candidate feature and the best of these randomly-generated thresholds is
picked as the splitting rule. This usually allows to reduce the variance
of the model a bit more, at the expense of a slightly greater increase
in bias::

    >>> from sklearn.cross_validation import cross_val_score
    >>> from sklearn.datasets import make_blobs
    >>> from sklearn.ensemble import RandomForestClassifier
    >>> from sklearn.ensemble import ExtraTreesClassifier
    >>> from sklearn.tree import DecisionTreeClassifier

    >>> X, y = make_blobs(n_samples=10000, n_features=10, centers=100,
    ...     random_state=0)

    >>> clf = DecisionTreeClassifier(max_depth=None, min_samples_split=1,
    ...     random_state=0)
    >>> scores = cross_val_score(clf, X, y)
    >>> scores.mean()                             # doctest: +ELLIPSIS
    0.97...

    >>> clf = RandomForestClassifier(n_estimators=10, max_depth=None,
    ...     min_samples_split=1, random_state=0)
    >>> scores = cross_val_score(clf, X, y)
    >>> scores.mean()                             # doctest: +ELLIPSIS
    0.999...

    >>> clf = ExtraTreesClassifier(n_estimators=10, max_depth=None,
    ...     min_samples_split=1, random_state=0)
    >>> scores = cross_val_score(clf, X, y)
    >>> scores.mean() > 0.999
    True

.. figure:: ../auto_examples/ensemble/images/plot_forest_iris_001.png
    :target: ../auto_examples/ensemble/plot_forest_iris.html
    :align: center
    :scale: 75%

Parameters
----------

The main parameters to adjust when using these methods is ``n_estimators``
and ``max_features``. The former is the number of trees in the forest. The
larger the better, but also the longer it will take to compute. In
addition, note that results will stop getting significantly better
beyond a critical number of trees. The latter is the size of the random
subsets of features to consider when splitting a node. The lower the
greater the reduction of variance, but also the greater the increase in
bias. Empirical good default values are ``max_features=n_features``
for regression problems, and ``max_features=sqrt(n_features)`` for
classification tasks (where ``n_features`` is the number of features
in the data). The best results are also usually reached when setting
``max_depth=None`` in combination with ``min_samples_split=1`` (i.e.,
when fully developing the trees). Bear in mind though that these values
are usually not optimal. The best parameter values should always be cross-
validated. In addition, note that bootstrap samples are used by default
in random forests (``bootstrap=True``) while the default strategy is to
use the original dataset for building extra-trees (``bootstrap=False``).

Parallelization
---------------

Finally, this module also features the parallel construction of the trees
and the parallel computation of the predictions through the ``n_jobs``
parameter. If ``n_jobs=k`` then computations are partitioned into
``k`` jobs, and run on ``k`` cores of the machine. If ``n_jobs=-1``
then all cores available on the machine are used. Note that because of
inter-process communication overhead, the speedup might not be linear
(i.e., using ``k`` jobs will unfortunately not be ``k`` times as
fast). Significant speedup can still be achieved though when building
a large number of trees, or when building a single tree requires a fair
amount of time (e.g., on large datasets).

.. topic:: Examples:

 * :ref:`example_ensemble_plot_forest_iris.py`
 * :ref:`example_ensemble_plot_forest_importances_faces.py`
 * :ref:`example_plot_multioutput_face_completion.py`

.. topic:: References

 .. [B2001] L. Breiman, "Random Forests", Machine Learning, 45(1), 5-32, 2001.

 .. [B1998] L. Breiman, "Arcing Classifiers", Annals of Statistics 1998.

 .. [GEW2006] P. Geurts, D. Ernst., and L. Wehenkel, "Extremely randomized
   trees", Machine Learning, 63(1), 3-42, 2006.

.. _random_forest_feature_importance:

Feature importance evaluation
-----------------------------

The relative rank (i.e. depth) of a feature used as a decision node in a
tree can be used to assess the relative importance of that feature with
respect to the predictability of the target variable. Features used at
the top of the tree are used contribute to the final prediction decision
of a larger fraction of the input samples. The **expected fraction of
the samples** they contribute to can thus be used as an estimate of the
**relative importance of the features**.

By **averaging** those expected activity rates over several randomized
trees one can **reduce the variance** of such an estimate and use it
for feature selection.

The following example shows a color-coded representation of the relative
importances of each individual pixel for a face recognition task using
a :class:`ExtraTreesClassifier` model.

.. figure:: ../auto_examples/ensemble/images/plot_forest_importances_faces_001.png
   :target: ../auto_examples/ensemble/plot_forest_importances_faces.html
   :align: center
   :scale: 75

In practice those estimates are stored as an attribute named
``feature_importances_`` on the fitted model. This is an array with shape
``(n_features,)`` whose values are positive and sum to 1.0. The higher
the value, the more important is the contribution of the matching feature
to the prediction function.

.. topic:: Examples:

 * :ref:`example_ensemble_plot_forest_importances_faces.py`
 * :ref:`example_ensemble_plot_forest_importances.py`

.. _random_trees_embedding:

Totally Random Trees Embedding
------------------------------

:class:`RandomTreesEmbedding` implements an unsupervised transformation of the
data.  Using a forest of completely random trees, :class:`RandomTreesEmbedding`
encodes the data by the indices of the leaves a data point ends up in.  This
index is then encoded in a one-of-K manner, leading to a high dimensional,
sparse binary coding.
This coding can be computed very efficiently and can then be used as a basis
for other learning tasks.
The size and sparsity of the code can be influenced by choosing the number of
trees and the maximum depth per tree. For each tree in the ensemble, the coding
contains one entry of one. The size of the coding is at most ``n_estimators * 2
** max_depth``, the maximum number of leaves in the forest.

As neighboring data points are more likely to lie within the same leaf of a tree,
the transformation performs an implicit, non-parametric density estimation.

.. topic:: Examples:

 * :ref:`example_ensemble_plot_random_forest_embedding.py`

 * :ref:`example_manifold_plot_lle_digits.py` compares non-linear
   dimensionality reduction techniques on handwritten digits.

.. seealso::

   :ref:`manifold` techniques can also be useful to derive non-linear
   representations of feature space, also these approaches focus also on
   dimensionality reduction.

.. _adaboost:

AdaBoost
========

The module :mod:`sklearn.ensemble` includes the popular boosting algorithm
AdaBoost, introduced in 1995 by Freund and Schapire [FS1995]_.

The core principle of AdaBoost is to fit a sequence of weak learners (i.e.,
models that are only slightly better than random guessing, such as small
decision trees) on repeatedly modified versions of the data. The predictions
from all of them are then combined through a weighted majority vote (or sum) to
produce the final prediction. The data modifications at each so-called boosting
iteration consist of applying weights :math:`w_1`, :math:`w_2`, ..., :math:`w_N`
to each of the training samples. Initially, those weights are all set to
:math:`w_i = 1/N`, so that the first step simply trains a weak learner on the
original data. For each successive iteration, the sample weights are
individually modified and the learning algorithm is reapplied to the reweighted
data. At a given step, those training examples that were incorrectly predicted
by the boosted model induced at the previous step have their weights increased,
whereas the weights are decreased for those that were predicted correctly. As
iterations proceed, examples that are difficult to predict receive
ever-increasing influence. Each subsequent weak learner is thereby forced to
concentrate on the examples that are missed by the previous ones in the sequence
[HTF]_.

.. figure:: ../auto_examples/ensemble/images/plot_adaboost_hastie_10_2_001.png
   :target: ../auto_examples/ensemble/plot_adaboost_hastie_10_2.html
   :align: center
   :scale: 75

AdaBoost can be used both for classification and regression problems:

  - For multi-class classification, :class:`AdaBoostClassifier` implements
    AdaBoost-SAMME and AdaBoost-SAMME.R [ZZRH2009]_.

  - For regression, :class:`AdaBoostRegressor` implements AdaBoost.R2 [D1997]_.

Usage
-----

The following example shows how to fit an AdaBoost classifier with 100 weak
learners::

    >>> from sklearn.cross_validation import cross_val_score
    >>> from sklearn.datasets import load_iris
    >>> from sklearn.ensemble import AdaBoostClassifier

    >>> iris = load_iris()
    >>> clf = AdaBoostClassifier(n_estimators=100)
    >>> scores = cross_val_score(clf, iris.data, iris.target)
    >>> scores.mean()                             # doctest: +ELLIPSIS
    0.9...

The number of weak learners is controlled by the parameter ``n_estimators``. The
``learning_rate`` parameter controls the contribution of the weak learners in
the final combination. By default, weak learners are decision stumps. Different
weak learners can be specified through the ``base_estimator`` parameter.
The main parameters to tune to obtain good results are ``n_estimators`` and
the complexity of the base estimators (e.g., its depth ``max_depth`` or
minimum required number of samples at a leaf ``min_samples_leaf`` in case of
decision trees).

.. topic:: Examples:

 * :ref:`example_ensemble_plot_adaboost_hastie_10_2.py` compares the
   classification error of a decision stump, decision tree, and a boosted
   decision stump using AdaBoost-SAMME and AdaBoost-SAMME.R.

 * :ref:`example_ensemble_plot_adaboost_multiclass.py` shows the performance
   of AdaBoost-SAMME and AdaBoost-SAMME.R on a multi-class problem.

 * :ref:`example_ensemble_plot_adaboost_twoclass.py` shows the decision boundary
   and decision function values for a non-linearly separable two-class problem
   using AdaBoost-SAMME.

 * :ref:`example_ensemble_plot_adaboost_regression.py` demonstrates regression
   with the AdaBoost.R2 algorithm.

.. topic:: References

 .. [FS1995] Y. Freund, and R. Schapire, "A Decision-Theoretic Generalization of
             On-Line Learning and an Application to Boosting", 1997.

 .. [ZZRH2009] J. Zhu, H. Zou, S. Rosset, T. Hastie. "Multi-class AdaBoost",
               2009.

 .. [D1997] H. Drucker. "Improving Regressors using Boosting Techniques", 1997.

 .. [HTF] T. Hastie, R. Tibshirani and J. Friedman, "Elements of
              Statistical Learning Ed. 2", Springer, 2009.

.. _gradient_boosting:

Gradient Tree Boosting
======================

`Gradient Tree Boosting <http://en.wikipedia.org/wiki/Gradient_boosting>`_
or Gradient Boosted Regression Trees (GBRT) is a generalization
of boosting to arbitrary
differentiable loss functions. GBRT is an accurate and effective
off-the-shelf procedure that can be used for both regression and
classification problems.  Gradient Tree Boosting models are used in a
variety of areas including Web search ranking and ecology.

The advantages of GBRT are:

  + Natural handling of data of mixed type (= heterogeneous features)

  + Predictive power

  + Robustness to outliers in output space (via robust loss functions)

The disadvantages of GBRT are:

  + Scalability, due to the sequential nature of boosting it can
    hardly be parallelized.

The module :mod:`sklearn.ensemble` provides methods
for both classification and regression via gradient boosted regression
trees.

Classification
---------------

:class:`GradientBoostingClassifier` supports both binary and multi-class
classification.
The following example shows how to fit a gradient boosting classifier
with 100 decision stumps as weak learners::

    >>> from sklearn.datasets import make_hastie_10_2
    >>> from sklearn.ensemble import GradientBoostingClassifier

    >>> X, y = make_hastie_10_2(random_state=0)
    >>> X_train, X_test = X[:2000], X[2000:]
    >>> y_train, y_test = y[:2000], y[2000:]

    >>> clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0,
    ...     max_depth=1, random_state=0).fit(X_train, y_train)
    >>> clf.score(X_test, y_test)                 # doctest: +ELLIPSIS
    0.913...

The number of weak learners (i.e. regression trees) is controlled by the parameter ``n_estimators``; :ref:`The size of each tree <gradient_boosting_tree_size>` can be controlled either by setting the tree depth via ``max_depth`` or by setting the number of leaf nodes via ``max_leaf_nodes``. The ``learning_rate`` is a hyper-parameter in the range (0.0, 1.0] that controls overfitting via :ref:`shrinkage <gradient_boosting_shrinkage>` .

.. note::

   Classification with more than 2 classes requires the induction
   of ``n_classes`` regression trees at each at each iteration,
   thus, the total number of induced trees equals
   ``n_classes * n_estimators``. For datasets with a large number
   of classes we strongly recommend to use
   :class:`RandomForestClassifier` as an alternative to :class:`GradientBoostingClassifier` .

Regression
----------

:class:`GradientBoostingRegressor` supports a number of
:ref:`different loss functions <gradient_boosting_loss>`
for regression which can be specified via the argument
``loss``; the default loss function for regression is least squares (``'ls'``).

::

    >>> import numpy as np
    >>> from sklearn.metrics import mean_squared_error
    >>> from sklearn.datasets import make_friedman1
    >>> from sklearn.ensemble import GradientBoostingRegressor

    >>> X, y = make_friedman1(n_samples=1200, random_state=0, noise=1.0)
    >>> X_train, X_test = X[:200], X[200:]
    >>> y_train, y_test = y[:200], y[200:]
    >>> est = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1,
    ...     max_depth=1, random_state=0, loss='ls').fit(X_train, y_train)
    >>> mean_squared_error(y_test, est.predict(X_test))    # doctest: +ELLIPSIS
    5.00...

The figure below shows the results of applying :class:`GradientBoostingRegressor`
with least squares loss and 500 base learners to the Boston house price dataset
(:func:`sklearn.datasets.load_boston`).
The plot on the left shows the train and test error at each iteration.
The train error at each iteration is stored in the
:attr:`~GradientBoostingRegressor.train_score_` attribute
of the gradient boosting model. The test error at each iterations can be obtained
via the :meth:`~GradientBoostingRegressor.staged_predict` method which returns a
generator that yields the predictions at each stage. Plots like these can be used
to determine the optimal number of trees (i.e. ``n_estimators``) by early stopping.
The plot on the right shows the feature importances which can be obtained via
the ``feature_importances_`` property.

.. figure:: ../auto_examples/ensemble/images/plot_gradient_boosting_regression_001.png
   :target: ../auto_examples/ensemble/plot_gradient_boosting_regression.html
   :align: center
   :scale: 75

.. topic:: Examples:

 * :ref:`example_ensemble_plot_gradient_boosting_regression.py`
 * :ref:`example_ensemble_plot_gradient_boosting_oob.py`

.. _gradient_boosting_warm_start:

Fitting additional weak-learners
--------------------------------

Both :class:`GradientBoostingRegressor` and :class:`GradientBoostingClassifier`
support ``warm_start=True`` which allows you to add more estimators to an already
fitted model.

::

  >>> _ = est.set_params(n_estimators=200, warm_start=True)  # set warm_start and new nr of trees
  >>> _ = est.fit(X_train, y_train) # fit additional 100 trees to est
  >>> mean_squared_error(y_test, est.predict(X_test))    # doctest: +ELLIPSIS
  3.84...

.. _gradient_boosting_tree_size:

Controlling the tree size
-------------------------

The size of the regression tree base learners defines the level of variable
interactions that can be captured by the gradient boosting model. In general,
a tree of depth ``h`` can capture interactions of order ``h`` .
There are two ways in which the size of the individual regression trees can
be controlled.

If you specify ``max_depth=h`` then complete binary trees
of depth ``h`` will be grown. Such trees will have (at most) ``2**h`` leaf nodes
and ``2**h - 1`` split nodes.

Alternatively, you can control the tree size by specifying the number of
leaf nodes via the parameter ``max_leaf_nodes``. In this case,
trees will be grown using best-first search where nodes with the highest improvement
in impurity will be expanded first.
A tree with ``max_leaf_nodes=k`` has ``k - 1`` split nodes and thus can
model interactions of up to order ``max_leaf_nodes - 1`` .

We found that ``max_leaf_nodes=k`` gives comparable results to ``max_depth=k-1``
but is significantly faster to train at the expense of a slightly higher
training error.
The parameter ``max_leaf_nodes`` corresponds to the variable ``J`` in the
chapter on gradient boosting in [F2001]_ and is related to the parameter
``interaction.depth`` in R's gbm package where ``max_leaf_nodes == interaction.depth + 1`` .

Mathematical formulation
-------------------------

GBRT considers additive models of the following form:

  .. math::

    F(x) = \sum_{m=1}^{M} \gamma_m h_m(x)

where :math:`h_m(x)` are the basis functions which are usually called
*weak learners* in the context of boosting. Gradient Tree Boosting
uses :ref:`decision trees <tree>` of fixed size as weak
learners. Decision trees have a number of abilities that make them
valuable for boosting, namely the ability to handle data of mixed type
and the ability to model complex functions.

Similar to other boosting algorithms GBRT builds the additive model in
a forward stagewise fashion:

  .. math::

    F_m(x) = F_{m-1}(x) + \gamma_m h_m(x)

At each stage the decision tree :math:`h_m(x)` is chosen to
minimize the loss function :math:`L` given the current model
:math:`F_{m-1}` and its fit :math:`F_{m-1}(x_i)`

  .. math::

    F_m(x) = F_{m-1}(x) + \arg\min_{h} \sum_{i=1}^{n} L(y_i,
    F_{m-1}(x_i) - h(x))

The initial model :math:`F_{0}` is problem specific, for least-squares
regression one usually chooses the mean of the target values.

.. note:: The initial model can also be specified via the ``init``
          argument. The passed object has to implement ``fit`` and ``predict``.

Gradient Boosting attempts to solve this minimization problem
numerically via steepest descent: The steepest descent direction is
the negative gradient of the loss function evaluated at the current
model :math:`F_{m-1}` which can be calculated for any differentiable
loss function:

  .. math::

    F_m(x) = F_{m-1}(x) + \gamma_m \sum_{i=1}^{n} \nabla_F L(y_i,
    F_{m-1}(x_i))

Where the step length :math:`\gamma_m` is chosen using line search:

  .. math::

    \gamma_m = \arg\min_{\gamma} \sum_{i=1}^{n} L(y_i, F_{m-1}(x_i)
    - \gamma \frac{\partial L(y_i, F_{m-1}(x_i))}{\partial F_{m-1}(x_i)})

The algorithms for regression and classification
only differ in the concrete loss function used.

.. _gradient_boosting_loss:

Loss Functions
...............

The following loss functions are supported and can be specified using
the parameter ``loss``:

  * Regression

    * Least squares (``'ls'``): The natural choice for regression due
      to its superior computational properties. The initial model is
      given by the mean of the target values.
    * Least absolute deviation (``'lad'``): A robust loss function for
      regression. The initial model is given by the median of the
      target values.
    * Huber (``'huber'``): Another robust loss function that combines
      least squares and least absolute deviation; use ``alpha`` to
      control the sensitivity with regards to outliers (see [F2001]_ for
      more details).
    * Quantile (``'quantile'``): A loss function for quantile regression.
      Use ``0 < alpha < 1`` to specify the quantile. This loss function
      can be used to create prediction intervals
      (see :ref:`example_ensemble_plot_gradient_boosting_quantile.py`).

  * Classification

    * Binomial deviance (``'deviance'``): The negative binomial
      log-likelihood loss function for binary classification (provides
      probability estimates).  The initial model is given by the
      log odds-ratio.
    * Multinomial deviance (``'deviance'``): The negative multinomial
      log-likelihood loss function for multi-class classification with
      ``n_classes`` mutually exclusive classes. It provides
      probability estimates.  The initial model is given by the
      prior probability of each class. At each iteration ``n_classes``
      regression trees have to be constructed which makes GBRT rather
      inefficient for data sets with a large number of classes.
    * Exponential loss (``'exponential'``): The same loss function
      as :class:`AdaBoostClassifier`. Less robust to mislabeled
      examples than ``'deviance'``; can only be used for binary
      classification.

Regularization
----------------

.. _gradient_boosting_shrinkage:

Shrinkage
..........

[F2001]_ proposed a simple regularization strategy that scales
the contribution of each weak learner by a factor :math:`\nu`:

.. math::

    F_m(x) = F_{m-1}(x) + \nu \gamma_m h_m(x)

The parameter :math:`\nu` is also called the **learning rate** because
it scales the step length the the gradient descent procedure; it can
be set via the ``learning_rate`` parameter.

The parameter ``learning_rate`` strongly interacts with the parameter
``n_estimators``, the number of weak learners to fit. Smaller values
of ``learning_rate`` require larger numbers of weak learners to maintain
a constant training error. Empirical evidence suggests that small
values of ``learning_rate`` favor better test error. [HTF2009]_
recommend to set the learning rate to a small constant
(e.g. ``learning_rate <= 0.1``) and choose ``n_estimators`` by early
stopping. For a more detailed discussion of the interaction between
``learning_rate`` and ``n_estimators`` see [R2007]_.

Subsampling
............

[F1999]_ proposed stochastic gradient boosting, which combines gradient
boosting with bootstrap averaging (bagging). At each iteration
the base classifier is trained on a fraction ``subsample`` of
the available training data. The subsample is drawn without replacement.
A typical value of ``subsample`` is 0.5.

The figure below illustrates the effect of shrinkage and subsampling
on the goodness-of-fit of the model. We can clearly see that shrinkage
outperforms no-shrinkage. Subsampling with shrinkage can further increase
the accuracy of the model. Subsampling without shrinkage, on the other hand,
does poorly.

.. figure:: ../auto_examples/ensemble/images/plot_gradient_boosting_regularization_001.png
   :target: ../auto_examples/ensemble/plot_gradient_boosting_regularization.html
   :align: center
   :scale: 75

Another strategy to reduce the variance is by subsampling the features
analogous to the random splits in :class:`RandomForestClassifier` .
The number of subsampled features can be controlled via the ``max_features``
parameter.

.. note:: Using a small ``max_features`` value can significantly decrease the runtime.

Stochastic gradient boosting allows to compute out-of-bag estimates of the
test deviance by computing the improvement in deviance on the examples that are
not included in the bootstrap sample (i.e. the out-of-bag examples).
The improvements are stored in the attribute
:attr:`~GradientBoostingRegressor.oob_improvement_`. ``oob_improvement_[i]`` holds
the improvement in terms of the loss on the OOB samples if you add the i-th stage
to the current predictions.
Out-of-bag estimates can be used for model selection, for example to determine
the optimal number of iterations. OOB estimates are usually very pessimistic thus
we recommend to use cross-validation instead and only use OOB if cross-validation
is too time consuming.

.. topic:: Examples:

 * :ref:`example_ensemble_plot_gradient_boosting_regularization.py`
 * :ref:`example_ensemble_plot_gradient_boosting_oob.py`

Interpretation
--------------

Individual decision trees can be interpreted easily by simply
visualizing the tree structure. Gradient boosting models, however,
comprise hundreds of regression trees thus they cannot be easily
interpreted by visual inspection of the individual trees. Fortunately,
a number of techniques have been proposed to summarize and interpret
gradient boosting models.

Feature importance
..................

Often features do not contribute equally to predict the target
response; in many situations the majority of the features are in fact
irrelevant.
When interpreting a model, the first question usually is: what are
those important features and how do they contributing in predicting
the target response?

Individual decision trees intrinsically perform feature selection by selecting
appropriate split points. This information can be used to measure the
importance of each feature; the basic idea is: the more often a
feature is used in the split points of a tree the more important that
feature is. This notion of importance can be extended to decision tree
ensembles by simply averaging the feature importance of each tree (see
:ref:`random_forest_feature_importance` for more details).

The feature importance scores of a fit gradient boosting model can be
accessed via the ``feature_importances_`` property::

    >>> from sklearn.datasets import make_hastie_10_2
    >>> from sklearn.ensemble import GradientBoostingClassifier

    >>> X, y = make_hastie_10_2(random_state=0)
    >>> clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0,
    ...     max_depth=1, random_state=0).fit(X, y)
    >>> clf.feature_importances_  # doctest: +ELLIPSIS
    array([ 0.11,  0.1 ,  0.11,  ...

.. topic:: Examples:

 * :ref:`example_ensemble_plot_gradient_boosting_regression.py`

.. currentmodule:: sklearn.ensemble.partial_dependence

Partial dependence
..................

Partial dependence plots (PDP) show the dependence between the target response
and a set of 'target' features, marginalizing over the
values of all other features (the 'complement' features).
Intuitively, we can interpret the partial dependence as the expected
target response [1]_ as a function of the 'target' features [2]_.

Due to the limits of human perception the size of the target feature
set must be small (usually, one or two) thus the target features are
usually chosen among the most important features.

The Figure below shows four one-way and one two-way partial dependence plots
for the California housing dataset:

.. figure:: ../auto_examples/ensemble/images/plot_partial_dependence_001.png
   :target: ../auto_examples/ensemble/plot_partial_dependence.html
   :align: center
   :scale: 70

One-way PDPs tell us about the interaction between the target
response and the target feature (e.g. linear, non-linear).
The upper left plot in the above Figure shows the effect of the
median income in a district on the median house price; we can
clearly see a linear relationship among them.

PDPs with two target features show the
interactions among the two features. For example, the two-variable PDP in the
above Figure shows the dependence of median house price on joint
values of house age and avg. occupants per household. We can clearly
see an interaction between the two features:
For an avg. occupancy greater than two, the house price is nearly independent
of the house age, whereas for values less than two there is a strong dependence
on age.

The module :mod:`partial_dependence` provides a convenience function
:func:`~sklearn.ensemble.partial_dependence.plot_partial_dependence`
to create one-way and two-way partial dependence plots. In the below example
we show how to create a grid of partial dependence plots: two one-way
PDPs for the features ``0`` and ``1`` and a two-way PDP between the two
features::

    >>> from sklearn.datasets import make_hastie_10_2
    >>> from sklearn.ensemble import GradientBoostingClassifier
    >>> from sklearn.ensemble.partial_dependence import plot_partial_dependence

    >>> X, y = make_hastie_10_2(random_state=0)
    >>> clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0,
    ...     max_depth=1, random_state=0).fit(X, y)
    >>> features = [0, 1, (0, 1)]
    >>> fig, axs = plot_partial_dependence(clf, X, features) #doctest: +SKIP

For multi-class models, you need to set the class label for which the
PDPs should be created via the ``label`` argument::

    >>> from sklearn.datasets import load_iris
    >>> iris = load_iris()
    >>> mc_clf = GradientBoostingClassifier(n_estimators=10,
    ...     max_depth=1).fit(iris.data, iris.target)
    >>> features = [3, 2, (3, 2)]
    >>> fig, axs = plot_partial_dependence(mc_clf, X, features, label=0) #doctest: +SKIP

If you need the raw values of the partial dependence function rather
than the plots you can use the
:func:`~sklearn.ensemble.partial_dependence.partial_dependence` function::

    >>> from sklearn.ensemble.partial_dependence import partial_dependence

    >>> pdp, axes = partial_dependence(clf, [0], X=X)
    >>> pdp  # doctest: +ELLIPSIS
    array([[ 2.46643157,  2.46643157, ...
    >>> axes  # doctest: +ELLIPSIS
    [array([-1.62497054, -1.59201391, ...

The function requires either the argument ``grid`` which specifies the
values of the target features on which the partial dependence function
should be evaluated or the argument ``X`` which is a convenience mode
for automatically creating ``grid`` from the training data. If ``X``
is given, the ``axes`` value returned by the function gives the axis
for each target feature.

For each value of the 'target' features in the ``grid`` the partial
dependence function need to marginalize the predictions of a tree over
all possible values of the 'complement' features. In decision trees
this function can be evaluated efficiently without reference to the
training data. For each grid point a weighted tree traversal is
performed: if a split node involves a 'target' feature, the
corresponding left or right branch is followed, otherwise both
branches are followed, each branch is weighted by the fraction of
training samples that entered that branch. Finally, the partial
dependence is given by a weighted average of all visited leaves. For
tree ensembles the results of each individual tree are again
averaged.

.. rubric:: Footnotes

.. [1] For classification with ``loss='deviance'``  the target
   response is logit(p).

.. [2] More precisely its the expectation of the target response after
   accounting for the initial model; partial dependence plots
   do not include the ``init`` model.

.. topic:: Examples:

 * :ref:`example_ensemble_plot_partial_dependence.py`

.. topic:: References

 .. [F2001] J. Friedman, "Greedy Function Approximation: A Gradient Boosting Machine",
   The Annals of Statistics, Vol. 29, No. 5, 2001.

 .. [F1999] J. Friedman, "Stochastic Gradient Boosting", 1999

 .. [HTF2009] T. Hastie, R. Tibshirani and J. Friedman, "Elements of Statistical Learning Ed. 2", Springer, 2009.

 .. [R2007] G. Ridgeway, "Generalized Boosted Models: A guide to the gbm package", 2007


text:
namespace Microsoft.Graph
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization;

    /// <summary>
    /// The type WorkbookFunctionsColumnsRequestBody.
    /// </summary>
    [DataContract]
    public partial class WorkbookFunctionsColumnsRequestBody
    {

        /// <summary>
        /// Gets or sets Array.
        /// </summary>
        [DataMember(Name = "array", EmitDefaultValue = false, IsRequired = false)]
        public Newtonsoft.Json.Linq.JToken Array { get; set; }

    }
}

redpajama_set_name: RedPajamaGithub


text:
namespace X {
  int x;
};

struct B {
  static int ib; // expected-note {{'B::ib' declared here}}
  static int bfoo() { return 8; }
};

int bfoo() { return 4; }

int z;
const int C1 = 1;
const int C2 = 2;
void test_linear_colons()
{
  int B = 0;
  #pragma omp parallel for simd linear(B:bfoo())
  for (int i = 0; i < 10; ++i) ;
  // expected-error@+1 {{unexpected ':' in nested name specifier; did you mean '::'}}
  #pragma omp parallel for simd linear(B::ib:B:bfoo())
  for (int i = 0; i < 10; ++i) ;
  // expected-error@+1 {{use of undeclared identifier 'ib'; did you mean 'B::ib'}}
  #pragma omp parallel for simd linear(B:ib)
  for (int i = 0; i < 10; ++i) ;
  // expected-error@+1 {{unexpected ':' in nested name specifier; did you mean '::'?}}
  #pragma omp parallel for simd linear(z:B:ib)
  for (int i = 0; i < 10; ++i) ;
  #pragma omp parallel for simd linear(B:B::bfoo())
  for (int i = 0; i < 10; ++i) ;
  #pragma omp parallel for simd linear(X::x : ::z)
  for (int i = 0; i < 10; ++i) ;
  #pragma omp parallel for simd linear(B,::z, X::x)
  for (int i = 0; i < 10; ++i) ;
  #pragma omp parallel for simd linear(::z)
  for (int i = 0; i < 10; ++i) ;
  // expected-error@+1 {{expected variable name}}
  #pragma omp parallel for simd linear(B::bfoo())
  for (int i = 0; i < 10; ++i) ;
  #pragma omp parallel for simd linear(B::ib,B:C1+C2)
  for (int i = 0; i < 10; ++i) ;
}

template<int L, class T, class N> T test_template(T* arr, N num) {
  N i;
  T sum = (T)0;
  T ind2 = - num * L; // expected-note {{'ind2' defined here}}
  // expected-error@+1 {{argument of a linear clause should be of integral or pointer type}}
#pragma omp parallel for simd linear(ind2:L)
  for (i = 0; i < num; ++i) {
    T cur = arr[(int)ind2];
    ind2 += L;
    sum += cur;
  }
  return T();
}

template<int LEN> int test_warn() {
  int ind2 = 0;
  // expected-warning@+1 {{zero linear step (ind2 should probably be const)}}
  #pragma omp parallel for simd linear(ind2:LEN)
  for (int i = 0; i < 100; i++) {
    ind2 += LEN;
  }
  return ind2;
}

struct S1; // expected-note 2 {{declared here}} expected-note 2 {{forward declaration of 'S1'}}
extern S1 a;
class S2 {
  mutable int a;
public:
  S2():a(0) { }
};
const S2 b; // expected-note 2 {{'b' defined here}}
const S2 ba[5];
class S3 {
  int a;
public:
  S3():a(0) { }
};
const S3 ca[5];
class S4 {
  int a;
  S4();
public:
  S4(int v):a(v) { }
};
class S5 {
  int a;
  S5():a(0) {}
public:
  S5(int v):a(v) { }
};

S3 h;
#pragma omp threadprivate(h) // expected-note 2 {{defined as threadprivate or thread local}}

template<class I, class C> int foomain(I argc, C **argv) {
  I e(4);
  I g(5);
  int i;
  int &j = i; // expected-note {{'j' defined here}}
  #pragma omp parallel for simd linear // expected-error {{expected '(' after 'linear'}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear ( // expected-error {{expected expression}} expected-error {{expected ')'}} expected-note {{to match this '('}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear () // expected-error {{expected expression}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (argc // expected-error {{expected ')'}} expected-note {{to match this '('}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (argc, // expected-error {{expected expression}} expected-error {{expected ')'}} expected-note {{to match this '('}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (argc > 0 ? argv[1] : argv[2]) // expected-error {{expected variable name}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (argc : 5)
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (S1) // expected-error {{'S1' does not refer to a value}}
  for (int k = 0; k < argc; ++k) ++k;
  // expected-error@+2 {{linear variable with incomplete type 'S1'}}
  // expected-error@+1 {{const-qualified variable cannot be linear}}
  #pragma omp parallel for simd linear (a, b:B::ib)
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (argv[1]) // expected-error {{expected variable name}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear(e, g)
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear(h) // expected-error {{threadprivate or thread local variable cannot be linear}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear(i)
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel
  {
    int v = 0;
    int i;
    #pragma omp parallel for simd linear(v:i)
    for (int k = 0; k < argc; ++k) { i = k; v += i; }
  }
  #pragma omp parallel for simd linear(j) // expected-error {{arguments of OpenMP clause 'linear' cannot be of reference type}}
  for (int k = 0; k < argc; ++k) ++k;
  int v = 0;
  #pragma omp parallel for simd linear(v:j)
  for (int k = 0; k < argc; ++k) { ++k; v += j; }
  #pragma omp parallel for simd linear(i)
  for (int k = 0; k < argc; ++k) ++k;
  return 0;
}

int main(int argc, char **argv) {
  double darr[100];
  // expected-note@+1 {{in instantiation of function template specialization 'test_template<-4, double, int>' requested here}}
  test_template<-4>(darr, 4);
  // expected-note@+1 {{in instantiation of function template specialization 'test_warn<0>' requested here}}
  test_warn<0>();

  S4 e(4); // expected-note {{'e' defined here}}
  S5 g(5); // expected-note {{'g' defined here}}
  int i;
  int &j = i; // expected-note {{'j' defined here}}
  #pragma omp parallel for simd linear // expected-error {{expected '(' after 'linear'}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear ( // expected-error {{expected expression}} expected-error {{expected ')'}} expected-note {{to match this '('}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear () // expected-error {{expected expression}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (argc // expected-error {{expected ')'}} expected-note {{to match this '('}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (argc, // expected-error {{expected expression}} expected-error {{expected ')'}} expected-note {{to match this '('}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (argc > 0 ? argv[1] : argv[2]) // expected-error {{expected variable name}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (argc)
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (S1) // expected-error {{'S1' does not refer to a value}}
  for (int k = 0; k < argc; ++k) ++k;
  // expected-error@+2 {{linear variable with incomplete type 'S1'}}
  // expected-error@+1 {{const-qualified variable cannot be linear}}
  #pragma omp parallel for simd linear (a, b)
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear (argv[1]) // expected-error {{expected variable name}}
  for (int k = 0; k < argc; ++k) ++k;
  // expected-error@+2 {{argument of a linear clause should be of integral or pointer type, not 'S4'}}
  // expected-error@+1 {{argument of a linear clause should be of integral or pointer type, not 'S5'}}
  #pragma omp parallel for simd linear(e, g)
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear(h) // expected-error {{threadprivate or thread local variable cannot be linear}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel
  {
    int i;
    #pragma omp parallel for simd linear(i)
    for (int k = 0; k < argc; ++k) ++k;
    #pragma omp parallel for simd linear(i : 4)
    for (int k = 0; k < argc; ++k) { ++k; i += 4; }
  }
  #pragma omp parallel for simd linear(j) // expected-error {{arguments of OpenMP clause 'linear' cannot be of reference type 'int &'}}
  for (int k = 0; k < argc; ++k) ++k;
  #pragma omp parallel for simd linear(i)
  for (int k = 0; k < argc; ++k) ++k;

  foomain<int,char>(argc,argv);
  return 0;
}


text:
<resources>
    <string name="app_name">8.File</string>
    <string name="save">save to file</string>
    <string name="load">load from file</string>
    <string name="delete">delete file</string>
    <string name="filename">file</string>
</resources>


text:
int main() {
  int i = 1;
  // NOTE: unary semantics differ between
  // GCC and Clang
  // One unary per line hides these differences.
  int temp1 = i--;
  int temp2 = i++;
  return temp1 + temp2;
}


text:
const Code = require('@hapi/code');
const Lab = require('@hapi/lab');
const Helper = require('../helper.js');
const Validate = require('../../lib/validate.js');

const expect = Code.expect;
const lab = (exports.lab = Lab.script());

lab.experiment('tags', () => {
  const routes = [
    {
      method: 'GET',
      path: '/test',
      handler: Helper.defaultHandler,
      options: {
        tags: ['api']
      }
    }
  ];

  lab.test('no tag objects passed', async () => {
    const server = await Helper.createServer({}, routes);
    const response = await server.inject({ method: 'GET', url: '/swagger.json' });
    expect(response.statusCode).to.equal(200);
    expect(response.result.tags).to.equal([]);
    const isValid = await Validate.test(response.result);
    expect(isValid).to.be.true();
  });

  lab.test('name property passed', async () => {
    const swaggerOptions = {
      tags: [
        {
          name: 'test'
        }
      ]
    };

    const server = await Helper.createServer(swaggerOptions, routes);
    const response = await server.inject({ method: 'GET', url: '/swagger.json' });
    expect(response.statusCode).to.equal(200);
    expect(response.result.tags[0].name).to.equal('test');

    const isValid = await Validate.test(response.result);
    expect(isValid).to.be.true();
  });

  lab.test('full tag object', async () => {
    const swaggerOptions = {
      tags: [
        {
          name: 'test',
          description: 'Everything about test',
          externalDocs: {
            description: 'Find out more',
            url: 'http://swagger.io'
          }
        }
      ]
    };

    const server = await Helper.createServer(swaggerOptions, routes);
    const response = await server.inject({ method: 'GET', url: '/swagger.json' });

    expect(response.statusCode).to.equal(200);
    expect(response.result.tags).to.equal(swaggerOptions.tags);
    const isValid = await Validate.test(response.result);
    expect(isValid).to.be.true();
  });
});


text:
<?php

namespace Magento\Paypal\Test\Unit\Model\Billing;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class AgreementTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var Agreement
     */
    protected $model;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $paymentDataMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $paymentMethodInstanceMock;

    protected function setUp()
    {
        $objectManager = new ObjectManager($this);

        $this->paymentDataMock = $this->getMockBuilder('Magento\Payment\Helper\Data')
            ->disableOriginalConstructor()
            ->setMethods(['getMethodInstance'])
            ->getMock();

        $this->paymentMethodInstanceMock = $this->getMockBuilder('Magento\Payment\Model\Method\AbstractMethod')
            ->disableOriginalConstructor()
            ->setMethods([
                'setStore',
                'getCode',
                'getFormBlockType',
                'getTitle',
                'getStore',
                'initBillingAgreementToken',
                'getBillingAgreementTokenInfo',
                'placeBillingAgreement'
            ])
            ->getMock();

        $this->model = $objectManager->getObject('Magento\Paypal\Model\Billing\Agreement', [
            'paymentData' => $this->paymentDataMock
        ]);
    }

    public function testImportOrderPaymentWithMethodCode()
    {
        $baData = [
            'billing_agreement_id' => 'B-5E3253653W103435Y',
            'method_code' => 'paypal_billing_agreement'
        ];

        $paymentMock = $this->importOrderPaymentCommonPart($baData);

        $paymentMock->expects($this->once())
            ->method('getMethodInstance')
            ->willReturn($this->paymentMethodInstanceMock);

        $this->paymentMethodInstanceMock->expects($this->once())
            ->method('getCode')
            ->willReturn($baData['method_code']);

        $this->paymentDataMock->expects($this->once())
            ->method('getMethodInstance')
            ->with($baData['method_code'])
            ->willReturn($this->paymentMethodInstanceMock);

        $this->assertSame($this->model, $this->model->importOrderPayment($paymentMock));
    }

    public function testImportOrderPaymentWithoutMethodCode()
    {
        $baData = [
            'billing_agreement_id' => 'B-5E3253653W103435Y'
        ];

        $paymentMock = $this->importOrderPaymentCommonPart($baData);

        $paymentMock->expects($this->exactly(2))
            ->method('getMethodInstance')
            ->willReturn($this->paymentMethodInstanceMock);

        $this->paymentMethodInstanceMock->expects($this->once())
            ->method('getCode')
            ->willReturn('paypal_billing_agreement');

        $this->assertSame($this->model, $this->model->importOrderPayment($paymentMock));
    }

    /**
     * @param $baData
     * @return \Magento\Payment\Helper\Data|\PHPUnit_Framework_MockObject_MockObject|
     */
    private function importOrderPaymentCommonPart($baData)
    {
        $paymentMock = $this->getMockBuilder('Magento\Sales\Model\Order\Payment')
            ->disableOriginalConstructor()
            ->setMethods(['getBillingAgreementData', 'getMethodInstance', 'getOrder'])
            ->getMock();

        $storeId = null;
        $customerId = 2;

        $order = $this->getMockBuilder('Magento\Sales\Model\Order')
            ->disableOriginalConstructor()
            ->setMethods(['getCustomerId'])
            ->getMock();

        $order->expects($this->once())
            ->method('getCustomerId')
            ->willReturn($customerId);

        $paymentMock->expects($this->once())
            ->method('getOrder')
            ->willReturn($order);

        $paymentMock->expects($this->once())
            ->method('getBillingAgreementData')
            ->willReturn($baData);

        $this->paymentMethodInstanceMock->expects($this->once())
            ->method('setStore')
            ->with($storeId);

        $this->paymentMethodInstanceMock->expects($this->once())
            ->method('getTitle')
            ->willReturn('some title');

        $this->paymentMethodInstanceMock->expects($this->once())
            ->method('getStore')
            ->willReturn($storeId);

        return $paymentMock;
    }

    public function testInitToken()
    {
        $this->initGetMethodInstance();

        $this->paymentMethodInstanceMock->expects($this->once())
            ->method('initBillingAgreementToken')
            ->with($this->model)
            ->willReturn($this->model);

        $url = 'http://dddd';
        $this->model->setRedirectUrl($url);
        $this->assertEquals($url, $this->model->initToken());
    }

    public function testVerifyToken()
    {
        $this->initGetMethodInstance();

        $this->paymentMethodInstanceMock->expects($this->once())
            ->method('getBillingAgreementTokenInfo')
            ->with($this->model)
            ->willReturn($this->model);

        $this->assertEquals($this->model, $this->model->verifyToken());
    }

    private function initGetMethodInstance()
    {
        $this->paymentDataMock->expects($this->once())
            ->method('getMethodInstance')
            ->willReturn($this->paymentMethodInstanceMock);

        $this->paymentMethodInstanceMock->expects($this->once())
            ->method('setStore');
    }
}


text:
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';

import { ExtendedMetadata } from './utils/ExtendedMetadata';
import { AstUtils } from './utils/AstUtils';

const PATH_PARAMETER_POSITIONS: { [key: string]: number[] } = {
    appendFile: [0],
    appendFileSync: [0],
    chmod: [0],
    chmodSync: [0],
    chown: [0],
    chownSync: [0],
    createReadStream: [0],
    createWriteStream: [0],
    exists: [0],
    existsSync: [0],
    lchmod: [0],
    lchmodSync: [0],
    lchown: [0],
    lchownSync: [0],
    link: [0, 1],
    linkSync: [0, 1],
    lstat: [0],
    lstatSync: [0],
    mkdir: [0],
    mkdirSync: [0],
    open: [0],
    openSync: [0],
    readdir: [0],
    readdirSync: [0],
    readFile: [0],
    readFileSync: [0],
    readlink: [0],
    readlinkSync: [0],
    realpath: [0],
    realpathSync: [0],
    rename: [0, 1],
    renameSync: [0, 1],
    rmdir: [0],
    rmdirSync: [0],
    stat: [0],
    statSync: [0],
    symlink: [0, 1],
    symlinkSync: [0, 1],
    truncate: [0],
    truncateSync: [0],
    unlink: [0],
    unlinkSync: [0],
    unwatchFile: [0],
    utimes: [0],
    utimesSync: [0],
    watch: [0],
    watchFile: [0],
    writeFile: [0],
    writeFileSync: [0]
};

export class Rule extends Lint.Rules.AbstractRule {
    public static metadata: ExtendedMetadata = {
        ruleName: 'non-literal-fs-path',
        type: 'functionality',
        description: 'Detect calls to fs functions with a non literal filepath',
        options: null, // tslint:disable-line:no-null-keyword
        optionsDescription: '',
        typescriptOnly: true,
        issueClass: 'SDL',
        issueType: 'Error',
        severity: 'Critical',
        level: 'Mandatory',
        group: 'Security',
        commonWeaknessEnumeration: '22'
    };

    public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
        return this.applyWithFunction(sourceFile, walk);
    }
}

function walk(ctx: Lint.WalkContext<void>) {
    function cb(node: ts.Node): void {
        if (tsutils.isCallExpression(node)) {
            if (AstUtils.getFunctionTarget(node) === 'fs' && node.arguments.length > 0) {
                const functionName = AstUtils.getFunctionName(node);
                const positions = PATH_PARAMETER_POSITIONS[functionName];

                if (positions && node.arguments.length >= positions.length) {
                    positions.forEach(position => {
                        const argument = node.arguments[position];

                        if (!tsutils.isStringLiteral(argument)) {
                            fail(AstUtils.getFunctionName(node), argument);
                        }
                    });
                }
            }
        }

        return ts.forEachChild(node, cb);
    }

    return ts.forEachChild(ctx.sourceFile, cb);

    function fail(functionName: string, argument: ts.Expression): void {
        const start: number = argument.getStart();
        const width: number = argument.getWidth();
        const message: string = `Non-literal (insecure) path passed to fs.${functionName}: ${argument.getText()}`;

        ctx.addFailureAt(start, width, message);
    }
}


text:
<?php

namespace AppBundle\Model\Security;

use AppBundle\Entity\User;
use AppBundle\Model\Response\ResponseTypeInterface;

/**
 * Class JWSModel
 * @package AppBundle\Model\User
 */
class CredentialModel implements ResponseTypeInterface
{
    /**
     * The user that the jws token belongs to
     * @var User
     */
    private $user;

    /**
     * @var AuthTokenModel
     */
    private $tokenModel;

    /**
     * @var AuthTokenModel
     */
    private $refreshTokenModel;

    /**
     * JWSModel constructor.
     * @param User $user
     * @param AuthTokenModel $tokenModel
     * @param AuthTokenModel $refreshTokenModel
     */
    public function __construct(User $user, AuthTokenModel $tokenModel, AuthTokenModel $refreshTokenModel)
    {
        $this->user = $user;
        $this->tokenModel = $tokenModel;
        $this->refreshTokenModel = $refreshTokenModel;
    }

    /**
     * @return AuthTokenModel
     */
    public function getTokenModel()
    {
        return $this->tokenModel;
    }

    /**
     * @return AuthTokenModel
     */
    public function getRefreshTokenModel()
    {
        return $this->refreshTokenModel;
    }

    /**
     * @return User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Returns the type of response being serialized
     *
     * @return string
     */
    public function getResponseType()
    {
        return 'credentials';
    }

}


text:
from django.core.exceptions import FieldError, MultipleObjectsReturned, ObjectDoesNotExist
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from extras.models import Tag
from utilities.utils import dict_to_filter_params
from .base import BaseModelSerializer

__all__ = (
    'NestedTagSerializer',
    'WritableNestedSerializer',
)

class WritableNestedSerializer(BaseModelSerializer):
    """
    Represents an object related through a ForeignKey field. On write, it accepts a primary key (PK) value or a
    dictionary of attributes which can be used to uniquely identify the related object. This class should be
    subclassed to return a full representation of the related object on read.
    """
    def to_internal_value(self, data):

        if data is None:
            return None

        # Dictionary of related object attributes
        if isinstance(data, dict):
            params = dict_to_filter_params(data)
            queryset = self.Meta.model.objects
            try:
                return queryset.get(**params)
            except ObjectDoesNotExist:
                raise ValidationError(f"Related object not found using the provided attributes: {params}")
            except MultipleObjectsReturned:
                raise ValidationError(f"Multiple objects match the provided attributes: {params}")
            except FieldError as e:
                raise ValidationError(e)

        # Integer PK of related object
        try:
            # Cast as integer in case a PK was mistakenly sent as a string
            pk = int(data)
        except (TypeError, ValueError):
            raise ValidationError(
                f"Related objects must be referenced by numeric ID or by dictionary of attributes. Received an "
                f"unrecognized value: {data}"
            )

        # Look up object by PK
        try:
            return self.Meta.model.objects.get(pk=pk)
        except ObjectDoesNotExist:
            raise ValidationError(f"Related object not found using the provided numeric ID: {pk}")

# Declared here for use by PrimaryModelSerializer, but should be imported from extras.api.nested_serializers
class NestedTagSerializer(WritableNestedSerializer):
    url = serializers.HyperlinkedIdentityField(view_name='extras-api:tag-detail')

    class Meta:
        model = Tag
        fields = ['id', 'url', 'display', 'name', 'slug', 'color']


text:
package org.apereo.portal.security.oauth;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apereo.portal.groups.IEntityGroup;
import org.apereo.portal.groups.IGroupMember;
import org.apereo.portal.security.IPerson;
import org.apereo.portal.services.GroupService;
import org.apereo.portal.soffit.Headers;
import org.apereo.portal.soffit.service.AbstractJwtService;
import org.apereo.portal.soffit.service.JwtEncryptor;
import org.apereo.portal.soffit.service.JwtSignatureAlgorithmFactory;
import org.apereo.portal.url.IAuthUrlCustomizer;
import org.apereo.services.persondir.IPersonAttributeDao;
import org.apereo.services.persondir.IPersonAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

/**
 * Produces OIDC ID Tokens for the OIDC userinfo endpoint. Supports nearly all the Standard Claims
 * as defined by OpenID Connect Core 1.0 (http://openid.net/specs/openid-connect-core-1_0.html).
 *
 * @since 5.1
 */
@Component
public class IdTokenFactory {

    private static final String LIST_SEPARATOR = ",";

    @Value("${portal.protocol.server.context}")
    private String issuer;

    @Autowired private IPersonAttributeDao personAttributeDao;

    @Value(
            "${"
                    + AbstractJwtService.SIGNATURE_KEY_PROPERTY
                    + ":"
                    + AbstractJwtService.DEFAULT_SIGNATURE_KEY
                    + "}")
    private String signatureKey;

    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.timeoutSeconds:300}")
    private long timeoutSeconds;

    /*
     * OpenID Standard Claims
     *
     * Mapping to uPortal user attributes;  defaults (where specified) are based on the Lightweight
     * Directory Access Protocol (LDAP): Schema for User Applications
     * (https://tools.ietf.org/html/rfc4519) and the eduPerson Object Class Specification
     * (http://software.internet2.edu/eduperson/internet2-mace-dir-eduperson-201310.html), except
     * 'username' and 'displayName' (which are a uPortal standards).
     */

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.name:displayName}")
    private String nameAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.given_name:givenName}")
    private String givenNameAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.family_name:sn}")
    private String familyNameAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.middle_name:}")
    private String middleNameAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.nickname:eduPersonNickname}")
    private String nicknameAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.preferred_username:}")
    private String preferredUsernameAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.profile:}")
    private String profileAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.picture:}")
    private String pictureAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.website:}")
    private String websiteAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.email:mail}")
    private String emailAttr;

    /** JSON data type 'boolean' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.email_verified:}")
    private String emailVerifiedAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.gender:}")
    private String genderAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.birthdate:}")
    private String birthdateAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.zoneinfo:}")
    private String zoneinfoAttr;

    /** JSON data type 'string' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.locale:}")
    private String localeAttr;

    /** JSON data type 'string' */
    @Value(
            "${org.apereo.portal.security.oauth.IdTokenFactory.mapping.phone_number:telephoneNumber}")
    private String phoneNumberAttr;

    /** JSON data type 'boolean' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.phone_number_verified:}")
    private String phoneNumberVerifiedAttr;

    /*
     * NB:  The 'address' claim requires additional complexity b/c it's type is JSON object.  In
     * light of that, and because most portals don't have address info in the user attributes
     * collection, we'll skip it (for now),
     */

    /** JSON data type 'number' */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.mapping.updated_at:}")
    private String updatedAtAttributeName;

    /*
     * uPortal Custom Claims
     */

    /**
     * The custom claim groups may contain some or all of the user's group
     * affiliations. Use the Spring property
     * org.apereo.portal.security.oauth.IdTokenFactory.groups.whitelist to control which
     * portal groups are included in the claim.
     */
    @Value(
            "${org.apereo.portal.security.oauth.IdTokenFactory.groups.whitelist:Students,Faculty,Staff,Portal Administrators}")
    private String groupsWhitelistProperty;

    /**
     * Additional user attributes in the portal may be included in the ID Token as custom claims.
     * Use the Spring property org.apereo.portal.security.oauth.IdTokenFactory.customClaims
     *  to specify which additional attributes to include. The claim name will always be the
     * same as the attribute name. The JSON type of a custom claim will be inferred from it's value.
     */
    @Value("${org.apereo.portal.security.oauth.IdTokenFactory.customClaims:}")
    private String customClaimsProperty;

    private Set<ClaimMapping> mappings;

    private Set<String> groupsWhitelist;

    private Set<String> customClaims;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired private JwtSignatureAlgorithmFactory algorithmFactory;

    @Autowired private JwtEncryptor jwtEncryptor;

    @Autowired
    @Qualifier("oidcIssuerUrlCustomizer")
    private IAuthUrlCustomizer issuerUrlCustomizer;

    @PostConstruct
    public void init() {

        // Mappings for Standard Claims
        final Set<ClaimMapping> set = new HashSet<>();
        set.add(new ClaimMapping("name", nameAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("given_name", givenNameAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("family_name", familyNameAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("middle_name", middleNameAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("nickname", nicknameAttr, DataTypeConverter.STRING));
        set.add(
                new ClaimMapping(
                        "preferred_username", preferredUsernameAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("profile", profileAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("picture", pictureAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("website", websiteAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("email", emailAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("email_verified", emailVerifiedAttr, DataTypeConverter.BOOLEAN));
        set.add(new ClaimMapping("gender", genderAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("birthdate", birthdateAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("zoneinfo", zoneinfoAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("locale", localeAttr, DataTypeConverter.STRING));
        set.add(new ClaimMapping("phone_number", phoneNumberAttr, DataTypeConverter.STRING));
        set.add(
                new ClaimMapping(
                        "phone_number_verified",
                        phoneNumberVerifiedAttr,
                        DataTypeConverter.BOOLEAN));
        set.add(new ClaimMapping("updated_at", updatedAtAttributeName, DataTypeConverter.NUMBER));
        mappings = Collections.unmodifiableSet(set);

        if (logger.isInfoEnabled()) {
            final StringBuilder msg = new StringBuilder();
            msg.append("Using the following mappings for OIDC Standard Claims:");
            set.forEach(mapping -> msg.append("\n\t").append(mapping));
            logger.info(msg.toString());
        }

        // Portal Groups ('groups' custom claim)
        groupsWhitelist =
                Collections.unmodifiableSet(
                        Arrays.stream(groupsWhitelistProperty.split(LIST_SEPARATOR))
                                .map(String::trim)
                                .filter(item -> item.length() != 0)
                                .collect(Collectors.toSet()));
        logger.info(
                "Using the following portal groups to build the custom 'groups' claim:  {}",
                groupsWhitelist);

        // Other Custom Claims (a.k.a user attributes)
        customClaims =
                Collections.unmodifiableSet(
                        Arrays.stream(customClaimsProperty.split(LIST_SEPARATOR))
                                .map(String::trim)
                                .filter(item -> item.length() != 0)
                                .collect(Collectors.toSet()));
        logger.info("Using the following custom claims:  {}", customClaims);
    }

    public String createUserInfo(HttpServletRequest request, String username) {
        return this.createUserInfo(request, username, null, null);
    }

    public String createUserInfo(
            HttpServletRequest request,
            String username,
            Set<String> claimsToInclude,
            Set<String> groupsToInclude) {

        final Date now = new Date();
        final Date expires = new Date(now.getTime() + (timeoutSeconds * 1000L));

        final String iss = issuerUrlCustomizer.customizeUrl(request, issuer);

        final JwtBuilder builder =
                Jwts.builder()
                        .setIssuer(iss)
                        .setSubject(username)
                        .setAudience(iss)
                        .setExpiration(expires)
                        .setIssuedAt(now);

        final IPersonAttributes person = personAttributeDao.getPerson(username);

        // Attribute mappings
        mappings.stream()
                .filter(mapping -> includeClaim(mapping.getClaimName(), claimsToInclude))
                .forEach(
                        item -> {
                            final Object value = person.getAttributeValue(item.getAttributeName());
                            if (value != null) {
                                builder.claim(
                                        item.getClaimName(),
                                        item.getDataTypeConverter().convert(value));
                            }
                        });

        // Groups
        final List<String> groups = new ArrayList<>();
        final IGroupMember groupMember = GroupService.getGroupMember(username, IPerson.class);
        if (groupMember != null) {
            final Set<IEntityGroup> ancestors = groupMember.getAncestorGroups();
            for (IEntityGroup g : ancestors) {
                if (includeGroup(g, groupsToInclude)) {
                    groups.add(g.getName());
                }
            }
        }
        if (!groups.isEmpty()) {
            /*
             * If a Claim is not returned, that Claim Name SHOULD be omitted from the JSON object
             * representing the Claims; it SHOULD NOT be present with a null or empty string value.
             */
            builder.claim("groups", groups);
        }

        // Default custom claims defined by uPortal.properties
        customClaims.stream()
                .filter(claimName -> includeClaim(claimName, claimsToInclude))
                .map(
                        attributeName ->
                                new CustomClaim(
                                        attributeName, person.getAttributeValues(attributeName)))
                .filter(claim -> claim.getClaimValue() != null)
                .forEach(claim -> builder.claim(claim.getClaimName(), claim.getClaimValue()));

        final String rslt =
                builder.signWith(algorithmFactory.getAlgorithm(), signatureKey).compact();

        logger.debug("Produced the following JWT for username='{}':  {}", username, rslt);

        return rslt;
    }

    /**
     * Convenience method for obtaining an OIDC Id Token from a request, if present.
     *
     * @return A fully-parsed JWT if a valid bearer token is present in the Authorization
     *      header, otherwise null
     */
    public Jws<Claims> getUserInfo(HttpServletRequest request) {
        final String bearerToken = getBearerToken(request);
        return StringUtils.isNotBlank(bearerToken) ? parseBearerToken(bearerToken) : null;
    }

    public String getBearerToken(HttpServletRequest request) {
        final String authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
        logger.debug("{} header value:  {}", HttpHeaders.AUTHORIZATION, authorization);
        return StringUtils.isNotBlank(authorization)
                        && authorization.length() > Headers.BEARER_TOKEN_PREFIX.length()
                ? authorization.substring(Headers.BEARER_TOKEN_PREFIX.length())
                : null;
    }

    public Jws<Claims> parseBearerToken(String bearerToken) {
        try {
            final String jwt = jwtEncryptor.decryptIfInvalidFormat(bearerToken);
            return Jwts.parser().setSigningKey(signatureKey).parseClaimsJws(jwt);
        } catch (Exception e) {
            logger.warn("Unsupported bearerToken:  {}", bearerToken);
            logger.debug("Stack trace", e);
        }
        return null;
    }

    private boolean includeClaim(String claimName, Set<String> claimsToInclude) {
        boolean rslt = true; // default
        if (claimsToInclude != null && !claimsToInclude.contains(claimName)) {
            /*
             * This group is included in the deployed configuration,
             * but is not wanted by the REST request.
             */
            rslt = false;
        }
        return rslt;
    }

    private boolean includeGroup(IEntityGroup group, Set<String> groupsToInclude) {
        boolean rslt = groupsWhitelist.contains(group.getName()); // default
        if (rslt && groupsToInclude != null && !groupsToInclude.contains(group.getName())) {
            /*
             * This group is included in the deployed configuration,
             * but is not wanted by the REST request.
             */
            rslt = false;
        }
        return rslt;
    }

    /*
     * Nested Types
     */

    enum DataTypeConverter {
        STRING {
            @Override
            Object convert(Object inpt) {
                return inpt.toString();
            }
        },

        BOOLEAN {
            @Override
            Object convert(Object inpt) {
                return Boolean.valueOf(inpt.toString());
            }
        },

        NUMBER {
            @Override
            Object convert(Object inpt) {
                return new BigDecimal(inpt.toString());
            }
        };

        abstract Object convert(Object inpt);
    }

    private static final class ClaimMapping {

        private final String claimName;
        private final String attributeName;
        private final DataTypeConverter dataTypeConverter;

        public ClaimMapping(
                String claimName, String attributeName, DataTypeConverter dataTypeConverter) {
            this.claimName = claimName;
            this.attributeName = attributeName;
            this.dataTypeConverter = dataTypeConverter;
        }

        public String getClaimName() {
            return claimName;
        }

        public String getAttributeName() {
            return attributeName;
        }

        public DataTypeConverter getDataTypeConverter() {
            return dataTypeConverter;
        }

        @Override
        public String toString() {
            return "ClaimMapping{"
                    + "claimName='"
                    + claimName
                    + '\''
                    + ", attributeName='"
                    + attributeName
                    + '\''
                    + ", dataTypeConverter="
                    + dataTypeConverter
                    + '}';
        }
    }

    private static final class CustomClaim {

        private final String claimName;
        private final Object claimValue;

        public CustomClaim(String claimName, Object claimValue) {
            this.claimName = claimName;
            this.claimValue = claimValue;
        }

        public String getClaimName() {
            return claimName;
        }

        public Object getClaimValue() {
            return claimValue;
        }
    }
}


text:
/** @module utils */
"use strict";
var request = require('request'),
    _ = require('underscore'),
    heimdallrClient = require('./heimdallr-client.js');

var options = {
        encoding: 'utf-8',
        headers: {
            'content-type': 'application/json',
            'authorization': 'Token '
        },
        json: true
    },
    privateOptions = {
        authSource: 'heimdallr',
        url: heimdallrClient.Consumer.prototype.url
    },
    finishedFn = function emptyFn() { return undefined; },
    results = {},
    numCallbacks;

function setPrivate(options) {
    _.extend(privateOptions, options);
}

function processResults(err, res, body) {
    // this is the UUID of the provider
    results[this] = {
        error: err,
        response: res,
        body: body
    };
    numCallbacks--;

    if (numCallbacks === 0) {
        finishedFn(results);
    }
}

/**
 * This is a convenience function for posting packet schemas to the
 * Heimdallr server. The top-level keys for packetSchemas should be
 * one or more of 'event', 'sensor', and 'control'. The value of
 * these fields should be an object whose keys are the names of the
 * packet subtypes and whose values are valid JSON schema.
 *
 * @func postSchemas
 * @arg {string} token - Authentication token.
 * @arg {string|array} uuids - A single UUID or a list of UUIDs of the
 *    provider(s) that the packet schemas are for.
 * @arg {object} packetSchemas - The packet schemas.
 * @arg {function} [fn] - Callback to run once all the POSTS have
 *    completed. It will be passed an object with keys that are the
 *    provider UUIDs and values that are the response from the POST.
 */
function postSchemas(token, uuids, packetSchemas, fn) {
    var i,
        packetType;

    options.headers.authorization += token;
    if (!(uuids instanceof Array)) {
        uuids = [uuids];
    }
    packetSchemas = packetSchemas || {};
    if (_.isFunction(fn)) {
        finishedFn = fn;
    }

    // Calculate the number of times the processResults should be called
    numCallbacks = uuids.length * Object.keys(packetSchemas).length;

    options.qs = {authSource: privateOptions.authSource};
    for (i = 0; i < uuids.length; i++) {
        options.url = privateOptions.url + '/api/v1/provider/' +
            uuids[i] + '/subtype-schemas';

        for (packetType in packetSchemas) {
            if (packetSchemas.hasOwnProperty(packetType)) {
                options.body = {
                    packetType: packetType,
                    subtypeSchemas: packetSchemas[packetType]
                };
                request.post(options, processResults.bind(uuids[i]));
            }
        }
    }
}

module.exports = {
    postSchemas: postSchemas,
    setPrivate: setPrivate
};


text:
package org.ferrit.core.uri

/**
 * Stores all unvisited URIs, queued for fetching.
 */
trait Frontier {

  /**
   * Yes you guessed it, the size of the frontier. How many items are still to be fetched?
   */
  def size: Int

  /**
   * Add a fetch job to the frontier.
   */
  def enqueue(f: FetchJob): Int

  /**
   * Remove a job from the frontier if there are any left.
   */
  def dequeue: Option[FetchJob]

}


text:
using System;
using KaVE.JetBrains.Annotations;

namespace KaVE.Commons.Tests.ExternalSerializationTests
{
    public class TestCase
    {
        [NotNull]
        public readonly string Name;

        [NotNull]
        public readonly Type SerializedType;

        [NotNull]
        public readonly string Input;

        [NotNull]
        public readonly string ExpectedCompact;

        [CanBeNull]
        public readonly string ExpectedFormatted;

        public TestCase([NotNull] string name,
            [NotNull] Type serializedType,
            [NotNull] string input,
            [NotNull] string expectedCompact,
            [CanBeNull] string expectedFormatted)
        {
            Name = name;
            SerializedType = serializedType;
            Input = input;
            ExpectedCompact = expectedCompact;
            ExpectedFormatted = expectedFormatted;
        }

        public override string ToString()
        {
            return string.Format("[TestCase: {0}]", Name);
        }
    }
}


text:
require 'multi_json'

module Billy
  class ProxyRequestStub
    def initialize(url, options = {})
      @options = { method: :get }.merge(options)
      @method = @options[:method].to_s.upcase
      @url = url
      @response = { code: 204, headers: {}, text: '' }
    end

    def and_return(response)
      @response = response
      self
    end

    def call(params, headers, body)
      if @response.respond_to?(:call)
        res = @response.call(params, headers, body)
      else
        res = @response
      end

      code = res[:code] || 200

      headers = res[:headers] || {}
      headers['Content-Type'] = res[:content_type] if res[:content_type]

      if res[:json]
        headers = { 'Content-Type' => 'application/json' }.merge(headers)
        body = MultiJson.dump(res[:json])
      elsif res[:jsonp]
        headers = { 'Content-Type' => 'application/javascript' }.merge(headers)
        if res[:callback]
          callback = res[:callback]
        elsif res[:callback_param]
          callback = params[res[:callback_param]][0]
        else
          callback = params['callback'][0]
        end
        body = "#{callback}(#{MultiJson.dump(res[:jsonp])})"
      elsif res[:text]
        headers = { 'Content-Type' => 'text/plain' }.merge(headers)
        body = res[:text]
      elsif res[:redirect_to]
        code = 302
        headers = { 'Location' => res[:redirect_to] }
      else
        body = res[:body]
      end

      [code, headers, body]
    end

    def matches?(method, url)
      if method == @method
        if @url.is_a?(Regexp)
          url.match(@url)
        else
          Billy.config.strip_query_params ? (url.split('?')[0] == @url) : (url == @url)
        end
      end
    end
  end
end


text:
[![build status](https://github.com/e-karge/toolbelt/actions/workflows/build.yaml/badge.svg)](https://github.com/e-karge/toolbelt/actions/workflows/build.yaml)
[![image size](https://img.shields.io/docker/image-size/toolbelt/oathtool/latest?logo=docker)](https://hub.docker.com/repository/docker/toolbelt/oathtool)

[](https://github.com/e-karge/toolbelt/tree/master/oathtool)
[](https://hub.docker.com/repository/docker/toolbelt/oathtool)

oathtool
========

Docker image for `oathtool` from the
[OATH Toolkit](https://www.nongnu.org/oath-toolkit/). To use run

```bash
docker run --rm -ti toolbelt/oathtool
```

See [here](http://www.nongnu.org/oath-toolkit/oathtool.1.html) for invocation
details.


text:
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!--
@group Paper Elements
@class paper-menu-button-transition
@extends core-transition-css
-->
<link href="../polymer/polymer.html" rel="import">
<link href="../core-transition/core-transition-css.html" rel="import">
<link href="../core-animation/web-animations.html" rel="import">

<polymer-element name="paper-menu-button-transition" extends="core-transition-css" attributes="duration transformOrigin">
  <template>
    <link no-shim href="paper-menu-button-transition.css" rel="stylesheet">
  </template>
  <script>
    Polymer('paper-menu-button-transition', {

      baseClass: 'paper-menu-button-transition',
      revealedClass: 'paper-menu-button-revealed',
      openedClass: 'paper-menu-button-opened',
      closedClass: 'paper-menu-button-closed',

      duration: 500,

      setup: function(node) {
        this.super(arguments);

        var bg = node.querySelector('.paper-menu-button-overlay-bg');
        bg.style.transformOrigin = this.transformOrigin;
        bg.style.webkitTransformOrigin = this.transformOrigin;
      },

      transitionOpened: function(node, opened) {
        this.super(arguments);

        if (opened) {
          if (this.player) {
            this.player.cancel();
          }

          var anims = [];

          var ink = node.querySelector('.paper-menu-button-overlay-ink');
          var offset = 40 / Math.max(node.cachedSize.width, node.cachedSize.height);
          anims.push(new Animation(ink, [{
            'opacity': 0.9,
            'transform': 'scale(0)',
          }, {
            'opacity': 0.9,
            'transform': 'scale(1)'
          }], {
            duration: this.duration * offset
          }));

          var bg = node.querySelector('.paper-menu-button-overlay-bg');
          anims.push(new Animation(bg, [{
            'opacity': 0.9,
            'transform': 'scale(' + 40 / node.cachedSize.width + ',' + 40 / node.cachedSize.height + ')',
          }, {
            'opacity': 1,
            'transform': 'scale(0.95, 0.5)'
          }, {
            'opacity': 1,
            'transform': 'scale(1, 1)'
          }], {
            delay: this.duration * offset,
            duration: this.duration * (1 - offset),
            fill: 'forwards'
          }));

          var nodes = window.ShadowDOMPolyfill ? Platform.queryAllShadows(node.querySelector('core-menu'), 'content').getDistributedNodes() : node.querySelector('core-menu::shadow content').getDistributedNodes().array();
          var items = nodes.filter(function(n) {
            return n.nodeType === Node.ELEMENT_NODE;
          });
          var itemDelay = offset + (1 - offset) / 2;
          var itemDuration = this.duration * (1 - itemDelay) / items.length;
          items.forEach(function(item, i) {
            anims.push(new Animation(item, [{
              'opacity': 0
            }, {
              'opacity': 1
            }], {
              delay: this.duration * itemDelay + itemDuration * i,
              duration: itemDuration,
              fill: 'both'
            }));
          }.bind(this));

          var shadow = node.querySelector('paper-shadow');
          anims.push(new Animation(shadow, function(t, target) {
            if (t > offset * 2 && shadow.z === 0) {
              shadow.z = 1
            }
          }, {
            duration: this.duration
          }));

          var group = new AnimationGroup(anims, {
            easing: 'cubic-bezier(0.4, 0, 0.2, 1)'
          });
          this.player = document.timeline.play(group);
        }
      },

    });
  </script>
</polymer-element>

<paper-menu-button-transition id="paper-menu-button-transition-top-left" transformOrigin="0% 0%"></paper-menu-button-transition>
<paper-menu-button-transition id="paper-menu-button-transition-top-right" transformOrigin="100% 0%"></paper-menu-button-transition>
<paper-menu-button-transition id="paper-menu-button-transition-top-right-slow" transformOrigin="100% 0%" duration="10000"></paper-menu-button-transition>


text:
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.UI;

public enum MenuTypes
{
	None,
	GameOver,
	Pause,
	Info,
    Settings,
    Credits
}

public class MenuManager : Singleton<MenuManager>
{
	[SerializeField]
	RectTransform canvasRectTransform;
	public Rect CanvasRect
	{
		get
		{
			return canvasRectTransform.rect;
		}
	}

	Dictionary<MenuTypes, UIMenu> menus = new Dictionary<MenuTypes, UIMenu>();

    public List<TileButton> paths;

	[SerializeField]
	RectTransform boardRect, boardPaddingRect;
	[SerializeField]
	Vector2 boardPadding;

	[SerializeField]
	float zoomSpeed, mouseWheelSpeed, zoomMax;
	float zoomMin;

	public GameObject loadingSpinner, playerInputBlock;

	[SerializeField]
	Button playerPassBtn, playerForfeitBtn, playerRetryBtn, fastForwardBtn;

    const string k_IS_NEW_PLAYER = "isNewPlayer";
	const string k_IS_ENGAGED = "isEngaged";

    [SerializeField]
    Text blockNumberLabel;

	#region Initialization
	// Use this for initialization
	protected MenuManager()
	{
		// guarantee this will be always a singleton only - can't use the constructor!
	}
	#endregion

	void Start()
	{
		Application.targetFrameRate = 30;

        if (PlayerPrefs.GetInt(k_IS_NEW_PLAYER, -1) < 0)
        {
            ShowMenu(MenuTypes.Info);
            PlayerPrefs.SetInt(k_IS_NEW_PLAYER, 0);
        }

        for(int i = 0; i < transform.childCount; ++i)
        {
            UIMenu childScreen = transform.GetChild(i).GetComponent<UIMenu>();
            if (childScreen != null)
            {
                AddScreen(childScreen);
            }
        }
	}

	public string BuildVersionStr;

	/// <summary>
	/// Adds a UI screen to the stack and deletes its scene
	/// </summary>
	/// <param name="screen">Screen.</param>
	void AddScreen(UIMenu screen)
	{
		//Hide the screen
		screen.gameObject.SetActive(false);

		//Add this screen to the screens dictionary
		if(!menus.ContainsKey(screen.type))
		{
			menus.Add(screen.type, screen);
		}
		else
		{
			Debug.LogError("Duplicate screens: " + screen.type);
		}
	}

	void Update()
	{
		#region Zooming
		if(Input.touchCount >= 2)
		{
			// Store both touches.
			Touch touchZero = Input.GetTouch(0);
			Touch touchOne = Input.GetTouch(1);

			// Find the position in the previous frame of each touch.
			Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
			Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

			// Find the magnitude of the vector (the distance) between the touches in each frame.
			float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
			float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

			// Find the difference in the distances between each frame.
			float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

			SetZoom(deltaMagnitudeDiff, zoomSpeed);
		}

		#if UNITY_EDITOR
		if(Input.mouseScrollDelta.y != 0)
		{
			SetZoom(-Input.mouseScrollDelta.y, mouseWheelSpeed);
		}
		#endif
		#endregion

		#region Game Buttons Activation Status
		playerPassBtn.interactable = GameManager.Instance.IsPlayerTurn();

		playerForfeitBtn.interactable = GameManager.Instance.IsGameActive();

		playerInputBlock.SetActive(!GameManager.Instance.IsPlayerTurn());

        playerRetryBtn.interactable = GameManager.Instance.IsGameActive();
		#endregion
	}

	void SetZoom(float delta, float zoomSpeed)
	{
		Vector3 rectScale = boardPaddingRect.localScale;

		//	X
		rectScale.x -= delta * zoomSpeed;
		if(rectScale.x > zoomMax)
		{
			rectScale.x = zoomMax;
		}
		else if(rectScale.x < zoomMin)
		{
			rectScale.x = zoomMin;
		}

		//	Y
		rectScale.y -= delta * zoomSpeed;
		if(rectScale.y > zoomMax)
		{
			rectScale.y = zoomMax;
		}
		else if(rectScale.y < zoomMin)
		{
			rectScale.y = zoomMin;
		}

		boardPaddingRect.localScale = rectScale;
	}

	public void ShowMenu(int type)
	{
		ShowMenu((MenuTypes)type);
	}
	public void ShowMenu(MenuTypes type)
	{
		menus[type].SetVisible(true);
	}

    public void HideMenu(MenuTypes type)
    {
        menus[type].SetVisible(false);
    }

	public void ResizeGameBoard(Vector2 boardSize)
	{
		boardRect.offsetMax = boardSize;
		boardRect.offsetMin = -boardSize;

		boardPaddingRect.offsetMax = boardSize + boardPadding;
		boardPaddingRect.offsetMin = -boardSize - boardPadding;

		zoomMin = Mathf.Min(CanvasRect.width / boardPaddingRect.rect.width, CanvasRect.height / boardPaddingRect.rect.height);
	}

	public void SetZoomSpeed(float value)
	{
		zoomSpeed = value;
	}

	public void OpenFeedback()
	{
		Application.OpenURL("https://github.com/patches34/TheHunt/issues/new");
	}

	public void SetFastForwardBtnState()
	{
        fastForwardBtn.GetComponent<Animator>().SetBool("IsActive", GameManager.Instance.IsFastForward);
	}

    public void SetBlockTileCount(int count)
    {
        if (GameManager.Instance.MaxPlayerBlocks == 0)
        {
            blockNumberLabel.text = "unlimited";
        }
        else
        {
            blockNumberLabel.text = (GameManager.Instance.MaxPlayerBlocks - count).ToString();
        }
    }
}


text:
import argparse
import math
from datetime import datetime

from ldsconf import studyplan
from ldsconf.studyplan import get_default_dates

def valid_date(s):
    try:
        return datetime.strptime(s, "%Y-%m-%d")
    except ValueError:
        msg = "Not a valid date: '{0}'.".format(s)
        raise argparse.ArgumentTypeError(msg)

def main():
    parser = argparse.ArgumentParser()
    default_start_date, default_end_date = get_default_dates()
    parser.add_argument('-s', '--start_date', help="Start date for the study plan - format YYYY-MM-DD",
                        default=default_start_date, type=valid_date)
    parser.add_argument('-e', '--end_date', help="End date for the study plan - format YYYY-MM-DD",
                        default=default_end_date, type=valid_date)
    parser.add_argument('-n', '--study_periods', help="Number of study periods", default=None, type=int)
    args = parser.parse_args()

    # Validation
    if args.start_date >= args.end_date:
        raise Exception("Start date must be before end date")
    number_of_days = (args.end_date - args.start_date).days
    number_of_talks = studyplan.get_number_of_talks_in_most_recent_conference()
    maximum_study_periods = math.ceil(number_of_days / float(number_of_talks))
    study_periods = args.study_periods
    while study_periods is None:
        try:
            study_periods = int(raw_input("How many study periods would like? (1-%d)" % maximum_study_periods))
        except ValueError:
            print "Please enter a number."

    if study_periods > maximum_study_periods or study_periods <= 0:
        raise Exception("Number of study periods must be in range 1-%d" % maximum_study_periods)

    result = studyplan.generate_study_plan(args.start_date, args.end_date, study_periods)
    for plan in result:
        print "Conference Plan"
        for date, talk in plan:
            print "  %s -- %s, %s (%s-%s)" % (date.strftime("%m-%d-%Y"), talk.title, talk.author, talk.year, talk.month)

if __name__ == '__main__':
    main()


text:
package com.gshakhn.privatewiki.client

import com.gshakhn.privatewiki.client.components.PrivateWiki
import com.gshakhn.privatewiki.shared.{AuthenticationRequest, AuthenticationResponse}
import org.scalajs.dom
import upickle.default._

import scala.concurrent.Future
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scalatags.JsDom.all._

@JSExportTopLevel("PrivateWikiRenderer")
object PrivateWikiRenderer {
  @JSExport
  def render(): Unit = {
    val mainDiv = div().render
    dom.document.body.appendChild(mainDiv)
    PrivateWiki(ActualClient).renderIntoDOM(mainDiv)
  }
}

object ActualClient extends Client {
  @SuppressWarnings(Array("MethodNames"))
  def authenticateBinder(request: AuthenticationRequest): Future[AuthenticationResponse] = {
    dom.ext.Ajax.post(
      url = "/authenticateBinder",
      data = write(request)
    ).map(response => read[AuthenticationResponse](response.responseText))
  }
}