From e38fa4db748ccff965789d8906552a2c89eb5f35 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 10 Feb 2015 13:39:37 -0800 Subject: [PATCH 01/10] Fix test adding matrix to read-only file The existing test was adding a matrix of an incompatible shape, which doesn't test the intended code path. --- api/python/openmatrix/test/test_file.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/api/python/openmatrix/test/test_file.py b/api/python/openmatrix/test/test_file.py index cd8088d..3cd06c4 100644 --- a/api/python/openmatrix/test/test_file.py +++ b/api/python/openmatrix/test/test_file.py @@ -35,7 +35,7 @@ def test_open_readonly_hdf5_file(): f = tables.openFile('test2.omx','w') f.close() f = omx.openFile('test2.omx','r') - f.close() + f.close() def test_add_numpy_matrix_using_brackets(): f = omx.openFile('test3.omx','w') @@ -47,13 +47,14 @@ def test_add_numpy_matrix_using_create_matrix(): f.createMatrix('m1', obj=numpy.ones((5,5))) f.close() +@raises(tables.FileModeError) def test_add_matrix_to_readonly_file(): f = omx.openFile('test6.omx','w') f['m2'] = numpy.ones((5,5)) f.close() f = omx.openFile('test6.omx','r') - assert_raises(tables.FileModeError, add_m1_node, f) - f.close() + f.createMatrix('m1', obj=numpy.ones((5, 5))) + f.close() def test_add_matrix_with_same_name(): f = omx.openFile('test5.omx','w') From 014dddc9f67d1dc5342219dfe1820358a0f8ed53 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 10 Feb 2015 14:29:55 -0800 Subject: [PATCH 02/10] numpy -> np per conventions --- api/python/openmatrix/test/test_file.py | 27 +++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/api/python/openmatrix/test/test_file.py b/api/python/openmatrix/test/test_file.py index 3cd06c4..1c97859 100644 --- a/api/python/openmatrix/test/test_file.py +++ b/api/python/openmatrix/test/test_file.py @@ -1,5 +1,10 @@ -import tables,numpy,os +import os + +import numpy as np +import tables + import openmatrix as omx + from nose.tools import * def setup_clean(): @@ -39,21 +44,21 @@ def test_open_readonly_hdf5_file(): def test_add_numpy_matrix_using_brackets(): f = omx.openFile('test3.omx','w') - f['m1'] = numpy.ones((5,5)) + f['m1'] = np.ones((5,5)) f.close() def test_add_numpy_matrix_using_create_matrix(): f = omx.openFile('test4.omx','w') - f.createMatrix('m1', obj=numpy.ones((5,5))) + f.createMatrix('m1', obj=np.ones((5,5))) f.close() @raises(tables.FileModeError) def test_add_matrix_to_readonly_file(): f = omx.openFile('test6.omx','w') - f['m2'] = numpy.ones((5,5)) + f['m2'] = np.ones((5,5)) f.close() f = omx.openFile('test6.omx','r') - f.createMatrix('m1', obj=numpy.ones((5, 5))) + f.createMatrix('m1', obj=np.ones((5, 5))) f.close() def test_add_matrix_with_same_name(): @@ -66,15 +71,15 @@ def test_add_matrix_with_same_name(): @with_setup(setup_clean,teardown_clean) def test_get_length_of_file(): f = omx.openFile('test7.omx','w') - f['m1'] = numpy.ones((5,5)) - f['m2'] = numpy.ones((5,5)) - f['m3'] = numpy.ones((5,5)) - f['m4'] = numpy.ones((5,5)) - f['m5'] = numpy.ones((5,5)) + f['m1'] = np.ones((5,5)) + f['m2'] = np.ones((5,5)) + f['m3'] = np.ones((5,5)) + f['m4'] = np.ones((5,5)) + f['m5'] = np.ones((5,5)) assert(len(f)==5) assert(len(f.listMatrices())==5) f.close() def add_m1_node(f): - f.createMatrix('m1', obj=numpy.ones((7,7))) + f.createMatrix('m1', obj=np.ones((7,7))) From 08760e3aad8b71432b388823b7eb42101625dada Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 10 Feb 2015 14:57:49 -0800 Subject: [PATCH 03/10] Use a test fixture for test OMX files Uses a setup fixture that generates a test file name using the tempfile module and a teardown fixture that deletes the file after each test is complete. --- api/python/openmatrix/test/test_file.py | 65 ++++++++++++++----------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/api/python/openmatrix/test/test_file.py b/api/python/openmatrix/test/test_file.py index 1c97859..e202f86 100644 --- a/api/python/openmatrix/test/test_file.py +++ b/api/python/openmatrix/test/test_file.py @@ -1,4 +1,5 @@ import os +import tempfile import numpy as np import tables @@ -7,70 +8,76 @@ from nose.tools import * -def setup_clean(): - try: - os.remove('test1.omx') - except: - pass +TEST_FILE = None -def setup_empty_file(): - try: - os.remove('test1.omx') - f = omx.openFile('test.omx','w') - f.close() - except: - pass +def setup_func(): + global TEST_FILE + if TEST_FILE is not None and os.path.isfile(TEST_FILE): + os.remove(TEST_FILE) -def teardown_clean(): - try: - os.remove('test2.omx') - except: - pass + with tempfile.NamedTemporaryFile(suffix='.omx') as tmp: + TEST_FILE = tmp.name -@with_setup(setup_clean,teardown_clean) +def teardown_func(): + if TEST_FILE is not None and os.path.isfile(TEST_FILE): + os.remove(TEST_FILE) + + +@with_setup(setup_func, teardown_func) def test_create_file(): - f = omx.openFile('test1.omx','w') + f = omx.openFile(TEST_FILE, 'w') f.close() - assert(os.path.exists('test1.omx')) + assert os.path.isfile(TEST_FILE) + +@with_setup(setup_func, teardown_func) def test_open_readonly_hdf5_file(): - f = tables.openFile('test2.omx','w') + f = tables.openFile(TEST_FILE, 'w') f.close() - f = omx.openFile('test2.omx','r') + f = omx.openFile(TEST_FILE, 'r') f.close() + +@with_setup(setup_func, teardown_func) def test_add_numpy_matrix_using_brackets(): - f = omx.openFile('test3.omx','w') + f = omx.openFile(TEST_FILE, 'w') f['m1'] = np.ones((5,5)) f.close() + +@with_setup(setup_func, teardown_func) def test_add_numpy_matrix_using_create_matrix(): - f = omx.openFile('test4.omx','w') + f = omx.openFile(TEST_FILE, 'w') f.createMatrix('m1', obj=np.ones((5,5))) f.close() + +@with_setup(setup_func, teardown_func) @raises(tables.FileModeError) def test_add_matrix_to_readonly_file(): - f = omx.openFile('test6.omx','w') + f = omx.openFile(TEST_FILE, 'w') f['m2'] = np.ones((5,5)) f.close() - f = omx.openFile('test6.omx','r') + f = omx.openFile(TEST_FILE, 'r') f.createMatrix('m1', obj=np.ones((5, 5))) f.close() + +@with_setup(setup_func, teardown_func) def test_add_matrix_with_same_name(): - f = omx.openFile('test5.omx','w') + f = omx.openFile(TEST_FILE, 'w') add_m1_node(f) # now add m1 again: assert_raises(tables.NodeError, add_m1_node, f) f.close() -@with_setup(setup_clean,teardown_clean) + +@with_setup(setup_func, teardown_func) def test_get_length_of_file(): - f = omx.openFile('test7.omx','w') + f = omx.openFile(TEST_FILE, 'w') f['m1'] = np.ones((5,5)) f['m2'] = np.ones((5,5)) f['m3'] = np.ones((5,5)) From 7349a3c01062ecfb6da1a36af7214886627cfeb9 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 10 Feb 2015 15:02:33 -0800 Subject: [PATCH 04/10] Use with context manager for opening OMX files This ensures that all the files are properly closed when the tests are done with them. --- api/python/openmatrix/test/test_file.py | 61 ++++++++++++------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/api/python/openmatrix/test/test_file.py b/api/python/openmatrix/test/test_file.py index e202f86..2e31e42 100644 --- a/api/python/openmatrix/test/test_file.py +++ b/api/python/openmatrix/test/test_file.py @@ -28,65 +28,60 @@ def teardown_func(): @with_setup(setup_func, teardown_func) def test_create_file(): - f = omx.openFile(TEST_FILE, 'w') - f.close() + with omx.openFile(TEST_FILE, 'w'): + pass assert os.path.isfile(TEST_FILE) @with_setup(setup_func, teardown_func) def test_open_readonly_hdf5_file(): - f = tables.openFile(TEST_FILE, 'w') - f.close() - f = omx.openFile(TEST_FILE, 'r') - f.close() + with tables.openFile(TEST_FILE, 'w'): + pass + with omx.openFile(TEST_FILE, 'r'): + pass @with_setup(setup_func, teardown_func) def test_add_numpy_matrix_using_brackets(): - f = omx.openFile(TEST_FILE, 'w') - f['m1'] = np.ones((5,5)) - f.close() + with omx.openFile(TEST_FILE, 'w') as f: + f['m1'] = np.ones((5,5)) @with_setup(setup_func, teardown_func) def test_add_numpy_matrix_using_create_matrix(): - f = omx.openFile(TEST_FILE, 'w') - f.createMatrix('m1', obj=np.ones((5,5))) - f.close() + with omx.openFile(TEST_FILE, 'w') as f: + f.createMatrix('m1', obj=np.ones((5,5))) @with_setup(setup_func, teardown_func) @raises(tables.FileModeError) def test_add_matrix_to_readonly_file(): - f = omx.openFile(TEST_FILE, 'w') - f['m2'] = np.ones((5,5)) - f.close() - f = omx.openFile(TEST_FILE, 'r') - f.createMatrix('m1', obj=np.ones((5, 5))) - f.close() + with omx.openFile(TEST_FILE, 'w') as f: + f['m2'] = np.ones((5,5)) + + with omx.openFile(TEST_FILE, 'r') as f: + f.createMatrix('m1', obj=np.ones((5, 5))) @with_setup(setup_func, teardown_func) def test_add_matrix_with_same_name(): - f = omx.openFile(TEST_FILE, 'w') - add_m1_node(f) - # now add m1 again: - assert_raises(tables.NodeError, add_m1_node, f) - f.close() + with omx.openFile(TEST_FILE, 'w') as f: + add_m1_node(f) + # now add m1 again: + assert_raises(tables.NodeError, add_m1_node, f) @with_setup(setup_func, teardown_func) def test_get_length_of_file(): - f = omx.openFile(TEST_FILE, 'w') - f['m1'] = np.ones((5,5)) - f['m2'] = np.ones((5,5)) - f['m3'] = np.ones((5,5)) - f['m4'] = np.ones((5,5)) - f['m5'] = np.ones((5,5)) - assert(len(f)==5) - assert(len(f.listMatrices())==5) - f.close() + with omx.openFile(TEST_FILE, 'w') as f: + f['m1'] = np.ones((5,5)) + f['m2'] = np.ones((5,5)) + f['m3'] = np.ones((5,5)) + f['m4'] = np.ones((5,5)) + f['m5'] = np.ones((5,5)) + assert(len(f)==5) + assert(len(f.listMatrices())==5) + def add_m1_node(f): f.createMatrix('m1', obj=np.ones((7,7))) - From 14ef4ce7aa88fdc39b9ef5afb6e75a469006bea7 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 10 Feb 2015 15:05:21 -0800 Subject: [PATCH 05/10] Use nt. prefix for nose.tools functions To eliminate the from nose.tools import * --- api/python/openmatrix/test/test_file.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/api/python/openmatrix/test/test_file.py b/api/python/openmatrix/test/test_file.py index 2e31e42..15d80c1 100644 --- a/api/python/openmatrix/test/test_file.py +++ b/api/python/openmatrix/test/test_file.py @@ -6,7 +6,7 @@ import openmatrix as omx -from nose.tools import * +import nose.tools as nt TEST_FILE = None @@ -26,14 +26,14 @@ def teardown_func(): os.remove(TEST_FILE) -@with_setup(setup_func, teardown_func) +@nt.with_setup(setup_func, teardown_func) def test_create_file(): with omx.openFile(TEST_FILE, 'w'): pass assert os.path.isfile(TEST_FILE) -@with_setup(setup_func, teardown_func) +@nt.with_setup(setup_func, teardown_func) def test_open_readonly_hdf5_file(): with tables.openFile(TEST_FILE, 'w'): pass @@ -41,20 +41,20 @@ def test_open_readonly_hdf5_file(): pass -@with_setup(setup_func, teardown_func) +@nt.with_setup(setup_func, teardown_func) def test_add_numpy_matrix_using_brackets(): with omx.openFile(TEST_FILE, 'w') as f: f['m1'] = np.ones((5,5)) -@with_setup(setup_func, teardown_func) +@nt.with_setup(setup_func, teardown_func) def test_add_numpy_matrix_using_create_matrix(): with omx.openFile(TEST_FILE, 'w') as f: f.createMatrix('m1', obj=np.ones((5,5))) -@with_setup(setup_func, teardown_func) -@raises(tables.FileModeError) +@nt.with_setup(setup_func, teardown_func) +@nt.raises(tables.FileModeError) def test_add_matrix_to_readonly_file(): with omx.openFile(TEST_FILE, 'w') as f: f['m2'] = np.ones((5,5)) @@ -63,15 +63,16 @@ def test_add_matrix_to_readonly_file(): f.createMatrix('m1', obj=np.ones((5, 5))) -@with_setup(setup_func, teardown_func) +@nt.with_setup(setup_func, teardown_func) +@nt.raises(tables.NodeError) def test_add_matrix_with_same_name(): with omx.openFile(TEST_FILE, 'w') as f: add_m1_node(f) # now add m1 again: - assert_raises(tables.NodeError, add_m1_node, f) + add_m1_node(f) -@with_setup(setup_func, teardown_func) +@nt.with_setup(setup_func, teardown_func) def test_get_length_of_file(): with omx.openFile(TEST_FILE, 'w') as f: f['m1'] = np.ones((5,5)) From c7a4b0df070782ac9f22f5c7eb25316bdcdfef67 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 10 Feb 2015 15:07:05 -0800 Subject: [PATCH 06/10] PEP8 changes --- api/python/openmatrix/test/test_file.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/api/python/openmatrix/test/test_file.py b/api/python/openmatrix/test/test_file.py index 15d80c1..27795bb 100644 --- a/api/python/openmatrix/test/test_file.py +++ b/api/python/openmatrix/test/test_file.py @@ -44,20 +44,20 @@ def test_open_readonly_hdf5_file(): @nt.with_setup(setup_func, teardown_func) def test_add_numpy_matrix_using_brackets(): with omx.openFile(TEST_FILE, 'w') as f: - f['m1'] = np.ones((5,5)) + f['m1'] = np.ones((5, 5)) @nt.with_setup(setup_func, teardown_func) def test_add_numpy_matrix_using_create_matrix(): with omx.openFile(TEST_FILE, 'w') as f: - f.createMatrix('m1', obj=np.ones((5,5))) + f.createMatrix('m1', obj=np.ones((5, 5))) @nt.with_setup(setup_func, teardown_func) @nt.raises(tables.FileModeError) def test_add_matrix_to_readonly_file(): with omx.openFile(TEST_FILE, 'w') as f: - f['m2'] = np.ones((5,5)) + f['m2'] = np.ones((5, 5)) with omx.openFile(TEST_FILE, 'r') as f: f.createMatrix('m1', obj=np.ones((5, 5))) @@ -75,14 +75,14 @@ def test_add_matrix_with_same_name(): @nt.with_setup(setup_func, teardown_func) def test_get_length_of_file(): with omx.openFile(TEST_FILE, 'w') as f: - f['m1'] = np.ones((5,5)) - f['m2'] = np.ones((5,5)) - f['m3'] = np.ones((5,5)) - f['m4'] = np.ones((5,5)) - f['m5'] = np.ones((5,5)) - assert(len(f)==5) - assert(len(f.listMatrices())==5) + f['m1'] = np.ones((5, 5)) + f['m2'] = np.ones((5, 5)) + f['m3'] = np.ones((5, 5)) + f['m4'] = np.ones((5, 5)) + f['m5'] = np.ones((5, 5)) + assert(len(f) == 5) + assert(len(f.listMatrices()) == 5) def add_m1_node(f): - f.createMatrix('m1', obj=np.ones((7,7))) + f.createMatrix('m1', obj=np.ones((7, 7))) From 9828b1eca8bced18f212f166ca59e1a7dfaaf884 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 10 Feb 2015 15:40:23 -0800 Subject: [PATCH 07/10] Additional tests for a bit more coverage of File --- api/python/openmatrix/test/test_file.py | 101 +++++++++++++++++++++++- 1 file changed, 97 insertions(+), 4 deletions(-) diff --git a/api/python/openmatrix/test/test_file.py b/api/python/openmatrix/test/test_file.py index 27795bb..c7c70a2 100644 --- a/api/python/openmatrix/test/test_file.py +++ b/api/python/openmatrix/test/test_file.py @@ -2,6 +2,7 @@ import tempfile import numpy as np +import numpy.testing as npt import tables import openmatrix as omx @@ -11,6 +12,14 @@ TEST_FILE = None +def ones5x5(): + return np.ones((5, 5)) + + +def add_m1_node(f): + f.createMatrix('m1', obj=ones5x5()) + + def setup_func(): global TEST_FILE @@ -37,20 +46,41 @@ def test_create_file(): def test_open_readonly_hdf5_file(): with tables.openFile(TEST_FILE, 'w'): pass + + assert os.path.isfile(TEST_FILE) + with omx.openFile(TEST_FILE, 'r'): pass +@nt.with_setup(setup_func, teardown_func) +def test_set_get_del(): + with omx.openFile(TEST_FILE, 'w') as f: + add_m1_node(f) + npt.assert_array_equal(f['m1'], ones5x5()) + assert f.shape() == (5, 5) + del f['m1'] + assert 'm1' not in f + + @nt.with_setup(setup_func, teardown_func) def test_add_numpy_matrix_using_brackets(): with omx.openFile(TEST_FILE, 'w') as f: - f['m1'] = np.ones((5, 5)) + f['m1'] = ones5x5() + npt.assert_array_equal(f['m1'], ones5x5()) + assert f.shape() == (5, 5) + + # test check for shape matching + with nt.assert_raises(omx.Exceptions.ShapeError): + f.createMatrix('m2', obj=np.ones((8, 8))) @nt.with_setup(setup_func, teardown_func) def test_add_numpy_matrix_using_create_matrix(): with omx.openFile(TEST_FILE, 'w') as f: - f.createMatrix('m1', obj=np.ones((5, 5))) + f.createMatrix('m1', obj=ones5x5()) + npt.assert_array_equal(f['m1'], ones5x5()) + assert f.shape() == (5, 5) @nt.with_setup(setup_func, teardown_func) @@ -84,5 +114,68 @@ def test_get_length_of_file(): assert(len(f.listMatrices()) == 5) -def add_m1_node(f): - f.createMatrix('m1', obj=np.ones((7, 7))) +@nt.with_setup(setup_func, teardown_func) +def test_len_list_iter(): + names = ['m{}'.format(x) for x in range(5)] + with omx.openFile(TEST_FILE, 'w') as f: + for m in names: + f[m] = ones5x5() + + for mat in f: + npt.assert_array_equal(mat, ones5x5()) + + assert len(f) == len(names) + assert f.listMatrices() == names + + +@nt.with_setup(setup_func, teardown_func) +def test_contains(): + with omx.openFile(TEST_FILE, 'w') as f: + add_m1_node(f) + assert 'm1' in f + + +@nt.with_setup(setup_func, teardown_func) +def test_list_all_attrs(): + with omx.openFile(TEST_FILE, 'w') as f: + add_m1_node(f) + f['m2'] = ones5x5() + + nt.assert_equal(f.listAllAttributes(), []) + + f['m1'].attrs['a1'] = 'a1' + f['m1'].attrs['a2'] = 'a2' + f['m2'].attrs['a2'] = 'a2' + f['m2'].attrs['a3'] = 'a3' + + nt.assert_equal(f.listAllAttributes(), ['a1', 'a2', 'a3']) + + +@nt.with_setup(setup_func, teardown_func) +def test_matrices_by_attr(): + with omx.openFile(TEST_FILE, 'w') as f: + f['m1'] = ones5x5() + f['m2'] = ones5x5() + f['m3'] = ones5x5() + + for m in f: + m.attrs['a1'] = 'a1' + m.attrs['a2'] = 'a2' + f['m3'].attrs['a2'] = 'a22' + f['m3'].attrs['a3'] = 'a3' + + gmba = f._getMatricesByAttribute + + assert gmba('zz', 'zz') == [] + assert gmba('a1', 'a1') == [f['m1'], f['m2'], f['m3']] + assert gmba('a2', 'a2') == [f['m1'], f['m2']] + assert gmba('a2', 'a22') == [f['m3']] + assert gmba('a3', 'a3') == [f['m3']] + + +@nt.with_setup(setup_func, teardown_func) +def test_set_with_carray(): + with omx.openFile(TEST_FILE, 'w') as f: + f['m1'] = ones5x5() + f['m2'] = f['m1'] + npt.assert_array_equal(f['m2'], f['m1']) From 57069c7e3d5b053c7483783b92226ba661d50fc1 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 10 Feb 2015 15:47:06 -0800 Subject: [PATCH 08/10] Use assertion functions for tests These tend to give more useful error messages than plain asserts. --- api/python/openmatrix/test/test_file.py | 29 ++++++++++++++----------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/api/python/openmatrix/test/test_file.py b/api/python/openmatrix/test/test_file.py index c7c70a2..f89c004 100644 --- a/api/python/openmatrix/test/test_file.py +++ b/api/python/openmatrix/test/test_file.py @@ -58,9 +58,9 @@ def test_set_get_del(): with omx.openFile(TEST_FILE, 'w') as f: add_m1_node(f) npt.assert_array_equal(f['m1'], ones5x5()) - assert f.shape() == (5, 5) + nt.assert_equal(f.shape(), (5, 5)) del f['m1'] - assert 'm1' not in f + nt.assert_not_in('m1', f) @nt.with_setup(setup_func, teardown_func) @@ -68,7 +68,7 @@ def test_add_numpy_matrix_using_brackets(): with omx.openFile(TEST_FILE, 'w') as f: f['m1'] = ones5x5() npt.assert_array_equal(f['m1'], ones5x5()) - assert f.shape() == (5, 5) + nt.assert_equal(f.shape(), (5, 5)) # test check for shape matching with nt.assert_raises(omx.Exceptions.ShapeError): @@ -80,7 +80,7 @@ def test_add_numpy_matrix_using_create_matrix(): with omx.openFile(TEST_FILE, 'w') as f: f.createMatrix('m1', obj=ones5x5()) npt.assert_array_equal(f['m1'], ones5x5()) - assert f.shape() == (5, 5) + nt.assert_equal(f.shape(), (5, 5)) @nt.with_setup(setup_func, teardown_func) @@ -110,8 +110,8 @@ def test_get_length_of_file(): f['m3'] = np.ones((5, 5)) f['m4'] = np.ones((5, 5)) f['m5'] = np.ones((5, 5)) - assert(len(f) == 5) - assert(len(f.listMatrices()) == 5) + nt.assert_equal(len(f), 5) + nt.assert_equal(len(f.listMatrices()), 5) @nt.with_setup(setup_func, teardown_func) @@ -124,14 +124,17 @@ def test_len_list_iter(): for mat in f: npt.assert_array_equal(mat, ones5x5()) - assert len(f) == len(names) - assert f.listMatrices() == names + nt.assert_equal(len(f), len(names)) + nt.assert_equal(f.listMatrices(), names) @nt.with_setup(setup_func, teardown_func) def test_contains(): with omx.openFile(TEST_FILE, 'w') as f: add_m1_node(f) + nt.assert_in('m1', f) + # keep this here to be sure we're actually running + # File.__contains__ assert 'm1' in f @@ -166,11 +169,11 @@ def test_matrices_by_attr(): gmba = f._getMatricesByAttribute - assert gmba('zz', 'zz') == [] - assert gmba('a1', 'a1') == [f['m1'], f['m2'], f['m3']] - assert gmba('a2', 'a2') == [f['m1'], f['m2']] - assert gmba('a2', 'a22') == [f['m3']] - assert gmba('a3', 'a3') == [f['m3']] + nt.assert_equal(gmba('zz', 'zz'), []) + nt.assert_equal(gmba('a1', 'a1'), [f['m1'], f['m2'], f['m3']]) + nt.assert_equal(gmba('a2', 'a2'), [f['m1'], f['m2']]) + nt.assert_equal(gmba('a2', 'a22'), [f['m3']]) + nt.assert_equal(gmba('a3', 'a3'), [f['m3']]) @nt.with_setup(setup_func, teardown_func) From 47355a5a51e41b19eeb22634a620c3f2195d52b1 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 10 Feb 2015 15:55:11 -0800 Subject: [PATCH 09/10] Fix a bug in File.listAllAttributes Need to search self.root.data, not self.root, for nodes. --- api/python/openmatrix/File.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/api/python/openmatrix/File.py b/api/python/openmatrix/File.py index 82e3e91..b44df31 100644 --- a/api/python/openmatrix/File.py +++ b/api/python/openmatrix/File.py @@ -30,7 +30,7 @@ def createMatrix(self, name, atom=None, shape=None, title='', filters=None, if self.shape() != None and obj != None and obj.shape != self.shape(): raise ShapeError('%s has shape %s but this file requires shape %s' % (name, obj.shape, self.shape())) - + # Create the HDF5 array if tables.__version__.startswith('3'): matrix = self.createCArray(self.root.data, name, atom, shape, title, filters, @@ -96,10 +96,9 @@ def listMatrices(self): def listAllAttributes(self): """Return combined list of all attributes used for any Matrix in this File""" all_tags = set() - for m in self.listNodes(self.root,'CArray'): - if m.attrs != None: - all_tags.update(m.attrs._v_attrnames) - return sorted(list(all_tags)) + for m in self.iterNodes(self.root.data, 'CArray'): + all_tags.update(m.attrs._v_attrnamesuser) + return sorted(all_tags) # MAPPINGS ----------------------------------------------- From bcfb90e7bb11f87c834f4e8efab34feb95825c8a Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 10 Feb 2015 16:12:44 -0800 Subject: [PATCH 10/10] Use is/is note for comparisons to None Stops this warning from appearing when using openmatrix: File.py:30: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future. --- api/python/openmatrix/File.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/api/python/openmatrix/File.py b/api/python/openmatrix/File.py index b44df31..3682463 100644 --- a/api/python/openmatrix/File.py +++ b/api/python/openmatrix/File.py @@ -27,7 +27,7 @@ def createMatrix(self, name, atom=None, shape=None, title='', filters=None, an existing numpy matrix, or a shape and an atom type.""" # If object was passed in, make sure its shape is correct - if self.shape() != None and obj != None and obj.shape != self.shape(): + if self.shape() is not None and obj is not None and obj.shape != self.shape(): raise ShapeError('%s has shape %s but this file requires shape %s' % (name, obj.shape, self.shape())) @@ -39,11 +39,11 @@ def createMatrix(self, name, atom=None, shape=None, title='', filters=None, # this version is tables 2.4-compatible: matrix = self.createCArray(self.root.data, name, atom, shape, title, filters, chunkshape, byteorder, createparents) - if (obj != None): + if (obj is not None): matrix[:] = obj # Store shape if we don't have one yet - if self._shape == None: + if self._shape is None: storeshape = np.array([matrix.shape[0],matrix.shape[1]], dtype='int32') self.root._v_attrs['SHAPE'] = storeshape self._shape = matrix.shape @@ -198,11 +198,12 @@ def _getMatricesByAttribute(self, key, value, matrices=None): answer = [] - if matrices==None: + if matrices is None: matrices = self.listNodes(self.root.data,'CArray') for m in matrices: - if m.attrs == None: continue + if m.attrs is None: + continue # Only test if key is present in matrix attributes if key in m.attrs._v_attrnames and m.attrs[key] == value: