def test_in_ports(self): # Selector with multiple levels: i = Interface('/foo[0:2]') i['/foo[0]'] = [0, 'in', 'spike'] i['/foo[1]'] = [1, 'out', 'spike'] df = pd.DataFrame([(0, 'in', 'spike')], pd.MultiIndex.from_tuples([('foo', 0)], names=['0', '1']), ['interface', 'io', 'type'], dtype=object) # Test returning result as Interface: assert_frame_equal(i.in_ports(0).data, df) # Test returning result as list of tuples: self.assertItemsEqual(i.in_ports(0, True), df.index.tolist()) # Selector with single level: i = Interface('/[foo,bar]') i['/foo'] = [0, 'in', 'spike'] i['/bar'] = [1, 'out', 'spike'] df = pd.DataFrame([(0, 'in', 'spike')], pd.MultiIndex.from_tuples([('foo', )], names=['0']), ['interface', 'io', 'type'], dtype=object) # Test returning result as Interface: assert_frame_equal(i.in_ports(0).data, df) # Test returning result as list of tuples: self.assertItemsEqual(i.in_ports(0, True), df.index.tolist())
def test_in_ports(self): # Selector with multiple levels: i = Interface('/foo[0:2]') i['/foo[0]'] = [0, 'in', 'spike'] i['/foo[1]'] = [1, 'out', 'spike'] df = pd.DataFrame([(0, 'in', 'spike')], pd.MultiIndex.from_tuples([('foo', 0)], names=['0', '1']), ['interface', 'io', 'type'], dtype=object) # Test returning result as Interface: assert_frame_equal(i.in_ports(0).data, df) # Test returning result as list of tuples: self.assertItemsEqual(i.in_ports(0, True), df.index.tolist()) # Selector with single level: i = Interface('/[foo,bar]') i['/foo'] = [0, 'in', 'spike'] i['/bar'] = [1, 'out', 'spike'] df = pd.DataFrame([(0, 'in', 'spike')], pd.MultiIndex.from_tuples([('foo',)], names=['0']), ['interface', 'io', 'type'], dtype=object) # Test returning result as Interface: assert_frame_equal(i.in_ports(0).data, df) # Test returning result as list of tuples: self.assertItemsEqual(i.in_ports(0, True), df.index.tolist())
class test_interface(TestCase): def setUp(self): self.interface = Interface('/foo[0:3]') self.interface['/foo[0]', 'interface', 'io'] = [0, 'in'] self.interface['/foo[1:3]', 'interface', 'io'] = [0, 'out'] def test_clear(self): i = Interface('/foo[0:4]') i.clear() assert len(i) == 0 def test_create_empty(self): i = Interface('') assert len(i) == 0 def test_create_dup_identifiers(self): self.assertRaises(Exception, Interface, '/foo[0],/foo[0]') def test_to_selectors(self): i = Interface('/foo[0:4]') i['/foo[0:2]', 'interface'] = 0 i['/foo[2:4]', 'interface'] = 1 self.assertSequenceEqual(i.to_selectors(0), ['/foo[0]', '/foo[1]']) self.assertSequenceEqual(i.to_selectors(), ['/foo[0]', '/foo[1]', '/foo[2]', '/foo[3]']) def test_to_tuples(self): i = Interface('/foo[0:4]') i['/foo[0:2]', 'interface'] = 0 i['/foo[2:4]', 'interface'] = 1 self.assertSequenceEqual(i.to_tuples(0), [('foo', 0), ('foo', 1)]) self.assertSequenceEqual(i.to_tuples(), [('foo', 0), ('foo', 1), ('foo', 2), ('foo', 3)]) i = Interface('[0:4]') i['[0:2]', 'interface'] = 0 i['[2:4]', 'interface'] = 1 self.assertSequenceEqual(i.to_tuples(0), [(0,), (1,)]) self.assertSequenceEqual(i.to_tuples(), [(0,), (1,), (2,), (3,)]) def test_data_select(self): i = self.interface.data_select(lambda x: x['io'] >= 'out') assert_index_equal(i.data.index, pd.MultiIndex.from_tuples([('foo', 1), ('foo', 2)])) def test_from_df_index(self): idx = pd.Index(['foo', 'bar', 'baz']) data = [(0, 'in', 'spike'), (1, 'in', 'gpot'), (1, 'out', 'gpot')] columns = ['interface', 'io', 'type'] df = pd.DataFrame(data, index=idx, columns=columns) i = Interface.from_df(df) assert_index_equal(i.data.index, idx) assert_frame_equal(i.data, df) def test_from_df_multiindex(self): idx = pd.MultiIndex.from_tuples([('foo', 0), ('foo', 1), ('foo', 2)]) data = [(0, 'in', 'spike'), (1, 'in', 'gpot'), (1, 'out', 'gpot')] columns = ['interface', 'io', 'type'] df = pd.DataFrame(data, index=idx, columns=columns) i = Interface.from_df(df) assert_index_equal(i.data.index, idx) assert_frame_equal(i.data, df) def test_from_df_dup(self): idx = pd.MultiIndex.from_tuples([('foo', 0), ('foo', 0), ('foo', 2)]) data = [(0, 'in', 'spike'), (1, 'in', 'gpot'), (1, 'out', 'gpot')] columns = ['interface', 'io', 'type'] df = pd.DataFrame(data, index=idx, columns=columns) self.assertRaises(Exception, Interface.from_df, df) def test_from_dict(self): i = Interface.from_dict({'/foo[0:3]': np.nan}) assert_index_equal(i.data.index, pd.MultiIndex.from_tuples([('foo', 0), ('foo', 1), ('foo', 2)])) def test_from_graph(self): i = Interface('/foo[0:3]') i['/foo[0]'] = [0, 'in', 'gpot'] i['/foo[1]'] = [0, 'out', 'gpot'] i['/foo[2]'] = [0, 'out', 'spike'] g = nx.Graph() g.add_node('/foo[0]', interface=0, io='in', type='gpot') g.add_node('/foo[1]', interface=0, io='out', type='gpot') g.add_node('/foo[2]', interface=0, io='out', type='spike') ig = Interface.from_graph(g) assert_index_equal(i.data.index, ig.data.index) assert_frame_equal(i.data, ig.data) def test_is_in_interfaces(self): assert self.interface.is_in_interfaces('/foo[0:3]') == True assert self.interface.is_in_interfaces('/foo[0:4]') == False def test_in_ports(self): i = Interface('/foo[0]') i['/foo[0]', 'interface', 'io'] = [0, 'in'] assert_frame_equal(self.interface.in_ports(0).data, i.data) assert_index_equal(self.interface.in_ports(0).index, i.index) def test_interface_ports(self): i = Interface('/foo[0:4]') i['/foo[0:2]', 'interface'] = 0 i['/foo[2:4]', 'interface'] = 1 j = Interface('/foo[2:4]') j['/foo[2:4]', 'interface'] = 1 assert_frame_equal(i.interface_ports(1).data, j.data) assert_index_equal(i.interface_ports(1).index, j.index) def test_out_ports(self): i = Interface('/foo[1:3]') i['/foo[1:3]', 'interface', 'io'] = [0, 'out'] assert_frame_equal(self.interface.out_ports(0).data, i.data) assert_index_equal(self.interface.out_ports(0).index, i.index) def test_gpot_ports(self): i = Interface('/foo[0:6]') i['/foo[0]'] = [0, 'in', 'spike'] i['/foo[1:3]'] = [0, 'out', 'spike'] i['/foo[3]'] = [0, 'in', 'gpot'] i['/foo[4:6]'] = [0, 'out', 'gpot'] j = Interface('/foo[3:6]') j['/foo[3]'] = [0, 'in', 'gpot'] j['/foo[4:6]'] = [0, 'out', 'gpot'] assert_frame_equal(i.gpot_ports(0).data, j.data) assert_index_equal(i.gpot_ports(0).index, j.index) def test_spike_ports(self): i = Interface('/foo[0:6]') i['/foo[0]'] = [0, 'in', 'spike'] i['/foo[1:3]'] = [0, 'out', 'spike'] i['/foo[3]'] = [0, 'in', 'gpot'] i['/foo[4:6]'] = [0, 'out', 'gpot'] j = Interface('/foo[0:3]') j['/foo[0]'] = [0, 'in', 'spike'] j['/foo[1:3]'] = [0, 'out', 'spike'] assert_frame_equal(i.spike_ports(0).data, j.data) assert_index_equal(i.spike_ports(0).index, j.index) def test_port_select(self): i = self.interface.port_select(lambda x: x[1] >= 1) assert_index_equal(i.data.index, pd.MultiIndex.from_tuples([('foo', 1), ('foo', 2)])) def test_index(self): assert_index_equal(self.interface.index, pd.MultiIndex(levels=[['foo'], [0, 1, 2]], labels=[[0, 0, 0], [0, 1, 2]], names=['0', '1'])) def test_interface_ids(self): i = Interface('/foo[0:4]') i['/foo[0:2]', 'interface', 'io'] = [0, 'out'] i['/foo[2:4]', 'interface', 'io'] = [1, 'in'] assert i.interface_ids == set([0, 1]) def test_io_inv(self): i = Interface('/foo[0:4]') i['/foo[0:2]', 'interface', 'io'] = [0, 'out'] i['/foo[2:4]', 'interface', 'io'] = [1, 'in'] j = Interface('/foo[0:4]') j['/foo[0:2]', 'interface', 'io'] = [0, 'in'] j['/foo[2:4]', 'interface', 'io'] = [1, 'out'] assert_frame_equal(i.data, j.io_inv.data) def test_is_compatible_both_dirs(self): i = Interface('/foo[0:4]') i['/foo[0:2]', 'interface', 'io'] = [0, 'out'] i['/foo[2:4]', 'interface', 'io'] = [0, 'in'] j = Interface('/foo[0:4]') j['/foo[0:2]', 'interface', 'io'] = [1, 'in'] j['/foo[2:4]', 'interface', 'io'] = [1, 'out'] assert i.is_compatible(0, j, 1) def test_is_compatible_both_dirs_types(self): i = Interface('/foo[0:4]') i['/foo[0:2]'] = [0, 'out', 'gpot'] i['/foo[2:4]'] = [0, 'in', 'spike'] j = Interface('/foo[0:4]') j['/foo[0:2]'] = [1, 'in', 'gpot'] j['/foo[2:4]'] = [1, 'out', 'spike'] assert i.is_compatible(0, j, 1) def test_is_compatible_one_dir(self): i = Interface('/foo[0:2]') i['/foo[0:2]', 'interface', 'io'] = [0, 'out'] j = Interface('/foo[0:2]') j['/foo[0:2]', 'interface', 'io'] = [1, 'in'] assert i.is_compatible(0, j, 1) def test_is_compatible_one_dir_types(self): i = Interface('/foo[0:2]') i['/foo[0:2]'] = [0, 'out', 'spike'] j = Interface('/foo[0:2]') j['/foo[0:2]'] = [1, 'in', 'spike'] assert i.is_compatible(0, j, 1) def test_is_compatible_with_nulls(self): i = Interface('/foo[0:3]') i['/foo[0:2]', 'interface', 'io'] = [0, 'out'] i['/foo[2]', 'interface'] = 0 j = Interface('/foo[0:3]') j['/foo[0:2]', 'interface', 'io'] = [1, 'in'] j['/foo[2]', 'interface'] = 1 assert i.is_compatible(0, j, 1) def test_is_compatible_with_nulls_types(self): i = Interface('/foo[0:3]') i['/foo[0:2]'] = [0, 'out', 'gpot'] i['/foo[2]', 'interface'] = 0 j = Interface('/foo[0:3]') j['/foo[0:2]'] = [1, 'in', 'gpot'] j['/foo[2]', 'interface'] = 1 assert i.is_compatible(0, j, 1) def test_which_int_unset(self): i = Interface('/foo[0:4]') assert i.which_int('/foo[0:2]') == set() def test_which_int_set(self): i = Interface('/foo[0:4]') i['/foo[0]', 'interface', 'io'] = [0, 'out'] i['/foo[1]', 'interface', 'io'] = [0, 'in'] i['/foo[2]', 'interface', 'io'] = [1, 'in'] i['/foo[3]', 'interface', 'io'] = [1, 'out'] assert i.which_int('/foo[0:2]') == {0} assert i.which_int('/foo[0:4]') == {0, 1}