python/pymethylprocess/preprocess.py
def imputation_pipeline(input_pkl,split_by_subtype=True,method='knn', solver='fancyimpute', n_neighbors=5, orientation='rows', output_pkl='', n_top_cpgs=0, feature_selection_method='mad', metric='correlation', n_neighbors_fs=10, disease_only=False, subtype_delimiter=',', sample_threshold=-1, cpg_threshold=-1, key='disease'): # wrap a class around this
    """Imputation of subtype or no subtype using various imputation methods."""
    import copy
    orientation_dict = {'CpGs':'columns','Samples':'rows'}
    orientation = orientation_dict[orientation]
    #print("Selecting orientation for imputation not implemented yet.")
    os.makedirs(output_pkl[:output_pkl.rfind('/')],exist_ok=True)
    if method in ['DeepCpG', 'DAPL', 'EM']:
        print('Method {} coming soon...'.format(method))
    elif solver in ['impyute']:
        print('Impyute coming soon...')
    else:
        imputer = ImputerObject(solver, method, opts=dict(k=n_neighbors, orientation=orientation, min_value=0., max_value=1.)).return_imputer()
    input_dict = pickle.load(open(input_pkl,'rb'))

    if cpg_threshold == -1.:
        cpg_threshold = None

    if sample_threshold == -1.:
        sample_threshold = None

    if split_by_subtype:

        def impute_arrays(methyl_arrays):
            for methyl_array in methyl_arrays:
                if key!='disease':
                    methyl_array=methyl_array[1]
                methyl_array.remove_missingness(cpg_threshold=cpg_threshold, sample_threshold=sample_threshold)
                methyl_array.impute(copy.deepcopy(imputer))
                yield methyl_array

        methyl_array = MethylationArray(*extract_pheno_beta_df_from_pickle_dict(input_dict))
        methyl_arrays = impute_arrays(methyl_array.split_by_subtype(disease_only, subtype_delimiter) if key=='disease' else methyl_array.groupby(key))
        methyl_array=MethylationArrays([next(methyl_arrays)]).combine(methyl_arrays)

    else:
        methyl_array = MethylationArray(*extract_pheno_beta_df_from_pickle_dict(input_dict))
        methyl_array.remove_missingness(cpg_threshold=cpg_threshold, sample_threshold=sample_threshold)
        methyl_array.impute(imputer)

    if n_top_cpgs:
        methyl_array.feature_select(n_top_cpgs,feature_selection_method, metric, nn=n_neighbors_fs)

    methyl_array.write_pickle(output_pkl)
Imputation of subtype or no subtype using various imputation methods.
Imputation of subtype or no subtype using various imputation methods.
Imputation of subtype or no subtype using various imputation methods .
def imputation_pipeline(input_pkl,split_by_subtype=True,method='knn', solver='fancyimpute', n_neighbors=5, orientation='rows', output_pkl='', n_top_cpgs=0, feature_selection_method='mad', metric='correlation', n_neighbors_fs=10, disease_only=False, subtype_delimiter=',', sample_threshold=-1, cpg_threshold=-1, key='disease'): 
    import copy
    orientation_dict = {'CpGs':'columns','Samples':'rows'}
    orientation = orientation_dict[orientation]
    os.makedirs(output_pkl[:output_pkl.rfind('/')],exist_ok=True)
    if method in ['DeepCpG', 'DAPL', 'EM']:
        print('Method {} coming soon...'.format(method))
    elif solver in ['impyute']:
        print('Impyute coming soon...')
    else:
        imputer = ImputerObject(solver, method, opts=dict(k=n_neighbors, orientation=orientation, min_value=0., max_value=1.)).return_imputer()
    input_dict = pickle.load(open(input_pkl,'rb'))
    if cpg_threshold == -1.:
        cpg_threshold = None
    if sample_threshold == -1.:
        sample_threshold = None
    if split_by_subtype:
        def impute_arrays(methyl_arrays):
            for methyl_array in methyl_arrays:
                if key!='disease':
                    methyl_array=methyl_array[1]
                methyl_array.remove_missingness(cpg_threshold=cpg_threshold, sample_threshold=sample_threshold)
                methyl_array.impute(copy.deepcopy(imputer))
                yield methyl_array
        methyl_array = MethylationArray(*extract_pheno_beta_df_from_pickle_dict(input_dict))
        methyl_arrays = impute_arrays(methyl_array.split_by_subtype(disease_only, subtype_delimiter) if key=='disease' else methyl_array.groupby(key))
        methyl_array=MethylationArrays([next(methyl_arrays)]).combine(methyl_arrays)
    else:
        methyl_array = MethylationArray(*extract_pheno_beta_df_from_pickle_dict(input_dict))
        methyl_array.remove_missingness(cpg_threshold=cpg_threshold, sample_threshold=sample_threshold)
        methyl_array.impute(imputer)
    if n_top_cpgs:
        methyl_array.feature_select(n_top_cpgs,feature_selection_method, metric, nn=n_neighbors_fs)
    methyl_array.write_pickle(output_pkl)
