arbor-sim / arbor

The Arbor multi-compartment neural network simulation library.
https://arbor-sim.org
BSD 3-Clause "New" or "Revised" License
105 stars 59 forks source link

Improve arithmetic interface of quanity stubs #2254

Open Helveg opened 5 months ago

Helveg commented 5 months ago

There's some stubs like arbor.membrane_capacitance that aren't very Python friendly, to retrieve the value it seems I have to parse their string representations:

>>> x = arbor.membrane_capacitance(1 * units.F / units.m2)
>>> x + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'arbor._arbor.membrane_capacitance' and 'int'
>>> x * 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'arbor._arbor.membrane_capacitance' and 'int'
>>> int(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'arbor._arbor.membrane_capacitance'
>>> float(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: float() argument must be a string or a real number, not 'arbor._arbor.membrane_capacitance'
>>> dir(x)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> x < 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'arbor._arbor.membrane_capacitance' and 'int'
>>> x >= 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>=' not supported between instances of 'arbor._arbor.membrane_capacitance' and 'int'
>>> float(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: float() argument must be a string or a real number, not 'arbor._arbor.membrane_capacitance'
>>> float(str(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'Cm=1'
>>> float(str(x)[3:])
1.0

There's also arbor.axial_resistivity and probably others. These are returned from decor.paintings() and make it pretty opaque to inspection.