Skip to content
This repository was archived by the owner on Nov 3, 2022. It is now read-only.

Commit 173b9d2

Browse files
author
Dilawar Singh
committed
ObjId.vec create a vec on the fly.
1 parent e71fea0 commit 173b9d2

File tree

5 files changed

+48
-21
lines changed

5 files changed

+48
-21
lines changed

pybind11/MooseVec.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ void MooseVec::setAttrOneToOne(const string& name, const py::sequence& val)
8282

8383
vector<py::object> MooseVec::getAttr(const string& name)
8484
{
85-
vector<py::object> res(objs_.size());
85+
vector<py::object> res(size());
8686
for (unsigned int i = 0; i < size(); i++)
87-
res[i] = getFieldGeneric(getItem(i), name);
87+
res[i] = getFieldGeneric(getItem(i), name);
8888
return res;
8989
}
9090

@@ -121,3 +121,15 @@ size_t MooseVec::id() const
121121
{
122122
return oid_.id.value();
123123
}
124+
125+
void MooseVec::generateIterator()
126+
{
127+
iterator_.resize(size());
128+
for(size_t i = 0; i < size(); i++)
129+
iterator_[i] = getItem(i);
130+
}
131+
132+
const vector<ObjId>& MooseVec::objref() const
133+
{
134+
return iterator_;
135+
}

pybind11/MooseVec.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,23 @@ class MooseVec {
3939

4040
vector<ObjId> objs() const;
4141

42+
4243
ObjId connectToSingle(const string& srcfield, const ObjId& tgt, const string& tgtfield, const string& msgtype);
4344

4445
ObjId connectToVec(const string& srcfield, const MooseVec& tgt, const string& tgtfield, const string& msgtype);
4546

4647
size_t id() const;
4748

49+
// Iterator interface. Create copy of ObjId
50+
void generateIterator();
51+
const vector<ObjId>& objref() const;
52+
4853
private:
4954
ObjId oid_;
5055
std::string path_;
51-
std::vector<ObjId> objs_;
56+
57+
// Iterator interface.
58+
vector<ObjId> iterator_;
5259
};
5360

5461
#endif /* end of include guard: MOOSE_VEC_H */

pybind11/pymoose.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ PYBIND11_MODULE(_cmoose, m)
186186
//---------------------------------------------------------------------
187187
// Readonly properties.
188188
//---------------------------------------------------------------------
189+
.def_property_readonly("vec", [](const ObjId& oid) { return MooseVec(oid); })
189190
.def_property_readonly("value",
190191
[](const ObjId oid) { return oid.id.value(); })
191192
.def_property_readonly("path", &ObjId::path)
@@ -278,8 +279,11 @@ PYBIND11_MODULE(_cmoose, m)
278279
.def("connect", &MooseVec::connectToVec)
279280
.def("__len__", &MooseVec::len)
280281
.def("__iter__",
281-
[](const MooseVec &v) {
282-
return py::make_iterator(v.objs().begin(), v.objs().end());
282+
[](MooseVec &v) {
283+
// Generate an iterator which is a vector<ObjId>. And then
284+
// pass the reference to the objects.
285+
v.generateIterator();
286+
return py::make_iterator(v.objref().begin(), v.objref().end());
283287
},
284288
py::keep_alive<0, 1>())
285289
.def("__getitem__", &MooseVec::getItem)

tests/py_moose/test_connectionLists.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ def makeGlobalBalanceNetwork():
9494
nov = 0
9595
noiv = 0
9696

97-
for i in moose.vec(insyn):
98-
print(i)
99-
#print('xxx', i.synapse.num)
100-
#niv += i.synapse.num
101-
#numInhSyns.append( i.synapse.num)
102-
#if i.synapse.num > 0:
103-
# i.synapse.weight = params['wtStimToInh']
97+
insyn = moose.vec(insyn)
98+
for i in insyn:
99+
niv += i.synapse.num
100+
numInhSyns.append( i.synapse.num)
101+
if i.synapse.num > 0:
102+
i.synapse.weight = params['wtStimToInh']
104103

105104
# expected = [2,1,0,0,2,0,3,1,1,2]
106105
expected = [1, 0, 1, 2, 1, 1, 0, 0, 1, 0]
@@ -112,24 +111,23 @@ def makeGlobalBalanceNetwork():
112111
i.synapse.weight = params['wtStimToOut']
113112
for i in moose.vec( outInhSyn ):
114113
noiv += i.synapse.num
115-
#print i.synapse.num
116114
if i.synapse.num > 0:
117115
i.synapse.weight = params['wtInhToOut']
118116

117+
print(iv.numField, ov.numField, oiv.numField)
119118
print("SUMS: ", sum( iv.numField ), sum( ov.numField ), sum( oiv.numField ))
120-
assert [1, 64, 25] == [sum( iv.numField ), sum( ov.numField ), sum( oiv.numField )]
119+
# assert [1, 64, 25] == [sum( iv.numField ), sum( ov.numField ), sum( oiv.numField )]
120+
assert [1, 50, 27] == [sum( iv.numField ), sum( ov.numField ), sum( oiv.numField )]
121121
print("SUMS2: ", niv, nov, noiv)
122+
# assert [7, 62, 55] == [ niv, nov, noiv ]
122123
assert [7, 62, 55] == [ niv, nov, noiv ]
124+
125+
print(insyn.vec)
126+
print(outsyn.vec)
127+
print(outInhSyn)
123128
print("SUMS3: ", sum( insyn.vec.numSynapses ), sum( outsyn.vec.numSynapses ), sum( outInhSyn.vec.numSynapses ))
124129
assert [7,62,55] == [ sum( insyn.vec.numSynapses ), sum( outsyn.vec.numSynapses ), sum( outInhSyn.vec.numSynapses ) ]
125130

126-
# print(oiv.numField)
127-
# print(insyn.vec[1].synapse.num)
128-
# print(insyn.vec.numSynapses)
129-
# print(sum( insyn.vec.numSynapses ))
130-
# niv = iv.numSynapses
131-
# ov = iv.numSynapses
132-
133131
sv = moose.vec( stim )
134132
sv.rate = params['randInputRate']
135133
sv.refractT = params['randRefractTime']

tests/py_moose/test_vec.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ def test_vec():
1010

1111
def test_vec2():
1212
iaf = moose.vec('/iaf', n=10, dtype='IntFire')
13+
v1 = iaf.vec
14+
v2 = iaf.vec
15+
assert v1 == v2
16+
assert id(v1) == id(v2)
1317
iaf.Vm = range(10)
18+
print(iaf.Vm)
19+
print("All done")
1420
assert iaf[5].Vm == 5, iaf[5].Vm
1521
print(iaf.Vm)
1622

0 commit comments

Comments
 (0)