Coverage for ibllib/time.py: 96%
24 statements
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-08 17:16 +0100
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-08 17:16 +0100
1# library of small functions
2import datetime
3import numpy as np
6def isostr2date(isostr):
7 """
8 Convert strings representing dates into datetime.datetime objects aimed ad Django REST API
9 ISO 8601: '2018-05-22T14:35:22.99585' or '2018-05-22T14:35:22'
11 :param isostr: a string, list of strings or panda Series / numpy arrays containing strings
12 :return: a scalar, list of
13 """
14 # NB this is intended for scalars or small list. See the ciso8601 pypi module instead for
15 # a performance implementation
16 if not isinstance(isostr, str): 1uvwcbxyaz
17 return [isostr2date(el) for el in isostr] 1c
19 format = '%Y-%m-%dT%H:%M:%S' 1uvwcbxyaz
20 if '.' in isostr: 1uvwcbxyaz
21 format += '.%f' 1uvwcbxyaz
22 if '+' in isostr: 1uvwcbxyaz
23 format += '.%f'
24 return datetime.datetime.strptime(isostr, format) 1uvwcbxyaz
27def date2isostr(adate):
28 # NB this is intended for scalars or small list. See the ciso8601 pypi module instead for
29 # a performance implementation
30 if type(adate) is datetime.date: 1Aa
31 adate = datetime.datetime.fromordinal(adate.toordinal()) 1A
32 return datetime.datetime.isoformat(adate) 1Aa
35def convert_pgts(time):
36 """Convert PointGray cameras timestamps to seconds.
37 Use convert then uncycle"""
38 # offset = time & 0xFFF
39 cycle1 = (time >> 12) & 0x1FFF 1defgbhijklmnopqrsa
40 cycle2 = (time >> 25) & 0x7F 1defgbhijklmnopqrsa
41 seconds = cycle2 + cycle1 / 8000. 1defgbhijklmnopqrsa
42 return seconds 1defgbhijklmnopqrsa
45def uncycle_pgts(time):
46 """Unwrap the converted seconds of a PointGray camera timestamp series."""
47 cycles = np.insert(np.diff(time) < 0, 0, False) 1defgbhijklmnopqrsa
48 cycleindex = np.cumsum(cycles) 1defgbhijklmnopqrsa
49 return time + cycleindex * 128 1defgbhijklmnopqrsa