lama/stats/permutation_stats/distributions.py
def alternative(input_data: pd.DataFrame,
                plot_dir: Union[None, Path] = None,
                boxcox: bool = False) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]:
    """
    Generate alterntive (mutant) distributions for line and pecimen-level data

    Parameters
    ----------
    input_data
    plot_dir
    boxcox

    Returns
    -------
    alternative distribution dataframes with either line or specimen as index.
        0: line-level p values
        1: specimen-level p values
        2: line-level t-values
        3: specimen-level t-values
    """

    # Group by line and sequntaily run
    info_columns = ['staging', 'line', 'genotype']  # Columns of non-organ volumes in input_data

    line_groupby = input_data.groupby('line')

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

    baseline = input_data[input_data['line'] == 'baseline']
    baseline.loc[:, 'genotype'] = 'wt'

    alt_line_pvalues = []
    alt_spec_pvalues = []
    alt_line_t = []
    alt_spec_t = []

    # Get line-level alternative distributions
    for line_id, line_df in line_groupby:

        if line_id == 'baseline':
            continue

        line_df.loc[:, 'genotype'] = 'hom'
        line_df.drop(['line'], axis=1)

        df_wt_mut = pd.concat([baseline, line_df])

        # Lm code needs daatpoints in numpy array and genotype+staging in dataframe
        data_df = df_wt_mut.drop(columns=info_columns)

        # Get columns (labels) where all speciemns have null values (i.e. all the line is QC-flagged at these labels)
        labels_to_skip = [col for col, isany in line_df.any().iteritems() if not isany]
        if labels_to_skip:
            # Set the whole label column to zero. R:lm() will return NaN for this column
            data_df[labels_to_skip] = 0.0

        # Get a numpy array of the organ volumes
        data = data_df.values

        info = df_wt_mut[['staging', 'line', 'genotype']]

        p: np.array
        t: np.array

        p, t = lm_sm(data, info)  # returns p_values for all organs, 1 iteration

        res_p = [line_id] + list(p)  # line_name, label_1, label_2 ......
        alt_line_pvalues.append(res_p)

        res_t = [line_id] + list(t)
        alt_line_t.append(res_t)

    ### Get specimen-level alternative distributions ###
    mutants = input_data[input_data['line'] != 'baseline']
    # baselines = input_data[input_data['line'] == 'baseline']
    for specimen_id, row in mutants.iterrows():
        row['genotype'] = 'hom'
        line_id = row['line']
        df_wt_mut = baseline.append(row)
        data_df = df_wt_mut.drop(columns=['line', 'genotype', 'staging'])

        # 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)
        labels_to_skip = [col for col, isany in pd.DataFrame(row).T.any().iteritems() if not isany]
        if labels_to_skip:
            # Set the whole label column to zero. R:lm() will return NaN for this column
            data_df[labels_to_skip] = 0.0

        data = data_df.values

        info = df_wt_mut[['genotype', 'staging']]

        p, t = lm_sm(data, info)  # returns p_values for all organs, 1 iteration
        res_p = [line_id, specimen_id] + list(p)
        alt_spec_pvalues.append(res_p)

        res_t = [specimen_id] + list(t)
        alt_spec_t.append(res_t)

    # result dataframes have either line or specimen in index then labels
    alt_line_df = pd.DataFrame.from_records(alt_line_pvalues, columns=['line'] + label_names, index='line')
    alt_spec_df = pd.DataFrame.from_records(alt_spec_pvalues, columns=['line', 'specimen'] + label_names,
                                            index='specimen')

    alt_line_t_df = pd.DataFrame.from_records(alt_line_t, columns=['line'] + label_names, index='line')
    alt_spec_t_df = pd.DataFrame.from_records(alt_spec_t, columns=['specimen'] + label_names,
                                            index='specimen')

    return strip_x([alt_line_df, alt_spec_df, alt_line_t_df, alt_spec_t_df])

    Generate alterntive (mutant) distributions for line and pecimen-level data

    Parameters
    ----------
    input_data
    plot_dir
    boxcox

    Returns
    -------
    alternative distribution dataframes with either line or specimen as index.
        0: line-level p values
        1: specimen-level p values
        2: line-level t-values
        3: specimen-level t-values
    
