text:
from __future__ import division

import numpy as np
import scipy as sp
from scipy import sparse
import pandas as pd
from scipy.stats import gamma
import matplotlib.pyplot as plt
from six.moves import range, StringIO, cPickle

class HRFModel(object):
    """Abstract class definition for HRF Models."""
    def __init__(self):
        raise NotImplementedError

    def _default_frametimes(self, ntp):
        """Generate an array of frametimes based on HRF properties."""
        orig_ntp = ntp / self._oversampling
        frametimes = np.arange(0, orig_ntp * self._tr,
                               self._tr / self._oversampling)
        return frametimes

    @property
    def kernel(self):
        """Evaluate the kernel at timepoints."""
        raise NotImplementedError

    @property
    def impulse_response(self):
        """Return the impulse response function for a given HRF"""
        hires_timepoints = self._timepoints
        impulse = pd.Series(np.zeros_like(hires_timepoints),
                            index=hires_timepoints,
                            name="Impulse response")
        impulse.loc[0] = 1
        hires_response = self.convolve(impulse)

        out_response = pd.DataFrame(index=self._sampled_timepoints,
                                    columns=hires_response.columns,
                                    dtype=np.float)
        for col, col_data in hires_response.iteritems():
            resampler = sp.interpolate.interp1d(hires_timepoints,
                                                col_data,
                                                kind="nearest")
            out_response[col] = resampler(self._sampled_timepoints)

        return out_response

    def convolve(self, data):
        """Convolve the kernel with some data."""
        raise NotImplementedError

class IdentityHRF(object):
    """HRF that does not transform the data. Mostly useful for testing."""
    def convolve(self, data, frametimes, name):
        return pd.DataFrame({name: data}, index=frametimes)

class GammaDifferenceHRF(HRFModel):
    """Canonical difference of gamma variates HRF model."""
    def __init__(self, temporal_deriv=False, tr=2, oversampling=16,
                 kernel_secs=32, pos_shape=6, pos_scale=1,
                 neg_shape=16, neg_scale=1, ratio=1 / 6):
        """Create the HRF object with FSL parameters as default."""
        self._rv_pos = gamma(pos_shape, scale=pos_scale)
        self._rv_neg = gamma(neg_shape, scale=neg_scale)
        self._tr = tr
        self._oversampling = oversampling
        dt = tr / oversampling
        self._timepoints = np.arange(0, kernel_secs, dt, np.float)
        self._sampled_timepoints = np.arange(0, kernel_secs, tr,
                                             np.float) + (tr * .5)
        self._kernel_secs = kernel_secs
        self._temporal_deriv = temporal_deriv
        self._ratio = ratio

    @property
    def kernel(self):
        """Evaluate the kernel at timepoints, maybe with derivative."""
        y = self._rv_pos.pdf(self._timepoints)
        y -= self._ratio * self._rv_neg.pdf(self._timepoints)
        y /= y.sum()

        if self._temporal_deriv:
            dy = np.diff(y)
            dy = np.concatenate([[0], dy])
            scale = np.sqrt(np.square(y).sum() / np.square(dy).sum())
            dy *= scale
            y = np.c_[y, dy]
        else:
            y = np.c_[y]

        return y

    def convolve(self, data, frametimes=None, name=None):
        """Convolve the kernel with some data.

        Parameters
        ----------
        data : Series or 1d array
            Data to convolve.
        frametimes : Series or 1d array, optional
            Timepoints corresponding to data - if None, assume
            data is sampled with same TR and oversampling as kernel.
        name : string
            Name to associate with data if not passing Series object.

        Returns
        -------
        out : DataFrame
            n_cols depends on whether kernel has temporal derivative.

        """
        ntp = len(data)

        # Without frametimes, assume data and kernel have same sampling
        if frametimes is None:
            frametimes = self._default_frametimes(ntp)

        # Get the output name for this condition
        if name is None:
            try:
                name = data.name
            except AttributeError:
                name = "event"
        cols = [name]

        # Obtain the current kernel and set up the output
        kernel = self.kernel.T
        out = np.empty((ntp, len(kernel)))

        # Do the convolution
        if self._temporal_deriv:
            cols.append(name + "_deriv")
            main, deriv = kernel
            out[:, 0] = np.convolve(data, main)[:ntp]
            out[:, 1] = np.convolve(data, deriv)[:ntp]
        else:
            out[:, 0] = np.convolve(data, kernel.ravel())[:ntp]

        # Build the output DataFrame
        out = pd.DataFrame(out, columns=cols, index=frametimes)
        return out

class FIR(HRFModel):
    """Finite Impule Response HRF model."""
    def __init__(self, tr=2, nbasis=12, offset=0):
        """Create the HRF object.

        tr : float
            Time resolution the data are sampled at.
        nbasis : int
            Number of basis functions (resulting array will be ntp x nbasis).
        offset: float
            Time (in tr units) of the first basis function relative to the
            event onset in the input data.
        """
        self._tr = tr
        self._nbasis = nbasis
        self._offset = offset
        self._oversampling = 1
        self._timepoints = np.arange(0, tr * nbasis, tr)
        self._sampled_timepoints = self._timepoints
        self._kernel_secs = tr * nbasis

    def convolve(self, data, frametimes=None, name=None):
        """'Convolve' the kernel with some data.

        Parameters
        ----------
        data : Series or 1d array
            Data to 'convolve'.
        frametimes : Series or 1d array, optional
            Timepoints corresponding to data - if None, assume
            data is sampled with same TR and oversampling as kernel.
        name : string
            Name to associate with data if not passing Series object.

        Returns
        -------
        out : DataFrame
            Shape of output is len(data) x nbasis.

        """
        ntp = len(data)

        # Without frametimes, assume data and kernel have same sampling
        if frametimes is None:
            frametimes = self._default_frametimes(ntp)

        # Get the basis times
        tr, offset, nbasis = self._tr, self._offset, self._nbasis
        basis_points = np.arange(offset, offset + nbasis)

        # Get the output names for this condition
        if name is None:
            try:
                name = data.name
            except AttributeError:
                name = "event"

        cols = ["{}_{:02d}".format(name, p) for p in range(nbasis)]

        # Pad the data array to avoid rollover
        data = np.concatenate([np.zeros(nbasis), data, np.zeros(nbasis)])

        # Build the matrix of delta functions
        out = pd.DataFrame(0, index=frametimes, columns=cols, dtype=np.float)
        for point, name in zip(basis_points, cols):
            rolled_data = np.roll(data, point)
            out.loc[:, name] = rolled_data[nbasis:-nbasis]

        return out

