saving and organising work to call form cmd line

This commit is contained in:
Tanushree Tunstall 2022-05-28 11:25:04 +01:00
parent d9a1888e8c
commit f2634f77ef
5 changed files with 232 additions and 106 deletions

View file

@ -16,11 +16,29 @@ import pprint as pp
from copy import deepcopy
from collections import Counter
from sklearn.impute import KNNImputer as KNN
from imblearn.over_sampling import RandomOverSampler
from imblearn.under_sampling import RandomUnderSampler
from imblearn.over_sampling import SMOTE
from sklearn.datasets import make_classification
from imblearn.combine import SMOTEENN
from imblearn.combine import SMOTETomek
from imblearn.over_sampling import SMOTENC
from imblearn.under_sampling import EditedNearestNeighbours
from imblearn.under_sampling import RepeatedEditedNearestNeighbours
#%% REMOVE once config is set up
from UQ_MultModelsCl import MultModelsCl
rs = {'random_state': 42}
njobs = {'n_jobs': 10}
#%%
homedir = os.path.expanduser("~")
#==============
# directories
#==============
#==============a
datadir = homedir + '/git/Data/'
indir = datadir + drug + '/input/'
outdir = datadir + drug + '/output/'
@ -122,12 +140,12 @@ common_cols_stabiltyN = ['ligand_distance'
, 'ddg_dynamut2']
foldX_cols = ['contacts'
#, 'electro_rr', 'electro_mm', 'electro_sm', 'electro_ss'
#, 'disulfide_rr', 'disulfide_mm', 'disulfide_sm', 'disulfide_ss'
#, 'hbonds_rr', 'hbonds_mm', 'hbonds_sm', 'hbonds_ss'
#, 'partcov_rr', 'partcov_mm', 'partcov_sm', 'partcov_ss'
#, 'vdwclashes_rr', 'vdwclashes_mm', 'vdwclashes_sm', 'vdwclashes_ss'
#, 'volumetric_rr', 'volumetric_mm', 'volumetric_ss'
, 'electro_rr', 'electro_mm', 'electro_sm', 'electro_ss'
, 'disulfide_rr', 'disulfide_mm', 'disulfide_sm', 'disulfide_ss'
, 'hbonds_rr', 'hbonds_mm', 'hbonds_sm', 'hbonds_ss'
, 'partcov_rr', 'partcov_mm', 'partcov_sm', 'partcov_ss'
, 'vdwclashes_rr', 'vdwclashes_mm', 'vdwclashes_sm', 'vdwclashes_ss'
, 'volumetric_rr', 'volumetric_mm', 'volumetric_ss'
]
X_strFN = ['rsa'
@ -196,7 +214,6 @@ all_df_wtgt = training_df[numerical_FN + categorical_FN + ['dst_mode']]
all_df_wtgt.shape
#%%================================================================
#%% Apply ML
#TODO: A
#%% Data
#------
@ -222,17 +239,89 @@ X_bts_wt = blind_test_df[numerical_FN + ['dst_mode']]
# Quick check
(X['ligand_affinity_change']==0).sum() == (X['ligand_distance']>10).sum()
#%% MultClassPipeSKFCV: function call()
# mm_skf_scoresD = MultClassPipeSKFCV(input_df = X
# , target = y
# , var_type = 'numerical'
# , skf_cv = skf_cv)
##############################################################################
print('Original Data\n', Counter(y)
, 'Data dim:', X.shape)
###############################################################################
#%%
############################################################################
# RESAMPLING
###############################################################################
#------------------------------
# Simple Random oversampling
# [Numerical + catgeorical]
#------------------------------
oversample = RandomOverSampler(sampling_strategy='minority')
X_ros, y_ros = oversample.fit_resample(X, y)
print('Simple Random OverSampling\n', Counter(y_ros))
print(X_ros.shape)
# mm_skf_scores_df_all = pd.DataFrame(mm_skf_scoresD)
# mm_skf_scores_df_all
# mm_skf_scores_df_test = mm_skf_scores_df_all.filter(like='test_', axis=0)
# mm_skf_scores_df_train = mm_skf_scores_df_all.filter(like='train_', axis=0) # helps to see if you trust the results
# print(mm_skf_scores_df_train)
# print(mm_skf_scores_df_test)
#------------------------------
# Simple Random Undersampling
# [Numerical + catgeorical]
#------------------------------
undersample = RandomUnderSampler(sampling_strategy='majority')
X_rus, y_rus = undersample.fit_resample(X, y)
print('Simple Random UnderSampling\n', Counter(y_rus))
print(X_rus.shape)
#------------------------------
# Simple combine ROS and RUS
# [Numerical + catgeorical]
#------------------------------
oversample = RandomOverSampler(sampling_strategy='minority')
X_ros, y_ros = oversample.fit_resample(X, y)
undersample = RandomUnderSampler(sampling_strategy='majority')
X_rouC, y_rouC = undersample.fit_resample(X_ros, y_ros)
print('Simple Combined Over and UnderSampling\n', Counter(y_rouC))
print(X_rouC.shape)
#------------------------------
# SMOTE_NC: oversampling
# [numerical + categorical]
#https://stackoverflow.com/questions/47655813/oversampling-smote-for-binary-and-categorical-data-in-python
#------------------------------
# Determine categorical and numerical features
numerical_ix = X.select_dtypes(include=['int64', 'float64']).columns
numerical_ix
num_featuresL = list(numerical_ix)
numerical_colind = X.columns.get_indexer(list(numerical_ix) )
numerical_colind
categorical_ix = X.select_dtypes(include=['object', 'bool']).columns
categorical_ix
categorical_colind = X.columns.get_indexer(list(categorical_ix))
categorical_colind
k_sm = 5 # 5 is deafult
sm_nc = SMOTENC(categorical_features=categorical_colind, k_neighbors = k_sm, **rs, **njobs)
X_smnc, y_smnc = sm_nc.fit_resample(X, y)
print('SMOTE_NC OverSampling\n', Counter(y_smnc))
print(X_smnc.shape)
###############################################################################
#%% SMOTE RESAMPLING for NUMERICAL ONLY*
# #------------------------------
# # SMOTE: Oversampling
# # [Numerical ONLY]
# #------------------------------
# k_sm = 1
# sm = SMOTE(sampling_strategy = 'auto', k_neighbors = k_sm, **rs)
# X_sm, y_sm = sm.fit_resample(X, y)
# print(X_sm.shape)
# print('SMOTE OverSampling\n', Counter(y_sm))
# y_sm_df = y_sm.to_frame()
# y_sm_df.value_counts().plot(kind = 'bar')
# #------------------------------
# # SMOTE: Over + Undersampling COMBINED
# # [Numerical ONLY]
# #-----------------------------
# sm_enn = SMOTEENN(enn=EditedNearestNeighbours(sampling_strategy='all', **rs, **njobs ))
# X_enn, y_enn = sm_enn.fit_resample(X, y)
# print(X_enn.shape)
# print('SMOTE Over+Under Sampling combined\n', Counter(y_enn))
###############################################################################
# TODO: Find over and undersampling JUST for categorical data