Skip to content

Commit

Permalink
Don't convert integers used to identify interfaces within an Interfac…
Browse files Browse the repository at this point in the history
…e instance to str; add convenience function are_compatible() to check compatibility of interfaces specified as selectors.
  • Loading branch information
lebedov committed Mar 22, 2016
1 parent 0e4dd7f commit 36819e2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
44 changes: 43 additions & 1 deletion neurokernel/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, selector='', columns=['interface', 'io', 'type']):
self.sel = SelectorMethods()
assert not(self.sel.is_ambiguous(selector))
self.num_levels = self.sel.max_levels(selector)
names = [str(i) for i in xrange(self.num_levels)]
names = [i for i in xrange(self.num_levels)]
idx = self.sel.make_index(selector, names)
self.__validate_index__(idx)
self.data = pd.DataFrame(index=idx, columns=columns, dtype=object)
Expand Down Expand Up @@ -1990,3 +1990,45 @@ def to_graph(self):
g.add_edge(id_from, id_to, d)

return g

def are_compatible(sel_in_0, sel_out_0, sel_spike_0, sel_gpot_0,
sel_in_1, sel_out_1, sel_spike_1, sel_gpot_1,
allow_subsets=False):
"""
Check whether two interfaces specified as selectors can be connected.
Parameters
----------
sel_in_0, sel_out_0, sel_spike_0, sel_gpot_0 : Selector, str, unicode
Input, output, spiking, and graded potential ports in first interface.
sel_in_0, sel_out_0, sel_spike_0, sel_gpot_0 : Selector, str, unicode
Input, output, spiking, and graded potential ports in second interface.
allow_subsets : bool
If True, interfaces that contain a compatible subset of ports are
deemed to be compatible; otherwise, all ports in the two interfaces
must be compatible.
Results
-------
result : bool
True if interfaces are compatible, False otherwise.
"""

sel_in_0 = Selector(sel_in_0)
sel_out_0 = Selector(sel_out_0)
sel_spike_0 = Selector(sel_spike_0)
sel_gpot_0 = Selector(sel_gpot_0)
sel_0 = Selector.union(sel_in_0, sel_out_0, sel_spike_0, sel_gpot_0)

sel_in_1 = Selector(sel_in_1)
sel_out_1 = Selector(sel_out_1)
sel_spike_1 = Selector(sel_spike_1)
sel_gpot_1 = Selector(sel_gpot_1)
sel_1 = Selector.union(sel_in_1, sel_out_1, sel_spike_1, sel_gpot_1)

int_0 = Interface.from_selectors(sel_0, sel_in_0, sel_out_0,
sel_spike_0, sel_gpot_0, sel_0)
int_1 = Interface.from_selectors(sel_1, sel_in_1, sel_out_1,
sel_spike_1, sel_gpot_1, sel_1)

return int_0.is_compatible(0, int_1, 0)
42 changes: 25 additions & 17 deletions tests/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pandas.util.testing import assert_frame_equal, assert_index_equal, \
assert_series_equal

from neurokernel.pattern import Interface, Pattern
from neurokernel.pattern import Interface, Pattern, are_compatible

class test_interface(TestCase):
def setUp(self):
Expand Down Expand Up @@ -179,22 +179,24 @@ def test_data_select(self):
j = i.data_select(lambda x: x['io'] != 'in')
assert_index_equal(j.data.index,
pd.MultiIndex.from_tuples([('foo', 1),
('foo', 2)]))
('foo', 2)],
names=[0, 1]))

# Selector with single level:
i = Interface('/[foo,bar,baz]')
i['/[foo,bar]', 'interface', 'io'] = [0, 'in']
i['/baz', 'interface', 'io'] = [0, 'out']
j = i.data_select(lambda x: x['io'] != 'in')
assert_index_equal(j.data.index,
pd.Index(['baz']))
pd.Index(['baz'], name=0))

# Selectors with different numbers of levels:
i = Interface('/a/b/c,/x/y')
i['/a/b/c', 'interface', 'io'] = [0, 'in']
j = i.data_select(lambda x: x['io'] != 'in')
assert_index_equal(j.data.index,
pd.MultiIndex.from_tuples([('x', 'y', '')]))
pd.MultiIndex.from_tuples([('x', 'y', '')],
names=[0, 1, 2]))

