Skip to content

Commit 15eae1d

Browse files
author
Dilawar Singh
committed
vec values are returned as numpy array.
1 parent cc89202 commit 15eae1d

File tree

8 files changed

+60
-32
lines changed

8 files changed

+60
-32
lines changed

pybind11/MooseVec.cpp

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ namespace py = pybind11;
2222

2323
MooseVec::MooseVec(const string& path, unsigned int n = 0,
2424
const string& dtype = "Neutral")
25-
: path_(path), useDataIndex_(false)
25+
: path_(path)
2626
{
27-
if (! mooseExists(path))
28-
oid_ = mooseCreateFromPath(dtype, path, n);
29-
else
30-
oid_ = ObjId(path);
27+
oid_ = ObjId(path);
3128
}
3229

3330
MooseVec::MooseVec(const ObjId& oid) : oid_(oid), path_(oid.path())
@@ -100,14 +97,44 @@ void MooseVec::setAttrOneToOne(const string& name, const py::sequence& val)
10097
setFieldGeneric(getItem(i), name, val[i]);
10198
}
10299

103-
vector<py::object> MooseVec::getAttr(const string& name)
100+
vector<py::object> MooseVec::getAttribute(const string& name)
104101
{
105102
vector<py::object> res(size());
106103
for (unsigned int i = 0; i < size(); i++)
107104
res[i] = getFieldGeneric(getItem(i), name);
108105
return res;
109106
}
110107

108+
// FIXME: Only double is supported here. Not sure if this is enough.
109+
py::array_t<double> MooseVec::getAttributeNumpy(const string &name)
110+
{
111+
auto cinfo = oid_.element()->cinfo();
112+
auto finfo = cinfo->findFinfo(name);
113+
114+
if (!finfo) {
115+
throw py::key_error(name + " is not found on '" + oid_.path() + "'.");
116+
}
117+
118+
string finfoType = cinfo->getFinfoType(finfo);
119+
120+
// Either return a simple value (ValueFinfo), list, dict or DestFinfo
121+
// setter.
122+
// The DestFinfo setter is a function.
123+
124+
vector<double> res(size());
125+
if (finfoType == "ValueFinfo") {
126+
for (unsigned int i = 0; i < size(); i++)
127+
res[i] = getField<double>(getItem(i), name);
128+
return py::array_t<double>(res.size(), res.data());
129+
}
130+
131+
throw runtime_error("getAttributeNumpy::NotImplemented : " + name +
132+
" with rttType " + finfo->rttiType() + " and type: '" +
133+
finfoType + "'");
134+
return py::array_t<double>();
135+
}
136+
137+
111138
ObjId MooseVec::connectToSingle(const string& srcfield, const ObjId& tgt,
112139
const string& tgtfield, const string& msgtype)
113140
{
@@ -144,11 +171,11 @@ size_t MooseVec::id() const
144171

145172
void MooseVec::generateIterator()
146173
{
147-
iterator_.resize(size());
148-
for (size_t i = 0; i < size(); i++) iterator_[i] = getItem(i);
174+
objs_.resize(size());
175+
for (size_t i = 0; i < size(); i++) objs_[i] = getItem(i);
149176
}
150177

151178
const vector<ObjId>& MooseVec::objref() const
152179
{
153-
return iterator_;
180+
return objs_;
154181
}

pybind11/MooseVec.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class MooseVec
4343

4444
void setAttrOneToOne(const string& name, const py::sequence& val);
4545

46-
vector<py::object> getAttr(const string& name);
46+
vector<py::object> getAttribute(const string& name);
47+
py::array_t<double> getAttributeNumpy(const string& name);
4748

4849
vector<ObjId> objs() const;
4950

@@ -67,8 +68,7 @@ class MooseVec
6768
// >>> av = moose.vec(a)
6869
// will use dataIndex for indexing.
6970
// For FieldElementInfo, create objects.
70-
vector<ObjId> iterator_;
71-
bool useDataIndex_;
71+
vector<ObjId> objs_;
7272
};
7373

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

pybind11/helper.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool mooseExists(const string& path);
4242
void mooseMoveId(const Id& a, const ObjId& b);
4343
void mooseMoveObjId(const ObjId& a, const ObjId& b);
4444

45-
inline ObjId mooseObjIdPath(const string& p)
45+
inline MooseVec mooseObjIdPath(const string& p)
4646
{
4747
// handle relative path.
4848
string path(p);
@@ -51,19 +51,18 @@ inline ObjId mooseObjIdPath(const string& p)
5151
ObjId oid(path);
5252
if (oid.bad()) {
5353
cerr << "moose_element: " << path << " does not exist!" << endl;
54-
return ObjId(Id());
5554
}
56-
return oid;
55+
return MooseVec(oid);
5756
}
5857