class DesignMatrix(object):
    """fMRI-specific design matrix object.

    This class creates a design matrix that is most directly consistent with
    FSL; i.e., the design is filtered with the gaussian running-line approach
    implemented by fslmaths, and the columns are de-meaned by default (so there
    is no constant regressor in the final model).

    Note that only the regressors resulting from the passed design are
    high-pass filtered. If your other regressors of interest, or confound
    regressors must be filtered, it is up to you to filter them on your own.

    The matrix is represented by a Pandas DataFrame with the ev names in the
    columns and the frametimes in the index. In addition to the full design
    matrix, this object also exposes views on some subsets of the data:

    - main_submatrix
        The condition evs that are created from the experimental design
        along with any continuous regressors or interest. If the HRF
        model includes a temporal derivative, those columns are also
        included in this submatrix.
    - condition_submatrix
        Only the condition ev columns.
    - confound_submatrix
        The continuous evs considered representing nuisance variables.
    - artifact_submatrix
        The set of indicator vectors used to censor individual frames
        from the model.

    In the case where one of these components does not exist in the
    matrix (e.g., no frames had artifacts), these attributes are `None`.

    """
    def __init__(self, design=None, hrf_model=None, ntp=None, regressors=None,
                 confounds=None, artifacts=None, condition_names=None,
                 confound_pca=False, tr=2, hpf_cutoff=128, hpf_kernel=None,
                 oversampling=16, demean=True):
        """Initialize the design matrix object.

        Parameters
        ----------
        design : DataFrame
            At a minimum, this dataframe must have `condition` and `onset`
            columns. the `duration` and `value` (aka amplitude) of each element
            can also be specified, with a default duration of 0 (i.e. an
            impulse) and value of 1. The `onset` and `duration` are specified
            in seconds. If a value is not fiven for `condition names`, the
            resulting design is formed from the sorted unique value in the
            condition column.
        hrf_model : HRFModel class
            This class must specify its own "convolution" semantics.
        ntp : int
            Number of timepoints in the data.
        regressors : array or DataFrame
            Other regressors of interest (e.g. timecourse of data from some
            seed ROI). If a DataFrame, index must have the frametimes and
            match those inferred from the `ntp` and `tr` arguments. The
            columns are de-meaned, but not filtered or otherwise transformed.
        confounds : array or DataFrame
            Similar to `regressors`, but considered to be of no interest
            (e.g., motion parameters).
        artifacts : boolean(esque) array with length equal to `ntp`
            A mask indicating frames that have some kind of artifact. This
            information is transformed into a set of indicator vectors.
        condition_names : list of string
            A subset of the names that can be found in the `condition`
            column of the design dataframe. Can be used to exclude conditions
            from a particualr design or reorder the columns in the resulting
            matrix.
        confound_pca : bool
            If True, transform the confound matrix with PCA (using a maximum
            likelihood method to guess the dimensionality of the data).
        tr : float
            Sampling interval (in seconds) of the data/design.
        hpf_cutoff : float
            Filter cutoff (in seconds), or None to skip the filter.
        hpf_kernel : ntp x ntp array
            Precomputed matrix to implement the highpass filter; overrides the
            hpf_cutoff (i.e. it is not checked if they match).
        oversampling : float
            Construction of the condition evs and convolution
            are performed on high-resolution data with this oversampling.
        demean : bool
            If True, regressors for the main conditions are set to zero mean.

        """
        self.tr = tr
        if hrf_model is None:
            hrf_model = GammaDifferenceHRF(tr=tr, oversampling=oversampling)

        # Try to infer the timepoints from passed regressors or confounds
        if ntp is None:
            if regressors is not None:
                ntp = regressors.shape[0]
            elif confounds is not None:
                ntp = confounds.shape[0]
            else:
                raise ValueError("Could not infer ntp")

        # Infer the original and resampled frametime vectors
        stop = ntp * tr
        frametimes = np.arange(0, stop, tr, np.float)
        self.frametimes = pd.Series(frametimes, name="frametimes")

        stop = stop + 1 if oversampling == 1 else stop
        hires_frametimes = np.arange(0, stop, tr / oversampling, np.float)
        self._hires_frametimes = hires_frametimes

        # The design portion is optional
        if design is None:
            conditions = None
            self._condition_names = pd.Series([], name="conditions",
                                              dtype=object)
            pp_heights = []
            self.design = design
        else:
            if "duration" not in design:
                design.loc[:, "duration"] = 0
            if "value" not in design:
                design.loc[:, "value"] = 1

            self.design = design

            if condition_names is None:
                condition_names = np.sort(design.condition.unique())
            self._condition_names = pd.Series(condition_names,
                                              name="conditions")

            self._ntp = ntp

            # Convolve the oversampled condition evs
            self._make_hires_base(oversampling)
            self._convolve(hrf_model)

            # Subsample the condition evs and highpass filter
            conditions = self._subsample_condition_matrix()
            if demean:
                conditions -= conditions.mean()
            pp_heights = (conditions.max() - conditions.min()).tolist()
            if hpf_kernel is not None:
                hpf_cutoff = hpf_kernel
            if hpf_cutoff is not None:
                conditions = self._highpass_filter(conditions, hpf_cutoff)

        # Set up the other regressors of interest
        regressors = self._validate_component(regressors, "regressor")

        # Set up the confound submatrix
        confounds = self._validate_component(confounds, "confound")

        if confound_pca:
            from sklearn.decomposition import PCA
            pca = PCA(0.99).fit_transform(confounds)
            n_conf = pca.shape[1]
            new_columns = pd.Series(["confound_{:d}".format(i)
                                     for i in range(n_conf)])
            confounds = pd.DataFrame(pca, confounds.index, new_columns)

        # Set up the artifacts submatrix
        if artifacts is not None:
            if artifacts.any():
                n_art = artifacts.sum()
                art = np.zeros((artifacts.size, n_art))
                art[np.where(artifacts.astype(bool)), np.arange(n_art)] = 1
                artifacts = self._validate_component(art, "artifact")
            else:
                artifacts = None

        # Now build the full design matrix
        pieces = []
        if conditions is not None:
            pieces.append(conditions)
        if regressors is not None:
            pieces.append(regressors)
        if confounds is not None:
            pieces.append(confounds)
        if artifacts is not None:
            pieces.append(artifacts)

        X = pd.concat(pieces, axis=1)
        X.index = self.frametimes
        X.columns.name = "evs"
        if demean:
            X -= X.mean(axis=0)
        self.design_matrix = X

        # Now build the column name lists that will let us index
        # into the submatrices
        conf_names, art_names = [], []
        condition_names = self._condition_names.tolist()
        main_names = [n for n in X.columns
                      if any([n.startswith(c) for c in condition_names])]
        if regressors is not None:
            main_names += regressors.columns.tolist()
            pp_heights += (regressors.max() - regressors.min()).tolist()
        if confounds is not None:
            conf_names = confounds.columns.tolist()
            pp_heights += (confounds.max() - confounds.min()).tolist()
        if artifacts is not None:
            art_names = artifacts.columns.tolist()
            pp_heights += (artifacts.max() - artifacts.min()).tolist()
        self._full_names = X.columns.tolist()
        self._main_names = main_names
        self._confound_names = conf_names
        self._artifact_names = art_names

        # Set up boolean arrays that can be used to mask beta vectors
        cols = self.design_matrix.columns
        self.main_vector = cols.isin(main_names).reshape(-1, 1)
        self.condition_vector = cols.isin(self._condition_names).reshape(-1, 1)
        self.confound_vector, self.artifact_vector = None, None
        if confounds is not None:
            self.confound_vector = cols.isin(conf_names).reshape(-1, 1)
        if artifacts is not None:
            self.artifact_vector = cols.isin(art_names).reshape(-1, 1)

        # Here is the additional design information
        self._pp_heights = pp_heights
        self._singular_values = np.linalg.svd(self.design_matrix.values,
                                              compute_uv=False)

    def __repr__(self):
        """Represent the object with the design matrix."""
        return self.design_matrix.__repr__()

    def _repr_html_(self):
        """Represent the object with the design matrix."""
        return self.design_matrix._repr_html_()

    def _make_hires_base(self, oversampling):
        """Make the oversampled condition base submatrix."""
        hires_base = pd.DataFrame(columns=self._condition_names,
                                  index=self._hires_frametimes)

        for cond in self._condition_names:
            cond_info = self.design[self.design.condition == cond]
            cond_info = cond_info[["onset", "duration", "value"]]
            ev = self._make_hires_ev_base(cond_info)
            hires_base[cond] = ev
        self._hires_base = hires_base

    def _make_hires_ev_base(self, info):
        """Oversample a condition ev vector."""
        hft = self._hires_frametimes

        # Get the condition information
        onsets, durations, vals = info.values.T

        # Make the ev timecourse
        tmax = len(hft)
        ev = np.zeros_like(hft).astype(np.float)
        t_onset = np.minimum(np.searchsorted(hft, onsets), tmax - 1)
        ev[t_onset] += vals
        t_offset = np.minimum(np.searchsorted(hft, onsets + durations),
                              len(hft) - 1)

        # Handle the case where duration is 0 by offsetting at t + 1
        for i, off in enumerate(t_offset):
            if off < (tmax - 1) and off == t_onset[i]:
                t_offset[i] += 1

        ev[t_offset] -= vals
        ev = np.cumsum(ev)

        return ev

    def _convolve(self, hrf_model):
        """Convolve the condition evs with the HRF model."""
        self._hires_conditions = pd.DataFrame(index=self._hires_base.index)
        for cond in self._condition_names:
            res = hrf_model.convolve(self._hires_base[cond],
                                     self._hires_frametimes,
                                     cond)
            for key, vals in res.iteritems():
                self._hires_conditions[key] = vals

    def _subsample_condition_matrix(self):
        """Sample the hires convolved matrix at the TR midpoint."""
        condition_X = pd.DataFrame(columns=self._hires_conditions.columns,
                                   index=self.frametimes)

        frametime_midpoints = self.frametimes + self.tr / 2
        for key, vals in self._hires_conditions.iteritems():
            resampler = sp.interpolate.interp1d(self._hires_frametimes, vals,
                                                kind="nearest")
            condition_X[key] = resampler(frametime_midpoints)

        return condition_X

    def _validate_component(self, comp, name_base):
        """For components that can be an an array or df, build the df."""
        if comp is None:
            return None

        n = comp.shape[1]
        comp_ids = list(range(n))
        try:
            names = comp.columns
        except AttributeError:
            names = pd.Series(["{}_{:d}".format(name_base, i)
                               for i in comp_ids])
            comp = pd.DataFrame(comp, self.frametimes, names)

        if names.tolist() == comp_ids:
            names = pd.Series(["{}_{:d}".format(name_base, i)
                               for i in comp_ids])
            comp.columns = names

        frametimes_match = (np.all(comp.index == self.frametimes) or
                            np.all(comp.index == np.arange(len(comp))))
        if not frametimes_match:
            err = "Frametimes for %ss do not match design." % name_base
            raise ValueError(err)

        comp.index = self.frametimes

        return comp

    def _highpass_filter(self, mat, cutoff):
        """Highpass-filter each column in mat."""
        if np.isscalar(cutoff):
            F = fsl_highpass_matrix(self._ntp, cutoff, self.tr)
        else:
            F = cutoff
        for key, vals in mat.iteritems():
            mat[key] = np.dot(F, vals)
        return mat

    def contrast_vector(self, names, weights):
        """Return a full contrast vector given condition names and weights."""
        vector = np.zeros(self.design_matrix.shape[1])
        for name, weight in zip(names, weights):
            vector[self.design_matrix.columns == name] = weight
        return vector

    def plot(self, kind="full", fname=None, cmap="bone", close=False):
        """Draw an image representation of the design matrix.

        Parameters
        ----------
        kind : string
            Which submatrix to plot, or "full" for the whole matrix.
        fname : string, optional
            If provided, save the plot to this file name.
        cmap : string or colormap object
            Colormap for the plot.
        close: bool
            If True, close the figure after plotting.

        """
        names = getattr(self, "_%s_names" % kind)
        mat = self.design_matrix[names].copy()
        mat -= mat.min()
        mat /= mat.max()

        x, y = .66 * mat.shape[1], .04 * mat.shape[0]
        figsize = min(x, 10), min(y, 14)
        f, ax = plt.subplots(1, 1, figsize=figsize)
        ax.imshow(mat, aspect="auto", cmap=cmap, vmin=-.2, vmax=1.2,
                  interpolation="nearest", zorder=2)
        ax.set_yticks([])
        ax.set_xticks(np.arange(len(names)))
        ax.set_xticklabels(names, ha="right", rotation=30)
        for x in range(len(names) - 1):
            ax.axvline(x + .5, c="k", lw=3, zorder=3)
        plt.tight_layout()

        if fname is not None:
            f.savefig(fname)
        if close:
            plt.close(f)

    def plot_confound_correlation(self, fname=None, legend=True, close=False):
        """Plot how correlated the condition and confound regressors are."""
        import seaborn as sns
        corrs = self.design_matrix.corr()
        corrs = corrs.loc[self._confound_names, self._condition_names]

        n_bars = len(self._condition_names) * len(self._confound_names)
        ysize = min(n_bars * .2, 10)
        figsize = (9, ysize)

        f, ax = plt.subplots(1, 1, figsize=figsize)
        n_conf = corrs.shape[0]
        colors = sns.husl_palette(len(corrs))

        for i, (cond, conf_corrs) in enumerate(corrs.iteritems()):
            barpos = np.linspace(i, i + 1, n_conf + 1)[:-1][::-1]
            bars = ax.barh(barpos, conf_corrs.abs(), height=1 / n_conf,
                           color=colors, linewidth=.3, edgecolor="white")

        ax.set_xlim(0, 1)
        ax.set_xlabel("| correlation |")
        ax.set_yticks(np.arange(len(self._condition_names)) + 0.5)
        ax.set_yticklabels(self._condition_names)
        ax.set_ylim(0, len(self._condition_names))
        ax.yaxis.grid(False)

        for y in range(1, len(self._condition_names)):
            ax.axhline(y, ls=":", c=".6", lw=1, zorder=0)

        if legend:
            ncol = len(self._confound_names) // 15 + 1
            box = ax.get_position()
            ax.set_position([box.x0, box.y0,
                             box.width * (1 - .15 * ncol), box.height])
            lgd = ax.legend(bars, self._confound_names, ncol=ncol, fontsize=10,
                            loc='center left', bbox_to_anchor=(1, 0.5))
        else:
            lgd = []

        if fname is not None:
            f.savefig(fname, dpi=100,
                      bbox_extra_artists=[lgd],
                      bbox_inches="tight")
        if close:
            plt.close(f)

    def plot_singular_values(self, fname=None, close=False):
        """Plot the singular values of the full design matrix."""
        s = self._singular_values
        smat = s * np.eye(len(s))

        size = min(.3 * len(s), 8)
        f, ax = plt.subplots(figsize=(size, size))
        ax.matshow(smat, cmap="bone", zorder=2)
        ax.axis("off")

        plt.tight_layout()
        if fname is not None:
            f.savefig(fname, dpi=100)
        if close:
            plt.close(f)

    def to_csv(self, fname):
        """Save the full design matrix to csv."""
        self.design_matrix.to_csv(fname)

    def to_fsl_files(self, fstem, contrasts=None):
        """Save to FEAT-style {fstem}.mat and optionally {fstem}.con files."""
        m, n = self.design_matrix.shape
        header = "/NumWaves\t%d\n/NumPoints\t%d\n/PPheights\t\t%s\n"
        heights = "\t".join(["%7.7g" % p for p in self._pp_heights])
        header %= (n, m, heights)

        header += "\n/Matrix\n"

        sio = StringIO()
        self.design_matrix.to_csv(sio, "\t", float_format="%7.7g",
                                  header=False, index=False)

        with open(fstem + ".mat", "w") as fid:
            fid.write(header)
            fid.write(sio.getvalue())

        if contrasts is not None:
            n_cont = len(contrasts)
            names = [c[0] for c in contrasts]
            header = ""
            for i_name in enumerate(names, 1):
                header += "/ContrastName%d\t%s\n" % i_name

            header += "/NumWaves\t%d\n/NumContrasts\t%d\n" % (n, n_cont)
            header += "/PPheights\t\t\n/RequiredEffect\t\t\n\n/Matrix\n"

            Cs = []
            for _, names, weights in contrasts:
                Cs.append(self.contrast_vector(names, weights))
            C_all = pd.DataFrame(Cs)

            sio = StringIO()
            C_all.to_csv(sio, "\t", float_format="%7.7g",
                         header=False, index=False)

            with open(fstem + ".con", "w") as fid:
                fid.write(header)
                fid.write(sio.getvalue())

    def to_pickle(self, fname):
        """Save the object as a pickle to a file."""
        with open(fname, "wb") as fid:
            cPickle.dump(self, fid)

    @classmethod
    def from_pickle(cls, fname):
        """Load an object from a pickled file."""
        with open(fname, "rb") as fid:
            return cPickle.load(fid)

    @property
    def main_submatrix(self):
        """Conditions (no derivatives) and regressors."""
        return self.design_matrix[self._main_names]

    @property
    def condition_submatrix(self):
        """Only condition information."""
        return self.design_matrix[self._condition_names]

    @property
    def confound_submatrix(self):
        """Submatrix of confound regressors."""
        if not self._confound_names:
            return None
        return self.design_matrix[self._confound_names]

    @property
    def artifact_submatrix(self):
        """Submatrix of artifact regressors."""
        if not self._artifact_names:
            return None
        return self.design_matrix[self._artifact_names]

    @property
    def shape(self):
        """Shape of the full design matrix."""
        return self.design_matrix.shape

