-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkfold_cv.py
More file actions
185 lines (129 loc) · 5.79 KB
/
kfold_cv.py
File metadata and controls
185 lines (129 loc) · 5.79 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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import helper_funcs as hf
import testing_funcs as testf
import concurrent.futures
import time
import os
def k_fold_partition(tsv_file_path, k):
"""
Partitions the dataset into k folds for cross-validation.
Args:
tsv_file_path (str) : Path to the TSV data file
k (int) : Number of folds
Output:
list_of_dfs (list) : List of DataFrames for each fold"""
print(f"Loading data from {tsv_file_path}...")
df = hf.load_tsv_file(tsv_file_path)
df = df.sample(frac=1, random_state=42).reset_index(drop=True)
indices = np.array_split(np.arange(len(df)), k)
list_of_dfs = [df.iloc[idx].reset_index(drop=True) for idx in indices]
return list_of_dfs
def divide_datasets(list_of_dfs):
"""
Divides the list of DataFrames into training and testing pairs for k-fold CV.
Args:
list_of_dfs (list) : List of DataFrames for each fold
Output:
ds_pairs (list) : List of [training_set, testing_set] pairs for each fold
"""
k = len(list_of_dfs)
ds_pairs = []
for i in range(k):
test_df = list_of_dfs[i].copy()
train_dfs = [list_of_dfs[j] for j in range(k) if j != i]
train_df = pd.concat(train_dfs).reset_index(drop=True)
ds_pairs.append([train_df, test_df])
return ds_pairs
def single_pair_test(args):
"""
Tests a single train-test pair for given parameters.
Also saves the result as a .csv file.
Args:
ds_pair (list) : [training_set, testing_set] DataFrames
tf_id (str) : Transcription Factor ID
chr_id (str) : Chromosome ID
fasta_file_path (str) : Path to the FASTA file
markov_order (int) : Markov Order
Output:
results (dict) : Dictionary containing AU_PRC and AU_ROC results
"""
ds_pair,tf_id,chr_id,fasta_file_path,markov_order,k,fold_idx = args
training_set = ds_pair[0]
test_set = ds_pair[1]
pid = os.getpid()
print(f"[Process {pid}] Starting Fold {fold_idx + 1}...")
start_time = time.time()
#TRAINING
print(f"--- Processing Fold (Order {markov_order}) ---")
bound_df = hf.stripped_df(df=training_set,
tf_id = tf_id,
bclass='B'
)
unbound_df = hf.stripped_df(df=training_set,
tf_id = tf_id,
bclass='U'
)
b_matrix = hf.construct_transition_matrix(markov_order=markov_order,
fasta_file_path=f'{fasta_file_path}',
target_df=bound_df,
chr_id=f'{chr_id}',
tf_id=f'{tf_id}')
u_matrix = hf.construct_transition_matrix(markov_order=markov_order,
fasta_file_path=f'{fasta_file_path}',
target_df=unbound_df,
chr_id=f'{chr_id}',
tf_id=f'{tf_id}')
#TESTING
print("Beginning Testing...")
test_res_df = hf.binding_prob_database(markov_order=markov_order,
tf_data=test_set,
fasta_file_path=fasta_file_path,
chr_id=chr_id,
bmatrix=b_matrix,
umatrix=u_matrix)
prs_vals = testf.prec_rec_spec(test_res_df=test_res_df,
chr_id = chr_id,
tf_id = tf_id,
markov_order=markov_order)
prs_vals.to_csv(f'resultData/m{markov_order}k{k}f{fold_idx + 1}.csv', index=False)
results = {
'Fold': fold_idx + 1,
'AU_PRC': testf.AU_PRC(prs_vals=prs_vals),
'AU_ROC': testf.AU_ROC(prs_vals=prs_vals),
'Time_Sec': round(time.time() - start_time, 2)
}
print(f"[Process {pid}] Finished Fold {fold_idx + 1}. Time: {results['Time_Sec']}s")
return results
def run_kfold_parallel(tsv_path, fasta_path, tf_id, chr_id, markov_order, k=10, num_cpus=None):
"""
Runs k-fold cross-validation in parallel.
Args:
tsv_path (str) : Path to the TSV data file
fasta_path (str) : Path to the FASTA file path
tf_id (str) : Transcription Factor ID
chr_id (str) : Chromosome ID
markov_order (int) : Markov Order
k (int) : Number of folds
num_cpus(int) : Nuumber of cores to be utilized for parallel processing. Uses all available cores if set to None
Output:
results_df (DataFrame) : DataFrame containing results for each fold"""
partitions = k_fold_partition(tsv_path, k)
ds_pairs = divide_datasets(partitions)
effective_cpus = num_cpus if num_cpus is not None else os.cpu_count()
tasks = []
for i, pair in enumerate(ds_pairs):
tasks.append((pair, tf_id, chr_id, fasta_path, markov_order,k, i))
print(f"\n--- Starting Parallel Execution on {effective_cpus} Cores ---\n")
results = []
with concurrent.futures.ProcessPoolExecutor(max_workers=num_cpus) as executor:
# map returns results in the order they were started
for res in executor.map(single_pair_test, tasks):
results.append(res)
results_df = pd.DataFrame(results)
print(f"\n--- Final K-Fold Results; m={markov_order},k={k}---")
print(results_df)
print("\nAverage AU_PRC:", results_df['AU_PRC'].mean(), "Standard Deviation: ", results_df['AU_PRC'].std())
print("Average AU_ROC:", results_df['AU_ROC'].mean(), "Standard Deviation: ", results_df['AU_ROC'].std())
return results_df