def test_from_df_index(self):
idx = pd.Index(['foo', 'bar', 'baz'])
Expand Down Expand Up @@ -255,7 +257,8 @@ def test_from_dict(self):
assert_index_equal(i.data.index,
pd.MultiIndex.from_tuples([('foo', 0),
('foo', 1),
('foo', 2)]))
('foo', 2)],
names=[0, 1]))

def test_from_graph(self):
i = Interface('/foo[0:3]')
Expand Down Expand Up @@ -301,7 +304,7 @@ def test_in_ports(self):
i['/foo[1]'] = [1, 'out', 'spike']
df = pd.DataFrame([(0, 'in', 'spike')],
pd.MultiIndex.from_tuples([('foo', 0)],
names=['0', '1']),
names=[0, 1]),
['interface', 'io', 'type'],
dtype=object)

Expand All @@ -317,7 +320,7 @@ def test_in_ports(self):
i['/bar'] = [1, 'out', 'spike']
df = pd.DataFrame([(0, 'in', 'spike')],
pd.MultiIndex.from_tuples([('foo',)],
names=['0']),
names=[0]),
['interface', 'io', 'type'],
dtype=object)

Expand Down Expand Up @@ -361,7 +364,7 @@ def test_out_ports(self):
i['/foo[1]'] = [1, 'out', 'spike']
df = pd.DataFrame([(1, 'out', 'spike')],
pd.MultiIndex.from_tuples([('foo', 1)],
names=['0', '1']),
names=[0, 1]),
['interface', 'io', 'type'],
dtype=object)

Expand All @@ -377,7 +380,7 @@ def test_out_ports(self):
i['/bar'] = [1, 'out', 'spike']
df = pd.DataFrame([(1, 'out', 'spike')],
pd.MultiIndex.from_tuples([('bar',)],
names=['0']),
names=[0]),
['interface', 'io', 'type'],
dtype=object)

Expand Down Expand Up @@ -425,13 +428,14 @@ 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)]))
('foo', 2)],
names=[0, 1]))

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']))
names=[0, 1]))

def test_interface_ids(self):
i = Interface('/foo[0:4]')
Expand Down Expand Up @@ -576,6 +580,10 @@ def test_is_compatible_subsets_with_null_types(self):
assert i.is_compatible(0, j, 1, True)
assert i.is_compatible(0, k, 1, True) == False

def test_are_compatible(self):
assert are_compatible('/foo[2:4]', '/foo[0:2]', '/foo[2:4]', '/foo[0:2]',
'/foo[0:2]', '/foo[2:4]', '/foo[2:4]', '/foo[0:2]')

def test_which_int_unset(self):
i = Interface('/foo[0:4]')
assert i.which_int('/foo[0:2]') == set()
Expand Down Expand Up @@ -609,11 +617,11 @@ def setUp(self):
'out', 'out', 'out', 'in', 'in'],
'type': ['spike', 'spike', 'gpot', 'gpot', 'gpot',
'spike', 'spike', np.nan, 'gpot', 'gpot'],
'0': ['foo', 'foo', 'foo', 'foo', 'foo',
0: ['foo', 'foo', 'foo', 'foo', 'foo',
'bar', 'bar', 'bar', 'bar', 'bar'],
'1': [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]})
self.df_i.set_index('0', append=False, inplace=True)
self.df_i.set_index('1', append=True, inplace=True)
1: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]})
self.df_i.set_index(0, append=False, inplace=True)
self.df_i.set_index(1, append=True, inplace=True)

def test_create(self):
p = Pattern('/foo[0:5]', '/bar[0:5]')
Expand Down Expand Up @@ -986,7 +994,7 @@ def test_from_concat(self):
'type': ['gpot', 'spike', 'gpot', 'spike']},
index=pd.MultiIndex(levels=[['bar', 'foo'], [0, 1]],
labels=[[1, 1, 0, 0], [0, 1, 0, 1]],
names=[u'0', u'1'],
names=[0, 1],
dtype=object),
dtype=object)
df = pd.DataFrame(data=[1, 1],
Expand All @@ -1010,7 +1018,7 @@ def test_from_df(self):
(1, 'out', np.nan)],
index=pd.MultiIndex(levels=[['aaa', 'bbb', 'ccc', 'ddd'], [0]],
labels=[[0, 1, 2, 3], [0, 0, 0, 0]],
names=['0', '1']),
names=[0, 1]),
columns=['interface', 'io', 'type'],
dtype=object)
df_pat = pd.DataFrame(data=[(1,), (1,)],
Expand Down

0 comments on commit 36819e2

Please sign in to comment.