def fsl_highpass_matrix(ntp, cutoff, tr=2):
    """Return an array to implement FSL's gaussian running line filter.

    To implement the filter, premultiply your data with this array.

    Parameters
    ----------
    ntp : int
        number of observations in data
    cutoff : float
        filter cutoff in seconds
    tr : float
        TR of data in seconds

    Return
    ------
    F : ntp square array
        filter matrix

    """
    cutoff = cutoff / tr
    sig2n = np.square(cutoff / np.sqrt(2))

    kernel = np.exp(-np.square(np.arange(ntp)) / (2 * sig2n))
    kernel = 1 / np.sqrt(2 * np.pi * sig2n) * kernel

    K = sp.linalg.toeplitz(kernel)
    K = np.dot(np.diag(1 / K.sum(axis=1)), K)

    H = np.empty((ntp, ntp))
    X = np.column_stack((np.ones(ntp), np.arange(ntp)))
    for k in range(ntp):
        W = sparse.diags(K[k])
        hat = np.dot(X, np.linalg.pinv(W * X) * W)
        H[k] = hat[k]
    F = np.eye(ntp) - H
    return F

def fsl_highpass_filter(data, cutoff=128, tr=2, copy=True):
    """Highpass filter data with gaussian running line filter.

    Parameters
    ----------
    data : 1d or 2d array
        data array where first dimension is observations
    cutoff : float
        filter cutoff in seconds
    tr : float
        data TR in seconds
    copy : boolean
        if False data is filtered in place

    Returns
    -------
    data : 1d or 2d array
        filtered version of the data

    """
    if copy:
        data = data.copy()
    # Ensure data is in right shape
    ntp = len(data)
    data = np.atleast_2d(data).reshape(ntp, -1)

    # Filter each column of the data
    F = fsl_highpass_matrix(ntp, cutoff, tr)
    data[:] = np.dot(F, data)

    return data.squeeze()


text:
import contextlib
import fixtures
import mox

from nova import test
from nova import utils
from nova.virt.xenapi import vm_utils

@contextlib.contextmanager
def contextified(result):
    yield result

def _fake_noop(*args, **kwargs):
    return

class GenerateConfigDriveTestCase(test.TestCase):
    def test_no_admin_pass(self):
        # This is here to avoid masking errors, it shouldn't be used normally
        self.useFixture(fixtures.MonkeyPatch(
                'nova.virt.xenapi.vm_utils.destroy_vdi', _fake_noop))

        # Mocks
        instance = {}

        self.mox.StubOutWithMock(vm_utils, 'safe_find_sr')
        vm_utils.safe_find_sr('session').AndReturn('sr_ref')

        self.mox.StubOutWithMock(vm_utils, 'create_vdi')
        vm_utils.create_vdi('session', 'sr_ref', instance, 'config-2',
                            'configdrive',
                            64 * 1024 * 1024).AndReturn('vdi_ref')

        self.mox.StubOutWithMock(vm_utils, 'vdi_attached_here')
        vm_utils.vdi_attached_here(
            'session', 'vdi_ref', read_only=False).AndReturn(
                contextified('mounted_dev'))

        class FakeInstanceMetadata(object):
            def __init__(self, instance, content=None, extra_md=None):
                pass

            def metadata_for_config_drive(self):
                return []

        self.useFixture(fixtures.MonkeyPatch(
                'nova.api.metadata.base.InstanceMetadata',
                FakeInstanceMetadata))

        self.mox.StubOutWithMock(utils, 'execute')
        utils.execute('genisoimage', '-o', mox.IgnoreArg(), '-ldots',
                      '-allow-lowercase', '-allow-multidot', '-l',
                      '-publisher', mox.IgnoreArg(), '-quiet',
                      '-J', '-r', '-V', 'config-2', mox.IgnoreArg(),
                      attempts=1, run_as_root=False).AndReturn(None)
        utils.execute('dd', mox.IgnoreArg(), mox.IgnoreArg(),
                      run_as_root=True).AndReturn(None)

        self.mox.StubOutWithMock(vm_utils, 'create_vbd')
        vm_utils.create_vbd('session', 'vm_ref', 'vdi_ref', mox.IgnoreArg(),
                            bootable=False, read_only=True).AndReturn(None)

        self.mox.ReplayAll()

        # And the actual call we're testing
        vm_utils.generate_configdrive('session', instance, 'vm_ref',
                                      'userdevice')

class XenAPIGetUUID(test.TestCase):
    def test_get_this_vm_uuid_new_kernel(self):
        self.mox.StubOutWithMock(vm_utils, '_get_sys_hypervisor_uuid')

        vm_utils._get_sys_hypervisor_uuid().AndReturn(
            '2f46f0f5-f14c-ef1b-1fac-9eeca0888a3f')

        self.mox.ReplayAll()
        self.assertEquals('2f46f0f5-f14c-ef1b-1fac-9eeca0888a3f',
                          vm_utils.get_this_vm_uuid())
        self.mox.VerifyAll()

    def test_get_this_vm_uuid_old_kernel_reboot(self):
        self.mox.StubOutWithMock(vm_utils, '_get_sys_hypervisor_uuid')
        self.mox.StubOutWithMock(utils, 'execute')

        vm_utils._get_sys_hypervisor_uuid().AndRaise(
            IOError(13, 'Permission denied'))
        utils.execute('xenstore-read', 'domid', run_as_root=True).AndReturn(
            ('27', ''))
        utils.execute('xenstore-read', '/local/domain/27/vm',
                      run_as_root=True).AndReturn(
            ('/vm/2f46f0f5-f14c-ef1b-1fac-9eeca0888a3f', ''))

        self.mox.ReplayAll()
        self.assertEquals('2f46f0f5-f14c-ef1b-1fac-9eeca0888a3f',
                          vm_utils.get_this_vm_uuid())
        self.mox.VerifyAll()

redpajama_set_name: RedPajamaGithub


text:
// Fig. 7.39: floorButton.h
// FloorButton class definition.
#ifndef FLOORBUTTON_H
#define FLOORBUTTON_H

class Elevator;  // forward declaration

class FloorButton {

public:
    FloorButton( int, Elevator & );  // constructor
   ~FloorButton();                   // destructor

   void pressButton();  // press the button
   void resetButton();  // reset the button

private:
   const int floorNumber;  // button's floor number
   bool pressed;           // button state

   // reference to elevator used to summon
   // elevator to floor
   Elevator &elevatorRef;

}; // end class FloorButton

#endif // FLOORBUTTON_H


text:
<?php

namespace Magento\SalesRule\Model\Resource;

use Magento\Framework\Model\AbstractModel;

/**
 * SalesRule Resource Coupon
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Coupon extends \Magento\Framework\Model\Resource\Db\AbstractDb
{
    /**
     * Constructor adds unique fields
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('salesrule_coupon', 'coupon_id');
        $this->addUniqueField(['field' => 'code', 'title' => __('Coupon with the same code')]);
    }

    /**
     * Perform actions before object save
     *
     * @param AbstractModel $object
     * @return $this
     */
    public function _beforeSave(AbstractModel $object)
    {
        if (!$object->getExpirationDate()) {
            $object->setExpirationDate(null);
        } elseif ($object->getExpirationDate() instanceof \Zend_Date) {
            $object->setExpirationDate(
                $object->getExpirationDate()->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT)
            );
        }

        // maintain single primary coupon per rule
        $object->setIsPrimary($object->getIsPrimary() ? 1 : null);

        return parent::_beforeSave($object);
    }

    /**
     * Load primary coupon (is_primary = 1) for specified rule
     *
     *
     * @param \Magento\SalesRule\Model\Coupon $object
     * @param \Magento\SalesRule\Model\Rule|int $rule
     * @return bool
     */
    public function loadPrimaryByRule(\Magento\SalesRule\Model\Coupon $object, $rule)
    {
        $read = $this->_getReadAdapter();

        if ($rule instanceof \Magento\SalesRule\Model\Rule) {
            $ruleId = $rule->getId();
        } else {
            $ruleId = (int)$rule;
        }

        $select = $read->select()->from(
            $this->getMainTable()
        )->where(
            'rule_id = :rule_id'
        )->where(
            'is_primary = :is_primary'
        );

        $data = $read->fetchRow($select, [':rule_id' => $ruleId, ':is_primary' => 1]);

        if (!$data) {
            return false;
        }

        $object->setData($data);

        $this->_afterLoad($object);
        return true;
    }

    /**
     * Check if code exists
     *
     * @param string $code
     * @return bool
     */
    public function exists($code)
    {
        $read = $this->_getReadAdapter();
        $select = $read->select();
        $select->from($this->getMainTable(), 'code');
        $select->where('code = :code');

        if ($read->fetchOne($select, ['code' => $code]) === false) {
            return false;
        }
        return true;
    }

    /**
     * Update auto generated Specific Coupon if it's rule changed
     *
     * @param \Magento\SalesRule\Model\Rule $rule
     * @return $this
     */
    public function updateSpecificCoupons(\Magento\SalesRule\Model\Rule $rule)
    {
        if (!$rule || !$rule->getId() || !$rule->hasDataChanges()) {
            return $this;
        }

        $updateArray = [];
        if ($rule->dataHasChangedFor('uses_per_coupon')) {
            $updateArray['usage_limit'] = $rule->getUsesPerCoupon();
        }

        if ($rule->dataHasChangedFor('uses_per_customer')) {
            $updateArray['usage_per_customer'] = $rule->getUsesPerCustomer();
        }

        $ruleNewDate = new \Magento\Framework\Stdlib\DateTime\Date($rule->getToDate());
        $ruleOldDate = new \Magento\Framework\Stdlib\DateTime\Date($rule->getOrigData('to_date'));

        if ($ruleNewDate->compare($ruleOldDate)) {
            $updateArray['expiration_date'] = $rule->getToDate();
        }

        if (!empty($updateArray)) {
            $this->_getWriteAdapter()->update(
                $this->getTable('salesrule_coupon'),
                $updateArray,
                ['rule_id = ?' => $rule->getId()]
            );
        }

        return $this;
    }
}


text:
angular.module('scrumFramework')
  .controller('SelectController', ['$scope', function($scope) {
    $scope.teams = [
      {name:'black'},
      {name:'white'},
      {name:'red'},
      {name:'blue'},
      {name:'yellow'}
    ];
    $scope.myTeam = $scope.teams[0]; // red
  }]);


text:
namespace Google.Apis.SmartDeviceManagement.v1
{
    /// The SmartDeviceManagement Service.
    public class SmartDeviceManagementService : Google.Apis.Services.BaseClientService
    {
        /// The API version.
        public const string Version = "v1";

        /// The discovery version used to generate this service.
        public static Google.Apis.Discovery.DiscoveryVersion DiscoveryVersionUsed = Google.Apis.Discovery.DiscoveryVersion.Version_1_0;

        /// Constructs a new service.
        public SmartDeviceManagementService() : this(new Google.Apis.Services.BaseClientService.Initializer())
        {
        }

        /// Constructs a new service.
        /// <param name="initializer">The service initializer.</param>
        public SmartDeviceManagementService(Google.Apis.Services.BaseClientService.Initializer initializer) : base(initializer)
        {
            Enterprises = new EnterprisesResource(this);
        }

        /// Gets the service supported features.
        public override System.Collections.Generic.IList<string> Features => new string[0];

        /// Gets the service name.
        public override string Name => "smartdevicemanagement";

        /// Gets the service base URI.
        public override string BaseUri =>
        #if NETSTANDARD1_3 || NETSTANDARD2_0 || NET45
            BaseUriOverride ?? "https://smartdevicemanagement.googleapis.com/";
        #else
            "https://smartdevicemanagement.googleapis.com/";
        #endif

        /// Gets the service base path.
        public override string BasePath => "";

        #if !NET40
        /// Gets the batch base URI; <c>null</c> if unspecified.
        public override string BatchUri => "https://smartdevicemanagement.googleapis.com/batch";

        /// Gets the batch base path; <c>null</c> if unspecified.
        public override string BatchPath => "batch";
        #endif

        /// Available OAuth 2.0 scopes for use with the Smart Device Management API.
        public class Scope
        {
            /// See and/or control the devices that you selected
            public static string SdmService = "https://www.googleapis.com/auth/sdm.service";

            /// See and control the Nest thermostats that you select
            public static string SdmThermostatService = "https://www.googleapis.com/auth/sdm.thermostat.service";
        }

        /// Available OAuth 2.0 scope constants for use with the Smart Device Management API.
        public static class ScopeConstants
        {
            /// See and/or control the devices that you selected
            public const string SdmService = "https://www.googleapis.com/auth/sdm.service";

            /// See and control the Nest thermostats that you select
            public const string SdmThermostatService = "https://www.googleapis.com/auth/sdm.thermostat.service";
        }

        /// Gets the Enterprises resource.
        public virtual EnterprisesResource Enterprises { get; }
    }

    /// A base abstract class for SmartDeviceManagement requests.
    public abstract class SmartDeviceManagementBaseServiceRequest<TResponse> : Google.Apis.Requests.ClientServiceRequest<TResponse>
    {
        /// Constructs a new SmartDeviceManagementBaseServiceRequest instance.
        protected SmartDeviceManagementBaseServiceRequest(Google.Apis.Services.IClientService service) : base(service)
        {
        }

        /// V1 error format.
        [Google.Apis.Util.RequestParameterAttribute("$.xgafv", Google.Apis.Util.RequestParameterType.Query)]
        public virtual System.Nullable<XgafvEnum> Xgafv { get; set; }

        /// V1 error format.
        public enum XgafvEnum
        {
            /// v1 error format
            [Google.Apis.Util.StringValueAttribute("1")]
            Value1 = 0,

            /// v2 error format
            [Google.Apis.Util.StringValueAttribute("2")]
            Value2 = 1,
        }

        /// OAuth access token.
        [Google.Apis.Util.RequestParameterAttribute("access_token", Google.Apis.Util.RequestParameterType.Query)]
        public virtual string AccessToken { get; set; }

        /// Data format for response.
        [Google.Apis.Util.RequestParameterAttribute("alt", Google.Apis.Util.RequestParameterType.Query)]
        public virtual System.Nullable<AltEnum> Alt { get; set; }

        /// Data format for response.
        public enum AltEnum
        {
            /// Responses with Content-Type of application/json
            [Google.Apis.Util.StringValueAttribute("json")]
            Json = 0,

            /// Media download with context-dependent Content-Type
            [Google.Apis.Util.StringValueAttribute("media")]
            Media = 1,

            /// Responses with Content-Type of application/x-protobuf
            [Google.Apis.Util.StringValueAttribute("proto")]
            Proto = 2,
        }

        /// JSONP
        [Google.Apis.Util.RequestParameterAttribute("callback", Google.Apis.Util.RequestParameterType.Query)]
        public virtual string Callback { get; set; }

        /// Selector specifying which fields to include in a partial response.
        [Google.Apis.Util.RequestParameterAttribute("fields", Google.Apis.Util.RequestParameterType.Query)]
        public virtual string Fields { get; set; }

        ///
        /// API key. Your API key identifies your project and provides you with API access, quota, and reports. Required
        /// unless you provide an OAuth 2.0 token.
        ///
        [Google.Apis.Util.RequestParameterAttribute("key", Google.Apis.Util.RequestParameterType.Query)]
        public virtual string Key { get; set; }

        /// OAuth 2.0 token for the current user.
        [Google.Apis.Util.RequestParameterAttribute("oauth_token", Google.Apis.Util.RequestParameterType.Query)]
        public virtual string OauthToken { get; set; }

        /// Returns response with indentations and line breaks.
        [Google.Apis.Util.RequestParameterAttribute("prettyPrint", Google.Apis.Util.RequestParameterType.Query)]
        public virtual System.Nullable<bool> PrettyPrint { get; set; }

        ///
        /// Available to use for quota purposes for server-side applications. Can be any arbitrary string assigned to a
        /// user, but should not exceed 40 characters.
        ///
        [Google.Apis.Util.RequestParameterAttribute("quotaUser", Google.Apis.Util.RequestParameterType.Query)]
        public virtual string QuotaUser { get; set; }

        /// Legacy upload protocol for media (e.g. "media", "multipart").
        [Google.Apis.Util.RequestParameterAttribute("uploadType", Google.Apis.Util.RequestParameterType.Query)]
        public virtual string UploadType { get; set; }

        /// Upload protocol for media (e.g. "raw", "multipart").
        [Google.Apis.Util.RequestParameterAttribute("upload_protocol", Google.Apis.Util.RequestParameterType.Query)]
        public virtual string UploadProtocol { get; set; }

        /// Initializes SmartDeviceManagement parameter list.
        protected override void InitParameters()
        {
            base.InitParameters();
            RequestParameters.Add("$.xgafv", new Google.Apis.Discovery.Parameter
            {
                Name = "$.xgafv",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = null,
                Pattern = null,
            });
            RequestParameters.Add("access_token", new Google.Apis.Discovery.Parameter
            {
                Name = "access_token",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = null,
                Pattern = null,
            });
            RequestParameters.Add("alt", new Google.Apis.Discovery.Parameter
            {
                Name = "alt",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = "json",
                Pattern = null,
            });
            RequestParameters.Add("callback", new Google.Apis.Discovery.Parameter
            {
                Name = "callback",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = null,
                Pattern = null,
            });
            RequestParameters.Add("fields", new Google.Apis.Discovery.Parameter
            {
                Name = "fields",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = null,
                Pattern = null,
            });
            RequestParameters.Add("key", new Google.Apis.Discovery.Parameter
            {
                Name = "key",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = null,
                Pattern = null,
            });
            RequestParameters.Add("oauth_token", new Google.Apis.Discovery.Parameter
            {
                Name = "oauth_token",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = null,
                Pattern = null,
            });
            RequestParameters.Add("prettyPrint", new Google.Apis.Discovery.Parameter
            {
                Name = "prettyPrint",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = "true",
                Pattern = null,
            });
            RequestParameters.Add("quotaUser", new Google.Apis.Discovery.Parameter
            {
                Name = "quotaUser",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = null,
                Pattern = null,
            });
            RequestParameters.Add("uploadType", new Google.Apis.Discovery.Parameter
            {
                Name = "uploadType",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = null,
                Pattern = null,
            });
            RequestParameters.Add("upload_protocol", new Google.Apis.Discovery.Parameter
            {
                Name = "upload_protocol",
                IsRequired = false,
                ParameterType = "query",
                DefaultValue = null,
                Pattern = null,
            });
        }
    }