def imputation_pipeline ( input_pkl , split_by_subtype = True , method = 'knn' , solver = 'fancyimpute' , n_neighbors = 5 , orientation = 'rows' , output_pkl = '' , n_top_cpgs = 0 , feature_selection_method = 'mad' , metric = 'correlation' , n_neighbors_fs = 10 , disease_only = False , subtype_delimiter = ',' , sample_threshold = - 1 , cpg_threshold = - 1 , key = 'disease' ) : import copy orientation_dict = { 'CpGs' : 'columns' , 'Samples' : 'rows' } orientation = orientation_dict [ orientation ] os . makedirs ( output_pkl [ : output_pkl . rfind ( '/' ) ] , exist_ok = True ) if method in [ 'DeepCpG' , 'DAPL' , 'EM' ] : print ( 'Method {} coming soon...' . format ( method ) ) elif solver in [ 'impyute' ] : print ( 'Impyute coming soon...' ) else : imputer = ImputerObject ( solver , method , opts = dict ( k = n_neighbors , orientation = orientation , min_value = 0. , max_value = 1. ) ) . return_imputer ( ) input_dict = pickle . load ( open ( input_pkl , 'rb' ) ) if cpg_threshold == - 1. : cpg_threshold = None if sample_threshold == - 1. : sample_threshold = None if split_by_subtype : def impute_arrays ( methyl_arrays ) : for methyl_array in methyl_arrays : if key != 'disease' : methyl_array = methyl_array [ 1 ] methyl_array . remove_missingness ( cpg_threshold = cpg_threshold , sample_threshold = sample_threshold ) methyl_array . impute ( copy . deepcopy ( imputer ) ) yield methyl_array methyl_array = MethylationArray ( * extract_pheno_beta_df_from_pickle_dict ( input_dict ) ) methyl_arrays = impute_arrays ( methyl_array . split_by_subtype ( disease_only , subtype_delimiter ) if key == 'disease' else methyl_array . groupby ( key ) ) methyl_array = MethylationArrays ( [ next ( methyl_arrays ) ] ) . combine ( methyl_arrays ) else : methyl_array = MethylationArray ( * extract_pheno_beta_df_from_pickle_dict ( input_dict ) ) methyl_array . remove_missingness ( cpg_threshold = cpg_threshold , sample_threshold = sample_threshold ) methyl_array . impute ( imputer ) if n_top_cpgs : methyl_array . feature_select ( n_top_cpgs , feature_selection_method , metric , nn = n_neighbors_fs ) methyl_array . write_pickle ( output_pkl )
Imputation of subtype or no subtype using various imputation methods.
Imputation of subtype or no subtype using various imputation methods .
# wrap a class around this """Imputation of subtype or no subtype using various imputation methods.""" #print("Selecting orientation for imputation not implemented yet.")
input_pkl
split_by_subtype
method
solver
n_neighbors
orientation
output_pkl
n_top_cpgs
feature_selection_method
metric
n_neighbors_fs
disease_only
subtype_delimiter
sample_threshold
cpg_threshold
key