Meaningful Python types#1858
Conversation
|
The above commit makes the following work: from neuron import h, hoc
v = h.Vector([1,2,3])
assert(type(v) == hoc.Vector)
assert(not (type(v) == hoc.HocObject))
assert(not (type(h.Deck()) == hoc.Vector))
assert(type(h.Deck()) == hoc.HocObject)However: While >>> type(v)
<class 'hoc.HocObject'>Furthermore, the >>> isinstance(v, hoc.Vector)
True
>>> isinstance(v, hoc.HocObject)
FalseMost problematically, the new >>> v == v
False |
|
✔️ c0eee1e -> Azure artifacts URL |
|
With the above commit, the different types print out differently. In particular, the following now works: from neuron import h, hoc
v = h.Vector([1,2,3])
w = h.Vector([1,5])
assert(type(v) == hoc.Vector)
assert(not (type(v) == hoc.HocObject))
assert(not (type(h.Deck()) == hoc.Vector))
assert(type(h.Deck()) == hoc.HocObject)
assert(isinstance(v, hoc.HocObject))
assert(isinstance(v, hoc.Vector))
assert(not isinstance(h.Deck(), hoc.Vector))
assert(str(type(v)) == "<class 'hoc.Vector'>")
assert(str(type(h.Deck())) == "<class 'hoc.HocObject'>")
assert(v == v)
assert(v != w)The only remaining issue for the "get one working" stage that I'm aware of is allowing inhertiance without ... that and |
|
✔️ 944bcbc -> Azure artifacts URL |
|
For generalizing to all built-in classes, note that these are registered with the underlying NEURON stack machine by a call to I'm not sure if HOC template definitions also call that function. |
|
Maybe the inheritance issue can be addressed by having >>> hoc.Vector([1,2,3])
<TopLevelHocInterpreter>(By contrast right now, |
I think the hoc.Vector([1,2,3], hocbase=h.Vector)I don't see why it is strictly necessary to keep the Or, what I would do if I were cpp proficient, is go the hardcore way and refactor every occurence of |
Codecov Report
@@ Coverage Diff @@
## master #1858 +/- ##
==========================================
+ Coverage 61.48% 61.49% +0.01%
==========================================
Files 623 623
Lines 119164 119200 +36
==========================================
+ Hits 73265 73303 +38
+ Misses 45899 45897 -2
... and 22 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
|
✔️ 146643d -> Azure artifacts URL |
|
I think everything that was identified early on works now. You can inherit directly from e.g. Seven things before this is done-done:
|
|
✔️ 97976fe -> Azure artifacts URL |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Wow great. Thanks everyone who's involved. I'm looking forward to using the types. |
|
✔️ 3ee944b -> Azure artifacts URL |
This comment has been minimized.
This comment has been minimized.
|
✔️ f092099 -> Azure artifacts URL |
|
Second pipeline with BlueConfigs tests just passed https://bbpgitlab.epfl.ch/hpc/sim/blueconfigs/-/jobs/929615 |
|
✔️ 5b059f0 -> Azure artifacts URL |
This comment has been minimized.
This comment has been minimized.
|
adding @nrnhines for a final review |
|
✔️ f4be23d -> Azure artifacts URL |
Context
All NEURON’s internal interpreter objects are instances of a global top-level type:
HocObject. Before this PR they were considered direct instances, without any intermediate hierarchy.For instance we want
h.Vector()to return an object with a different data type thanh.Deck(). In current NEURON, they both are of typehoc.HocObject. Likewise for all other built-in NEURON classes. The types should display meaningful names when printed (e.g.hoc.Vectoror maybenrn.Vector, depending on where they are defined).This PR
With this PR Hoc classes are now associated with actual Python types, created dynamically. Such change enables type instances to be properly recognized as such, respecting e.g.
isinstance()predicates and subclassing. E.g.Additionally, each of the built-in class types is a subclass of
hoc.HocObject, for backwards compatibility.This happens with negligible degradation of performance thanks to hash mapping from NEURON symbol to Python object type.
Some questions:
h.Import3D_GUI)hocmodule the right place for the subtypes? Should they go innrninstead? I would prefer to think of anh.Vectoras a NEURON Vector than as a HOC Vector.Additional considerations:
nrnpy_ho2po, but newly created NEURON objects are not (possibly due tonrnpy_ho2poincreasing the NEURON reference count, but that ot being needed at object creation).h.Vectoritself be? After the first three commits below, it's ahoc.HocObjectstill, but this is problematic for inheritance. It;s also an instance oftypehclass, we can only have one HOC base class.