    /// The "enterprises" collection of methods.
    public class EnterprisesResource
    {
        private const string Resource = "enterprises";

        /// The service which this resource belongs to.
        private readonly Google.Apis.Services.IClientService service;

        /// Constructs a new resource.
        public EnterprisesResource(Google.Apis.Services.IClientService service)
        {
            this.service = service;
            Devices = new DevicesResource(service);
            Structures = new StructuresResource(service);
        }

        /// Gets the Devices resource.
        public virtual DevicesResource Devices { get; }

        /// The "devices" collection of methods.
        public class DevicesResource
        {
            private const string Resource = "devices";

            /// The service which this resource belongs to.
            private readonly Google.Apis.Services.IClientService service;

            /// Constructs a new resource.
            public DevicesResource(Google.Apis.Services.IClientService service)
            {
                this.service = service;
            }

            /// Executes a command to device managed by the enterprise.
            /// <param name="body">The body of the request.</param>
            /// <param name="name">The name of the device requested. For example: "enterprises/XYZ/devices/123"</param>
            public virtual ExecuteCommandRequest ExecuteCommand(Google.Apis.SmartDeviceManagement.v1.Data.GoogleHomeEnterpriseSdmV1ExecuteDeviceCommandRequest body, string name)
            {
                return new ExecuteCommandRequest(service, body, name);
            }

            /// Executes a command to device managed by the enterprise.
            public class ExecuteCommandRequest : SmartDeviceManagementBaseServiceRequest<Google.Apis.SmartDeviceManagement.v1.Data.GoogleHomeEnterpriseSdmV1ExecuteDeviceCommandResponse>
            {
                /// Constructs a new ExecuteCommand request.
                public ExecuteCommandRequest(Google.Apis.Services.IClientService service, Google.Apis.SmartDeviceManagement.v1.Data.GoogleHomeEnterpriseSdmV1ExecuteDeviceCommandRequest body, string name) : base(service)
                {
                    Name = name;
                    Body = body;
                    InitParameters();
                }

                /// The name of the device requested. For example: "enterprises/XYZ/devices/123"
                [Google.Apis.Util.RequestParameterAttribute("name", Google.Apis.Util.RequestParameterType.Path)]
                public virtual string Name { get; private set; }

                /// Gets or sets the body of this request.
                Google.Apis.SmartDeviceManagement.v1.Data.GoogleHomeEnterpriseSdmV1ExecuteDeviceCommandRequest Body { get; set; }

                /// Returns the body of the request.
                protected override object GetBody() => Body;

                /// Gets the method name.
                public override string MethodName => "executeCommand";

                /// Gets the HTTP method.
                public override string HttpMethod => "POST";

                /// Gets the REST path.
                public override string RestPath => "v1/{+name}:executeCommand";

                /// Initializes ExecuteCommand parameter list.
                protected override void InitParameters()
                {
                    base.InitParameters();
                    RequestParameters.Add("name", new Google.Apis.Discovery.Parameter
                    {
                        Name = "name",
                        IsRequired = true,
                        ParameterType = "path",
                        DefaultValue = null,
                        Pattern = @"^enterprises/[^/]+/devices/[^/]+$",
                    });
                }
            }

            /// Gets a device managed by the enterprise.
            /// <param name="name">The name of the device requested. For example: "enterprises/XYZ/devices/123"</param>
            public virtual GetRequest Get(string name)
            {
                return new GetRequest(service, name);
            }

            /// Gets a device managed by the enterprise.
            public class GetRequest : SmartDeviceManagementBaseServiceRequest<Google.Apis.SmartDeviceManagement.v1.Data.GoogleHomeEnterpriseSdmV1Device>
            {
                /// Constructs a new Get request.
                public GetRequest(Google.Apis.Services.IClientService service, string name) : base(service)
                {
                    Name = name;
                    InitParameters();
                }

                /// The name of the device requested. For example: "enterprises/XYZ/devices/123"
                [Google.Apis.Util.RequestParameterAttribute("name", Google.Apis.Util.RequestParameterType.Path)]
                public virtual string Name { get; private set; }

                /// Gets the method name.
                public override string MethodName => "get";

                /// Gets the HTTP method.
                public override string HttpMethod => "GET";

                /// Gets the REST path.
                public override string RestPath => "v1/{+name}";

                /// Initializes Get parameter list.
                protected override void InitParameters()
                {
                    base.InitParameters();
                    RequestParameters.Add("name", new Google.Apis.Discovery.Parameter
                    {
                        Name = "name",
                        IsRequired = true,
                        ParameterType = "path",
                        DefaultValue = null,
                        Pattern = @"^enterprises/[^/]+/devices/[^/]+$",
                    });
                }
            }

            /// Lists devices managed by the enterprise.
            /// <param name="parent">The parent enterprise to list devices under. E.g. "enterprises/XYZ".</param>
            public virtual ListRequest List(string parent)
            {
                return new ListRequest(service, parent);
            }

            /// Lists devices managed by the enterprise.
            public class ListRequest : SmartDeviceManagementBaseServiceRequest<Google.Apis.SmartDeviceManagement.v1.Data.GoogleHomeEnterpriseSdmV1ListDevicesResponse>
            {
                /// Constructs a new List request.
                public ListRequest(Google.Apis.Services.IClientService service, string parent) : base(service)
                {
                    Parent = parent;
                    InitParameters();
                }

                /// The parent enterprise to list devices under. E.g. "enterprises/XYZ".
                [Google.Apis.Util.RequestParameterAttribute("parent", Google.Apis.Util.RequestParameterType.Path)]
                public virtual string Parent { get; private set; }

                ///
                /// Optional filter to list devices. Filters can be done on: Device custom name (substring match):
                /// 'customName=wing'
                ///
                [Google.Apis.Util.RequestParameterAttribute("filter", Google.Apis.Util.RequestParameterType.Query)]
                public virtual string Filter { get; set; }

                ///
                /// Optional requested page size. Server may return fewer devices than requested. If unspecified, server
                /// will pick an appropriate default.
                ///
                [Google.Apis.Util.RequestParameterAttribute("pageSize", Google.Apis.Util.RequestParameterType.Query)]
                public virtual System.Nullable<int> PageSize { get; set; }

                /// Optional token of the page to retrieve.
                [Google.Apis.Util.RequestParameterAttribute("pageToken", Google.Apis.Util.RequestParameterType.Query)]
                public virtual string PageToken { get; set; }

                /// Gets the method name.
                public override string MethodName => "list";

                /// Gets the HTTP method.
                public override string HttpMethod => "GET";

                /// Gets the REST path.
                public override string RestPath => "v1/{+parent}/devices";

                /// Initializes List parameter list.
                protected override void InitParameters()
                {
                    base.InitParameters();
                    RequestParameters.Add("parent", new Google.Apis.Discovery.Parameter
                    {
                        Name = "parent",
                        IsRequired = true,
                        ParameterType = "path",
                        DefaultValue = null,
                        Pattern = @"^enterprises/[^/]+$",
                    });
                    RequestParameters.Add("filter", new Google.Apis.Discovery.Parameter
                    {
                        Name = "filter",
                        IsRequired = false,
                        ParameterType = "query",
                        DefaultValue = null,
                        Pattern = null,
                    });
                    RequestParameters.Add("pageSize", new Google.Apis.Discovery.Parameter
                    {
                        Name = "pageSize",
                        IsRequired = false,
                        ParameterType = "query",
                        DefaultValue = null,
                        Pattern = null,
                    });
                    RequestParameters.Add("pageToken", new Google.Apis.Discovery.Parameter
                    {
                        Name = "pageToken",
                        IsRequired = false,
                        ParameterType = "query",
                        DefaultValue = null,
                        Pattern = null,
                    });
                }
            }
        }

        /// Gets the Structures resource.
        public virtual StructuresResource Structures { get; }

        /// The "structures" collection of methods.
        public class StructuresResource
        {
            private const string Resource = "structures";

            /// The service which this resource belongs to.
            private readonly Google.Apis.Services.IClientService service;

            /// Constructs a new resource.
            public StructuresResource(Google.Apis.Services.IClientService service)
            {
                this.service = service;
                Rooms = new RoomsResource(service);
            }

            /// Gets the Rooms resource.
            public virtual RoomsResource Rooms { get; }

            /// The "rooms" collection of methods.
            public class RoomsResource
            {
                private const string Resource = "rooms";

                /// The service which this resource belongs to.
                private readonly Google.Apis.Services.IClientService service;

                /// Constructs a new resource.
                public RoomsResource(Google.Apis.Services.IClientService service)
                {
                    this.service = service;
                }

                /// Gets a room managed by the enterprise.
                /// <param name="name">
                /// The name of the room requested. For example: "enterprises/XYZ/structures/ABC/rooms/123".
                /// </param>
                public virtual GetRequest Get(string name)
                {
                    return new GetRequest(service, name);
                }

                /// Gets a room managed by the enterprise.
                public class GetRequest : SmartDeviceManagementBaseServiceRequest<Google.Apis.SmartDeviceManagement.v1.Data.GoogleHomeEnterpriseSdmV1Room>
                {
                    /// Constructs a new Get request.
                    public GetRequest(Google.Apis.Services.IClientService service, string name) : base(service)
                    {
                        Name = name;
                        InitParameters();
                    }

                    ///
                    /// The name of the room requested. For example: "enterprises/XYZ/structures/ABC/rooms/123".
                    ///
                    [Google.Apis.Util.RequestParameterAttribute("name", Google.Apis.Util.RequestParameterType.Path)]
                    public virtual string Name { get; private set; }

                    /// Gets the method name.
                    public override string MethodName => "get";

                    /// Gets the HTTP method.
                    public override string HttpMethod => "GET";

                    /// Gets the REST path.
                    public override string RestPath => "v1/{+name}";

                    /// Initializes Get parameter list.
                    protected override void InitParameters()
                    {
                        base.InitParameters();
                        RequestParameters.Add("name", new Google.Apis.Discovery.Parameter
                        {
                            Name = "name",
                            IsRequired = true,
                            ParameterType = "path",
                            DefaultValue = null,
                            Pattern = @"^enterprises/[^/]+/structures/[^/]+/rooms/[^/]+$",
                        });
                    }
                }

                /// Lists rooms managed by the enterprise.
                /// <param name="parent">
                /// The parent resource name of the rooms requested. For example: "enterprises/XYZ/structures/ABC".
                /// </param>
                public virtual ListRequest List(string parent)
                {
                    return new ListRequest(service, parent);
                }

                /// Lists rooms managed by the enterprise.
                public class ListRequest : SmartDeviceManagementBaseServiceRequest<Google.Apis.SmartDeviceManagement.v1.Data.GoogleHomeEnterpriseSdmV1ListRoomsResponse>
                {
                    /// Constructs a new List request.
                    public ListRequest(Google.Apis.Services.IClientService service, string parent) : base(service)
                    {
                        Parent = parent;
                        InitParameters();
                    }

                    ///
                    /// The parent resource name of the rooms requested. For example: "enterprises/XYZ/structures/ABC".
                    ///
                    [Google.Apis.Util.RequestParameterAttribute("parent", Google.Apis.Util.RequestParameterType.Path)]
                    public virtual string Parent { get; private set; }

                    ///
                    /// Requested page size. Server may return fewer rooms than requested. If unspecified, server will
                    /// pick an appropriate default.
                    ///
                    [Google.Apis.Util.RequestParameterAttribute("pageSize", Google.Apis.Util.RequestParameterType.Query)]
                    public virtual System.Nullable<int> PageSize { get; set; }

                    /// The token of the page to retrieve.
                    [Google.Apis.Util.RequestParameterAttribute("pageToken", Google.Apis.Util.RequestParameterType.Query)]
                    public virtual string PageToken { get; set; }

                    /// Gets the method name.
                    public override string MethodName => "list";

                    /// Gets the HTTP method.
                    public override string HttpMethod => "GET";

                    /// Gets the REST path.
                    public override string RestPath => "v1/{+parent}/rooms";

                    /// Initializes List parameter list.
                    protected override void InitParameters()
                    {
                        base.InitParameters();
                        RequestParameters.Add("parent", new Google.Apis.Discovery.Parameter
                        {
                            Name = "parent",
                            IsRequired = true,
                            ParameterType = "path",
                            DefaultValue = null,
                            Pattern = @"^enterprises/[^/]+/structures/[^/]+$",
                        });
                        RequestParameters.Add("pageSize", new Google.Apis.Discovery.Parameter
                        {
                            Name = "pageSize",
                            IsRequired = false,
                            ParameterType = "query",
                            DefaultValue = null,
                            Pattern = null,
                        });
                        RequestParameters.Add("pageToken", new Google.Apis.Discovery.Parameter
                        {
                            Name = "pageToken",
                            IsRequired = false,
                            ParameterType = "query",
                            DefaultValue = null,
                            Pattern = null,
                        });
                    }
                }
            }

            /// Gets a structure managed by the enterprise.
            /// <param name="name">
            /// The name of the structure requested. For example: "enterprises/XYZ/structures/ABC".
            /// </param>
            public virtual GetRequest Get(string name)
            {
                return new GetRequest(service, name);
            }

            /// Gets a structure managed by the enterprise.
            public class GetRequest : SmartDeviceManagementBaseServiceRequest<Google.Apis.SmartDeviceManagement.v1.Data.GoogleHomeEnterpriseSdmV1Structure>
            {
                /// Constructs a new Get request.
                public GetRequest(Google.Apis.Services.IClientService service, string name) : base(service)
                {
                    Name = name;
                    InitParameters();
                }

                ///
                /// The name of the structure requested. For example: "enterprises/XYZ/structures/ABC".
                ///
                [Google.Apis.Util.RequestParameterAttribute("name", Google.Apis.Util.RequestParameterType.Path)]
                public virtual string Name { get; private set; }

                /// Gets the method name.
                public override string MethodName => "get";

                /// Gets the HTTP method.
                public override string HttpMethod => "GET";

                /// Gets the REST path.
                public override string RestPath => "v1/{+name}";

                /// Initializes Get parameter list.
                protected override void InitParameters()
                {
                    base.InitParameters();
                    RequestParameters.Add("name", new Google.Apis.Discovery.Parameter
                    {
                        Name = "name",
                        IsRequired = true,
                        ParameterType = "path",
                        DefaultValue = null,
                        Pattern = @"^enterprises/[^/]+/structures/[^/]+$",
                    });
                }
            }

            /// Lists structures managed by the enterprise.
            /// <param name="parent">The parent enterprise to list structures under. E.g. "enterprises/XYZ".</param>
            public virtual ListRequest List(string parent)
            {
                return new ListRequest(service, parent);
            }

            /// Lists structures managed by the enterprise.
            public class ListRequest : SmartDeviceManagementBaseServiceRequest<Google.Apis.SmartDeviceManagement.v1.Data.GoogleHomeEnterpriseSdmV1ListStructuresResponse>
            {
                /// Constructs a new List request.
                public ListRequest(Google.Apis.Services.IClientService service, string parent) : base(service)
                {
                    Parent = parent;
                    InitParameters();
                }

                /// The parent enterprise to list structures under. E.g. "enterprises/XYZ".
                [Google.Apis.Util.RequestParameterAttribute("parent", Google.Apis.Util.RequestParameterType.Path)]
                public virtual string Parent { get; private set; }

                /// Optional filter to list structures.
                [Google.Apis.Util.RequestParameterAttribute("filter", Google.Apis.Util.RequestParameterType.Query)]
                public virtual string Filter { get; set; }

                ///
                /// Requested page size. Server may return fewer structures than requested. If unspecified, server will
                /// pick an appropriate default.
                ///
                [Google.Apis.Util.RequestParameterAttribute("pageSize", Google.Apis.Util.RequestParameterType.Query)]
                public virtual System.Nullable<int> PageSize { get; set; }

                /// The token of the page to retrieve.
                [Google.Apis.Util.RequestParameterAttribute("pageToken", Google.Apis.Util.RequestParameterType.Query)]
                public virtual string PageToken { get; set; }

                /// Gets the method name.
                public override string MethodName => "list";

                /// Gets the HTTP method.
                public override string HttpMethod => "GET";

                /// Gets the REST path.
                public override string RestPath => "v1/{+parent}/structures";

                /// Initializes List parameter list.
                protected override void InitParameters()
                {
                    base.InitParameters();
                    RequestParameters.Add("parent", new Google.Apis.Discovery.Parameter
                    {
                        Name = "parent",
                        IsRequired = true,
                        ParameterType = "path",
                        DefaultValue = null,
                        Pattern = @"^enterprises/[^/]+$",
                    });
                    RequestParameters.Add("filter", new Google.Apis.Discovery.Parameter
                    {
                        Name = "filter",
                        IsRequired = false,
                        ParameterType = "query",
                        DefaultValue = null,
                        Pattern = null,
                    });
                    RequestParameters.Add("pageSize", new Google.Apis.Discovery.Parameter
                    {
                        Name = "pageSize",
                        IsRequired = false,
                        ParameterType = "query",
                        DefaultValue = null,
                        Pattern = null,
                    });
                    RequestParameters.Add("pageToken", new Google.Apis.Discovery.Parameter
                    {
                        Name = "pageToken",
                        IsRequired = false,
                        ParameterType = "query",
                        DefaultValue = null,
                        Pattern = null,
                    });
                }
            }
        }
    }
}
namespace Google.Apis.SmartDeviceManagement.v1.Data
{
    /// Device resource represents an instance of enterprise managed device in the property.
    public class GoogleHomeEnterpriseSdmV1Device : Google.Apis.Requests.IDirectResponseSchema
    {
        /// Required. The resource name of the device. For example: "enterprises/XYZ/devices/123".
        [Newtonsoft.Json.JsonPropertyAttribute("name")]
        public virtual string Name { get; set; }

        /// Assignee details of the device.
        [Newtonsoft.Json.JsonPropertyAttribute("parentRelations")]
        public virtual System.Collections.Generic.IList<GoogleHomeEnterpriseSdmV1ParentRelation> ParentRelations { get; set; }

        /// Output only. Device traits.
        [Newtonsoft.Json.JsonPropertyAttribute("traits")]
        public virtual System.Collections.Generic.IDictionary<string, object> Traits { get; set; }

        ///
        /// Output only. Type of the device for general display purposes. For example: "THERMOSTAT". The device type
        /// should not be used to deduce or infer functionality of the actual device it is assigned to. Instead, use the
        /// returned traits for the device.
        ///
        [Newtonsoft.Json.JsonPropertyAttribute("type")]
        public virtual string Type { get; set; }

        /// The ETag of the item.
        public virtual string ETag { get; set; }
    }

    /// Request message for SmartDeviceManagementService.ExecuteDeviceCommand
    public class GoogleHomeEnterpriseSdmV1ExecuteDeviceCommandRequest : Google.Apis.Requests.IDirectResponseSchema
    {
        /// The command name to execute, represented by the fully qualified protobuf message name.
        [Newtonsoft.Json.JsonPropertyAttribute("command")]
        public virtual string Command { get; set; }

        /// The command message to execute, represented as a Struct.
        [Newtonsoft.Json.JsonPropertyAttribute("params")]
        public virtual System.Collections.Generic.IDictionary<string, object> Params__ { get; set; }

        /// The ETag of the item.
        public virtual string ETag { get; set; }
    }

    /// Response message for SmartDeviceManagementService.ExecuteDeviceCommand
    public class GoogleHomeEnterpriseSdmV1ExecuteDeviceCommandResponse : Google.Apis.Requests.IDirectResponseSchema
    {
        /// The results of executing the command.
        [Newtonsoft.Json.JsonPropertyAttribute("results")]
        public virtual System.Collections.Generic.IDictionary<string, object> Results { get; set; }

        /// The ETag of the item.
        public virtual string ETag { get; set; }
    }

    /// Response message for SmartDeviceManagementService.ListDevices
    public class GoogleHomeEnterpriseSdmV1ListDevicesResponse : Google.Apis.Requests.IDirectResponseSchema
    {
        /// The list of devices.
        [Newtonsoft.Json.JsonPropertyAttribute("devices")]
        public virtual System.Collections.Generic.IList<GoogleHomeEnterpriseSdmV1Device> Devices { get; set; }

