Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch conversion of timestamps #62

Closed
mhasself opened this issue Aug 6, 2021 · 3 comments
Closed

Batch conversion of timestamps #62

mhasself opened this issue Aug 6, 2021 · 3 comments

Comments

@mhasself
Copy link
Member

mhasself commented Aug 6, 2021

Is there a fast way to convert between arrays of G3VectorTime and numpy float arrays? I find myself writing this a lot:

  g3times = core.G3VectorTime([core.G3Time(t * core.G3Units.s) for t in timestamps])

and that's no fun. I am hoping for something like:

  g3times = core.G3VectorTime(timestamps * core.G3Units.s)
  timestamps = np.array(g3times) / core.G3Units.s
@nwhitehorn
Copy link
Member

You would want to write something like on lines 195-205 of G3Vector.cxx, which implements an efficient converter for integers. I might have some time to do it in the next few days, but you are more than welcome to beat me to it.

nwhitehorn added a commit that referenced this issue Aug 6, 2021
…ay (#62).

This will initialize a G3VectorTime from an array of timestamps (Unix time
in G3Units) with maximal efficiency. No reverse buffer-protocol view is
available yet because that is more complex.
@nwhitehorn
Copy link
Member

Should be all fixed now -- you can do fancy stuff like this:

In [3]: a = core.G3VectorTime(numpy.asarray([53452354453424453,6572334542367,723423235444,834534543535453223],dtype='int64'))

In [4]: core.G3VectorTime(numpy.asarray(a))
Out[4]: spt3g.core.G3VectorTime([09-Dec-1986:14:45:44.534244530, 01-Jan-1970:18:15:23.345423670, 01-Jan-1970:02:00:34.232354440, 15-Jun-2234:15:30:35.354532230])

And then you can get the numbers back:

In [10]: numpy.asarray(a)
Out[10]: 
array([ 53452354453424453,      6572334542367,       723423235444,
       834534543535453223], dtype=int64)

The numpy array is also editable in place:

In [10]: numpy.asarray(a)
Out[10]: 
array([ 53452354453424453,      6572334542367,       723423235444,
       834534543535453223], dtype=int64)

In [11]: numpy.asarray(a)[2] = 5

In [13]: print(a[2])
01-Jan-1970:00:00:00.000000050

In [14]: numpy.asarray(a)
Out[14]: 
array([ 53452354453424453,      6572334542367,                  5,
       834534543535453223], dtype=int64)

@mhasself
Copy link
Member Author

mhasself commented Aug 9, 2021

This is great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants