def quantile_normalize(matrix, inplace=False, target=None):
assert isinstance(matrix, ExpMatrix)<EOL>assert isinstance(inplace, bool)<EOL>if target is not None:<EOL><INDENT>assert isinstance(target, np.ndarray) andnp.issubdtype(target.dtype, np.float)<EOL><DEDENT>if not inplace:<EOL><INDENT>matrix = matrix.copy()<EOL><DEDENT>X = matrix.X<EOL>_, n = X.shape<EOL>nan = []<EOL>for j in range(n):<EOL><INDENT>nan.append(np.nonzero(np.isnan(X[:, j]))[<NUM_LIT:0>])<EOL>if nan[j].size > <NUM_LIT:0>:<EOL><INDENT>q = np.arange(<NUM_LIT:1>, nan[j].size + <NUM_LIT:1>, dtype=np.float64) /(nan[j].size + <NUM_LIT:1.0>)<EOL>fill = np.nanpercentile(X[:, j], <NUM_LIT:100> * q)<EOL>X[nan[j], j] = fill<EOL><DEDENT><DEDENT>A = np.argsort(X, axis=<NUM_LIT:0>, kind='<STR_LIT>')  <EOL>for j in range(n):<EOL><INDENT>matrix.iloc[:, j] = matrix.X[A[:, j], j]<EOL><DEDENT>if target is None:<EOL><INDENT>target = np.mean(matrix.X, axis=<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>target = np.sort(target)<EOL><DEDENT>A = np.argsort(A, axis=<NUM_LIT:0>, kind='<STR_LIT>')  <EOL>for j in range(n):<EOL><INDENT>matrix.iloc[:, j] = target[A[:, j]]<EOL><DEDENT>for j in range(n):<EOL><INDENT>if nan[j].size > <NUM_LIT:0>:<EOL><INDENT>matrix.iloc[nan[j], j] = np.nan<EOL><DEDENT><DEDENT>return matrix<EOL>
Quantile normalization, allowing for missing values (NaN).

    In case of nan values, this implementation will calculate evenly
    distributed quantiles and fill in the missing data with those values.
    Quantile normalization is then performed on the filled-in matrix,
    and the nan values are restored afterwards.

    Parameters
    ----------
    matrix: `ExpMatrix`
        The expression matrix (rows = genes, columns = samples).
    inplace: bool
        Whether or not to perform the operation in-place. [False]
    target: `numpy.ndarray`
        Target distribution to use. needs to be a vector whose first
        dimension matches that of the expression matrix. If ``None``,
        the target distribution is calculated based on the matrix
        itself. [None]

    Returns
    -------
    numpy.ndarray (ndim = 2)
        The normalized matrix.