        /// The pagination token to retrieve the next page of results.
        [Newtonsoft.Json.JsonPropertyAttribute("nextPageToken")]
        public virtual string NextPageToken { get; set; }

        /// The ETag of the item.
        public virtual string ETag { get; set; }
    }

    /// Response message for SmartDeviceManagementService.ListRooms
    public class GoogleHomeEnterpriseSdmV1ListRoomsResponse : Google.Apis.Requests.IDirectResponseSchema
    {
        ///
        /// The pagination token to retrieve the next page of results. If this field is omitted, there are no subsequent
        /// pages.
        ///
        [Newtonsoft.Json.JsonPropertyAttribute("nextPageToken")]
        public virtual string NextPageToken { get; set; }

        /// The list of rooms.
        [Newtonsoft.Json.JsonPropertyAttribute("rooms")]
        public virtual System.Collections.Generic.IList<GoogleHomeEnterpriseSdmV1Room> Rooms { get; set; }

        /// The ETag of the item.
        public virtual string ETag { get; set; }
    }

    /// Response message for SmartDeviceManagementService.ListStructures
    public class GoogleHomeEnterpriseSdmV1ListStructuresResponse : Google.Apis.Requests.IDirectResponseSchema
    {
        ///
        /// The pagination token to retrieve the next page of results. If this field is omitted, there are no subsequent
        /// pages.
        ///
        [Newtonsoft.Json.JsonPropertyAttribute("nextPageToken")]
        public virtual string NextPageToken { get; set; }

        /// The list of structures.
        [Newtonsoft.Json.JsonPropertyAttribute("structures")]
        public virtual System.Collections.Generic.IList<GoogleHomeEnterpriseSdmV1Structure> Structures { get; set; }

        /// The ETag of the item.
        public virtual string ETag { get; set; }
    }

    ///
    /// Represents device relationships, for instance, structure/room to which the device is assigned to.
    ///
    public class GoogleHomeEnterpriseSdmV1ParentRelation : Google.Apis.Requests.IDirectResponseSchema
    {
        ///
        /// Output only. The custom name of the relation -- e.g., structure/room where the device is assigned to.
        ///
        [Newtonsoft.Json.JsonPropertyAttribute("displayName")]
        public virtual string DisplayName { get; set; }

        ///
        /// Output only. The name of the relation -- e.g., structure/room where the device is assigned to. For example:
        /// "enterprises/XYZ/structures/ABC" or "enterprises/XYZ/structures/ABC/rooms/123"
        ///
        [Newtonsoft.Json.JsonPropertyAttribute("parent")]
        public virtual string Parent { get; set; }

        /// The ETag of the item.
        public virtual string ETag { get; set; }
    }

    ///
    /// Room resource represents an instance of sub-space within a structure such as rooms in a hotel suite or rental
    /// apartment.
    ///
    public class GoogleHomeEnterpriseSdmV1Room : Google.Apis.Requests.IDirectResponseSchema
    {
        ///
        /// Output only. The resource name of the room. For example: "enterprises/XYZ/structures/ABC/rooms/123".
        ///
        [Newtonsoft.Json.JsonPropertyAttribute("name")]
        public virtual string Name { get; set; }

        /// Room traits.
        [Newtonsoft.Json.JsonPropertyAttribute("traits")]
        public virtual System.Collections.Generic.IDictionary<string, object> Traits { get; set; }

        /// The ETag of the item.
        public virtual string ETag { get; set; }
    }

    /// Structure resource represents an instance of enterprise managed home or hotel room.
    public class GoogleHomeEnterpriseSdmV1Structure : Google.Apis.Requests.IDirectResponseSchema
    {
        ///
        /// Output only. The resource name of the structure. For example: "enterprises/XYZ/structures/ABC".
        ///
        [Newtonsoft.Json.JsonPropertyAttribute("name")]
        public virtual string Name { get; set; }

        /// Structure traits.
        [Newtonsoft.Json.JsonPropertyAttribute("traits")]
        public virtual System.Collections.Generic.IDictionary<string, object> Traits { get; set; }

        /// The ETag of the item.
        public virtual string ETag { get; set; }
    }
}


text:
Pants: A fast, scalable build system
====================================

### We are excited to announce *Pants 1.0!*

Pants is a build system designed for codebases that:

+ Are large and/or growing rapidly.
+ Consist of many subprojects that share a significant amount of code.
+ Have complex dependencies on third-party libraries.
+ Use a variety of languages, code generators and frameworks.

Pants supports Java, Scala, Python, C/C++, Go, Javascript/Node, Thrift, Protobuf and Android code.
Adding support for other languages, frameworks and code generators is straightforward.

Pants is a collaborative open-source project, built and used by Twitter, Foursquare, Square,
Medium and [[other companies|pants('src/docs:powered_by')]].

Getting Started
---------------

+ [[Installing Pants|pants('src/docs:install')]]
+ [[Setting Up Pants|pants('src/docs:setup_repo')]]
+ [[Tutorial|pants('src/docs:first_tutorial')]]

Pants Reference Documentation
-----------------------------

+ BUILD Dictionary
+ Options Reference

Powered by Pants
----------------

+ Find out [[who is using Pants|pants('src/docs:powered_by')]]

Contributing to Pants
---------------------

