def __init__(self, sel, sel_in, sel_out, sel_gpot, sel_spike, data_gpot, data_spike, columns=['interface', 'io', 'type'], ctrl_tag=CTRL_TAG, gpot_tag=GPOT_TAG, spike_tag=SPIKE_TAG, id=None, device=None, routing_table=None, rank_to_id=None, debug=False, time_sync=False, cache_file='cache.db'): if data_gpot is None: data_gpot = np.zeros(SelectorMethods.count_ports(sel_gpot), float) if data_spike is None: data_spike = np.zeros(SelectorMethods.count_ports(sel_spike), int) super(MyModule, self).__init__(sel, sel_in, sel_out, sel_gpot, sel_spike, data_gpot, data_spike, columns, ctrl_tag, gpot_tag, spike_tag, id, device, routing_table, rank_to_id, debug, time_sync) self.cache_file = cache_file self.pm['gpot'][self.interface.out_ports().gpot_ports( tuples=True)] = 1.0 self.pm['spike'][self.interface.out_ports().spike_ports( tuples=True)] = 1
def __init__(self, sel, sel_in, sel_out, sel_gpot, sel_spike, data_gpot=None, data_spike=None, columns=['interface', 'io', 'type'], port_data=PORT_DATA, port_ctrl=PORT_CTRL, port_time=PORT_TIME, id=None, device=None, debug=False): if data_gpot is None: data_gpot = np.zeros(SelectorMethods.count_ports(sel_gpot), float) if data_spike is None: data_spike = np.zeros(SelectorMethods.count_ports(sel_spike), int) super(MyModule, self).__init__(sel, sel_in, sel_out, sel_gpot, sel_spike, data_gpot, data_spike, columns, port_data, port_ctrl, port_time, id, device, debug, True) self.pm['gpot'][self.interface.out_ports().gpot_ports( tuples=True)] = 1.0 self.pm['spike'][self.interface.out_ports().spike_ports( tuples=True)] = 1
def __init__(self, sel, sel_in, sel_out, sel_gpot, sel_spike, data_gpot=None, data_spike=None, columns=['interface', 'io', 'type'], port_data=PORT_DATA, port_ctrl=PORT_CTRL, port_time=PORT_TIME, id=None, device=None, debug=False): if data_gpot is None: data_gpot = np.zeros(SelectorMethods.count_ports(sel_gpot), float) if data_spike is None: data_spike = np.zeros(SelectorMethods.count_ports(sel_spike), int) super(MyModule, self).__init__(sel, sel_in, sel_out, sel_gpot, sel_spike, data_gpot, data_spike, columns, port_data, port_ctrl, port_time, id, device, debug, True) # Initialize GPU arrays associated with ports: self.pm['gpot'][self.interface.out_ports().gpot_ports(tuples=True)] = 1.0 self.pm['spike'][self.interface.out_ports().spike_ports(tuples=True)] = 1
def __init__(self, sel, sel_in, sel_out, sel_gpot, sel_spike, data_gpot, data_spike, columns=['interface', 'io', 'type'], ctrl_tag=CTRL_TAG, gpot_tag=GPOT_TAG, spike_tag=SPIKE_TAG, id=None, device=None, routing_table=None, rank_to_id=None, debug=False, time_sync=False): if data_gpot is None: data_gpot = np.zeros(SelectorMethods.count_ports(sel_gpot), float) if data_spike is None: data_spike = np.zeros(SelectorMethods.count_ports(sel_spike), int) super(MyModule, self).__init__(sel, sel_in, sel_out, sel_gpot, sel_spike, data_gpot, data_spike, columns, ctrl_tag, gpot_tag, spike_tag, id, device, routing_table, rank_to_id, debug, time_sync) self.pm['gpot'][self.interface.out_ports().gpot_ports(tuples=True)] = 1.0 self.pm['spike'][self.interface.out_ports().spike_ports(tuples=True)] = 1
def __init__(self, sel, sel_in_gpot, sel_in_spike, sel_out_gpot, sel_out_spike, data_gpot, data_spike, port_data, port_ctrl, port_time, id): super(TestModule, self).__init__(sel, Selector.add(sel_in_gpot, sel_out_gpot), Selector.add(sel_in_spike, sel_out_spike), data_gpot, data_spike, ['interface', 'io', 'type'], port_data, port_ctrl, port_time, id, None, True, True) assert SelectorMethods.is_in(sel_in_gpot, sel) assert SelectorMethods.is_in(sel_out_gpot, sel) assert SelectorMethods.are_disjoint(sel_in_gpot, sel_out_gpot) assert SelectorMethods.is_in(sel_in_spike, sel) assert SelectorMethods.is_in(sel_out_spike, sel) assert SelectorMethods.are_disjoint(sel_in_spike, sel_out_spike) self.interface[sel_in_gpot, 'io', 'type'] = ['in', 'gpot'] self.interface[sel_out_gpot, 'io', 'type'] = ['out', 'gpot'] self.interface[sel_in_spike, 'io', 'type'] = ['in', 'spike'] self.interface[sel_out_spike, 'io', 'type'] = ['out', 'spike']
def setUp(self): self.df = df.copy() self.sel = SelectorMethods()
class test_path_like_selector(TestCase): def setUp(self): self.df = df.copy() self.sel = SelectorMethods() def test_select_empty(self): result = self.sel.select(self.df, [[]]) assert_frame_equal(result, self.df.drop(self.df.index)) def test_select_str(self): result = self.sel.select(self.df, '/foo') idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1), ('foo','mof',0), ('foo','mof',1), ('foo','mof',2)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_list(self): result = self.sel.select(self.df, [['foo']]) idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1), ('foo','mof',0), ('foo','mof',1), ('foo','mof',2)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_Selector(self): result = self.sel.select(self.df, Selector('/foo/qux[0:2]')) idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_comma(self): result = self.sel.select(self.df, '/foo/qux,/baz/mof') idx = pd.MultiIndex.from_tuples([('foo','qux', 0), ('foo','qux', 1), ('baz','mof', 0)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_plus(self): result = self.sel.select(self.df, '/foo+/qux+[0,1]') idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_dotplus(self): result = self.sel.select(self.df, '/[bar,baz].+/[qux,mof].+/[0,0]') idx = pd.MultiIndex.from_tuples([('bar','qux',0), ('baz','mof',0)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_paren(self): result = self.sel.select(self.df, '(/bar,/baz)') idx = pd.MultiIndex.from_tuples([('bar','qux',0), ('bar','qux',1), ('bar','qux',2), ('baz','qux',0), ('baz','mof',0)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_paren_plus(self): result = self.sel.select(self.df, '(/bar,/baz)+/qux') idx = pd.MultiIndex.from_tuples([('bar','qux',0), ('bar','qux',1), ('bar','qux',2), ('baz','qux',0)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_asterisk(self): result = self.sel.select(self.df, '/*/qux') idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1), ('bar','qux',0), ('bar','qux',1), ('bar','qux',2), ('baz','qux',0)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_integer_with_brackets(self): result = self.sel.select(self.df, '/bar/qux[1]') idx = pd.MultiIndex.from_tuples([('bar','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_integer_no_brackets(self): result = self.sel.select(self.df, '/bar/qux/1') idx = pd.MultiIndex.from_tuples([('bar','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_integer_set(self): result = self.sel.select(self.df, '/foo/qux[0,1]') idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_string_set(self): result = self.sel.select(self.df, '/foo/[qux,mof]') idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1), ('foo','mof',0), ('foo','mof',1), ('foo','mof',2)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_interval_no_bounds(self): result = self.sel.select(self.df, '/foo/mof[:]') idx = pd.MultiIndex.from_tuples([('foo','mof',0), ('foo','mof',1), ('foo','mof',2)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_interval_lower_bound(self): result = self.sel.select(self.df, '/foo/mof[1:]') idx = pd.MultiIndex.from_tuples([('foo','mof',1), ('foo','mof',2)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_interval_upper_bound(self): result = self.sel.select(self.df, '/foo/mof[:2]') idx = pd.MultiIndex.from_tuples([('foo','mof',0), ('foo','mof',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_interval_both_bounds(self): result = self.sel.select(self.df, '/bar/qux[0:2]') idx = pd.MultiIndex.from_tuples([('bar','qux',0), ('bar','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.loc[idx]) def test_select_order(self): data = np.random.rand(3) df = pd.DataFrame(data, pd.MultiIndex.from_tuples([('foo', i) for i in range(3)], names=[0, 1])) assert_array_equal(self.sel.select(df, '/foo[2,1,0]').values.flatten(), data[[2, 1, 0]]) def test_are_disjoint(self): self.assertTrue(self.sel.are_disjoint('/foo[0:10]/baz', '/bar[10:20]/qux')) self.assertFalse(self.sel.are_disjoint('/foo[0:10]/baz', '/foo[5:15]/[baz,qux]')) self.assertTrue(self.sel.are_disjoint('/foo', '')) self.assertTrue(self.sel.are_disjoint('', '')) self.assertFalse(self.sel.are_disjoint('/foo', '/foo', '')) self.assertTrue(self.sel.are_disjoint([['foo', slice(0, 10), 'baz']], [['bar', slice(10, 20), 'qux']])) self.assertFalse(self.sel.are_disjoint([['foo', slice(0, 10), 'baz']], [['foo', slice(5, 15), ['baz','qux']]])) def test_count_ports(self): self.assertEqual(self.sel.count_ports('/foo/bar[0:2],/moo/[qux,baz]'), 4) self.assertEqual(self.sel.count_ports(''), 0) # XXX Should this be allowed? [] isn't a valid selector: self.assertEqual(self.sel.count_ports([]), 0) def test_expand_str(self): self.assertSequenceEqual(self.sel.expand('/foo/bar[0:2],/moo/[qux,baz]'), [('foo', 'bar', 0), ('foo', 'bar', 1), ('moo', 'qux'), ('moo', 'baz')]) def test_expand_list(self): self.assertSequenceEqual(self.sel.expand([['foo', 'bar', slice(0, 2)], ['moo', ['qux', 'baz']]]), [('foo', 'bar', 0), ('foo', 'bar', 1), ('moo', 'qux'), ('moo', 'baz')]) def test_expand_empty(self): self.assertSequenceEqual(self.sel.expand([()]), [()]) self.assertSequenceEqual(self.sel.expand(''), [()]) self.assertSequenceEqual(self.sel.expand('/foo[0:0]'), [()]) def test_expand_pad(self): result = self.sel.expand('/foo/bar[0:2],/moo', float('inf')) self.assertSequenceEqual(result, [('foo', 'bar', 0), ('foo', 'bar', 1), ('moo', '', '')]) result = self.sel.expand('/foo/bar[0:2],/moo', 4) self.assertSequenceEqual(result, [('foo', 'bar', 0, ''), ('foo', 'bar', 1, ''), ('moo', '', '', '')]) result = self.sel.expand(Selector('/foo/bar[0:2],/moo'), float('inf')) self.assertSequenceEqual(result, [('foo', 'bar', 0), ('foo', 'bar', 1), ('moo', '', '')]) result = self.sel.expand(Selector('/foo/bar[0:2],/moo'), 4) self.assertSequenceEqual(result, [('foo', 'bar', 0, ''), ('foo', 'bar', 1, ''), ('moo', '', '', '')]) def test_get_index_str(self): idx = self.sel.get_index(self.df, '/foo/mof/*') assert_index_equal(idx, pd.MultiIndex(levels=[['foo'], ['mof'], [0, 1, 2]], labels=[[0, 0, 0], [0, 0, 0], [0, 1, 2]])) def test_get_index_list(self): idx = self.sel.get_index(self.df, [['foo', 'mof', '*']]) assert_index_equal(idx, pd.MultiIndex(levels=[['foo'], ['mof'], [0, 1, 2]], labels=[[0, 0, 0], [0, 0, 0], [0, 1, 2]])) def test_get_tuples_str(self): result = self.sel.get_tuples(df, '/foo/mof/*') self.assertSequenceEqual(result, [('foo', 'mof', 0), ('foo', 'mof', 1), ('foo', 'mof', 2)]) result = self.sel.get_tuples(df_single, '/foo') self.assertSequenceEqual(result, [('foo',), ('foo',)]) def test_get_tuples_list(self): result = self.sel.get_tuples(df, [['foo', 'mof', '*']]) self.assertSequenceEqual(result, [('foo', 'mof', 0), ('foo', 'mof', 1), ('foo', 'mof', 2)]) result = self.sel.get_tuples(df_single, [['foo']]) self.assertSequenceEqual(result, [('foo',), ('foo',)]) def test_get_tuples_empty(self): result = self.sel.get_tuples(df, [['foo', 'xxx', 0]]) self.assertSequenceEqual(result, []) result = self.sel.get_tuples(df_single, [['xxx']]) self.assertSequenceEqual(result, []) def test_is_ambiguous_str(self): self.assertTrue(self.sel.is_ambiguous('/foo/*')) self.assertTrue(self.sel.is_ambiguous('/foo/[5:]')) self.assertFalse(self.sel.is_ambiguous('/foo/[:10]')) self.assertFalse(self.sel.is_ambiguous('/foo/[5:10]')) def test_is_ambiguous_list(self): self.assertTrue(self.sel.is_ambiguous([['foo', '*']])) self.assertTrue(self.sel.is_ambiguous([['foo', slice(5, None)]])) self.assertFalse(self.sel.is_ambiguous([['foo', slice(0, 10)]])) self.assertFalse(self.sel.is_ambiguous([['foo', slice(5, 10)]])) def test_is_identifier(self): self.assertTrue(self.sel.is_identifier('/foo/bar')) self.assertFalse(self.sel.is_identifier(0)) self.assertFalse(self.sel.is_identifier('foo')) #self.assertFalse(self.sel.is_identifier('0')) # this doesn't work self.assertTrue(self.sel.is_identifier(['foo', 'bar'])) self.assertTrue(self.sel.is_identifier(['foo', 0])) self.assertFalse(self.sel.is_identifier(['foo', [0, 1]])) self.assertTrue(self.sel.is_identifier([['foo', 'bar']])) self.assertFalse(self.sel.is_identifier([['foo', 'bar'], ['baz']])) self.assertTrue(self.sel.is_identifier([['foo', 0]])) def test_to_identifier(self): self.assertEqual(self.sel.to_identifier(['foo']), '/foo') self.assertEqual(self.sel.to_identifier(['foo', 0]), '/foo[0]') self.assertEqual(self.sel.to_identifier(['foo', long(0)]), '/foo[0]') self.assertRaises(Exception, self.sel.to_identifier, 'foo') self.assertRaises(Exception, self.sel.to_identifier, [['foo', ['a', 'b']]]) self.assertRaises(Exception, self.sel.to_identifier, ['foo', (0, 2)]) def test_index_to_selector(self): idx = self.sel.make_index('/foo,/bar') self.assertSequenceEqual(self.sel.index_to_selector(idx), [('foo',), ('bar',)]) idx = self.sel.make_index('/foo[0:2]') self.assertSequenceEqual(self.sel.index_to_selector(idx), [('foo', 0), ('foo', 1)]) def test_is_expandable(self): self.assertFalse(self.sel.is_expandable('')) self.assertFalse(self.sel.is_expandable('/foo')) self.assertFalse(self.sel.is_expandable('/foo/bar')) self.assertFalse(self.sel.is_expandable('/foo/*')) self.assertFalse(self.sel.is_expandable([['foo']])) self.assertFalse(self.sel.is_expandable([['foo', 'bar']])) self.assertTrue(self.sel.is_expandable('/foo[0:2]')) self.assertTrue(self.sel.is_expandable('/foo[0,1,2]')) self.assertTrue(self.sel.is_expandable('[0:2]')) self.assertTrue(self.sel.is_expandable([['foo', [0, 1]]])) self.assertTrue(self.sel.is_expandable([['foo', [long(0), long(1)]]])) self.assertTrue(self.sel.is_expandable([['foo', 0], ['foo', 1]])) self.assertTrue(self.sel.is_expandable([[[0, 1]]])) def test_is_in_str(self): self.assertTrue(self.sel.is_in('', '/foo[0:5]')) self.assertTrue(self.sel.is_in('/foo/bar[5]', '/[foo,baz]/bar[0:10]')) self.assertFalse(self.sel.is_in('/qux/bar[5]', '/[foo,baz]/bar[0:10]')) def test_is_in_list(self): self.assertTrue(self.sel.is_in([()], [('foo', 0), ('foo', 1)])) self.assertTrue(self.sel.is_in([['foo', 'bar', [5]]], [[['foo', 'baz'], 'bar', slice(0, 10)]])) self.assertFalse(self.sel.is_in([['qux', 'bar', [5]]], [[['foo', 'baz'], 'bar', slice(0, 10)]])) def test_is_selector_empty(self): self.assertEqual(self.sel.is_selector_empty(''), True) self.assertEqual(self.sel.is_selector_empty([[]]), True) self.assertEqual(self.sel.is_selector_empty([()]), True) self.assertEqual(self.sel.is_selector_empty(((),)), True) self.assertEqual(self.sel.is_selector_empty([[], []]), True) self.assertEqual(self.sel.is_selector_empty([(), []]), True) self.assertEqual(self.sel.is_selector_empty(((), [])), True) self.assertEqual(self.sel.is_selector_empty('/foo'), False) self.assertEqual(self.sel.is_selector_empty('/foo/*'), False) self.assertEqual(self.sel.is_selector_empty([['foo']]), False) self.assertEqual(self.sel.is_selector_empty([['foo', 'bar']]), False) self.assertEqual(self.sel.is_selector_empty([['']]), False) # is this correct? def test_is_selector_str(self): self.assertEqual(self.sel.is_selector(''), True) self.assertEqual(self.sel.is_selector('/foo'), True) self.assertEqual(self.sel.is_selector('/foo/bar'), True) self.assertEqual(self.sel.is_selector('/foo!?'), True) self.assertEqual(self.sel.is_selector('/foo[0]'), True) self.assertEqual(self.sel.is_selector('/foo[0:2]'), True) self.assertEqual(self.sel.is_selector('/foo[0:]'), True) self.assertEqual(self.sel.is_selector('/foo[:2]'), True) self.assertEqual(self.sel.is_selector('/foo/*'), True) self.assertEqual(self.sel.is_selector('/foo,/bar'), True) self.assertEqual(self.sel.is_selector('/foo+/bar'), True) self.assertEqual(self.sel.is_selector('/foo[0:2].+/bar[0:2]'), True) self.assertEqual(self.sel.is_selector('/foo['), False) self.assertEqual(self.sel.is_selector('foo[0]'), False) def test_is_selector_list(self): self.assertEqual(self.sel.is_selector([[]]), True) self.assertEqual(self.sel.is_selector([['foo', 'bar']]), True) self.assertEqual(self.sel.is_selector([('foo', 'bar')]), True) self.assertEqual(self.sel.is_selector([('foo', '*')]), True) self.assertEqual(self.sel.is_selector([('foo', 'bar'), ('bar', 'qux')]), True) self.assertEqual(self.sel.is_selector([('foo', 0)]), True) self.assertEqual(self.sel.is_selector([('foo', long(0))]), True) self.assertEqual(self.sel.is_selector([('foo', slice(0, 2))]), True) self.assertEqual(self.sel.is_selector([('foo', slice(long(0), long(2)))]), True) self.assertEqual(self.sel.is_selector([('foo', slice(0, None))]), True) self.assertEqual(self.sel.is_selector([('foo', [0, 1])]), True) self.assertEqual(self.sel.is_selector([('foo', [long(0), long(1)])]), True) self.assertEqual(self.sel.is_selector([('foo', ['a', 'b'])]), True) self.assertEqual(self.sel.is_selector([('foo', ['a', 0])]), True) # XXX These are not correct: self.assertEqual(self.sel.is_selector([('foo', (0, 1, 2))]), False) self.assertEqual(self.sel.is_selector([('foo', 'bar'), ((0, 1, 2), 0)]), False) def test_make_index_empty(self): idx = self.sel.make_index('') assert_index_equal(idx, pd.MultiIndex(levels=[[]], labels=[[]], names=[0])) idx = self.sel.make_index(Selector('')) assert_index_equal(idx, pd.MultiIndex(levels=[[]], labels=[[]], names=[0])) def test_make_index_str_single_level(self): idx = self.sel.make_index('/foo') assert_index_equal(idx, pd.MultiIndex([['foo']], labels=[[0]], names=[0])) idx = self.sel.make_index('/foo,/bar') assert_index_equal(idx, pd.MultiIndex([['foo', 'bar']], labels=[[0,1]], names=[0])) def test_make_index_str_multiple_levels(self): idx = self.sel.make_index('/[foo,bar]/[0:3]') assert_index_equal(idx, pd.MultiIndex(levels=[['bar', 'foo'], [0, 1, 2]], labels=[[1, 1, 1, 0, 0, 0], [0, 1, 2, 0, 1, 2]], names=[0, 1])) def test_make_index_str_multiple_different_levels(self): idx = self.sel.make_index('/foo[0:3],/bar') assert_index_equal(idx, pd.MultiIndex(levels=[['bar', 'foo'], [0, 1, 2, '']], labels=[[1, 1, 1, 0], [0, 1, 2, 3]], names=[0, 1])) def test_make_index_list_single_level(self): idx = self.sel.make_index([['foo']]) assert_index_equal(idx, pd.MultiIndex([['foo']], labels=[[0]], names=[0])) idx = self.sel.make_index([['foo'], ['bar']]) assert_index_equal(idx, pd.MultiIndex([['foo', 'bar']], labels=[[0,1]], names=[0])) def test_make_index_list_multiple_levels(self): idx = self.sel.make_index([[['foo', 'bar'], slice(0, 3)]]) assert_index_equal(idx, pd.MultiIndex(levels=[['bar', 'foo'], [0, 1, 2]], labels=[[1, 1, 1, 0, 0, 0], [0, 1, 2, 0, 1, 2]], names=[0, 1])) def test_make_index_list_multiple_different_levels(self): idx = self.sel.make_index([['foo', [0, 1, 2]], ['bar']]) assert_index_equal(idx, pd.MultiIndex(levels=[['bar', 'foo'], [0, 1, 2, '']], labels=[[1, 1, 1, 0], [0, 1, 2, 3]], names=[0, 1])) def test_make_index_invalid(self): self.assertRaises(Exception, self.sel.make_index, 'foo/bar[') def test_max_levels_str(self): self.assertEqual(self.sel.max_levels('/foo/bar[0:10]'), 3) self.assertEqual(self.sel.max_levels('/foo/bar[0:10],/baz/qux'), 3) def test_max_levels_list(self): self.assertEqual(self.sel.max_levels([['foo', 'bar', slice(0, 10)]]), 3) self.assertEqual(self.sel.max_levels([['foo', 'bar', slice(0, 10)], ['baz', 'qux']]), 3) def test_pad_tuple_list(self): x = [('a', 'b'), ('c', 'd')] self.assertSequenceEqual(self.sel.pad_tuple_list(x, 0), x) self.assertSequenceEqual(self.sel.pad_tuple_list(x, 3), [('a', 'b', ''), ('c', 'd', '')]) x = [('a', 'b'), ('c',)] self.assertSequenceEqual(self.sel.pad_tuple_list(x, 0), x) self.assertSequenceEqual(self.sel.pad_tuple_list(x, 3), [('a', 'b', ''), ('c', '', '')]) def test_pad_parsed(self): sel = [['x', 'y'], ['a', 'b', 'c']] sel_id = id(sel) sel_padded = self.sel.pad_parsed(sel, float('inf')) self.assertSequenceEqual(sel_padded, [['x', 'y', ''], ['a', 'b', 'c']]) self.assertEqual(sel_id, id(sel_padded)) sel = [['x', 'y'], ['a', 'b', 'c']] sel_id = id(sel) sel_padded = self.sel.pad_parsed(sel, 4) self.assertSequenceEqual(sel_padded, [['x', 'y', '', ''], ['a', 'b', 'c', '']]) self.assertEqual(sel_id, id(sel_padded)) sel = [['x', 'y'], ['a', 'b', 'c']] sel_id = id(sel) sel_padded = self.sel.pad_parsed(sel, float('inf'), False) self.assertSequenceEqual(sel_padded, [['x', 'y', ''], ['a', 'b', 'c']]) self.assertNotEqual(sel_id, id(sel_padded)) def test_tokens_to_str(self): self.assertEqual(self.sel.tokens_to_str([]), '') self.assertEqual(self.sel.tokens_to_str(['a']), '/a') self.assertEqual(self.sel.tokens_to_str(['a', 0]), '/a/0') self.assertEqual(self.sel.tokens_to_str(('a', 0)), '/a/0') self.assertEqual(self.sel.tokens_to_str(('a', long(0))), '/a/0') self.assertEqual(self.sel.tokens_to_str(['a', '*']), '/a/*') self.assertEqual(self.sel.tokens_to_str(['a', 'b', 0]), '/a/b/0') self.assertEqual(self.sel.tokens_to_str(['a', 'b', [0, 1]]), '/a/b[0,1]') self.assertEqual(self.sel.tokens_to_str(['a', 'b', (0, 1)]), '/a/b[0,1]') self.assertEqual(self.sel.tokens_to_str(['a', 'b', slice(0, 5)]), '/a/b[0:5]') self.assertEqual(self.sel.tokens_to_str(['a', 'b', slice(long(0), long(5))]), '/a/b[0:5]') self.assertEqual(self.sel.tokens_to_str(['a', 'b', slice(None, 5)]), '/a/b[:5]') def test_collapse(self): self.assertEqual(self.sel.collapse([]), '') self.assertEqual(self.sel.collapse([['a']]), '/a') self.assertEqual(self.sel.collapse([['a', 0]]), '/a/0') self.assertEqual(self.sel.collapse([['a', long(0)]]), '/a/0') self.assertEqual(self.sel.collapse([('a', 0)]), '/a/0') self.assertEqual(self.sel.collapse([['a', 'b', 0]]), '/a/b/0') self.assertEqual(self.sel.collapse([['a', 0], ['b', 0]]), '/a/0,/b/0') self.assertEqual(self.sel.collapse([['a', 'b', (0, 1)], ['c', 'd']]), '/a/b[0,1],/c/d')
class test_path_like_selector(TestCase): def setUp(self): self.df = df.copy() self.sel = SelectorMethods() def test_select_empty(self): result = self.sel.select(self.df, [[]]) assert_frame_equal(result, self.df.drop(self.df.index)) def test_select_str(self): result = self.sel.select(self.df, '/foo') idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1), ('foo','mof',0), ('foo','mof',1), ('foo','mof',2)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_list(self): result = self.sel.select(self.df, [['foo']]) idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1), ('foo','mof',0), ('foo','mof',1), ('foo','mof',2)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_Selector(self): result = self.sel.select(self.df, Selector('/foo/qux[0:2]')) idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_comma(self): result = self.sel.select(self.df, '/foo/qux,/baz/mof') idx = pd.MultiIndex.from_tuples([('foo','qux', 0), ('foo','qux', 1), ('baz','mof', 0)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_plus(self): result = self.sel.select(self.df, '/foo+/qux+[0,1]') idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_dotplus(self): result = self.sel.select(self.df, '/[bar,baz].+/[qux,mof].+/[0,0]') idx = pd.MultiIndex.from_tuples([('bar','qux',0), ('baz','mof',0)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_paren(self): result = self.sel.select(self.df, '(/bar,/baz)') idx = pd.MultiIndex.from_tuples([('bar','qux',0), ('bar','qux',1), ('bar','qux',2), ('baz','qux',0), ('baz','mof',0)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_paren_plus(self): result = self.sel.select(self.df, '(/bar,/baz)+/qux') idx = pd.MultiIndex.from_tuples([('bar','qux',0), ('bar','qux',1), ('bar','qux',2), ('baz','qux',0)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_asterisk(self): result = self.sel.select(self.df, '/*/qux') idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1), ('bar','qux',0), ('bar','qux',1), ('bar','qux',2), ('baz','qux',0)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_integer_with_brackets(self): result = self.sel.select(self.df, '/bar/qux[1]') idx = pd.MultiIndex.from_tuples([('bar','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_integer_no_brackets(self): result = self.sel.select(self.df, '/bar/qux/1') idx = pd.MultiIndex.from_tuples([('bar','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_integer_set(self): result = self.sel.select(self.df, '/foo/qux[0,1]') idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_string_set(self): result = self.sel.select(self.df, '/foo/[qux,mof]') idx = pd.MultiIndex.from_tuples([('foo','qux',0), ('foo','qux',1), ('foo','mof',0), ('foo','mof',1), ('foo','mof',2)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_interval_no_bounds(self): result = self.sel.select(self.df, '/foo/mof[:]') idx = pd.MultiIndex.from_tuples([('foo','mof',0), ('foo','mof',1), ('foo','mof',2)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_interval_lower_bound(self): result = self.sel.select(self.df, '/foo/mof[1:]') idx = pd.MultiIndex.from_tuples([('foo','mof',1), ('foo','mof',2)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_interval_upper_bound(self): result = self.sel.select(self.df, '/foo/mof[:2]') idx = pd.MultiIndex.from_tuples([('foo','mof',0), ('foo','mof',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_interval_both_bounds(self): result = self.sel.select(self.df, '/bar/qux[0:2]') idx = pd.MultiIndex.from_tuples([('bar','qux',0), ('bar','qux',1)], names=[0, 1, 2]) assert_frame_equal(result, self.df.ix[idx]) def test_select_order(self): data = np.random.rand(3) df = pd.DataFrame(data, pd.MultiIndex.from_tuples([('foo', i) for i in xrange(3)], names=[0, 1])) assert_array_equal(self.sel.select(df, '/foo[2,1,0]').values.flatten(), data[[2, 1, 0]]) def test_are_disjoint(self): self.assertTrue(self.sel.are_disjoint('/foo[0:10]/baz', '/bar[10:20]/qux')) self.assertFalse(self.sel.are_disjoint('/foo[0:10]/baz', '/foo[5:15]/[baz,qux]')) self.assertTrue(self.sel.are_disjoint('/foo', '')) self.assertTrue(self.sel.are_disjoint('', '')) self.assertFalse(self.sel.are_disjoint('/foo', '/foo', '')) self.assertTrue(self.sel.are_disjoint([['foo', slice(0, 10), 'baz']], [['bar', slice(10, 20), 'qux']])) self.assertFalse(self.sel.are_disjoint([['foo', slice(0, 10), 'baz']], [['foo', slice(5, 15), ['baz','qux']]])) def test_count_ports(self): self.assertEqual(self.sel.count_ports('/foo/bar[0:2],/moo/[qux,baz]'), 4) self.assertEqual(self.sel.count_ports(''), 0) # XXX Should this be allowed? [] isn't a valid selector: self.assertEqual(self.sel.count_ports([]), 0) def test_expand_str(self): self.assertSequenceEqual(self.sel.expand('/foo/bar[0:2],/moo/[qux,baz]'), [('foo', 'bar', 0), ('foo', 'bar', 1), ('moo', 'qux'), ('moo', 'baz')]) def test_expand_list(self): self.assertSequenceEqual(self.sel.expand([['foo', 'bar', slice(0, 2)], ['moo', ['qux', 'baz']]]), [('foo', 'bar', 0), ('foo', 'bar', 1), ('moo', 'qux'), ('moo', 'baz')]) def test_expand_empty(self): self.assertSequenceEqual(self.sel.expand([()]), [()]) self.assertSequenceEqual(self.sel.expand(''), [()]) self.assertSequenceEqual(self.sel.expand('/foo[0:0]'), [()]) def test_expand_pad(self): result = self.sel.expand('/foo/bar[0:2],/moo', float('inf')) self.assertSequenceEqual(result, [('foo', 'bar', 0), ('foo', 'bar', 1), ('moo', '', '')]) result = self.sel.expand('/foo/bar[0:2],/moo', 4) self.assertSequenceEqual(result, [('foo', 'bar', 0, ''), ('foo', 'bar', 1, ''), ('moo', '', '', '')]) result = self.sel.expand(Selector('/foo/bar[0:2],/moo'), float('inf')) self.assertSequenceEqual(result, [('foo', 'bar', 0), ('foo', 'bar', 1), ('moo', '', '')]) result = self.sel.expand(Selector('/foo/bar[0:2],/moo'), 4) self.assertSequenceEqual(result, [('foo', 'bar', 0, ''), ('foo', 'bar', 1, ''), ('moo', '', '', '')]) def test_get_index_str(self): idx = self.sel.get_index(self.df, '/foo/mof/*') assert_index_equal(idx, pd.MultiIndex(levels=[['foo'], ['mof'], [0, 1, 2]], labels=[[0, 0, 0], [0, 0, 0], [0, 1, 2]])) def test_get_index_list(self): idx = self.sel.get_index(self.df, [['foo', 'mof', '*']]) assert_index_equal(idx, pd.MultiIndex(levels=[['foo'], ['mof'], [0, 1, 2]], labels=[[0, 0, 0], [0, 0, 0], [0, 1, 2]])) def test_get_tuples_str(self): result = self.sel.get_tuples(df, '/foo/mof/*') self.assertSequenceEqual(result, [('foo', 'mof', 0), ('foo', 'mof', 1), ('foo', 'mof', 2)]) result = self.sel.get_tuples(df_single, '/foo') self.assertSequenceEqual(result, [('foo',), ('foo',)]) def test_get_tuples_list(self): result = self.sel.get_tuples(df, [['foo', 'mof', '*']]) self.assertSequenceEqual(result, [('foo', 'mof', 0), ('foo', 'mof', 1), ('foo', 'mof', 2)]) result = self.sel.get_tuples(df_single, [['foo']]) self.assertSequenceEqual(result, [('foo',), ('foo',)]) def test_get_tuples_empty(self): result = self.sel.get_tuples(df, [['foo', 'xxx', 0]]) self.assertSequenceEqual(result, []) result = self.sel.get_tuples(df_single, [['xxx']]) self.assertSequenceEqual(result, []) def test_is_ambiguous_str(self): self.assertTrue(self.sel.is_ambiguous('/foo/*')) self.assertTrue(self.sel.is_ambiguous('/foo/[5:]')) self.assertFalse(self.sel.is_ambiguous('/foo/[:10]')) self.assertFalse(self.sel.is_ambiguous('/foo/[5:10]')) def test_is_ambiguous_list(self): self.assertTrue(self.sel.is_ambiguous([['foo', '*']])) self.assertTrue(self.sel.is_ambiguous([['foo', slice(5, None)]])) self.assertFalse(self.sel.is_ambiguous([['foo', slice(0, 10)]])) self.assertFalse(self.sel.is_ambiguous([['foo', slice(5, 10)]])) def test_is_identifier(self): self.assertTrue(self.sel.is_identifier('/foo/bar')) self.assertFalse(self.sel.is_identifier(0)) self.assertFalse(self.sel.is_identifier('foo')) #self.assertFalse(self.sel.is_identifier('0')) # this doesn't work self.assertTrue(self.sel.is_identifier(['foo', 'bar'])) self.assertTrue(self.sel.is_identifier(['foo', 0])) self.assertFalse(self.sel.is_identifier(['foo', [0, 1]])) self.assertTrue(self.sel.is_identifier([['foo', 'bar']])) self.assertFalse(self.sel.is_identifier([['foo', 'bar'], ['baz']])) self.assertTrue(self.sel.is_identifier([['foo', 0]])) def test_to_identifier(self): self.assertEqual(self.sel.to_identifier(['foo']), '/foo') self.assertEqual(self.sel.to_identifier(['foo', 0]), '/foo[0]') self.assertEqual(self.sel.to_identifier(['foo', 0L]), '/foo[0]') self.assertRaises(Exception, self.sel.to_identifier, 'foo') self.assertRaises(Exception, self.sel.to_identifier, [['foo', ['a', 'b']]]) self.assertRaises(Exception, self.sel.to_identifier, ['foo', (0, 2)]) def test_index_to_selector(self): idx = self.sel.make_index('/foo,/bar') self.assertSequenceEqual(self.sel.index_to_selector(idx), [('foo',), ('bar',)]) idx = self.sel.make_index('/foo[0:2]') self.assertSequenceEqual(self.sel.index_to_selector(idx), [('foo', 0), ('foo', 1)]) def test_is_expandable(self): self.assertFalse(self.sel.is_expandable('')) self.assertFalse(self.sel.is_expandable('/foo')) self.assertFalse(self.sel.is_expandable('/foo/bar')) self.assertFalse(self.sel.is_expandable('/foo/*')) self.assertFalse(self.sel.is_expandable([['foo']])) self.assertFalse(self.sel.is_expandable([['foo', 'bar']])) self.assertTrue(self.sel.is_expandable('/foo[0:2]')) self.assertTrue(self.sel.is_expandable('/foo[0,1,2]')) self.assertTrue(self.sel.is_expandable('[0:2]')) self.assertTrue(self.sel.is_expandable([['foo', [0, 1]]])) self.assertTrue(self.sel.is_expandable([['foo', [0L, 1L]]])) self.assertTrue(self.sel.is_expandable([['foo', 0], ['foo', 1]])) self.assertTrue(self.sel.is_expandable([[[0, 1]]])) def test_is_in_str(self): self.assertTrue(self.sel.is_in('', '/foo[0:5]')) self.assertTrue(self.sel.is_in('/foo/bar[5]', '/[foo,baz]/bar[0:10]')) self.assertFalse(self.sel.is_in('/qux/bar[5]', '/[foo,baz]/bar[0:10]')) def test_is_in_list(self): self.assertTrue(self.sel.is_in([()], [('foo', 0), ('foo', 1)])) self.assertTrue(self.sel.is_in([['foo', 'bar', [5]]], [[['foo', 'baz'], 'bar', slice(0, 10)]])) self.assertFalse(self.sel.is_in([['qux', 'bar', [5]]], [[['foo', 'baz'], 'bar', slice(0, 10)]])) def test_is_selector_empty(self): self.assertEqual(self.sel.is_selector_empty(''), True) self.assertEqual(self.sel.is_selector_empty([[]]), True) self.assertEqual(self.sel.is_selector_empty([()]), True) self.assertEqual(self.sel.is_selector_empty(((),)), True) self.assertEqual(self.sel.is_selector_empty([[], []]), True) self.assertEqual(self.sel.is_selector_empty([(), []]), True) self.assertEqual(self.sel.is_selector_empty(((), [])), True) self.assertEqual(self.sel.is_selector_empty('/foo'), False) self.assertEqual(self.sel.is_selector_empty('/foo/*'), False) self.assertEqual(self.sel.is_selector_empty([['foo']]), False) self.assertEqual(self.sel.is_selector_empty([['foo', 'bar']]), False) self.assertEqual(self.sel.is_selector_empty([['']]), False) # is this correct? def test_is_selector_str(self): self.assertEqual(self.sel.is_selector(''), True) self.assertEqual(self.sel.is_selector('/foo'), True) self.assertEqual(self.sel.is_selector('/foo/bar'), True) self.assertEqual(self.sel.is_selector('/foo!?'), True) self.assertEqual(self.sel.is_selector('/foo[0]'), True) self.assertEqual(self.sel.is_selector('/foo[0:2]'), True) self.assertEqual(self.sel.is_selector('/foo[0:]'), True) self.assertEqual(self.sel.is_selector('/foo[:2]'), True) self.assertEqual(self.sel.is_selector('/foo/*'), True) self.assertEqual(self.sel.is_selector('/foo,/bar'), True) self.assertEqual(self.sel.is_selector('/foo+/bar'), True) self.assertEqual(self.sel.is_selector('/foo[0:2].+/bar[0:2]'), True) self.assertEqual(self.sel.is_selector('/foo['), False) self.assertEqual(self.sel.is_selector('foo[0]'), False) def test_is_selector_list(self): self.assertEqual(self.sel.is_selector([[]]), True) self.assertEqual(self.sel.is_selector([['foo', 'bar']]), True) self.assertEqual(self.sel.is_selector([('foo', 'bar')]), True) self.assertEqual(self.sel.is_selector([('foo', '*')]), True) self.assertEqual(self.sel.is_selector([('foo', 'bar'), ('bar', 'qux')]), True) self.assertEqual(self.sel.is_selector([('foo', 0)]), True) self.assertEqual(self.sel.is_selector([('foo', 0L)]), True) self.assertEqual(self.sel.is_selector([('foo', slice(0, 2))]), True) self.assertEqual(self.sel.is_selector([('foo', slice(0L, 2L))]), True) self.assertEqual(self.sel.is_selector([('foo', slice(0, None))]), True) self.assertEqual(self.sel.is_selector([('foo', [0, 1])]), True) self.assertEqual(self.sel.is_selector([('foo', [0L, 1L])]), True) self.assertEqual(self.sel.is_selector([('foo', ['a', 'b'])]), True) self.assertEqual(self.sel.is_selector([('foo', ['a', 0])]), True) # XXX These are not correct: self.assertEqual(self.sel.is_selector([('foo', (0, 1, 2))]), False) self.assertEqual(self.sel.is_selector([('foo', 'bar'), ((0, 1, 2), 0)]), False) def test_make_index_empty(self): idx = self.sel.make_index('') assert_index_equal(idx, pd.Index([], dtype='object')) idx = self.sel.make_index(Selector('')) assert_index_equal(idx, pd.Index([], dtype='object')) def test_make_index_str_single_level(self): idx = self.sel.make_index('/foo') assert_index_equal(idx, pd.Index(['foo'], name=0, dtype='object')) idx = self.sel.make_index('/foo,/bar') assert_index_equal(idx, pd.Index(['foo', 'bar'], name=0, dtype='object')) def test_make_index_str_multiple_levels(self): idx = self.sel.make_index('/[foo,bar]/[0:3]') assert_index_equal(idx, pd.MultiIndex(levels=[['bar', 'foo'], [0, 1, 2]], labels=[[1, 1, 1, 0, 0, 0], [0, 1, 2, 0, 1, 2]], names=[0, 1])) def test_make_index_str_multiple_different_levels(self): idx = self.sel.make_index('/foo[0:3],/bar') assert_index_equal(idx, pd.MultiIndex(levels=[['bar', 'foo'], [0, 1, 2, '']], labels=[[1, 1, 1, 0], [0, 1, 2, 3]], names=[0, 1])) def test_make_index_list_single_level(self): idx = self.sel.make_index([['foo']]) assert_index_equal(idx, pd.Index(['foo'], name=0, dtype='object')) idx = self.sel.make_index([['foo'], ['bar']]) assert_index_equal(idx, pd.Index(['foo', 'bar'], name=0, dtype='object')) def test_make_index_list_multiple_levels(self): idx = self.sel.make_index([[['foo', 'bar'], slice(0, 3)]]) assert_index_equal(idx, pd.MultiIndex(levels=[['bar', 'foo'], [0, 1, 2]], labels=[[1, 1, 1, 0, 0, 0], [0, 1, 2, 0, 1, 2]], names=[0, 1])) def test_make_index_list_multiple_different_levels(self): idx = self.sel.make_index([['foo', [0, 1, 2]], ['bar']]) assert_index_equal(idx, pd.MultiIndex(levels=[['bar', 'foo'], [0, 1, 2, '']], labels=[[1, 1, 1, 0], [0, 1, 2, 3]], names=[0, 1])) def test_make_index_invalid(self): self.assertRaises(Exception, self.sel.make_index, 'foo/bar[') def test_max_levels_str(self): self.assertEqual(self.sel.max_levels('/foo/bar[0:10]'), 3) self.assertEqual(self.sel.max_levels('/foo/bar[0:10],/baz/qux'), 3) def test_max_levels_list(self): self.assertEqual(self.sel.max_levels([['foo', 'bar', slice(0, 10)]]), 3) self.assertEqual(self.sel.max_levels([['foo', 'bar', slice(0, 10)], ['baz', 'qux']]), 3) def test_pad_tuple_list(self): x = [('a', 'b'), ('c', 'd')] self.assertSequenceEqual(self.sel.pad_tuple_list(x, 0), x) self.assertSequenceEqual(self.sel.pad_tuple_list(x, 3), [('a', 'b', ''), ('c', 'd', '')]) x = [('a', 'b'), ('c',)] self.assertSequenceEqual(self.sel.pad_tuple_list(x, 0), x) self.assertSequenceEqual(self.sel.pad_tuple_list(x, 3), [('a', 'b', ''), ('c', '', '')]) def test_pad_parsed(self): sel = [['x', 'y'], ['a', 'b', 'c']] sel_id = id(sel) sel_padded = self.sel.pad_parsed(sel, float('inf')) self.assertSequenceEqual(sel_padded, [['x', 'y', ''], ['a', 'b', 'c']]) self.assertEqual(sel_id, id(sel_padded)) sel = [['x', 'y'], ['a', 'b', 'c']] sel_id = id(sel) sel_padded = self.sel.pad_parsed(sel, 4) self.assertSequenceEqual(sel_padded, [['x', 'y', '', ''], ['a', 'b', 'c', '']]) self.assertEqual(sel_id, id(sel_padded)) sel = [['x', 'y'], ['a', 'b', 'c']] sel_id = id(sel) sel_padded = self.sel.pad_parsed(sel, float('inf'), False) self.assertSequenceEqual(sel_padded, [['x', 'y', ''], ['a', 'b', 'c']]) self.assertNotEqual(sel_id, id(sel_padded)) def test_tokens_to_str(self): self.assertEqual(self.sel.tokens_to_str([]), '') self.assertEqual(self.sel.tokens_to_str(['a']), '/a') self.assertEqual(self.sel.tokens_to_str(['a', 0]), '/a/0') self.assertEqual(self.sel.tokens_to_str(('a', 0)), '/a/0') self.assertEqual(self.sel.tokens_to_str(('a', 0L)), '/a/0') self.assertEqual(self.sel.tokens_to_str(['a', '*']), '/a/*') self.assertEqual(self.sel.tokens_to_str(['a', 'b', 0]), '/a/b/0') self.assertEqual(self.sel.tokens_to_str(['a', 'b', [0, 1]]), '/a/b[0,1]') self.assertEqual(self.sel.tokens_to_str(['a', 'b', (0, 1)]), '/a/b[0,1]') self.assertEqual(self.sel.tokens_to_str(['a', 'b', slice(0, 5)]), '/a/b[0:5]') self.assertEqual(self.sel.tokens_to_str(['a', 'b', slice(0L, 5L)]), '/a/b[0:5]') self.assertEqual(self.sel.tokens_to_str(['a', 'b', slice(None, 5)]), '/a/b[:5]') def test_collapse(self): self.assertEqual(self.sel.collapse([]), '') self.assertEqual(self.sel.collapse([['a']]), '/a') self.assertEqual(self.sel.collapse([['a', 0]]), '/a/0') self.assertEqual(self.sel.collapse([['a', 0L]]), '/a/0') self.assertEqual(self.sel.collapse([('a', 0)]), '/a/0') self.assertEqual(self.sel.collapse([['a', 'b', 0]]), '/a/b/0') self.assertEqual(self.sel.collapse([['a', 0], ['b', 0]]), '/a/0,/b/0') self.assertEqual(self.sel.collapse([['a', 'b', (0, 1)], ['c', 'd']]), '/a/b[0,1],/c/d')
def test_transmit_spikes_one_to_many(self): m1_sel_in_gpot = Selector('') m1_sel_out_gpot = Selector('') m1_sel_in_spike = Selector('') m1_sel_out_spike = Selector('/m1/out/spike[0:4]') m1_sel, m1_sel_in, m1_sel_out, m1_sel_gpot, m1_sel_spike = \ make_sels(m1_sel_in_gpot, m1_sel_out_gpot, m1_sel_in_spike, m1_sel_out_spike) N1_gpot = SelectorMethods.count_ports(m1_sel_gpot) N1_spike = SelectorMethods.count_ports(m1_sel_spike) m2_sel_in_gpot = Selector('') m2_sel_out_gpot = Selector('') m2_sel_in_spike = Selector('/m2/in/spike[0:4]') m2_sel_out_spike = Selector('') m2_sel, m2_sel_in, m2_sel_out, m2_sel_gpot, m2_sel_spike = \ make_sels(m2_sel_in_gpot, m2_sel_out_gpot, m2_sel_in_spike, m2_sel_out_spike) N2_gpot = SelectorMethods.count_ports(m2_sel_gpot) N2_spike = SelectorMethods.count_ports(m2_sel_spike) m1_id = 'm1' self.man.add(MyModule1, m1_id, m1_sel, m1_sel_in, m1_sel_out, m1_sel_gpot, m1_sel_spike, np.zeros(N1_gpot, dtype=np.double), np.zeros(N1_spike, dtype=int), device=0, debug=debug, out_spike_data=[1, 0, 0, 0]) f, out_file_name = tempfile.mkstemp() os.close(f) m2_id = 'm2' self.man.add(MyModule2, m2_id, m2_sel, m2_sel_in, m2_sel_out, m2_sel_gpot, m2_sel_spike, np.zeros(N2_gpot, dtype=np.double), np.zeros(N2_spike, dtype=int), device=1, debug=debug, out_file_name=out_file_name) pat12 = Pattern(m1_sel, m2_sel) pat12.interface[m1_sel_out_gpot] = [0, 'in', 'gpot'] pat12.interface[m1_sel_in_gpot] = [0, 'out', 'gpot'] pat12.interface[m1_sel_out_spike] = [0, 'in', 'spike'] pat12.interface[m1_sel_in_spike] = [0, 'out', 'spike'] pat12.interface[m2_sel_in_gpot] = [1, 'out', 'gpot'] pat12.interface[m2_sel_out_gpot] = [1, 'in', 'gpot'] pat12.interface[m2_sel_in_spike] = [1, 'out', 'spike'] pat12.interface[m2_sel_out_spike] = [1, 'in', 'spike'] pat12['/m1/out/spike[0]', '/m2/in/spike[0]'] = 1 pat12['/m1/out/spike[0]', '/m2/in/spike[1]'] = 1 pat12['/m1/out/spike[0]', '/m2/in/spike[2]'] = 1 pat12['/m1/out/spike[0]', '/m2/in/spike[3]'] = 1 self.man.connect(m1_id, m2_id, pat12, 0, 1) # Run emulation for 2 steps: self.man.spawn() self.man.start(2) self.man.wait() # Get output of m2: with open(out_file_name, 'r') as f: output = pickle.load(f) os.remove(out_file_name) self.assertSequenceEqual(list(output), [1, 1, 1, 1])
def __init__(self, sel, sel_in, sel_out, sel_gpot, sel_spike, data_gpot, data_spike, columns=['interface', 'io', 'type'], ctrl_tag=CTRL_TAG, gpot_tag=GPOT_TAG, spike_tag=SPIKE_TAG, id=None, device=None, routing_table=None, rank_to_id=None, debug=False, time_sync=False, print_timing=False): super(Module, self).__init__(ctrl_tag) self.debug = debug self.time_sync = time_sync self.device = device self.print_timing = print_timing self._gpot_tag = gpot_tag self._spike_tag = spike_tag # Require several necessary attribute columns: if 'interface' not in columns: raise ValueError('interface column required') if 'io' not in columns: raise ValueError('io column required') if 'type' not in columns: raise ValueError('type column required') # Initialize GPU here so as to be able to initialize a port mapper # containing GPU memory: self._init_gpu() # This is needed to ensure that MPI_Finalize is called before PyCUDA # attempts to clean up; see # https://groups.google.com/forum/#!topic/mpi4py/by0Rd5q0Ayw atexit.register(MPI.Finalize) # Manually register the file close method associated with MPIOutput # so that it is called by atexit before MPI.Finalize() (if the file is # closed after MPI.Finalize() is called, an error will occur): for k, v in iteritems(twiggy.emitters): if isinstance(v._output, MPIOutput): atexit.register(v._output.close) # Ensure that the input and output port selectors respectively # select mutually exclusive subsets of the set of all ports exposed by # the module: if not SelectorMethods.is_in(sel_in, sel): raise ValueError('input port selector not in selector of all ports') if not SelectorMethods.is_in(sel_out, sel): raise ValueError('output port selector not in selector of all ports') if not SelectorMethods.are_disjoint(sel_in, sel_out): raise ValueError('input and output port selectors not disjoint') #assert(SelectorMethods.is_in(sel_in, sel)) #assert(SelectorMethods.is_in(sel_out, sel)) #assert(SelectorMethods.are_disjoint(sel_in, sel_out)) # Ensure that the graded potential and spiking port selectors # respectively select mutually exclusive subsets of the set of all ports # exposed by the module: #assert(SelectorMethods.is_in(sel_gpot, sel)) #assert(SelectorMethods.is_in(sel_spike, sel)) #assert(SelectorMethods.are_disjoint(sel_gpot, sel_spike)) # Save routing table and mapping between MPI ranks and module IDs: self.routing_table = routing_table self.rank_to_id = rank_to_id # Generate a unique ID if none is specified: if id is None: self.id = uid() else: # If a unique ID was specified and the routing table is not empty # (i.e., there are connections between multiple modules), the id # must be a node in the routing table: if routing_table is not None and len(routing_table.ids) and \ not routing_table.has_node(id): raise ValueError('routing table must contain specified ' 'module ID: {}'.format(id)) self.id = id # Reformat logger name: LoggerMixin.__init__(self, 'mod %s' % self.id) if self.print_timing: start = time.time() # Create module interface given the specified ports: self.interface = Interface(sel, columns) # Set the interface ID to 0; we assume that a module only has one interface: self.interface[sel, 'interface'] = 0 # Set the port attributes: if len(sel_in): self.interface[sel_in, 'io'] = 'in' if len(sel_out): self.interface[sel_out, 'io'] = 'out' if len(sel_gpot): self.interface[sel_gpot, 'type'] = 'gpot' if len(sel_spike): self.interface[sel_spike, 'type'] = 'spike' if self.print_timing: self.log_info('Elapsed time for setting up interface: {:.3f} seconds'.format(time.time()-start)) start = time.time() # Find the graded potential and spiking ports: self.gpot_ports = self.interface.gpot_ports() self.spike_ports = self.interface.spike_ports() if len(self.gpot_ports): self.in_gpot_ports = self.gpot_ports.in_ports(tuples=True) self.out_gpot_ports = self.gpot_ports.out_ports(tuples=True) else: self.in_gpot_ports = [] self.out_gpot_ports = [] if len(self.spike_ports): self.in_spike_ports = self.spike_ports.in_ports(tuples=True) self.out_spike_ports = self.spike_ports.out_ports(tuples=True) else: self.in_spike_ports = [] self.out_spike_ports = [] if self.print_timing: self.log_info('Elapsed time for extracting ports: {:.3f} seconds'.format(time.time()-start)) start = time.time() # Set up mapper between port identifiers and their associated data: if len(data_gpot) != len(self.gpot_ports): raise ValueError('incompatible gpot port data array length') if len(data_spike) != len(self.spike_ports): raise ValueError('incompatible spike port data array length') self.data = {} self.data['gpot'] = gpuarray.to_gpu(data_gpot) self.data['spike'] = gpuarray.to_gpu(data_spike) self.pm = {} self.pm['gpot'] = GPUPortMapper(sel_gpot, self.data['gpot'], make_copy=False) self.pm['spike'] = GPUPortMapper(sel_spike, self.data['spike'], make_copy=False) if self.print_timing: cuda.Context.synchronize() self.log_info('Elapsed time for creating array and PortMapper {} seconds'.format(time.time()-start))
sel_spike = sel_in_spike+sel_out_spike return sel, sel_in, sel_out, sel_gpot, sel_spike logger = mpi.setup_logger(screen=True, file_name='neurokernel.log', mpi_comm=MPI.COMM_WORLD, multiline=True) man = Manager() m1_sel_in_gpot = Selector('/a/in/gpot[0:2]') m1_sel_out_gpot = Selector('/a/out/gpot[0:2]') m1_sel_in_spike = Selector('/a/in/spike[0:2]') m1_sel_out_spike = Selector('/a/out/spike[0:2]') m1_sel, m1_sel_in, m1_sel_out, m1_sel_gpot, m1_sel_spike = \ make_sels(m1_sel_in_gpot, m1_sel_out_gpot, m1_sel_in_spike, m1_sel_out_spike) N1_gpot = SelectorMethods.count_ports(m1_sel_gpot) N1_spike = SelectorMethods.count_ports(m1_sel_spike) m2_sel_in_gpot = Selector('/b/in/gpot[0:2]') m2_sel_out_gpot = Selector('/b/out/gpot[0:2]') m2_sel_in_spike = Selector('/b/in/spike[0:2]') m2_sel_out_spike = Selector('/b/out/spike[0:2]') m2_sel, m2_sel_in, m2_sel_out, m2_sel_gpot, m2_sel_spike = \ make_sels(m2_sel_in_gpot, m2_sel_out_gpot, m2_sel_in_spike, m2_sel_out_spike) N2_gpot = SelectorMethods.count_ports(m2_sel_gpot) N2_spike = SelectorMethods.count_ports(m2_sel_spike) # Note that the module ID doesn't need to be listed in the specified # constructor arguments: m1_id = 'm1 ' man.add(MyModule, m1_id, m1_sel, m1_sel_in, m1_sel_out,