### Install pycombat Source: https://github.com/coaxlab/pycombat/blob/master/README.md Install the pycombat package using pip. ```bash pip install pycombat ``` -------------------------------- ### Initialize Combat instance Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Create an instance of the Combat class. Optional parameters include 'method' ('p' for parametric) and 'conv' for convergence criterion. ```python combat = Combat() ``` -------------------------------- ### Import necessary libraries Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Import NumPy and Pandas for data manipulation and sys for system-specific parameters. ```python import numpy as np import pandas as pd import sys ``` -------------------------------- ### Combat Class Initialization Source: https://github.com/coaxlab/pycombat/blob/master/README.md Initializes an instance of the Combat class. Users can specify parameters to control the harmonization process. ```APIDOC ## Combat Class Initialization ### Description Initializes an instance of the Combat class. Users can specify parameters to control the harmonization process. ### Parameters - **method** (string) - Optional - Specifies the harmonization method, either 'p' for parametric or 'np' for non-parametric (not implemented yet). - **conv** (float) - Optional - The criterion to decide when to stop the EB optimization step. Defaults to 0.0001. ``` -------------------------------- ### Prepare covariates matrix (X) Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Create the matrix X of effects of interest by combining dummy-encoded categorical variables (e.g., 'cancer') and continuous variables (e.g., 'age'). Ensure categorical features have their first level dropped using pd.get_dummies with drop_first=True. ```python X = np.column_stack((pd.get_dummies(pheno.cancer.values, drop_first=True).values, pheno.age.values)) print(X[:10,:]) ``` -------------------------------- ### Import neuroCombat for comparison Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Import the neuroCombat function for comparison purposes. ```python from neuroCombat import neuroCombat ``` -------------------------------- ### Import Combat class Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Import the Combat class from the pycombat library. ```python from pycombat import Combat ``` -------------------------------- ### Fit and Transform Data with Combat Source: https://github.com/coaxlab/pycombat/blob/master/README.md Combine the fitting and transformation steps into a single call using the 'fit_transform' method. ```python Y_adjusted = combat.fit_trasnform(Y=Y, b=b, X=X, C=C) ``` -------------------------------- ### Combat.fit_transform Source: https://github.com/coaxlab/pycombat/blob/master/README.md Combines the fit and transform steps into a single operation. ```APIDOC ## Combat.fit_transform ### Description Combines the fit and transform steps into a single operation. ### Method fit_transform ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **Y** (matrix) - Required - The matrix of response variables, with dimensions [observations x features]. - **b** (array) - Required - The array of batch labels for each observation. - **X** (matrix) - Optional - The matrix of effects of interest to keep, with dimensions [observations x features_interest]. - **C** (matrix) - Optional - The matrix of covariates to remove, with dimensions [observations x features_covariates]. ### Response #### Success Response (200) - **Y_adjusted** (matrix) - The harmonized data matrix. ``` -------------------------------- ### Combat.transform Source: https://github.com/coaxlab/pycombat/blob/master/README.md Adjusts the data using the parameters learned during the fit step. ```APIDOC ## Combat.transform ### Description Adjusts the data using the parameters learned during the fit step. ### Method transform ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **Y** (matrix) - Required - The matrix of response variables, with dimensions [observations x features]. - **b** (array) - Required - The array of batch labels for each observation. - **X** (matrix) - Optional - The matrix of effects of interest to keep, with dimensions [observations x features_interest]. - **C** (matrix) - Optional - The matrix of covariates to remove, with dimensions [observations x features_covariates]. ### Response #### Success Response (200) - **Y_adjusted** (matrix) - The harmonized data matrix. ``` -------------------------------- ### Combat.fit Source: https://github.com/coaxlab/pycombat/blob/master/README.md Fits the Combat model to the data by finding the optimal parameters for the linear mixed model. ```APIDOC ## Combat.fit ### Description Fits the Combat model to the data by finding the optimal parameters for the linear mixed model. ### Method fit ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **Y** (matrix) - Required - The matrix of response variables, with dimensions [observations x features]. - **b** (array) - Required - The array of batch labels for each observation. - **X** (matrix) - Optional - The matrix of effects of interest to keep, with dimensions [observations x features_interest]. - **C** (matrix) - Optional - The matrix of covariates to remove, with dimensions [observations x features_covariates]. ``` -------------------------------- ### Fit Combat Model Source: https://github.com/coaxlab/pycombat/blob/master/README.md Fit the Combat model by calling the 'fit' method with the response variables (Y), batch labels (b), effects of interest (X), and covariates to remove (C). Ensure categorical features in X and C are handled to avoid singularity. ```python combat.fit(Y=Y, b=b, X=X, C=C) ``` -------------------------------- ### Transform Data with Combat Source: https://github.com/coaxlab/pycombat/blob/master/README.md Adjust the data using the fitted Combat model by calling the 'transform' method with the same input parameters. ```python Y_adjusted = combat.transform(Y=Y, b=b, X=X, C=C) ``` -------------------------------- ### Transform data with Combat Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Apply the learned parameters from the fitted Combat model to adjust the dataset (Y) using the batch labels (b) and covariates (X). ```python Y_combat = combat.transform(Y=Y, b=b, X=X) ``` -------------------------------- ### Load response variables (Y) Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Load the matrix of response variables from a .npy file. This matrix has dimensions [observations x features]. ```python # Y is the matrix of response variables Y = np.load('bladder-expr.npy') print("the matrix of response variables has %d observations and %d outcome variables" % (Y.shape[0], Y.shape[1])) ``` -------------------------------- ### Fit Combat model Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Fit the Combat model using the response variables (Y), batch labels (b), and the prepared covariates matrix (X). The C parameter for covariates to remove is optional and defaults to None. ```python combat.fit(Y, b, X=X, C=None) # X and C are None by default, so no need here to write C=None ``` -------------------------------- ### Load phenotype data Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Load phenotype data from a tab-delimited text file into a Pandas DataFrame. This includes batch labels and other covariates. ```python # This loads the set of independent variables, including the batch labels pheno = pd.read_csv('bladder-pheno.txt', delimiter=' ') pheno.head() ``` -------------------------------- ### Compare PyCombat and neuroCombat results Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Calculate the Pearson correlation coefficient between the harmonized data from PyCombat and neuroCombat to verify consistency. ```python np.corrcoef(Y_combat.flat, Y_neurocombat.flat) ``` -------------------------------- ### Extract batch labels Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Extract the batch labels from the phenotype DataFrame. The number of unique batches is then printed. ```python b = pheno.batch.values print("We have %d different batches" % len(np.unique(b))) ``` -------------------------------- ### Apply neuroCombat harmonization Source: https://github.com/coaxlab/pycombat/blob/master/test/test.ipynb Apply harmonization using the neuroCombat function, specifying discrete and continuous covariates, and the batch column. ```python discrete_cols = ['cancer'] continuous_cols = ['age'] batch_col = 'batch' Y_neurocombat = neuroCombat(data=Y, covars=pheno, batch_col=batch_col, discrete_cols=discrete_cols, continuous_cols=continuous_cols) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.