def compartments(M, normalize=True):
if not type(M) is np.ndarray:<EOL><INDENT>M = np.array(M)<EOL><DEDENT>if M.shape[<NUM_LIT:0>] != M.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if normalize:<EOL><INDENT>N = normalize_dense(M)<EOL><DEDENT>else:<EOL><INDENT>N = np.copy(M)<EOL><DEDENT>from sklearn.decomposition import PCA<EOL>pca = PCA(n_components=<NUM_LIT:2>)<EOL>PC1, PC2 = pca.fit_transform(N).T<EOL>return PC1, PC2<EOL>
A/B compartment analysis

    Perform a PCA-based A/B compartment analysis on a normalized, single
    chromosome contact map. The results are two vectors whose values (negative
    or positive) should presumably correlate with the presence of 'active'
    vs. 'inert' chromatin.

    Parameters
    ----------
    M : array_like
        The input, normalized contact map. Must be a single chromosome.
    normalize : bool
        Whether to normalize the matrix beforehand.
    Returns
    -------

    PC1 : numpy.ndarray
        A vector representing the first component.
    PC2 : numpy.ndarray
        A vector representing the second component.