Generate alterntive (mutant) distributions for line and pecimen-level data
Parameters



Returns

alternative distribution dataframes with either line or specimen as index.
Generate alterntive ( mutant ) distributions for line and pecimen - level data Parameters Returns alternative distribution dataframes with either line or specimen as index .
def alternative(input_data: pd.DataFrame,
                plot_dir: Union[None, Path] = None,
                boxcox: bool = False) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, pd.DataFrame]:
    info_columns = ['staging', 'line', 'genotype']  
    line_groupby = input_data.groupby('line')
    label_names = list(input_data.drop(['staging', 'line'], axis='columns').columns)
    baseline = input_data[input_data['line'] == 'baseline']
    baseline.loc[:, 'genotype'] = 'wt'
    alt_line_pvalues = []
    alt_spec_pvalues = []
    alt_line_t = []
    alt_spec_t = []
    for line_id, line_df in line_groupby:
        if line_id == 'baseline':
            continue
        line_df.loc[:, 'genotype'] = 'hom'
        line_df.drop(['line'], axis=1)
        df_wt_mut = pd.concat([baseline, line_df])
        data_df = df_wt_mut.drop(columns=info_columns)
        labels_to_skip = [col for col, isany in line_df.any().iteritems() if not isany]
        if labels_to_skip:
            data_df[labels_to_skip] = 0.0
        data = data_df.values
        info = df_wt_mut[['staging', 'line', 'genotype']]
        p: np.array
        t: np.array
        p, t = lm_sm(data, info)  
        res_p = [line_id] + list(p)  
        alt_line_pvalues.append(res_p)
        res_t = [line_id] + list(t)
        alt_line_t.append(res_t)
    mutants = input_data[input_data['line'] != 'baseline']
    for specimen_id, row in mutants.iterrows():
        row['genotype'] = 'hom'
        line_id = row['line']
        df_wt_mut = baseline.append(row)
        data_df = df_wt_mut.drop(columns=['line', 'genotype', 'staging'])
        labels_to_skip = [col for col, isany in pd.DataFrame(row).T.any().iteritems() if not isany]
        if labels_to_skip:
            data_df[labels_to_skip] = 0.0
        data = data_df.values
        info = df_wt_mut[['genotype', 'staging']]
        p, t = lm_sm(data, info)  
        res_p = [line_id, specimen_id] + list(p)
        alt_spec_pvalues.append(res_p)
        res_t = [specimen_id] + list(t)
        alt_spec_t.append(res_t)
    alt_line_df = pd.DataFrame.from_records(alt_line_pvalues, columns=['line'] + label_names, index='line')
    alt_spec_df = pd.DataFrame.from_records(alt_spec_pvalues, columns=['line', 'specimen'] + label_names,
                                            index='specimen')
    alt_line_t_df = pd.DataFrame.from_records(alt_line_t, columns=['line'] + label_names, index='line')
    alt_spec_t_df = pd.DataFrame.from_records(alt_spec_t, columns=['specimen'] + label_names,
                                            index='specimen')
    return strip_x([alt_line_df, alt_spec_df, alt_line_t_df, alt_spec_t_df])
