Coverage for ibllib/time.py: 96%

24 statements  

« prev     ^ index     » next       coverage.py v7.7.0, created at 2025-03-17 15:25 +0000

1# library of small functions 

2import datetime 

3import numpy as np 

4 

5 

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' 

10 

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): 1qrsbatuv

17 return [isostr2date(el) for el in isostr] 1b

18 

19 format = '%Y-%m-%dT%H:%M:%S' 1qrsbatuv

20 if '.' in isostr: 1qrsbatuv

21 format += '.%f' 1qrsbatuv

22 if '+' in isostr: 1qrsbatuv

23 format += '.%f' 

24 return datetime.datetime.strptime(isostr, format) 1qrsbatuv

25 

26 

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: 1wx

31 adate = datetime.datetime.fromordinal(adate.toordinal()) 1w

32 return datetime.datetime.isoformat(adate) 1wx

33 

34 

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 1cdefaghijklmno

40 cycle2 = (time >> 25) & 0x7F 1cdefaghijklmno

41 seconds = cycle2 + cycle1 / 8000. 1cdefaghijklmno

42 return seconds 1cdefaghijklmno

43 

44 

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) 1cdefaghijklmno

48 cycleindex = np.cumsum(cycles) 1cdefaghijklmno

49 return time + cycleindex * 128 1cdefaghijklmno