def _design_matrix(
        model: pd.DataFrame,
        batch_key: str,
        batch_levels: Collection[str],
) -> pd.DataFrame:
    """
    Computes a simple design matrix.

    Parameters
    --------
    model
        Contains the batch annotation
    batch_key
        Name of the batch column
    batch_levels
        Levels of the batch annotation

    Returns
    --------
    The design matrix for the regression problem
    """
    import patsy

    design = patsy.dmatrix(
        "~ 0 + C(Q('{}'), levels=batch_levels)".format(batch_key),
        model,
        return_type="dataframe",
    )
    model = model.drop([batch_key], axis=1)
    numerical_covariates = model.select_dtypes('number').columns.values

    logg.info("Found {} batches\n".format(design.shape[1]))
    other_cols = [c for c in model.columns.values if c not in numerical_covariates]

    if other_cols:
        col_repr = " + ".join("Q('{}')".format(x) for x in other_cols)
        factor_matrix = patsy.dmatrix("~ 0 + {}".format(col_repr),
                                      model[other_cols],
                                      return_type="dataframe")

        design = pd.concat((design, factor_matrix), axis=1)
        logg.info("Found {} categorical variables:".format(len(other_cols)))
        logg.info("\t" + ", ".join(other_cols) + '\n')

    if numerical_covariates is not None:
        logg.info("Found {} numerical variables:".format(len(numerical_covariates)))
        logg.info("\t" + ", ".join(numerical_covariates) + '\n')

        for nC in numerical_covariates:
            design[nC] = model[nC]

    return design
def _design_matrix ( model : pd . DataFrame , batch_key : str , batch_levels : Collection [ str ] , ) -> pd . DataFrame : import patsy design = patsy . dmatrix ( "~ 0 + C(Q('{}'), levels=batch_levels)" . format ( batch_key ) , model , return_type = "dataframe" , ) model = model . drop ( [ batch_key ] , axis = 1 ) numerical_covariates = model . select_dtypes ( 'number' ) . columns . values logg . info ( "Found {} batches\n" . format ( design . shape [ 1 ] ) ) other_cols = [ c for c in model . columns . values if c not in numerical_covariates ] if other_cols : col_repr = " + " . join ( "Q('{}')" . format ( x ) for x in other_cols ) factor_matrix = patsy . dmatrix ( "~ 0 + {}" . format ( col_repr ) , model [ other_cols ] , return_type = "dataframe" ) design = pd . concat ( ( design , factor_matrix ) , axis = 1 ) logg . info ( "Found {} categorical variables:" . format ( len ( other_cols ) ) ) logg . info ( "\t" + ", " . join ( other_cols ) + '\n' ) if numerical_covariates is not None : logg . info ( "Found {} numerical variables:" . format ( len ( numerical_covariates ) ) ) logg . info ( "\t" + ", " . join ( numerical_covariates ) + '\n' ) for nC in numerical_covariates : design [ nC ] = model [ nC ] return design
Computes a simple design matrix.

    Parameters
    --------
    model
        Contains the batch annotation
    batch_key
        Name of the batch column
    batch_levels
        Levels of the batch annotation

    Returns
    --------
    The design matrix for the regression problem