import torch


def ipca_transform(dataloader, components):
    """
    Transform data using incremental PCA.
    RH 2020

    Args:
        dataloader (torch.utils.data.DataLoader):
            Data to be decomposed.
        components (torch.Tensor or np.ndarray):
            The components of the decomposition. 
            2-D array.
            Each column is a component vector. Each row is a 
             feature weight.
    """
    out = []
    for iter_batch, batch in enumerate(dataloader):
        out.append(batch[0] @ components.T)
    return torch.cat(out, dim=0)