Skip to content

Commit 00b71a3

Browse files
committed
Modified the output of the code
1 parent 3fbbb8f commit 00b71a3

File tree

1 file changed

+63
-66
lines changed

1 file changed

+63
-66
lines changed

MAgentOmics.ipynb

+63-66
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@
8080
" \n",
8181
" def __str__(self):\n",
8282
" return f\"\"\"\n",
83-
"Number of featurs: {self._num_features}\n",
84-
"Pheromone values: {self._pheremone}\n",
85-
"Probability value: {self._probability_value}\n",
83+
" Number of featurs: {self._num_features}\n",
84+
" Pheromone values: {self._pheremone}\n",
85+
" Probability value: {self._probability_value}\n",
8686
"\"\"\""
8787
]
8888
},
@@ -166,12 +166,12 @@
166166
" \n",
167167
" def __str__(self):\n",
168168
" return f\"\"\"\n",
169-
"Selected features: {self._selected_features}\n",
170-
"Last selected feature: {self._last_selected_feature} \n",
171-
"Last selected view: {self._last_selected_view}\n",
172-
"Sum of relevance values: {self._total_relevance_value}\n",
173-
"Sum of correlation values: {self._total_correlation_value}\n",
174-
"Total performance: {self._total_performance_value}\n",
169+
" Selected features: {self._selected_features}\n",
170+
" Last selected feature: {self._last_selected_feature} \n",
171+
" Last selected view: {self._last_selected_view}\n",
172+
" Sum of relevance values: {self._total_relevance_value}\n",
173+
" Sum of correlation values: {self._total_correlation_value}\n",
174+
" Total performance: {self._total_performance_value}\n",
175175
"\"\"\""
176176
]
177177
},
@@ -473,26 +473,26 @@
473473
" \n",
474474
" def print_agents(self):\n",
475475
" for agent_index in range(self.total_num_agents):\n",
476-
" print(f\"########## Agent {agent_index} ###########\")\n",
476+
" print(f\" ########## Agent {agent_index} ###########\")\n",
477477
" print(self._agents[agent_index])\n",
478-
" print(\"#########################################\")\n",
478+
" print(\" #########################################\")\n",
479479
" \n",
480480
" def print_views(self):\n",
481481
" for view_index in range(self.num_views):\n",
482-
" print(f\"############# View {view_index} ########\")\n",
482+
" print(f\" ############# View {view_index} ########\")\n",
483483
" print(self.views[view_index])\n",
484-
" print(\"#########################################\")\n",
484+
" print(\" #########################################\")\n",
485485
" \n",
486486
" def start(self):\n",
487487
" for iteration_index in range(self.num_iters):\n",
488-
" print(f\"------------------------------- iteration {iteration_index} -------------------------------\")\n",
488+
" print(f\" ------------------------------- Iteration {iteration_index + 1} -------------------------------\")\n",
489489
" self.reset_feature_counter()\n",
490490
" self.reset_agents()\n",
491491
" self.set_agents_start_nodes()\n",
492492
" views_probability = self.get_views_probability()\n",
493493
" \n",
494494
" for feature_index in range(self.num_selected_features - 1):\n",
495-
" print(f\" ---------- Selected feature {feature_index} --------------------- \")\n",
495+
" print(f\" ---------- Current selected feature {feature_index + 2} --------------------- \")\n",
496496
" for agent_index in range(self.total_num_agents):\n",
497497
" next_view, next_feature, sum_correlation, relevance_value = self.apply_state_transition_rule(agent_index,\n",
498498
" views_probability)\n",
@@ -509,17 +509,17 @@
509509
" \n",
510510
" def __str__(self):\n",
511511
" return f\"\"\"\n",
512-
"Number of iteration: {self.num_iters}\n",
513-
"Number of selected features: {self.num_selected_features} \n",
514-
"Alpha: {self.alpha}\n",
515-
"Beta: {self.beta}\n",
516-
"Q0: {self.q0}\n",
517-
"Discount rate: {self.discount_rate}\n",
518-
"Number of views: {self.num_views}\n",
519-
"Number of agents: {self.num_agents}\n",
520-
"Total number of agents: {self.total_num_agents}\n",
521-
"Best selected features: {self.best_selected_features}\n",
522-
"Count corr computation: {self._count_correlation_computation}\n",
512+
" Number of iteration: {self.num_iters}\n",
513+
" Number of selected features: {self.num_selected_features} \n",
514+
" Alpha: {self.alpha}\n",
515+
" Beta: {self.beta}\n",
516+
" Q0: {self.q0}\n",
517+
" Discount rate: {self.discount_rate}\n",
518+
" Number of views: {self.num_views}\n",
519+
" Number of agents: {self.num_agents}\n",
520+
" Total number of agents: {self.total_num_agents}\n",
521+
" Best selected features: {self.best_selected_features}\n",
522+
" Count corr computation: {self._count_correlation_computation}\n",
523523
"\"\"\" "
524524
]
525525
},
@@ -900,21 +900,6 @@
900900
"omics3_final = omics3_final.dropna(axis=1)"
901901
]
902902
},
903-
{
904-
"cell_type": "code",
905-
"execution_count": null,
906-
"id": "eb7df36e",
907-
"metadata": {},
908-
"outputs": [],
909-
"source": [
910-
"def count_expected_removed_features(df):\n",
911-
" count_values = 0\n",
912-
" for col in df.columns.values:\n",
913-
" if df[col].isna().sum() > 0:\n",
914-
" count_values += 1\n",
915-
" return count_values"
916-
]
917-
},
918903
{
919904
"cell_type": "code",
920905
"execution_count": null,
@@ -923,9 +908,7 @@
923908
"outputs": [],
924909
"source": [
925910
"#DNA_methylation Dataset\n",
926-
"print('#Original features = ', len(omics1_final.columns))\n",
927-
"# print('#Remaind features (Without missing values) = ', len(df_dna_transposed_shrinked_removed.columns))\n",
928-
"print('#Expected removed features = ', count_expected_removed_features(omics1_final))"
911+
"print('#Remained features = ', len(omics1_final.columns))"
929912
]
930913
},
931914
{
@@ -936,9 +919,7 @@
936919
"outputs": [],
937920
"source": [
938921
"#Genelevel_copy_number_alteration_CNA Dataset\n",
939-
"print('#Original features = ', len(omics2_final.columns))\n",
940-
"# print('#Remaind features (Without missing values) = ', len(df_cna_transposed_shrinked_removed.columns))\n",
941-
"print('#Expected removed features = ', count_expected_removed_features(omics2_final))"
922+
"print('#Remained features = ', len(omics2_final.columns))"
942923
]
943924
},
944925
{
@@ -949,9 +930,7 @@
949930
"outputs": [],
950931
"source": [
951932
"#RNASeq Dataset\n",
952-
"print('#Original features = ', len(omics3_final.columns))\n",
953-
"# print('#Remaind features (Without missing values) = ', len(df_rna_transposed_shrinked_removed.columns))\n",
954-
"print('#Expected removed features = ', count_expected_removed_features(omics3_final))"
933+
"print('#Remained features = ', len(omics3_final.columns))"
955934
]
956935
},
957936
{
@@ -1064,7 +1043,8 @@
10641043
"from sklearn.model_selection import RepeatedStratifiedKFold\n",
10651044
"from sklearn.linear_model import LogisticRegression\n",
10661045
"from sklearn.ensemble import RandomForestClassifier\n",
1067-
"from sklearn.metrics import accuracy_score"
1046+
"from sklearn.metrics import accuracy_score\n",
1047+
"from sklearn.model_selection import cross_val_score"
10681048
]
10691049
},
10701050
{
@@ -1105,12 +1085,6 @@
11051085
},
11061086
"outputs": [],
11071087
"source": [
1108-
"from sklearn.model_selection import RepeatedStratifiedKFold\n",
1109-
"from sklearn.model_selection import cross_val_score\n",
1110-
"from sklearn.linear_model import LogisticRegression\n",
1111-
"from sklearn.metrics import accuracy_score\n",
1112-
"from sklearn.metrics import mean_absolute_error\n",
1113-
"\n",
11141088
"# settings of Multi Agent algorithm\n",
11151089
"init_pheromone_value = 0.2\n",
11161090
"num_views = 3\n",
@@ -1126,13 +1100,14 @@
11261100
"\n",
11271101
"# repeat algorithm for different sizes of feature subsets\n",
11281102
"for feature_size in feature_sizes:\n",
1129-
" print(f\"\\n\\n*************feature size {feature_size}**********************\")\n",
1103+
" print(\"\\n\\n***************************************************************\")\n",
1104+
" print(f\"********************* Feature size {feature_size} *************************\")\n",
1105+
" print(\"***************************************************************\")\n",
11301106
" model_acc = {}\n",
11311107
" # configurations to repeat the k-fold cross-validation process (designed for imbalanced Classification)\n",
1132-
" cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=10, random_state=1)\n",
1108+
" cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=5, random_state=1)\n",
11331109
" for train_index, test_index in cv.split(omics1_final, y=label_final):\n",
11341110
" # split datasets into training and test sets\n",
1135-
" print(\"\\n\\n****************** Split **********************\")\n",
11361111
" omics1_final_train, omics1_final_test = omics1_final.iloc[train_index], omics1_final.iloc[test_index]\n",
11371112
" omics2_final_train, omics2_final_test = omics2_final.iloc[train_index], omics2_final.iloc[test_index]\n",
11381113
" omics3_final_train, omics3_final_test = omics3_final.iloc[train_index], omics3_final.iloc[test_index]\n",
@@ -1161,7 +1136,7 @@
11611136
" alg.start()\n",
11621137
" final_subset = alg.best_selected_features\n",
11631138
"\n",
1164-
" print(f\"\\n\\n Final selected subset: {final_subset}\")\n",
1139+
" print(f\"\\n\\n Final selected subset: {final_subset}\")\n",
11651140
" \n",
11661141
" # create reduced datasets based on final selected features\n",
11671142
" feature_indices_view1 = final_subset.get(0, [])\n",
@@ -1184,6 +1159,7 @@
11841159
" # get the list of models to evaluate performance\n",
11851160
" models = get_models()\n",
11861161
" # evaluate each model\n",
1162+
" new_output_file_lines = f\"Feature size: {feature_size}\"\n",
11871163
" for model_index in models:\n",
11881164
" acc = evaluate_model(models[model_index], \n",
11891165
" X_train=final_train_dataset.values,\n",
@@ -1193,13 +1169,34 @@
11931169
" value = model_acc.get(model_index, [])\n",
11941170
" value.append(acc)\n",
11951171
" model_acc[model_index] = value\n",
1172+
" print(f\" Classifier index ({model_index}) --> classification accuracy: {acc}\")\n",
1173+
" new_output_file_lines += f\"\\nClassifier index ({model_index}) --> classification accuracy {model_acc[model_index]}\"\n",
1174+
"\n",
1175+
" # write the current results in the output file\n",
1176+
" output_file_lines = \"\"\n",
1177+
" try:\n",
1178+
" output_file_lines = open(\"output_multi_agent.txt\", 'r').readlines()\n",
1179+
" output_file_lines[-3:] = new_output_file_lines\n",
1180+
" except Exception:\n",
1181+
" output_file_lines = new_output_file_lines\n",
11961182
" \n",
1197-
" with open(\"output_multi_agent.txt\", \"a\") as f:\n",
1198-
" print(f\"Feature size: {feature_size}\", file=f)\n",
1199-
" print(model_acc, file=f)\n",
1200-
" \n",
1183+
" open(\"output_multi_agent.txt\", 'w').writelines(output_file_lines)\n",
1184+
" \n",
1185+
" \n",
1186+
" print(\" ###########################################################################\")\n",
1187+
" print(\" ###########################################################################\")\n",
1188+
" \n",
1189+
" # prepare the output file for the next feature size\n",
1190+
" try:\n",
1191+
" output_file_lines = open(\"output_multi_agent.txt\", 'r').readlines()\n",
1192+
" output_file_lines.append(\"\\n\\n\\n\\n\\n\")\n",
1193+
" open(\"output_multi_agent.txt\", 'w').writelines(output_file_lines)\n",
1194+
" except Exception:\n",
1195+
" pass\n",
1196+
" \n",
1197+
" print(f\"Final classification accuracies for {feature_size} selected features\")\n",
12011198
" for model_index in model_acc:\n",
1202-
" print(f\"model index: {model_index},,, acc: {model_acc[model_index]}\")"
1199+
" print(f\" Classifier index ({model_index}) --> classification accuracy: {model_acc[model_index]}\")"
12031200
]
12041201
}
12051202
],

0 commit comments

Comments
 (0)