lama/stats/permutation_stats/distributions.py
def null(input_data: pd.DataFrame,
         num_perm: int,) -> Tuple[pd.DataFrame, pd.DataFrame, List]:
    """
    Generate null distributions for line and specimen-level data

    Parameters
    ----------
    input_data
        columns
            staging
            line
            then multiple columns each one a label (organ)
        Baselines must be labelled 'baseline' in the line column

    num_perm
        number of permutations

    Returns
    -------
    line-level null distribution
    specimen-level null distribution


    Notes
    -----
    Labels must not start with a digit as R will throw a wobbly
    """
    random.seed(999)

    # Use the generic staging label from now on
    input_data.rename(columns={'crl': 'staging', 'volume': 'staging'}, inplace=True)

    label_names = input_data.drop(['staging', 'line'], axis='columns').columns

    # Store p-value and t-value results. One tuple (len==num labels) per iteration
    spec_p = []

    # Create synthetic specimens by iteratively relabelling each baseline as synthetic mutant
    baselines = input_data[input_data['line'] == 'baseline']

    # Get the line specimen n numbers. Keep the first column
    # line_specimen_counts = get_line_specimen_counts(input_data)
    # Pregenerate all the combinations
    wt_indx_combinations = generate_random_combinations(input_data, num_perm)

    # Split data into a numpy array of raw data and dataframe for staging and genotype fpr the LM code
    data = baselines.drop(columns=['staging', 'line']).values
    info = baselines[['staging', 'line']]

    # Get the specimen-level null distribution. i.e. the distributuion of p-values obtained from relabelling each
    # baseline once. Loop over each specimen and set to 'synth_hom'
    for index, _ in info.iterrows():
        info.loc[:, 'genotype'] = 'wt'               # Set all genotypes to WT
        info.loc[[index], 'genotype'] = 'synth_hom'  # Set the ith baseline to synth hom
        row = data[info.index.get_loc(index), :]

        # Get columns (labels) where the mutant specimen (as it's line level)
        # has a null value (i.e. This specimen is QC-flagged at these labels)
        # Set all values to zero
        labels_to_skip = np.isnan(row)
        if any(labels_to_skip):
            # Set the whole label column to zero. R:lm() will return NaN for this column
            d = np.copy(data)
            d[:, labels_to_skip] = 0.0
        else:
            d = data

        # Get a p-value for each organ
        p, t = lm_sm(d, info)  # TODO: move this to statsmodels

        # Check that there are equal amounts of p-values than there are data points
        if len(p) != data.shape[1]:
            raise ValueError(f'The length of p-values results: {data.shape[1]} does not match the length of the input data: {len(p)}')

        spec_p.append(p)

    spec_df = pd.DataFrame.from_records(spec_p, columns=label_names)

    line_df = null_line(wt_indx_combinations, baselines, num_perm)

    return strip_x([line_df, spec_df])

    Generate null distributions for line and specimen-level data

    Parameters
    ----------
    input_data
        columns
            staging
            line
            then multiple columns each one a label (organ)
        Baselines must be labelled 'baseline' in the line column

    num_perm
        number of permutations

    Returns
    -------
    line-level null distribution
    specimen-level null distribution


    Notes
    -----
    Labels must not start with a digit as R will throw a wobbly
    
Generate null distributions for line and specimen-level data
Parameters

input_data
columns
staging
line
then multiple columns each one a label (organ)
Baselines must be labelled 'baseline' in the line column

num_perm
number of permutations

Returns

line-level null distribution
specimen-level null distribution

Notes