+ [[Pants Developer Center|pants('src/docs:dev')]]
+ [Code of Conduct](https://github.com/pantsbuild/pants/blob/master/CODE_OF_CONDUCT.md)


text:
<#PSScriptInfo
.VERSION 1.0
.GUID 400370df-41bc-44d4-8730-0aa9a135383f
.AUTHOR Microsoft Corporation
.COMPANYNAME Microsoft Corporation
.COPYRIGHT (c) Microsoft Corporation. All rights reserved.
.TAGS DSCConfiguration
.LICENSEURI https://github.com/PowerShell/xActiveDirectory/blob/master/LICENSE
.PROJECTURI https://github.com/PowerShell/xActiveDirectory
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>

#Requires -module xActiveDirectory

<#
    .DESCRIPTION
        This configuration will create a highly available domain by adding
        a second domain controller to the newly created domain.
        The xWaitForDomain resource is used to ensure that the domain is
        present before the second domain controller is added.
#>

Configuration NewDomainWithTwoDCs_Config
{
    param
    (
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $SafemodeAdministratorCred,

        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $domainCred,

        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $DNSDelegationCred,

        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $NewADUserCred
    )

    Import-DscResource -ModuleName xActiveDirectory

    Node $AllNodes.Where{ $_.Role -eq 'Primary DC' }.NodeName
    {
        WindowsFeature 'ADDSInstall'
        {
            Ensure = 'Present'
            Name   = 'AD-Domain-Services'
        }

        xADDomain 'FirstDS'
        {
            DomainName                    = $Node.DomainName
            DomainAdministratorCredential = $domainCred
            SafemodeAdministratorPassword = $SafemodeAdministratorCred
            DnsDelegationCredential       = $DNSDelegationCred
            DependsOn                     = '[WindowsFeature]ADDSInstall'
        }

        xWaitForADDomain 'DscForestWait'
        {
            DomainName           = $Node.DomainName
            DomainUserCredential = $domainCred
            RetryCount           = $Node.RetryCount
            RetryIntervalSec     = $Node.RetryIntervalSec
            DependsOn            = '[xADDomain]FirstDS'
        }

        xADUser 'FirstUser'
        {
            DomainName                    = $Node.DomainName
            DomainAdministratorCredential = $domainCred
            UserName                      = 'dummy'
            Password                      = $NewADUserCred
            Ensure                        = 'Present'
            DependsOn                     = '[xWaitForADDomain]DscForestWait'
        }
    }

    Node $AllNodes.Where{ $_.Role -eq 'Replica DC' }.NodeName
    {
        WindowsFeature 'ADDSInstall'
        {
            Ensure = 'Present'
            Name   = 'AD-Domain-Services'
        }

        xWaitForADDomain 'DscForestWait'
        {
            DomainName           = $Node.DomainName
            DomainUserCredential = $domainCred
            RetryCount           = $Node.RetryCount
            RetryIntervalSec     = $Node.RetryIntervalSec
            DependsOn            = '[WindowsFeature]ADDSInstall'
        }

        xADDomainController 'SecondDC'
        {
            DomainName                    = $Node.DomainName
            DomainAdministratorCredential = $domainCred
            SafemodeAdministratorPassword = $SafemodeAdministratorCred
            DependsOn                     = '[xWaitForADDomain]DscForestWait'
        }
    }
}

# Configuration Data for AD
$ConfigurationData = @{
    AllNodes = @(
        @{
            NodeName         = 'dsc-testNode1'
            Role             = 'Primary DC'
            DomainName       = 'dsc-test.contoso.com'
            CertificateFile  = 'C:\publicKeys\targetNode.cer'
            Thumbprint       = 'AC23EA3A9E291A75757A556D0B71CBBF8C4F6FD8'
            RetryCount       = 20
            RetryIntervalSec = 30
        },
        @{
            NodeName         = 'dsc-testNode2'
            Role             = 'Replica DC'
            DomainName       = 'dsc-test.contoso.com'
            CertificateFile  = 'C:\publicKeys\targetNode.cer'
            Thumbprint       = 'AC23EA3A9E291A75757A556D0B71CBBF8C4F6FD8'
            RetryCount       = 20
            RetryIntervalSec = 30
        }
    )
}


text:
/* (c) 2014 LinkedIn Corp. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.
 */

package gobblin.writer;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;

import org.apache.avro.Schema;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import gobblin.configuration.ConfigurationKeys;
import gobblin.configuration.State;

/**
 * Unit tests for {@link AvroHdfsDataWriter}.
 *
 * @author ynli
 */
@Test(groups = {"gobblin.writer"})
public class AvroHdfsDataWriterTest {

  private static final Type FIELD_ENTRY_TYPE = new TypeToken<Map<String, Object>>() {
  }.getType();

  private Schema schema;
  private DataWriter<GenericRecord> writer;
  private String filePath;

  @BeforeClass
  @SuppressWarnings("unchecked")
  public void setUp()
      throws Exception {
    // Making the staging and/or output dirs if necessary
    File stagingDir = new File(TestConstants.TEST_STAGING_DIR);
    File outputDir = new File(TestConstants.TEST_OUTPUT_DIR);
    if (!stagingDir.exists()) {
      stagingDir.mkdirs();
    }
    if (!outputDir.exists()) {
      outputDir.mkdirs();
    }

    this.schema = new Schema.Parser().parse(TestConstants.AVRO_SCHEMA);

    this.filePath = TestConstants.TEST_EXTRACT_NAMESPACE.replaceAll("\\.", "/") + "/" +
        TestConstants.TEST_EXTRACT_TABLE + "/" + TestConstants.TEST_EXTRACT_ID + "_" +
        TestConstants.TEST_EXTRACT_PULL_TYPE;

    State properties = new State();
    properties.setProp(ConfigurationKeys.WRITER_BUFFER_SIZE, ConfigurationKeys.DEFAULT_BUFFER_SIZE);
    properties.setProp(ConfigurationKeys.WRITER_FILE_SYSTEM_URI, TestConstants.TEST_FS_URI);
    properties.setProp(ConfigurationKeys.WRITER_STAGING_DIR, TestConstants.TEST_STAGING_DIR);
    properties.setProp(ConfigurationKeys.WRITER_OUTPUT_DIR, TestConstants.TEST_OUTPUT_DIR);
    properties.setProp(ConfigurationKeys.WRITER_FILE_PATH, this.filePath);
    properties.setProp(ConfigurationKeys.WRITER_FILE_NAME, TestConstants.TEST_FILE_NAME);

    // Build a writer to write test records
    this.writer = new AvroDataWriterBuilder().writeTo(Destination.of(Destination.DestinationType.HDFS, properties))
        .writeInFormat(WriterOutputFormat.AVRO).withWriterId(TestConstants.TEST_WRITER_ID).withSchema(this.schema)
        .forBranch(-1).build();
  }

  @Test
  public void testWrite()
      throws IOException {
    // Write all test records
    for (String record : TestConstants.JSON_RECORDS) {
      this.writer.write(convertRecord(record));
    }

    Assert.assertEquals(this.writer.recordsWritten(), 3);

    this.writer.close();
    this.writer.commit();

    File outputFile = new File(TestConstants.TEST_OUTPUT_DIR + Path.SEPARATOR + this.filePath,
        TestConstants.TEST_FILE_NAME + "." + TestConstants.TEST_WRITER_ID + "." +
            TestConstants.TEST_FILE_EXTENSION);
    DataFileReader<GenericRecord> reader =
        new DataFileReader<GenericRecord>(outputFile, new GenericDatumReader<GenericRecord>(this.schema));

    // Read the records back and assert they are identical to the ones written
    GenericRecord user1 = reader.next();
    // Strings are in UTF8, so we have to call toString() here and below
    Assert.assertEquals(user1.get("name").toString(), "Alyssa");
    Assert.assertEquals(user1.get("favorite_number"), 256);
    Assert.assertEquals(user1.get("favorite_color").toString(), "yellow");

    GenericRecord user2 = reader.next();
    Assert.assertEquals(user2.get("name").toString(), "Ben");
    Assert.assertEquals(user2.get("favorite_number"), 7);
    Assert.assertEquals(user2.get("favorite_color").toString(), "red");

    GenericRecord user3 = reader.next();
    Assert.assertEquals(user3.get("name").toString(), "Charlie");
    Assert.assertEquals(user3.get("favorite_number"), 68);
    Assert.assertEquals(user3.get("favorite_color").toString(), "blue");

    reader.close();
  }

  @AfterClass
  public void tearDown()
      throws IOException {
    // Clean up the staging and/or output directories if necessary
    File testRootDir = new File(TestConstants.TEST_ROOT_DIR);
    if (testRootDir.exists()) {
      FileUtil.fullyDelete(testRootDir);
    }
  }

  private GenericRecord convertRecord(String inputRecord) {
    Gson gson = new Gson();
    JsonElement element = gson.fromJson(inputRecord, JsonElement.class);
    Map<String, Object> fields = gson.fromJson(element, FIELD_ENTRY_TYPE);
    GenericRecord outputRecord = new GenericData.Record(schema);
    for (Map.Entry<String, Object> entry : fields.entrySet()) {
      outputRecord.put(entry.getKey(), entry.getValue());
    }

    return outputRecord;
  }
}


text:
// Copyright [2018] <Malinovsky Rodion>

#pragma once

#include <boost/thread/barrier.hpp>
#include <condition_variable>
#include <cstddef>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "core/alias.h"
#include "core/iioservice.h"
#include "core/ischeduler.h"

namespace rms {
namespace core {

/**
 * Allows to create and manipulate thread pools.
 */
class ThreadPool : public IScheduler, public IIoService {
 public:
  /**
   * Creates thread pool with given name and count of threads.
   * @param thread_count Count of threads in the pool.
   * @param name Thread pull name.
   */
  ThreadPool(const std::size_t thread_count, const char* name);

  /**
   * Destroy thread pool, stopp all threads and wait until they are done.
   */
  ~ThreadPool();

  ThreadPool(const ThreadPool&) = delete;
  ThreadPool& operator=(const ThreadPool&) = delete;

  /**
   * Schedule the task to be executed on random thread in thread pool.
   * @param handler Task to be executed in thread pool.
   */
  void Schedule(HandlerType handler) override;

  /**
   * Wait until thread pool is done.
   */
  void Wait();

  /**
   * Get thread pool name.
   * @return Name of the thread pool.
   */
  const char* GetName() const override;

 private:
  /**
   * Get underlying asio io service.
   * @return Asio io service which executes all tasks.
   */
  AsioServiceType& GetAsioService() override;

  const char* name_;

  AsioServiceType asio_service_;

  std::unique_ptr<AsioServiceWorkType> work_;

  std::vector<std::thread> threads_;

  std::mutex mutex_;

  std::condition_variable awaiter_;

  bool stopped_ = false;

  boost::barrier barrier_;
};

}  // namespace core
}  // namespace rms


text:
require 'pathname'
require Pathname(__FILE__).ascend{|d| h=d+'spec_helper.rb'; break h if h.file?}

describe AdminModule::PageFactory do

  let(:mock_browser) do
    mock_browser_at_creation( mock_watir_browser )
  end

  context "#login_page" do

    context "with default ams version" do

      it 'returns LoginPage object' do
        mock_browser

        AdminModule.configure do |config|
          config.ams_version = "4.4.0"
        end

        factory = AdminModule::PageFactory.new
        expect( factory.login_page(false).class ).to eq AdminModule::Pages::LoginPage
      end
    end

    context "with ams version < 4.4.0" do

      it 'returns LoginPage400 object' do
        mock_browser

        AdminModule.configure do |config|
          config.ams_version = "4.0.0"
        end

        factory = AdminModule::PageFactory.new
        expect( factory.login_page(false).class ).to eq AdminModule::Pages::LoginPage400
      end
    end
  end

  context "#guidelines_version_all_page" do

    context "with default ams version" do

      it 'returns GuidelinesVersionAllPage object' do
        mock_browser

        AdminModule.configure do |config|
          config.ams_version = "4.4.0"
        end

        factory = AdminModule::PageFactory.new
        expect( factory.guidelines_version_all_page(false).class ).to eq AdminModule::Pages::GuidelinesVersionAllPage
      end
    end

    context "with ams version < 4.4.0" do

      it 'returns GuidelinesVersionAllPage400 object' do
        mock_browser

        AdminModule.configure do |config|
          config.ams_version = "4.0.0"
        end

        factory = AdminModule::PageFactory.new
        expect( factory.guidelines_version_all_page(false).class ).to eq AdminModule::Pages::GuidelinesVersionAllPage400
      end
    end
  end
end


text:
from openprovider.modules import E, common
from openprovider.models import Extension

class ExtensionModule(common.Module):
    """Bindings to API methods in the extension module."""

    def search_extension(self, limit=100, offset=0, **kw):
        """Search the list of available extensions."""

        response = self.request(E.searchExtensionRequest(
            E.limit(limit),
            E.offset(offset),
            E.withDescription(int(kw.get('with_description', 0))),
            E.withPrice(int(kw.get('with_price', 0))),
            E.withUsageCount(int(kw.get('with_usage_count', 0))),
        ))
        return response.as_models(Extension)

    def retrieve_extension(self, name, **kw):
        """Retrieve details on a single extension."""

        response = self.request(E.retrieveExtensionRequest(
            E.name(name),
            E.withDescription(int(kw.get('with_description', 0))),
            E.withPrice(int(kw.get('with_price', 0))),
            E.withUsageCount(int(kw.get('with_usage_count', 0))),
        ))

        return response.as_model(Extension)


text:
SYNONYM

#### According to
Index Fungorum

#### Published in
null

#### Original name
Helmisporium filicicola Henn.

### Remarks
null


text:
require 'rubygems'
require 'git'

# Open git repository.  If it fails on the first try, go to the parent
# directory of the current location and try again (case where the project
# is in a subdirectory and the tool - e.g. IntelliJ IDEA - is using the
# project directory as the root of execution).
@git = Git.open(`pwd`.chomp) rescue nil
if @git.nil?
  @git = Git.open(`pwd`.chomp + "/../") rescue nil
  if @git.nil?
    puts "ERROR: Unable to open git repository in '#{`pwd`}' or its parent directory."
    exit
  end
end

# Grab last commit
#   @git.dir - source directory
#   @git.branch.name - current branch name
#
#   @last_commit.sha - SHA
#   @last_commit.sha.slice(0,10) - Shortened SHA
#   @last_commit.message
#   @last_commit.author [.name, .email, .date]
#   @last_commit.committer [.name, .email, .date]
@last_commit = @git.log.first

# Other info we need
@snapshot_time = Time.now.to_i
@home_directory = "/Users/bdnelson"

# Break up repo path for tracking
@repo = @git.dir.path.gsub("#{@home_directory}/", "")
@repo = @repo.gsub("src/", "")

# Capture commit information
data_file = "#{@home_directory}/.gitcommits/#{@last_commit.committer.date.strftime("%Y%m%d")}.data"
File.open(data_file, "a+") do |fp|
  fp.puts "# #{@repo}: #{@last_commit.message}"
  fp.puts ""
  fp.puts "## Details"
  fp.puts " * *Commit:* #{@last_commit.sha}"
  fp.puts " * *Date:* #{@last_commit.author.date}"
  fp.puts " * *Author:* #{@last_commit.author.name}"
  fp.puts " * *Email:* #{@last_commit.author.email}"
  fp.puts ""
end

# Take camera snapshot
# From: https://coderwall.com/p/xlatfq
snapshot_file="#{@home_directory}/.gitshots/#{@snapshot_time}-#{@last_commit.sha}.jpg"
system "imagesnap -q -w 3 #{snapshot_file} &"

# Vocalization of completion - handy for testing but annoying otherwise
#`say 'Commit logging complete'`

exit 0