59-
inline ObjId mooseObjIdObj(const ObjId& obj)
58+
inline MooseVec mooseObjIdObj(const ObjId& obj)
6059
{
61-
return ObjId(obj.id, obj.dataIndex, obj.fieldIndex);
60+
return MooseVec(obj);
6261
}
6362

64-
inline ObjId mooseObjIdId(const Id& id)
63+
inline MooseVec mooseObjIdId(const Id& id)
6564
{
66-
return ObjId(id);
65+
return MooseVec(id);
6766
}
6867

6968
inline ObjId mooseCreateFromPath(const string type, const string& p, unsigned int numdata)

pybind11/pymoose.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ py::object getFieldGeneric(const ObjId &oid, const string &fieldName)
145145
return pybind11::none();
146146
}
147147

148+
148149
/* --------------------------------------------------------------------------*/
149150
/**
150151
* @Synopsis MOOSE extension module _moose.so.
@@ -316,7 +317,7 @@ PYBIND11_MODULE(_moose, m)
316317
.def("__getitem__", &MooseVec::getItem)
317318
.def("__setattr__", &MooseVec::setAttrOneToOne)
318319
.def("__setattr__", &MooseVec::setAttrOneToAll)
319-
.def("__getattr__", &MooseVec::getAttr)
320+
.def("__getattr__", &MooseVec::getAttributeNumpy)
320321
.def("__repr__", [](const MooseVec & v)->string {
321322
return "<moose.vec class=" + v.dtype() + " path=" + v.path() +
322323
" id=" + std::to_string(v.id()) + " size=" +

pybind11/pymoose.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@
2121
#include "MooseVec.h"
2222

2323
template <typename T>
24-
void setField(const ObjId& id, const string& fname, T val)
24+
inline void setField(const ObjId& id, const string& fname, T val)
2525
{
2626
// cout << "Setting " << fname << " to value " << val << typeid(T).name() <<
2727
// endl;
2828
Field<T>::set(id, fname, val);
2929
}
3030

3131
template <typename T>
32-
T getField(const ObjId& id, const string& fname)
32+
inline T getField(const ObjId& id, const string& fname)
3333
{
3434
return Field<T>::get(id, fname);
3535
}
3636

3737
// FIXME: Is it most efficient?
3838
// See discussion here: https://github.com/pybind/pybind11/issues/1042
3939
template <typename T = double>
40-
py::array_t<T> getFieldNumpy(const ObjId& id, const string& fname)
40+
inline py::array_t<T> getFieldNumpy(const ObjId& id, const string& fname)
4141
{
4242
auto v = Field<vector<T>>::get(id, fname);
4343
return py::array_t<T>(v.size(), v.data());

shell/Shell.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ Id Shell::doCreate(string type, ObjId parent, string name, unsigned int numData,
235235
return Id();
236236
}
237237

238-
ObjId Shell::doCreate2(string type, ObjId parent, string name,
238+
Id Shell::doCreate2(string type, ObjId parent, string name,
239239
unsigned int numData)
240240
{
241241
// if (parent.bad()) {

shell/Shell.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ class Shell
8686
NodePolicy nodePolicy = MooseBlockBalance,
8787
unsigned int preferredNode = 1 );
8888

89-
// Python bindings.
90-
ObjId doCreate2( string type, ObjId parent, string name, unsigned int numData);
89+
// Same as before but with default nodePolicy and preferredNode. We are
90+
// hidning them away from the python bindings.
91+
Id doCreate2( string type, ObjId parent, string name, unsigned int numData);
9192

9293
/**
9394
* Delete specified Element and all its children and all

tests/py_moose/test_api.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
__maintainer__ = "Dilawar Singh"
44
__email__ = "[email protected]"
55

6+
67
import moose
8+
import random
79
import numpy as np
810

911

@@ -39,7 +41,9 @@ def test_vec():
3941
assert len(v) == 100, len(v)
4042
assert v == v.vec
4143
assert v[0] == v.vec[0], (v[0], v.vec[0])
42-
44+
x = [random.random() for i in range(100)]
45+
v.conc = x
46+
assert np.equal(v.conc, x).all()
4347

4448
def test_finfos():
4549
s = moose.SimpleSynHandler('synh')
@@ -48,14 +52,11 @@ def test_finfos():
4852
assert s.numSynapses == 10
4953

5054
syns = s.synapse.vec
51-
print(syns)
5255
assert len(syns) == 10
5356
for i, s in enumerate(syns):
54-
print(s, type(s))
5557
s.weight = 9.0
5658
for s in syns:
5759
assert s.weight == 9.0
58-
print(syns.weight)
5960

6061
# this is a shorthand for above for loop.
6162
syns.weight = 11.121
@@ -138,12 +139,11 @@ def main():
138139
test_children()
139140
test_finfos()
140141
test_other()
141-
test_vec()
142142
test_delete()
143143
test_wrapper()
144144
test_inheritance()
145145
test_access()
146-
146+
test_vec()
147147

148148
if __name__ == '__main__':
149149
main()

0 commit comments

Comments
 (0)