Skip to content

Commit 0d54881

Browse files
Reduced double imports
1 parent c2e3d37 commit 0d54881

File tree

1 file changed

+32
-74
lines changed

1 file changed

+32
-74
lines changed

notebooks/Alhazen.ipynb

Lines changed: 32 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"metadata": {},
7676
"outputs": [],
7777
"source": [
78-
"from typing import List, Tuple, Dict, Any"
78+
"from typing import List, Tuple, Dict, Any, Optional"
7979
]
8080
},
8181
{
@@ -153,7 +153,9 @@
153153
"metadata": {},
154154
"outputs": [],
155155
"source": [
156-
"from fuzzingbook.Grammars import Grammar"
156+
" from fuzzingbook.Grammars import Grammar, EXPR_GRAMMAR, reachable_nonterminals, is_valid_grammar, START_SYMBOL\n",
157+
" from fuzzingbook.GrammarFuzzer import GrammarFuzzer, expansion_to_children, DerivationTree, tree_to_string, display_tree, is_nonterminal\n",
158+
" from fuzzingbook.Parser import EarleyParser"
157159
]
158160
},
159161
{
@@ -180,8 +182,7 @@
180182
"\n",
181183
" \"<digit>\":\n",
182184
" [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"]\n",
183-
"}\n",
184-
"START_SYMBOL = \"<start>\""
185+
"}"
185186
]
186187
},
187188
{
@@ -248,8 +249,15 @@
248249
"metadata": {},
249250
"outputs": [],
250251
"source": [
251-
"from enum import Enum\n",
252-
"\n",
252+
"from enum import Enum"
253+
]
254+
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": null,
258+
"metadata": {},
259+
"outputs": [],
260+
"source": [
253261
"class OracleResult(Enum):\n",
254262
" BUG = \"BUG\"\n",
255263
" NO_BUG = \"NO_BUG\"\n",
@@ -617,8 +625,6 @@
617625
"metadata": {},
618626
"outputs": [],
619627
"source": [
620-
"from fuzzingbook.GrammarFuzzer import expansion_to_children, DerivationTree\n",
621-
"\n",
622628
"class ExistenceFeature(Feature):\n",
623629
" '''\n",
624630
" This class represents existence features of a grammar. Existence features indicate\n",
@@ -680,7 +686,6 @@
680686
"metadata": {},
681687
"outputs": [],
682688
"source": [
683-
"from fuzzingbook.GrammarFuzzer import tree_to_string\n",
684689
"from numpy import nanmax, isnan\n",
685690
"\n",
686691
"class NumericInterpretation(Feature):\n",
@@ -762,7 +767,6 @@
762767
"metadata": {},
763768
"outputs": [],
764769
"source": [
765-
"from fuzzingbook.Grammars import reachable_nonterminals\n",
766770
"from collections import defaultdict\n",
767771
"import re\n",
768772
"\n",
@@ -855,9 +859,6 @@
855859
"metadata": {},
856860
"outputs": [],
857861
"source": [
858-
"from fuzzingbook.Parser import EarleyParser\n",
859-
"from fuzzingbook.Grammars import Grammar\n",
860-
"import pandas\n",
861862
"from pandas import DataFrame\n",
862863
"\n",
863864
"def collect_features(sample_list: List[str],\n",
@@ -905,8 +906,6 @@
905906
"outputs": [],
906907
"source": [
907908
"# TODO: handle multiple trees\n",
908-
"from fuzzingbook.Parser import EarleyParser\n",
909-
"\n",
910909
"def compute_feature_values(sample: str, grammar: Grammar, features: List[Feature]) -> Dict[str, float]:\n",
911910
" '''\n",
912911
" Extracts all feature values from an input.\n",
@@ -952,10 +951,6 @@
952951
"metadata": {},
953952
"outputs": [],
954953
"source": [
955-
"from fuzzingbook.Grammars import EXPR_GRAMMAR\n",
956-
"\n",
957-
"from fuzzingbook.GrammarFuzzer import GrammarFuzzer\n",
958-
"from fuzzingbook.GrammarFuzzer import tree_to_string, display_tree\n",
959954
"import random\n",
960955
"\n",
961956
"# For this example, fix the random seed so that the produced output is deterministic\n",
@@ -1023,9 +1018,6 @@
10231018
"metadata": {},
10241019
"outputs": [],
10251020
"source": [
1026-
"from fuzzingbook.Grammars import is_nonterminal\n",
1027-
"from fuzzingbook.GrammarFuzzer import tree_to_string\n",
1028-
"\n",
10291021
"# Then, recursively iterate through the derivation tree and for each non-terminal,\n",
10301022
"# add the derived word to the grammar\n",
10311023
"\n",
@@ -1050,16 +1042,8 @@
10501042
"metadata": {},
10511043
"outputs": [],
10521044
"source": [
1053-
"from fuzzingbook.GrammarFuzzer import GrammarFuzzer\n",
1054-
"from fuzzingbook.GrammarFuzzer import display_tree, tree_to_string\n",
1055-
"from fuzzingbook.Grammars import EXPR_GRAMMAR, Grammar\n",
1056-
"\n",
1057-
"import random\n",
10581045
"import copy\n",
10591046
"\n",
1060-
"from fuzzingbook.Parser import EarleyParser\n",
1061-
"from fuzzingbook.GrammarFuzzer import display_tree, tree_to_string\n",
1062-
"\n",
10631047
"START_SYMBOL = \"<start>\"\n",
10641048
"\n",
10651049
"def transform_grammar(sample: str,\n",
@@ -1115,10 +1099,9 @@
11151099
"metadata": {},
11161100
"outputs": [],
11171101
"source": [
1102+
"import sklearn\n",
11181103
"from sklearn.tree import DecisionTreeClassifier\n",
1119-
"from sklearn.feature_extraction import DictVectorizer\n",
1120-
"\n",
1121-
"import graphviz"
1104+
"from sklearn.feature_extraction import DictVectorizer"
11221105
]
11231106
},
11241107
{
@@ -1223,9 +1206,15 @@
12231206
"metadata": {},
12241207
"outputs": [],
12251208
"source": [
1226-
"import graphviz\n",
1227-
"import sklearn\n",
1228-
"\n",
1209+
"import graphviz"
1210+
]
1211+
},
1212+
{
1213+
"cell_type": "code",
1214+
"execution_count": null,
1215+
"metadata": {},
1216+
"outputs": [],
1217+
"source": [
12291218
"def show_decision_tree(clf, feature_names):\n",
12301219
" dot_data = sklearn.tree.export_graphviz(clf, out_file=None, \n",
12311220
" feature_names=feature_names,\n",
@@ -1351,9 +1340,6 @@
13511340
"metadata": {},
13521341
"outputs": [],
13531342
"source": [
1354-
"from sklearn.feature_extraction import DictVectorizer\n",
1355-
"import pandas\n",
1356-
"\n",
13571343
"# Features for each input, one dict per input\n",
13581344
"features = [\n",
13591345
" {'function-sqrt': 1, 'function-cos': 0, 'function-sin': 0, 'number': -900},\n",
@@ -1387,9 +1373,6 @@
13871373
"feature_names = ['function-sqrt', 'function-cos', 'function-sin', 'number']\n",
13881374
"X_data = pandas.DataFrame.from_records(features)\n",
13891375
"\n",
1390-
"from sklearn.tree import DecisionTreeClassifier\n",
1391-
"from sklearn import tree\n",
1392-
"\n",
13931376
"# Fix the random state to produce a deterministic result (for illustration purposes only)\n",
13941377
"clf = DecisionTreeClassifier(random_state=10)\n",
13951378
"\n",
@@ -1399,7 +1382,6 @@
13991382
"# Train with Pandas Dataframe\n",
14001383
"clf = clf.fit(X_data, oracle)\n",
14011384
"\n",
1402-
"import graphviz\n",
14031385
"dot_data = sklearn.tree.export_graphviz(clf, out_file=None, \n",
14041386
" feature_names=feature_names,\n",
14051387
" class_names=[\"BUG\", \"NO BUG\"], \n",
@@ -1688,9 +1670,7 @@
16881670
"source": [
16891671
"import logging\n",
16901672
"from pathlib import Path\n",
1691-
"\n",
16921673
"import numpy as np\n",
1693-
"from typing import List, Optional\n",
16941674
"\n",
16951675
"def tree_to_paths(tree, features: List[Feature]):\n",
16961676
" logging.info(\"Extracting requirements from tree ...\")\n",
@@ -1894,7 +1874,6 @@
18941874
"metadata": {},
18951875
"outputs": [],
18961876
"source": [
1897-
"import pandas\n",
18981877
"x = pandas.DataFrame.from_records(features)\n",
18991878
"bounds = pandas.DataFrame([{'feature': c, 'min': x[c].min(), 'max': x[c].max()}\n",
19001879
" for c in feature_names],\n",
@@ -1955,8 +1934,6 @@
19551934
"metadata": {},
19561935
"outputs": [],
19571936
"source": [
1958-
"import pandas\n",
1959-
"\n",
19601937
"def extracting_prediction_paths(clf, feature_names, data):\n",
19611938
" \n",
19621939
" # determine the bounds\n",
@@ -2032,30 +2009,28 @@
20322009
"outputs": [],
20332010
"source": [
20342011
"import string\n",
2035-
"from fuzzingbook.Grammars import Grammar, is_valid_grammar\n",
2036-
"START_SYMBOL = \"<start>\"\n",
20372012
"\n",
20382013
"SPECIFICATION: Grammar = {\n",
20392014
" \"<start>\":\n",
20402015
" [\"<req_list>\"],\n",
2041-
" \n",
2016+
"\n",
20422017
" \"<req_list>\": \n",
20432018
" [\"<req>\", \"<req>\"\", \"\"<req_list>\"],\n",
20442019
"\n",
20452020
" \"<req>\":\n",
20462021
" [\"<feature>\"\" \"\"<quant>\"\" \"\"<num>\"],\n",
2047-
" \n",
2022+
"\n",
20482023
" \"<feature>\": [\"exists(<string>)\",\n",
20492024
" \"num(<string>)\",\n",
20502025
" # currently not used\n",
20512026
" \"char(<string>)\",\n",
20522027
" \"length(<string>)\"], \n",
2053-
" \n",
2028+
"\n",
20542029
" \"<quant>\":\n",
20552030
" [\"<\", \">\", \"<=\", \">=\"],\n",
2056-
" \n",
2031+
"\n",
20572032
" \"<num>\": [\"-<value>\", \"<value>\"],\n",
2058-
" \n",
2033+
"\n",
20592034
" \"<value>\":\n",
20602035
" [\"<integer>.<integer>\",\n",
20612036
" \"<integer>\"],\n",
@@ -2065,12 +2040,12 @@
20652040
"\n",
20662041
" \"<digit>\":\n",
20672042
" [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"],\n",
2068-
" \n",
2043+
"\n",
20692044
" '<string>': ['<letters>'],\n",
20702045
" '<letters>': ['<letter><letters>', '<letter>'],\n",
20712046
" '<letter>': list(string.ascii_letters + string.digits + string.punctuation)\n",
20722047
"}\n",
2073-
" \n",
2048+
"\n",
20742049
"assert is_valid_grammar(SPECIFICATION, START_SYMBOL) == True"
20752050
]
20762051
},
@@ -2094,10 +2069,6 @@
20942069
"metadata": {},
20952070
"outputs": [],
20962071
"source": [
2097-
"from fuzzingbook.GrammarFuzzer import GrammarFuzzer\n",
2098-
"from fuzzingbook.Grammars import EXPR_GRAMMAR, Expansion\n",
2099-
"from fuzzingbook.Parser import EarleyParser, tree_to_string\n",
2100-
"\n",
21012072
"g = GrammarFuzzer(SPECIFICATION, START_SYMBOL ,max_nonterminals= 100)\n",
21022073
"earley = EarleyParser(SPECIFICATION)\n",
21032074
"for i in range(10):\n",
@@ -2163,9 +2134,6 @@
21632134
"metadata": {},
21642135
"outputs": [],
21652136
"source": [
2166-
"from typing import List\n",
2167-
"from fuzzingbook.GrammarFuzzer import DerivationTree\n",
2168-
"\n",
21692137
"class SpecRequirement:\n",
21702138
" '''\n",
21712139
" This class represents a requirement for a new input sample that should be generated.\n",
@@ -2383,11 +2351,6 @@
23832351
"from typing import List\n",
23842352
"from itertools import chain\n",
23852353
"\n",
2386-
"from fuzzingbook.Parser import EarleyParser\n",
2387-
"from fuzzingbook.GrammarFuzzer import DerivationTree, all_terminals, Grammar, tree_to_string\n",
2388-
"from fuzzingbook.Grammars import Grammar, nonterminals, opts, is_valid_grammar\n",
2389-
"from fuzzingbook.Grammars import reachable_nonterminals, unreachable_nonterminals\n",
2390-
"\n",
23912354
"\n",
23922355
"def best_trees(forest, spec):\n",
23932356
" samples = [tree_to_string(tree) for tree in forest]\n",
@@ -2531,8 +2494,6 @@
25312494
"metadata": {},
25322495
"outputs": [],
25332496
"source": [
2534-
"from fuzzingbook.GrammarFuzzer import GrammarFuzzer\n",
2535-
"\n",
25362497
"def generate_samples_random(grammar, new_input_specifications, num):\n",
25372498
" f = GrammarFuzzer(grammar ,max_nonterminals=50, log=False)\n",
25382499
" data = []\n",
@@ -2584,9 +2545,6 @@
25842545
"metadata": {},
25852546
"outputs": [],
25862547
"source": [
2587-
"from typing import List\n",
2588-
"import pandas\n",
2589-
"\n",
25902548
"GENERATOR_TIMEOUT = 10 # timeout in seconds\n",
25912549
"\n",
25922550
"class Alhazen:\n",

0 commit comments

Comments
 (0)