From cfc4ba491b9493135b9a1a274e05dd3e6fc74095 Mon Sep 17 00:00:00 2001 From: Andreas Zeller Date: Mon, 6 Jan 2025 15:56:22 +0100 Subject: [PATCH] Minor refinements --- notebooks/Alhazen.ipynb | 54 ++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/notebooks/Alhazen.ipynb b/notebooks/Alhazen.ipynb index 5d981ba3..201ca1eb 100644 --- a/notebooks/Alhazen.ipynb +++ b/notebooks/Alhazen.ipynb @@ -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", @@ -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)" ] }, { @@ -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, @@ -2864,7 +2856,7 @@ "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", @@ -2872,6 +2864,17 @@ " 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, @@ -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", @@ -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", @@ -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": {