def alternative ( input_data : pd . DataFrame , plot_dir : Union [ None , Path ] = None , boxcox : bool = False ) -> Tuple [ pd . DataFrame , pd . DataFrame , pd . DataFrame , pd . DataFrame ] : info_columns = [ 'staging' , 'line' , 'genotype' ] line_groupby = input_data . groupby ( 'line' ) label_names = list ( input_data . drop ( [ 'staging' , 'line' ] , axis = 'columns' ) . columns ) baseline = input_data [ input_data [ 'line' ] == 'baseline' ] baseline . loc [ : , 'genotype' ] = 'wt' alt_line_pvalues = [ ] alt_spec_pvalues = [ ] alt_line_t = [ ] alt_spec_t = [ ] for line_id , line_df in line_groupby : if line_id == 'baseline' : continue line_df . loc [ : , 'genotype' ] = 'hom' line_df . drop ( [ 'line' ] , axis = 1 ) df_wt_mut = pd . concat ( [ baseline , line_df ] ) data_df = df_wt_mut . drop ( columns = info_columns ) labels_to_skip = [ col for col , isany in line_df . any ( ) . iteritems ( ) if not isany ] if labels_to_skip : data_df [ labels_to_skip ] = 0.0 data = data_df . values info = df_wt_mut [ [ 'staging' , 'line' , 'genotype' ] ] p : np . array t : np . array p , t = lm_sm ( data , info ) res_p = [ line_id ] + list ( p ) alt_line_pvalues . append ( res_p ) res_t = [ line_id ] + list ( t ) alt_line_t . append ( res_t ) mutants = input_data [ input_data [ 'line' ] != 'baseline' ] for specimen_id , row in mutants . iterrows ( ) : row [ 'genotype' ] = 'hom' line_id = row [ 'line' ] df_wt_mut = baseline . append ( row ) data_df = df_wt_mut . drop ( columns = [ 'line' , 'genotype' , 'staging' ] ) labels_to_skip = [ col for col , isany in pd . DataFrame ( row ) . T . any ( ) . iteritems ( ) if not isany ] if labels_to_skip : data_df [ labels_to_skip ] = 0.0 data = data_df . values info = df_wt_mut [ [ 'genotype' , 'staging' ] ] p , t = lm_sm ( data , info ) res_p = [ line_id , specimen_id ] + list ( p ) alt_spec_pvalues . append ( res_p ) res_t = [ specimen_id ] + list ( t ) alt_spec_t . append ( res_t ) alt_line_df = pd . DataFrame . from_records ( alt_line_pvalues , columns = [ 'line' ] + label_names , index = 'line' ) alt_spec_df = pd . DataFrame . from_records ( alt_spec_pvalues , columns = [ 'line' , 'specimen' ] + label_names , index = 'specimen' ) alt_line_t_df = pd . DataFrame . from_records ( alt_line_t , columns = [ 'line' ] + label_names , index = 'line' ) alt_spec_t_df = pd . DataFrame . from_records ( alt_spec_t , columns = [ 'specimen' ] + label_names , index = 'specimen' ) return strip_x ( [ alt_line_df , alt_spec_df , alt_line_t_df , alt_spec_t_df ] )
Generate alterntive (mutant) distributions for line and pecimen-level data
Parameters
Generate alterntive ( mutant ) distributions for line and pecimen - level data Parameters
"""
    Generate alterntive (mutant) distributions for line and pecimen-level data

    Parameters
    ----------
    input_data
    plot_dir
    boxcox

    Returns
    -------
    alternative distribution dataframes with either line or specimen as index.
        0: line-level p values
        1: specimen-level p values
        2: line-level t-values
        3: specimen-level t-values
    """ # Group by line and sequntaily run # Columns of non-organ volumes in input_data # Get line-level alternative distributions # Lm code needs daatpoints in numpy array and genotype+staging in dataframe # Get columns (labels) where all speciemns have null values (i.e. all the line is QC-flagged at these labels) # Set the whole label column to zero. R:lm() will return NaN for this column # Get a numpy array of the organ volumes # returns p_values for all organs, 1 iteration # line_name, label_1, label_2 ...... ### Get specimen-level alternative distributions ### # baselines = input_data[input_data['line'] == 'baseline'] # 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 the whole label column to zero. R:lm() will return NaN for this column # returns p_values for all organs, 1 iteration # result dataframes have either line or specimen in index then labels
input_data
pd.DataFrame
plot_dir
Union[None, Path]
boxcox
bool