Labels must not start with a digit as R will throw a wobbly
Generate null distributions for line and specimen - level data Parameters input_data columns staging line then multiple columns each one a label ( organ ) Baselines must be labelled ' baseline ' in the line column num_perm number of permutations Returns line - level null distribution specimen - level null distribution Notes Labels must not start with a digit as R will throw a wobbly
def null(input_data: pd.DataFrame,
         num_perm: int,) -> Tuple[pd.DataFrame, pd.DataFrame, List]:
    random.seed(999)
    input_data.rename(columns={'crl': 'staging', 'volume': 'staging'}, inplace=True)
    label_names = input_data.drop(['staging', 'line'], axis='columns').columns
    spec_p = []
    baselines = input_data[input_data['line'] == 'baseline']
    wt_indx_combinations = generate_random_combinations(input_data, num_perm)
    data = baselines.drop(columns=['staging', 'line']).values
    info = baselines[['staging', 'line']]
    for index, _ in info.iterrows():
        info.loc[:, 'genotype'] = 'wt'               
        info.loc[[index], 'genotype'] = 'synth_hom'  
        row = data[info.index.get_loc(index), :]
        labels_to_skip = np.isnan(row)
        if any(labels_to_skip):
            d = np.copy(data)
            d[:, labels_to_skip] = 0.0
        else:
            d = data
        p, t = lm_sm(d, info)  
        if len(p) != data.shape[1]:
            raise ValueError(f'The length of p-values results: {data.shape[1]} does not match the length of the input data: {len(p)}')
        spec_p.append(p)
    spec_df = pd.DataFrame.from_records(spec_p, columns=label_names)
    line_df = null_line(wt_indx_combinations, baselines, num_perm)
    return strip_x([line_df, spec_df])
def null ( input_data : pd . DataFrame , num_perm : int , ) -> Tuple [ pd . DataFrame , pd . DataFrame , List ] : random . seed ( 999 ) input_data . rename ( columns = { 'crl' : 'staging' , 'volume' : 'staging' } , inplace = True ) label_names = input_data . drop ( [ 'staging' , 'line' ] , axis = 'columns' ) . columns spec_p = [ ] baselines = input_data [ input_data [ 'line' ] == 'baseline' ] wt_indx_combinations = generate_random_combinations ( input_data , num_perm ) data = baselines . drop ( columns = [ 'staging' , 'line' ] ) . values info = baselines [ [ 'staging' , 'line' ] ] for index , _ in info . iterrows ( ) : info . loc [ : , 'genotype' ] = 'wt' info . loc [ [ index ] , 'genotype' ] = 'synth_hom' row = data [ info . index . get_loc ( index ) , : ] labels_to_skip = np . isnan ( row ) if any ( labels_to_skip ) : d = np . copy ( data ) d [ : , labels_to_skip ] = 0.0 else : d = data p , t = lm_sm ( d , info ) if len ( p ) != data . shape [ 1 ] : raise ValueError ( f'The length of p-values results: {data.shape[1]} does not match the length of the input data: {len(p)}' ) spec_p . append ( p ) spec_df = pd . DataFrame . from_records ( spec_p , columns = label_names ) line_df = null_line ( wt_indx_combinations , baselines , num_perm ) return strip_x ( [ line_df , spec_df ] )
Generate null distributions for line and specimen-level data
Parameters
Generate null distributions for line and specimen - level data Parameters
"""
    Generate null distributions for line and specimen-level data

    Parameters
    ----------
    input_data
        columns
            staging
            line
            then multiple columns each one a label (organ)
        Baselines must be labelled 'baseline' in the line column

    num_perm
        number of permutations

    Returns
    -------
    line-level null distribution
    specimen-level null distribution


    Notes
    -----
    Labels must not start with a digit as R will throw a wobbly
    """ # Use the generic staging label from now on # Store p-value and t-value results. One tuple (len==num labels) per iteration # Create synthetic specimens by iteratively relabelling each baseline as synthetic mutant # Get the line specimen n numbers. Keep the first column # line_specimen_counts = get_line_specimen_counts(input_data) # Pregenerate all the combinations # Split data into a numpy array of raw data and dataframe for staging and genotype fpr the LM code # Get the specimen-level null distribution. i.e. the distributuion of p-values obtained from relabelling each # baseline once. Loop over each specimen and set to 'synth_hom' # Set all genotypes to WT # Set the ith baseline to synth hom # Get columns (labels) where the mutant specimen (as it's line level) # has a null value (i.e. This specimen is QC-flagged at these labels) # Set all values to zero # Set the whole label column to zero. R:lm() will return NaN for this column # Get a p-value for each organ # TODO: move this to statsmodels # Check that there are equal amounts of p-values than there are data points
input_data
pd.DataFrame
num_perm
int