Function for splitting drug combination dataset such that no combinations are shared across the split Args: df (pd.Dataframe): dataset to split seed (int): random seed frac (list): split fraction as a list Returns: dict: a dictionary of splitted dataframes, where keys are
(df, seed, frac)
| 737 | 'test': df.iloc[test].reset_index(drop = True)} |
| 738 | |
| 739 | def create_combination_split(df, seed, frac): |
| 740 | """ |
| 741 | Function for splitting drug combination dataset such that no combinations are shared across the split |
| 742 | |
| 743 | Args: |
| 744 | df (pd.Dataframe): dataset to split |
| 745 | seed (int): random seed |
| 746 | frac (list): split fraction as a list |
| 747 | |
| 748 | Returns: |
| 749 | dict: a dictionary of splitted dataframes, where keys are train/valid/test and values correspond to each dataframe |
| 750 | """ |
| 751 | |
| 752 | test_size = int(len(df) * frac[2]) |
| 753 | train_size = int(len(df) * frac[0]) |
| 754 | val_size = len(df) - train_size - test_size |
| 755 | np.random.seed(seed) |
| 756 | |
| 757 | # Create a new column for combination names |
| 758 | df['concat'] = df['Drug1_ID'] + ',' + df['Drug2_ID'] |
| 759 | |
| 760 | # Identify shared drug combinations across all target classes |
| 761 | combinations = [] |
| 762 | for c in df['Cell_Line_ID'].unique(): |
| 763 | df_cell = df[df['Cell_Line_ID'] == c] |
| 764 | combinations.append(set(df_cell['concat'].values)) |
| 765 | |
| 766 | intxn = combinations[0] |
| 767 | for c in combinations: |
| 768 | intxn = intxn.intersection(c) |
| 769 | |
| 770 | # Split combinations into train, val and test |
| 771 | test_choices = np.random.choice(list(intxn), |
| 772 | int(test_size / len(df['Cell_Line_ID'].unique())), |
| 773 | replace=False) |
| 774 | trainval_intxn = intxn.difference(test_choices) |
| 775 | val_choices = np.random.choice(list(trainval_intxn), |
| 776 | int(val_size / len(df['Cell_Line_ID'].unique())), |
| 777 | replace=False) |
| 778 | |
| 779 | ## Create train and test set |
| 780 | test_set = df[df['concat'].isin(test_choices)].drop(columns=['concat']) |
| 781 | val_set = df[df['concat'].isin(val_choices)] |
| 782 | train_set = df[~df['concat'].isin(test_choices)].reset_index(drop=True) |
| 783 | train_set = train_set[~train_set['concat'].isin(val_choices)] |
| 784 | |
| 785 | return {'train': train_set.reset_index(drop = True), |
| 786 | 'valid': val_set.reset_index(drop = True), |
| 787 | 'test': test_set.reset_index(drop = True)} |
| 788 | |
| 789 | # create time split |
| 790 |