diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 892b64d24..be78ab976 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -74,21 +74,21 @@ Running the Test-Suite ===================== The minimal requirement for running the testsuite is ``py.test``. You can -install it with:: +install it with: pip install pytest -Clone this repository:: +Clone this repository: git clone https://github.com/aimacode/aima-python.git -Fetch the aima-data submodule:: +Fetch the aima-data submodule: cd aima-python git submodule init git submodule update -Then you can run the testsuite with:: +Then you can run the testsuite from the `tests` directory with: py.test diff --git a/learning.py b/learning.py index afc0caceb..38ae1780e 100644 --- a/learning.py +++ b/learning.py @@ -4,7 +4,7 @@ removeall, unique, product, mode, argmax, argmax_random_tie, isclose, gaussian, dotproduct, vector_add, scalar_vector_product, weighted_sample_with_replacement, weighted_sampler, num_or_str, normalize, clip, sigmoid, print_table, - DataFile, sigmoid_derivative + open_data, sigmoid_derivative ) import copy @@ -95,7 +95,7 @@ def __init__(self, examples=None, attrs=None, attrnames=None, target=-1, if isinstance(examples, str): self.examples = parse_csv(examples) elif examples is None: - self.examples = parse_csv(DataFile(name + '.csv').read()) + self.examples = parse_csv(open_data(name + '.csv').read()) else: self.examples = examples # Attrs are the indices of examples, unless otherwise stated. @@ -949,7 +949,7 @@ def cross_validation_wrapper(learner, dataset, k=10, trials=1): err_val = [] err_train = [] size = 1 - + while True: errT, errV = cross_validation(learner, size, dataset, k) # Check for convergence provided err_val is not empty diff --git a/search-4e.ipynb b/search-4e.ipynb index 100e0bcda..785596ef0 100644 --- a/search-4e.ipynb +++ b/search-4e.ipynb @@ -346,7 +346,7 @@ "outputs": [], "source": [ "from search import *\n", - "sgb_words = DataFile(\"EN-text/sgb-words.txt\")" + "sgb_words = open_data(\"EN-text/sgb-words.txt\")" ] }, { diff --git a/search.py b/search.py index 2d5d7a127..932054874 100644 --- a/search.py +++ b/search.py @@ -6,7 +6,7 @@ from utils import ( is_in, argmin, argmax, argmax_random_tie, probability, weighted_sampler, - memoize, print_table, DataFile, Stack, FIFOQueue, PriorityQueue, name, + memoize, print_table, open_data, Stack, FIFOQueue, PriorityQueue, name, distance ) @@ -1044,7 +1044,7 @@ class BoggleFinder: def __init__(self, board=None): if BoggleFinder.wordlist is None: - BoggleFinder.wordlist = Wordlist(DataFile("EN-text/wordlist.txt")) + BoggleFinder.wordlist = Wordlist(open_data("EN-text/wordlist.txt")) self.found = {} if board: self.set_board(board) diff --git a/tests/pytest.ini b/tests/pytest.ini new file mode 100644 index 000000000..7043be6c8 --- /dev/null +++ b/tests/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +filterwarnings = + ignore::ResourceWarning \ No newline at end of file diff --git a/tests/test_learning.py b/tests/test_learning.py index fef6ba3bb..0709d0b5a 100644 --- a/tests/test_learning.py +++ b/tests/test_learning.py @@ -1,7 +1,7 @@ import pytest import math import random -from utils import DataFile +from utils import open_data from learning import * @@ -18,6 +18,7 @@ def test_euclidean(): distance = euclidean_distance([0, 0, 0], [0, 0, 0]) assert distance == 0 + def test_rms_error(): assert rms_error([2, 2], [2, 2]) == 0 assert rms_error((0, 0), (0, 1)) == math.sqrt(0.5) @@ -25,6 +26,7 @@ def test_rms_error(): assert rms_error((0, 0), (0, -1)) == math.sqrt(0.5) assert rms_error((0, 0.5), (0, -0.5)) == math.sqrt(0.5) + def test_manhattan_distance(): assert manhattan_distance([2, 2], [2, 2]) == 0 assert manhattan_distance([0, 0], [0, 1]) == 1 @@ -32,6 +34,7 @@ def test_manhattan_distance(): assert manhattan_distance([0, 0], [0, -1]) == 1 assert manhattan_distance([0, 0.5], [0, -0.5]) == 1 + def test_mean_boolean_error(): assert mean_boolean_error([1, 1], [0, 0]) == 1 assert mean_boolean_error([0, 1], [1, 0]) == 1 @@ -39,6 +42,7 @@ def test_mean_boolean_error(): assert mean_boolean_error([0, 0], [0, 0]) == 0 assert mean_boolean_error([1, 1], [1, 1]) == 0 + def test_mean_error(): assert mean_error([2, 2], [2, 2]) == 0 assert mean_error([0, 0], [0, 1]) == 0.5 @@ -53,7 +57,7 @@ def test_exclude(): def test_parse_csv(): - Iris = DataFile('iris.csv').read() + Iris = open_data('iris.csv').read() assert parse_csv(Iris)[0] == [5.1, 3.5, 1.4, 0.2, 'setosa'] diff --git a/tests/test_text.py b/tests/test_text.py index 757e6fe17..2b664cbf6 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -3,11 +3,11 @@ import random from text import * -from utils import isclose, DataFile +from utils import isclose, open_data def test_text_models(): - flatland = DataFile("EN-text/flatland.txt").read() + flatland = open_data("EN-text/flatland.txt").read() wordseq = words(flatland) P1 = UnigramTextModel(wordseq) P2 = NgramTextModel(2, wordseq) @@ -141,7 +141,7 @@ def test_char_models(): def test_viterbi_segmentation(): - flatland = DataFile("EN-text/flatland.txt").read() + flatland = open_data("EN-text/flatland.txt").read() wordseq = words(flatland) P = UnigramTextModel(wordseq) text = "itiseasytoreadwordswithoutspaces" @@ -158,7 +158,7 @@ def test_shift_encoding(): def test_shift_decoding(): - flatland = DataFile("EN-text/flatland.txt").read() + flatland = open_data("EN-text/flatland.txt").read() ring = ShiftDecoder(flatland) msg = ring.decode('Kyzj zj r jvtivk dvjjrxv.') @@ -166,12 +166,12 @@ def test_shift_decoding(): def test_permutation_decoder(): - gutenberg = DataFile("EN-text/gutenberg.txt").read() - flatland = DataFile("EN-text/flatland.txt").read() - + gutenberg = open_data("EN-text/gutenberg.txt").read() + flatland = open_data("EN-text/flatland.txt").read() + pd = PermutationDecoder(canonicalize(gutenberg)) assert pd.decode('aba') in ('ece', 'ete', 'tat', 'tit', 'txt') - + pd = PermutationDecoder(canonicalize(flatland)) assert pd.decode('aba') in ('ded', 'did', 'ece', 'ele', 'eme', 'ere', 'eve', 'eye', 'iti', 'mom', 'ses', 'tat', 'tit') @@ -183,7 +183,7 @@ def test_rot13_encoding(): def test_rot13_decoding(): - flatland = DataFile("EN-text/flatland.txt").read() + flatland = open_data("EN-text/flatland.txt").read() ring = ShiftDecoder(flatland) msg = ring.decode(rot13('Hello, world!')) diff --git a/text.ipynb b/text.ipynb index 0edb43b05..0376738cd 100644 --- a/text.ipynb +++ b/text.ipynb @@ -24,7 +24,7 @@ "outputs": [], "source": [ "from text import *\n", - "from utils import DataFile" + "from utils import open_data" ] }, { @@ -84,7 +84,7 @@ } ], "source": [ - "flatland = DataFile(\"EN-text/flatland.txt\").read()\n", + "flatland = open_data(\"EN-text/flatland.txt\").read()\n", "wordseq = words(flatland)\n", "\n", "P1 = UnigramTextModel(wordseq)\n", @@ -186,7 +186,7 @@ } ], "source": [ - "flatland = DataFile(\"EN-text/flatland.txt\").read()\n", + "flatland = open_data(\"EN-text/flatland.txt\").read()\n", "wordseq = words(flatland)\n", "P = UnigramTextModel(wordseq)\n", "text = \"itiseasytoreadwordswithoutspaces\"\n", @@ -358,7 +358,7 @@ } ], "source": [ - "flatland = DataFile(\"EN-text/flatland.txt\").read()\n", + "flatland = open_data(\"EN-text/flatland.txt\").read()\n", "decoder = ShiftDecoder(flatland)\n", "\n", "decoded_message = decoder.decode(ciphertext)\n", diff --git a/utils.py b/utils.py index aa24a55bd..698560569 100644 --- a/utils.py +++ b/utils.py @@ -383,20 +383,13 @@ def print_table(table, header=None, sep=' ', numfmt='{}'): str(x), j)(size) for (j, size, x) in zip(justs, sizes, row))) -def AIMAFile(components, mode='r'): - """Open a file based at the AIMA root directory.""" +def open_data(name, mode='r'): aima_root = os.path.dirname(__file__) - - aima_file = os.path.join(aima_root, *components) + aima_file = os.path.join(aima_root, *['aima-data', name]) return open(aima_file) -def DataFile(name, mode='r'): - "Return a file in the AIMA /aima-data directory." - return AIMAFile(['aima-data', name], mode) - - # ______________________________________________________________________________ # Expressions