Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lebedov committed Dec 6, 2015
2 parents cbb6a0c + 7d3cc3f commit b494fb7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
37 changes: 33 additions & 4 deletions neurokernel/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,9 @@ def _create_from(cls, *selectors, **kwargs):
Selectors that describe the pattern's initial index. If specified,
both selectors must be set. If no selectors are set, the index is
initially empty.
gpot_sel, spike_sel : str
Selectors that describe the graded potential and spiking ports in a
pattern's initial index.
data : numpy.ndarray, dict, or pandas.DataFrame
Data to load store in class instance.
columns : sequence of str
Expand All @@ -1182,6 +1185,8 @@ def _create_from(cls, *selectors, **kwargs):

from_sel = kwargs['from_sel'] if kwargs.has_key('from_sel') else None
to_sel = kwargs['to_sel'] if kwargs.has_key('to_sel') else None
gpot_sel = kwargs['gpot_sel'] if kwargs.has_key('gpot_sel') else None
spike_sel = kwargs['spike_sel'] if kwargs.has_key('spike_sel') else None
data = kwargs['data'] if kwargs.has_key('data') else None
columns = kwargs['columns'] if kwargs.has_key('columns') else ['conn']
comb_op = kwargs['comb_op'] if kwargs.has_key('comb_op') else '+'
Expand Down Expand Up @@ -1218,6 +1223,11 @@ def _create_from(cls, *selectors, **kwargs):
p.interface[from_sel, 'io'] = 'in'
p.interface[to_sel, 'io'] = 'out'

# Update the `type` attributes of the pattern's interface:
if gpot_sel is not None:
p.interface[gpot_sel, 'type'] = 'gpot'
p.interface[spike_sel, 'type'] = 'spike'

return p

def clear(self):
Expand All @@ -1228,7 +1238,6 @@ def clear(self):
self.interface.clear()
self.data.drop(self.data.index, inplace=True)

@classmethod
def from_df(cls, df_int, df_pat):
"""
Create a Pattern from properly formatted DataFrames.
Expand Down Expand Up @@ -1304,7 +1313,15 @@ def from_product(cls, *selectors, **kwargs):
pattern. These selectors must be disjoint, i.e., no identifier comprised
by one selector may be in any other selector.
from_sel, to_sel : str
Selectors that describe the pattern's initial index.
Selectors that describe the pattern's initial index. If specified,
both selectors must be set; the 'io' attribute of the ports
comprised by these selectors is respectively set to 'out' and
'in'. If no selectors are set, the index is initially empty.
gpot_sel, spike_sel : str
Selectors that describe the graded potential and spiking ports in a
pattern's initial index. If specified, the 'type' attribute of the
ports comprised by these selectors is respectively set to 'gpot'
and 'spike'.
data : numpy.ndarray, dict, or pandas.DataFrame
Data to load store in class instance.
columns : sequence of str
Expand All @@ -1320,10 +1337,13 @@ def from_product(cls, *selectors, **kwargs):

from_sel = kwargs['from_sel'] if kwargs.has_key('from_sel') else None
to_sel = kwargs['to_sel'] if kwargs.has_key('to_sel') else None
gpot_sel = kwargs['gpot_sel'] if kwargs.has_key('gpot_sel') else None
spike_sel = kwargs['spike_sel'] if kwargs.has_key('spike_sel') else None
data = kwargs['data'] if kwargs.has_key('data') else None
columns = kwargs['columns'] if kwargs.has_key('columns') else ['conn']
validate = kwargs['validate'] if kwargs.has_key('validate') else True
return cls._create_from(*selectors, from_sel=from_sel, to_sel=to_sel,
gpot_sel=gpot_sel, spike_sel=spike_sel,
data=data, columns=columns, comb_op='+', validate=validate)

def gpot_ports(self, i=None, tuples=False):
Expand Down Expand Up @@ -1413,8 +1433,14 @@ def from_concat(cls, *selectors, **kwargs):
Data to load store in class instance.
from_sel, to_sel : str
Selectors that describe the pattern's initial index. If specified,
both selectors must be set. If no selectors are set, the index is
initially empty.
both selectors must be set; the 'io' attribute of the ports
comprised by these selectors is respectively set to 'out' and
'in'. If no selectors are set, the index is initially empty.
gpot_sel, spike_sel : str
Selectors that describe the graded potential and spiking ports in a
pattern's initial index. If specified, the 'type' attribute of the
ports comprised by these selectors is respectively set to 'gpot'
and 'spike'.
columns : sequence of str
Data column names.
validate : bool
Expand All @@ -1428,10 +1454,13 @@ def from_concat(cls, *selectors, **kwargs):

from_sel = kwargs['from_sel'] if kwargs.has_key('from_sel') else None
to_sel = kwargs['to_sel'] if kwargs.has_key('to_sel') else None
gpot_sel = kwargs['gpot_sel'] if kwargs.has_key('gpot_sel') else None
spike_sel = kwargs['spike_sel'] if kwargs.has_key('spike_sel') else None
data = kwargs['data'] if kwargs.has_key('data') else None
columns = kwargs['columns'] if kwargs.has_key('columns') else ['conn']
validate = kwargs['validate'] if kwargs.has_key('validate') else True
return cls._create_from(*selectors, from_sel=from_sel, to_sel=to_sel,
gpot_sel=gpot_sel, spike_sel=spike_sel,
data=data, columns=columns, comb_op='.+', validate=validate)

def __validate_index__(self, idx):
Expand Down
21 changes: 21 additions & 0 deletions tests/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,27 @@ def test_from_concat(self):
columns=['conn'])
assert_frame_equal(p.data, df)

p = Pattern.from_concat('/foo[0:2]', '/bar[0:2]',
from_sel='/foo[0:2]', to_sel='/bar[0:2]',
gpot_sel='/foo[0],/bar[0]',
spike_sel='/foo[1:2],/bar/[1:2]',
data=1)
df_int = pd.DataFrame({'interface': [0, 0, 1, 1],
'io': ['in', 'in', 'out', 'out'],
'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']),
dtype=object)
df = pd.DataFrame(data=[1, 1],
index=pd.MultiIndex(levels=[['foo'], [0, 1], ['bar'], [0, 1]],
labels=[[0, 0], [0, 1], [0, 0], [0, 1]],
names=['from_0', 'from_1', 'to_0', 'to_1'],
dtype=object),
columns=['conn'])
assert_frame_equal(p.data, df)
assert_frame_equal(p.interface.data, df_int)

def test_from_df(self):
p = Pattern('/[aaa,bbb]/0', '/[ccc,ddd]/0')
p['/aaa/0', '/ccc/0'] = 1
Expand Down

0 comments on commit b494fb7

Please sign in to comment.