# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import pandas as pd
import numpy as np
import glob

data_df = pd.concat([pd.read_csv(x) for x in glob.glob('../datasets/lc_clusters_cv_96/unlabeled_*.csv')])

print('Cpds #: {}. Actives #: {}'.format(data_df.shape, data_df['PriA-SSB Activity'].sum()))
u, c = np.unique(data_df['Cluster_0.4'], return_counts=True)
n_clusters = u.shape[0]
n_singletons = np.where(c == 1)[0].shape[0]
n_singletons_with_hits = data_df[data_df['Cluster_0.4'].isin(u[np.where(c == 1)[0]])]['PriA-SSB Activity'].sum()
print('Clusters #: {}. Singletons #: {}. Singletons with hits #: {}'.format(n_clusters, 
                                                                            n_singletons,
                                                                            n_singletons_with_hits))
# -

# ---
# # Stratifying LC1234

# +
import pandas as pd
import numpy as np
import os
import glob

output_dir = '../datasets/'
n_folds = 10
random_seed = 20191101
np.random.seed(random_seed)

split_size = 96

dataset_dir = '../datasets/lc_clusters_cv/'
output_dir = '../datasets/lc_clusters_cv_96/'
data_df = pd.concat([pd.read_csv(x) for x in glob.glob(dataset_dir+'/*')])
cluster_col_name = 'Cluster_0.3'
task_col_name = 'PriA-SSB Activity'

active_indices = np.where(data_df[task_col_name] == 1)[0]
inactive_indices = np.where(data_df[task_col_name] == 0)[0]

print('Total molecules: {}, Total active: {}, Total inactive: {}, Total clusters: {}.'.format(data_df.shape[0], 
                                                                        active_indices.shape[0], 
                                                                        inactive_indices.shape[0],
                                                                        np.unique(data_df[cluster_col_name]).shape[0]))
# -

data_df = data_df.iloc[:,1:]

# +
num_splits = data_df.shape[0]//split_size + 1
actives_per_split = int(np.ceil(active_indices.shape[0]/num_splits))
inactives_per_split = split_size - actives_per_split
rnd_active_idx = np.random.permutation(active_indices)
rnd_inactive_idx = np.random.permutation(inactive_indices)

active_i, inactive_i = 0, 0
total_mols, total_actives, total_inactives = 0, 0, 0
for split_i in range(num_splits):
    curr_indices = []
    curr_split_size = split_size
    if split_i == (num_splits-1):
        curr_split_size = data_df.shape[0] - split_size*(num_splits-1)
    
    for i in range(actives_per_split):
        if active_i < rnd_active_idx.shape[0]:
            curr_indices.append(rnd_active_idx[active_i])
            active_i += 1
            curr_split_size-=1
    
    for i in range(curr_split_size):
        if inactive_i < rnd_inactive_idx.shape[0]:
            curr_indices.append(rnd_inactive_idx[inactive_i])
            inactive_i += 1
    
    curr_indices = np.random.permutation(curr_indices)
    curr_split_df = data_df.iloc[curr_indices,:]
    
    split_mols = curr_split_df.shape[0]
    split_actives = curr_split_df[curr_split_df[task_col_name] == 1].shape[0]
    split_inactives = curr_split_df[curr_split_df[task_col_name] == 0].shape[0]
    total_mols += split_mols
    total_actives += split_actives
    total_inactives += split_inactives
    curr_split_df.to_csv(output_dir+'/unlabeled_{}.csv'.format(split_i), 
                         index=False)
    print('Split {}: Total molecules: {}, Total active: {}, Total inactive: {}'.format(split_i, split_mols, 
                                                                                       split_actives, 
                                                                                       split_inactives))
    
print('Total molecules: {}, Total active: {}, Total inactive: {}.'.format(total_mols, total_actives, total_inactives))
# -

unlabeled_df.reset_index()



StarcoderdataJupyterScriptsDedupFiltered