[6ac965]: / src / iterpretability / datasets / data_loader.py

Download this file

225 lines (182 with data), 8.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import pickle
import pandas as pd
import numpy as np
from catenets.datasets import load as catenets_load
from src.iterpretability.datasets.news.process_news import process_news
from src.iterpretability.datasets.tcga.process_tcga import process_tcga
from src.utils import gen_unique_ids
import os
def remove_unbalanced(df, threshold=0.8):
ub_col = []
for col in df.select_dtypes(exclude='object').columns:
if df[col].nunique() < 6:
distribution = df[col].value_counts(normalize=True)
if distribution.max() > threshold:
ub_col.append(col)
df = df.drop(columns=ub_col)
return df
def load(dataset_name: str, train_ratio: float = 1.0,
directory_path_: str = None,
repo_path: str = None,
debug=False,
sim_type=None,
n_samples=None):
"""
Load the dataset
"""
feature_names = None
if "tcga" in dataset_name:
try:
tcga_dataset = pickle.load(
open(
repo_path + "/data/tcga/" + str(dataset_name) + ".p",
"rb",
)
)
except:
process_tcga(
max_num_genes=100, file_location=repo_path + "/data/tcga/"
)
tcga_dataset = pickle.load(
open(
repo_path + "/data/tcga/" + str(dataset_name) + ".p",
"rb",
)
)
# get gene names
# ids = tcga_dataset["ids"]
X_raw = tcga_dataset["rnaseq"][:5000]
elif "news" in dataset_name:
try:
news_dataset = pickle.load(
open(
"src/iterpretability/datasets/news/" + str(dataset_name) + ".p",
"rb",
)
)
except:
process_news(
max_num_features=100, file_location="src/iterpretability/datasets/news/"
)
news_dataset = pickle.load(
open(
"src/iterpretability/datasets/news/" + str(dataset_name) + ".p",
"rb",
)
)
X_raw = news_dataset
elif "twins" in dataset_name:
# Total features = 39
X_raw, _, _, _, _, _ = catenets_load(dataset_name, train_ratio=1.0)
# Remove columns which almost only contain one value from X_raw
# Assuming X_raw is your numpy array
# threshold = 2 # Adjust this value based on your definition of "almost only one value"
# # Find the columns to keep
# cols_to_keep = [i for i in range(X_raw.shape[1]) if np.unique(X_raw[:, i]).size > threshold and i != 3]
# # Create a new array with only the columns to keep
# X_raw = X_raw[:, cols_to_keep]
elif "acic" in dataset_name:
# Total features = 55
X_raw, _, _, _, _, _, _, _ = catenets_load("acic2016")
elif dataset_name.startswith("depmap_drug_screen"):
data = pd.read_csv(repo_path + "/data/DepMap_24Q2/real/"+dataset_name+".csv", index_col=0)
outcomes = data[["LFC_az_628", "LFC_imatinib"]].to_numpy()
outcomes = outcomes.reshape(outcomes.shape[0], outcomes.shape[1], 1)
data = data.drop(["LFC_az_628", "LFC_imatinib"], axis=1)
X_raw = data.to_numpy()
elif dataset_name.startswith("depmap_crispr_screen"):
data = pd.read_csv(repo_path + "/data/DepMap_24Q2/real/"+dataset_name+".csv", index_col=0)
outcomes = data[["LFC_BRAF", "LFC_EGFR"]].to_numpy()
outcomes = outcomes.reshape(outcomes.shape[0], outcomes.shape[1], 1)
data = data.drop(["LFC_BRAF", "LFC_EGFR"], axis=1)
X_raw = data.to_numpy()
elif dataset_name.startswith("ovarian_semi_synthetic"):
data = pd.read_csv(repo_path + "/data/DepMap_24Q2/real/"+dataset_name+".csv", index_col=0)
outcomes = data[["pred_a0_y0", "pred_a1_y0"]].to_numpy()
outcomes = outcomes.reshape(outcomes.shape[0], outcomes.shape[1], 1)
data = data.drop(["pred_a0_y0", "pred_a1_y0"], axis=1)
X_raw = data.to_numpy()
elif dataset_name.startswith("melanoma_semi_synthetic"):
data = pd.read_csv(repo_path + "/data/DepMap_24Q2/real/"+dataset_name+".csv", index_col=0)
outcomes = data[["pred_a0_y2 (immuno)", "pred_a1_y2 (immuno)"]].to_numpy()
outcomes = outcomes.reshape(outcomes.shape[0], outcomes.shape[1], 1)
outcomes = outcomes.astype(int)
data = data.drop(["pred_a0_y2 (immuno)", "pred_a1_y2 (immuno)"], axis=1)
X_raw = data.to_numpy()
elif dataset_name.startswith("toy_data"):
if sim_type == "T":
raise ValueError("Toy example data does not have treatment outcomes")
data = pd.read_csv(repo_path + "/data/toy/"+dataset_name+".csv", index_col=0)
X_raw = data.to_numpy()
elif dataset_name.startswith("cytof"):
data = pd.read_csv(directory_path_ + dataset_name + ".csv", index_col=0)
feature_names = data.columns
# Also return true outcomes for TSimulation
if sim_type == "T":
all_treatment_outcomes_cols = [col for col in data.columns if col.startswith("09")]
outcomes = data[all_treatment_outcomes_cols[:2]]
data = data.drop(all_treatment_outcomes_cols, axis=1)
# Raise value error if outcomes contains string or nan values
if outcomes.isnull().values.any():
raise ValueError("Outcomes contain nan values")
if outcomes.dtypes.apply(lambda x: x == 'object').any():
raise ValueError("Outcomes contain string values")
if type(outcomes) == pd.DataFrame:
outcomes = outcomes.replace({False:0, True:1})
outcomes = outcomes.to_numpy()
outcomes = outcomes.reshape(outcomes.shape[0], outcomes.shape[1], 1)
X_raw = data
elif directory_path_ is not None and os.path.isfile(directory_path_ + dataset_name + ".csv"):
data = pd.read_csv(directory_path_ + dataset_name + ".csv", index_col=0)
all_treatment_outcomes_cols = [col for col in data.columns if col.startswith("09")][:5]
# Also return true outcomes for TSimulation
if sim_type == "T":
outcomes = data[all_treatment_outcomes_cols]
# Raise value error if outcomes contains string or nan values
if outcomes.isnull().values.any():
raise ValueError("Outcomes contain nan values")
if outcomes.dtypes.apply(lambda x: x == 'object').any():
raise ValueError("Outcomes contain string values")
if type(outcomes) == pd.DataFrame:
outcomes = outcomes.replace({False:0, True:1})
outcomes = outcomes.to_numpy()
outcomes = outcomes.reshape(outcomes.shape[0], outcomes.shape[1], 1)
# Drop all columns starting with tre or out
X_raw = data.loc[:, ~data.columns.str.startswith('tre')]
X_raw = X_raw.loc[:, ~X_raw.columns.str.startswith('out')]
# One hot encode all categorical features
X_raw = pd.get_dummies(X_raw)
# Make sure there are no boolean variables
X_raw = X_raw.replace({False:0, True:1})
# Remove highly unbalanced features
X_raw = remove_unbalanced(X_raw, 0.8)
# Normalize all columns of the dataframe
X_raw = X_raw.apply(lambda x: (x - x.min()) / (x.max() - x.min()))
# X_raw.index = gen_unique_ids(X_raw.shape[0])
else:
raise Exception("Unknown dataset " + str(dataset_name) + "File:", directory_path_ + dataset_name + ".csv")
debug_size = 200
if type(X_raw) == pd.DataFrame:
X_raw = X_raw.to_numpy()
if train_ratio == 1.0:
if debug:
X_raw = X_raw[:int(debug_size*train_ratio),:]
if sim_type == "T":
outcomes = outcomes[:int(debug_size*train_ratio),:]
if sim_type == "T":
return X_raw, outcomes, feature_names
else:
return X_raw, feature_names
else:
X_raw_train = X_raw[: int(train_ratio * X_raw.shape[0])]
X_raw_test = X_raw[int(train_ratio * X_raw.shape[0]) :]
if debug:
X_raw_train = X_raw_train[:int(debug_size*train_ratio),:]
X_raw_test = X_raw_test[:int(debug_size*(1-train_ratio)),:]
if sim_type == "T":
outcomes_train = outcomes[:int(debug_size*train_ratio),:]
outcomes_test = outcomes[int(debug_size*train_ratio):int(debug_size*(1-train_ratio)),:]
if sim_type == "T":
return X_raw_train, X_raw_test, outcomes_train, outcomes_test, feature_names
else:
return X_raw_train, X_raw_test, feature_names