Skip to content

Commit

Permalink
Modified the output of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
SinaTabakhi committed Apr 11, 2022
1 parent 3fbbb8f commit 00b71a3
Showing 1 changed file with 63 additions and 66 deletions.
129 changes: 63 additions & 66 deletions MAgentOmics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
" \n",
" def __str__(self):\n",
" return f\"\"\"\n",
"Number of featurs: {self._num_features}\n",
"Pheromone values: {self._pheremone}\n",
"Probability value: {self._probability_value}\n",
" Number of featurs: {self._num_features}\n",
" Pheromone values: {self._pheremone}\n",
" Probability value: {self._probability_value}\n",
"\"\"\""
]
},
Expand Down Expand Up @@ -166,12 +166,12 @@
" \n",
" def __str__(self):\n",
" return f\"\"\"\n",
"Selected features: {self._selected_features}\n",
"Last selected feature: {self._last_selected_feature} \n",
"Last selected view: {self._last_selected_view}\n",
"Sum of relevance values: {self._total_relevance_value}\n",
"Sum of correlation values: {self._total_correlation_value}\n",
"Total performance: {self._total_performance_value}\n",
" Selected features: {self._selected_features}\n",
" Last selected feature: {self._last_selected_feature} \n",
" Last selected view: {self._last_selected_view}\n",
" Sum of relevance values: {self._total_relevance_value}\n",
" Sum of correlation values: {self._total_correlation_value}\n",
" Total performance: {self._total_performance_value}\n",
"\"\"\""
]
},
Expand Down Expand Up @@ -473,26 +473,26 @@
" \n",
" def print_agents(self):\n",
" for agent_index in range(self.total_num_agents):\n",
" print(f\"########## Agent {agent_index} ###########\")\n",
" print(f\" ########## Agent {agent_index} ###########\")\n",
" print(self._agents[agent_index])\n",
" print(\"#########################################\")\n",
" print(\" #########################################\")\n",
" \n",
" def print_views(self):\n",
" for view_index in range(self.num_views):\n",
" print(f\"############# View {view_index} ########\")\n",
" print(f\" ############# View {view_index} ########\")\n",
" print(self.views[view_index])\n",
" print(\"#########################################\")\n",
" print(\" #########################################\")\n",
" \n",
" def start(self):\n",
" for iteration_index in range(self.num_iters):\n",
" print(f\"------------------------------- iteration {iteration_index} -------------------------------\")\n",
" print(f\" ------------------------------- Iteration {iteration_index + 1} -------------------------------\")\n",
" self.reset_feature_counter()\n",
" self.reset_agents()\n",
" self.set_agents_start_nodes()\n",
" views_probability = self.get_views_probability()\n",
" \n",
" for feature_index in range(self.num_selected_features - 1):\n",
" print(f\" ---------- Selected feature {feature_index} --------------------- \")\n",
" print(f\" ---------- Current selected feature {feature_index + 2} --------------------- \")\n",
" for agent_index in range(self.total_num_agents):\n",
" next_view, next_feature, sum_correlation, relevance_value = self.apply_state_transition_rule(agent_index,\n",
" views_probability)\n",
Expand All @@ -509,17 +509,17 @@
" \n",
" def __str__(self):\n",
" return f\"\"\"\n",
"Number of iteration: {self.num_iters}\n",
"Number of selected features: {self.num_selected_features} \n",
"Alpha: {self.alpha}\n",
"Beta: {self.beta}\n",
"Q0: {self.q0}\n",
"Discount rate: {self.discount_rate}\n",
"Number of views: {self.num_views}\n",
"Number of agents: {self.num_agents}\n",
"Total number of agents: {self.total_num_agents}\n",
"Best selected features: {self.best_selected_features}\n",
"Count corr computation: {self._count_correlation_computation}\n",
" Number of iteration: {self.num_iters}\n",
" Number of selected features: {self.num_selected_features} \n",
" Alpha: {self.alpha}\n",
" Beta: {self.beta}\n",
" Q0: {self.q0}\n",
" Discount rate: {self.discount_rate}\n",
" Number of views: {self.num_views}\n",
" Number of agents: {self.num_agents}\n",
" Total number of agents: {self.total_num_agents}\n",
" Best selected features: {self.best_selected_features}\n",
" Count corr computation: {self._count_correlation_computation}\n",
"\"\"\" "
]
},
Expand Down Expand Up @@ -900,21 +900,6 @@
"omics3_final = omics3_final.dropna(axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb7df36e",
"metadata": {},
"outputs": [],
"source": [
"def count_expected_removed_features(df):\n",
" count_values = 0\n",
" for col in df.columns.values:\n",
" if df[col].isna().sum() > 0:\n",
" count_values += 1\n",
" return count_values"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -923,9 +908,7 @@
"outputs": [],
"source": [
"#DNA_methylation Dataset\n",
"print('#Original features = ', len(omics1_final.columns))\n",
"# print('#Remaind features (Without missing values) = ', len(df_dna_transposed_shrinked_removed.columns))\n",
"print('#Expected removed features = ', count_expected_removed_features(omics1_final))"
"print('#Remained features = ', len(omics1_final.columns))"
]
},
{
Expand All @@ -936,9 +919,7 @@
"outputs": [],
"source": [
"#Genelevel_copy_number_alteration_CNA Dataset\n",
"print('#Original features = ', len(omics2_final.columns))\n",
"# print('#Remaind features (Without missing values) = ', len(df_cna_transposed_shrinked_removed.columns))\n",
"print('#Expected removed features = ', count_expected_removed_features(omics2_final))"
"print('#Remained features = ', len(omics2_final.columns))"
]
},
{
Expand All @@ -949,9 +930,7 @@
"outputs": [],
"source": [
"#RNASeq Dataset\n",
"print('#Original features = ', len(omics3_final.columns))\n",
"# print('#Remaind features (Without missing values) = ', len(df_rna_transposed_shrinked_removed.columns))\n",
"print('#Expected removed features = ', count_expected_removed_features(omics3_final))"
"print('#Remained features = ', len(omics3_final.columns))"
]
},
{
Expand Down Expand Up @@ -1064,7 +1043,8 @@
"from sklearn.model_selection import RepeatedStratifiedKFold\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.metrics import accuracy_score"
"from sklearn.metrics import accuracy_score\n",
"from sklearn.model_selection import cross_val_score"
]
},
{
Expand Down Expand Up @@ -1105,12 +1085,6 @@
},
"outputs": [],
"source": [
"from sklearn.model_selection import RepeatedStratifiedKFold\n",
"from sklearn.model_selection import cross_val_score\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.metrics import accuracy_score\n",
"from sklearn.metrics import mean_absolute_error\n",
"\n",
"# settings of Multi Agent algorithm\n",
"init_pheromone_value = 0.2\n",
"num_views = 3\n",
Expand All @@ -1126,13 +1100,14 @@
"\n",
"# repeat algorithm for different sizes of feature subsets\n",
"for feature_size in feature_sizes:\n",
" print(f\"\\n\\n*************feature size {feature_size}**********************\")\n",
" print(\"\\n\\n***************************************************************\")\n",
" print(f\"********************* Feature size {feature_size} *************************\")\n",
" print(\"***************************************************************\")\n",
" model_acc = {}\n",
" # configurations to repeat the k-fold cross-validation process (designed for imbalanced Classification)\n",
" cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=10, random_state=1)\n",
" cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=5, random_state=1)\n",
" for train_index, test_index in cv.split(omics1_final, y=label_final):\n",
" # split datasets into training and test sets\n",
" print(\"\\n\\n****************** Split **********************\")\n",
" omics1_final_train, omics1_final_test = omics1_final.iloc[train_index], omics1_final.iloc[test_index]\n",
" omics2_final_train, omics2_final_test = omics2_final.iloc[train_index], omics2_final.iloc[test_index]\n",
" omics3_final_train, omics3_final_test = omics3_final.iloc[train_index], omics3_final.iloc[test_index]\n",
Expand Down Expand Up @@ -1161,7 +1136,7 @@
" alg.start()\n",
" final_subset = alg.best_selected_features\n",
"\n",
" print(f\"\\n\\n Final selected subset: {final_subset}\")\n",
" print(f\"\\n\\n Final selected subset: {final_subset}\")\n",
" \n",
" # create reduced datasets based on final selected features\n",
" feature_indices_view1 = final_subset.get(0, [])\n",
Expand All @@ -1184,6 +1159,7 @@
" # get the list of models to evaluate performance\n",
" models = get_models()\n",
" # evaluate each model\n",
" new_output_file_lines = f\"Feature size: {feature_size}\"\n",
" for model_index in models:\n",
" acc = evaluate_model(models[model_index], \n",
" X_train=final_train_dataset.values,\n",
Expand All @@ -1193,13 +1169,34 @@
" value = model_acc.get(model_index, [])\n",
" value.append(acc)\n",
" model_acc[model_index] = value\n",
" print(f\" Classifier index ({model_index}) --> classification accuracy: {acc}\")\n",
" new_output_file_lines += f\"\\nClassifier index ({model_index}) --> classification accuracy {model_acc[model_index]}\"\n",
"\n",
" # write the current results in the output file\n",
" output_file_lines = \"\"\n",
" try:\n",
" output_file_lines = open(\"output_multi_agent.txt\", 'r').readlines()\n",
" output_file_lines[-3:] = new_output_file_lines\n",
" except Exception:\n",
" output_file_lines = new_output_file_lines\n",
" \n",
" with open(\"output_multi_agent.txt\", \"a\") as f:\n",
" print(f\"Feature size: {feature_size}\", file=f)\n",
" print(model_acc, file=f)\n",
" \n",
" open(\"output_multi_agent.txt\", 'w').writelines(output_file_lines)\n",
" \n",
" \n",
" print(\" ###########################################################################\")\n",
" print(\" ###########################################################################\")\n",
" \n",
" # prepare the output file for the next feature size\n",
" try:\n",
" output_file_lines = open(\"output_multi_agent.txt\", 'r').readlines()\n",
" output_file_lines.append(\"\\n\\n\\n\\n\\n\")\n",
" open(\"output_multi_agent.txt\", 'w').writelines(output_file_lines)\n",
" except Exception:\n",
" pass\n",
" \n",
" print(f\"Final classification accuracies for {feature_size} selected features\")\n",
" for model_index in model_acc:\n",
" print(f\"model index: {model_index},,, acc: {model_acc[model_index]}\")"
" print(f\" Classifier index ({model_index}) --> classification accuracy: {model_acc[model_index]}\")"
]
}
],
Expand Down

0 comments on commit 00b71a3

Please sign in to comment.