You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
convert a list of Quantity object into an array of Quantity object #183
I wonder if it is possible to have a function to convert a list of Quantity into an array of Quantity object?
>>> import numpy as np
>>> import quantities as pq
>>> a = 1 * pq.e
>>> a
array(1.) * e
>>> b = [a, a]
>>> b
[array(1.) * e, array(1.) * e]
>>> np.array(b)
array([1., 1.])
I'm using the following function for this use-case (the name of it could be improved, though)
defquantitylistrescale(seq, unit=None):
"""Convert list of quantities to a rescaled quantity array Examples -------- >>> import quantities as pq >>> a = 3 * pq.m; b = 2.5 * pq.mm >>> quantitylistrescale([a, b], 'cm') array([3.0e+02, 2.5e-01]) * cm If no ``unit`` is given, the elements will be rescaled to the unit of the first quantity. >>> quantitylistrescale([a, b]) array([3.0e+00, 2.5e-03]) * m >>> quantitylistrescale([]) Traceback (most recent call last): ... StopIteration >>> quantitylistrescale([3 * pq.m, 4 * pq.V]) Traceback (most recent call last): ... ValueError: Unable to convert between units of "V" and "m" """assertall(isinstance(item, pq.Quantity) foriteminseq), (
f"{seq} contains objects which are not of type pq.Quantity")
ifunitisNone:
first=next(iter(seq))
unit=first.dimensionality.stringrescaled_seq= [item.rescale(unit).magnitudeforiteminseq]
returnpq.Quantity(rescaled_seq, unit)
I'm using a similar function (without type checking as I know what is in the array) but I felt that it might be good to have it in the package, like pq.array().
Activity
twmr commentedon May 22, 2021
I'm using the following function for this use-case (the name of it could be improved, though)
xiki-tempula commentedon May 23, 2021
I'm using a similar function (without type checking as I know what is in the array) but I felt that it might be good to have it in the package, like
pq.array().