Skip to content

Commit

Permalink
Minor refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-zeller committed Jan 6, 2025
1 parent b3f6fd1 commit cfc4ba4
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions notebooks/Alhazen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1320,8 +1320,10 @@
"source": [
"import math\n",
"\n",
"def friendly_decision_tree(clf, feature_names, class_names = ['NO_BUG', 'BUG']):\n",
" def _tree(index, indent=0):\n",
"def friendly_decision_tree(clf, feature_names,\n",
" class_names = ['NO_BUG', 'BUG'],\n",
" indent=0):\n",
" def _tree(index, indent):\n",
" s = \"\"\n",
" feature = clf.tree_.feature[index]\n",
" feature_name = feature_names[feature]\n",
Expand All @@ -1347,7 +1349,8 @@
" s += _tree(right, indent + 2)\n",
" return s\n",
"\n",
" return _tree(0)"
" ROOT_INDEX = 0\n",
" return _tree(ROOT_INDEX, indent)"
]
},
{
Expand Down Expand Up @@ -2826,17 +2829,6 @@
" self._data = pandas.concat([self._data, new_data], sort=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Alhazen(Alhazen):\n",
" def _finalize(self):\n",
" return self._trees"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -2864,14 +2856,25 @@
"source": [
"class Alhazen(Alhazen):\n",
" def run(self):\n",
" for iteration in range(self._max_iter):\n",
" for iteration in range(1, self._max_iter + 1):\n",
" if self._verbose:\n",
" print(f\"\\nIteration #{iteration}\")\n",
" self._iterate(self._previous_samples)\n",
"\n",
" return self._finalize()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Alhazen(Alhazen):\n",
" def _finalize(self):\n",
" return self._trees"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -2880,21 +2883,27 @@
"source": [
"class Alhazen(Alhazen):\n",
" def _iterate(self, sample_list):\n",
" # Obtain labels, execute samples (Initial Step)\n",
" # Run samples, obtain test outcomes\n",
" exec_data = self.execute_samples(sample_list)\n",
"\n",
" # Collect features from the new samples\n",
" # Step 1: Extract features from the new samples\n",
" feature_data = collect_features(sample_list, self._grammar)\n",
"\n",
" # Combine the new data with the already existing data\n",
" self._add_new_data(exec_data, feature_data)\n",
" # display(self._data)\n",
"\n",
" # Train a tree\n",
" # Step 2: Train the Decision Tree Classifier\n",
" dec_tree = train_tree(self._data)\n",
" self._trees.append(dec_tree)\n",
"\n",
" # Extract new requirements from the tree (Activity 3)\n",
" if self._verbose:\n",
" print(\" Decision Tree:\")\n",
" all_features = extract_all_features(self._grammar)\n",
" all_feature_names = [f.friendly_name() for f in all_features]\n",
" print(friendly_decision_tree(dec_tree, all_feature_names, indent=4))\n",
"\n",
" # Step 3: Extract new requirements from the tree\n",
" new_input_specifications = get_all_input_specifications(dec_tree,\n",
" self._all_features,\n",
" self._feature_names,\n",
Expand All @@ -2904,7 +2913,7 @@
" for spec in new_input_specifications:\n",
" print(f\" {spec.friendly()}\")\n",
"\n",
" # generate new inputs according to the new input specifications\n",
" # Step 4: Generate new inputs according to the new input specifications\n",
" new_samples = generate_samples(self._grammar,\n",
" new_input_specifications,\n",
" self._generator_timeout)\n",
Expand Down Expand Up @@ -3034,11 +3043,6 @@
"_For those only interested in using the code in this chapter (without wanting to know how it works), give an example. This will be copied to the beginning of the chapter (before the first section) as text with rendered input and output._"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {
Expand Down

0 comments on commit cfc4ba4

Please sign in to comment.