plexus-python-common 1.0.63__py3-none-any.whl → 1.0.64__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import contextlib
2
2
  import dataclasses
3
3
  import datetime
4
+ import math
4
5
  import os
5
6
  import pathlib
6
7
  import textwrap
@@ -16,6 +17,7 @@ import sqlalchemy as sa
16
17
  import sqlalchemy.dialects.sqlite as sa_sqlite
17
18
  import sqlalchemy.orm as sa_orm
18
19
  from iker.common.utils.dbutils import ConnectionMaker
20
+ from iker.common.utils.dtutils import dt_from_ts_us, dt_to_ts_us
19
21
  from iker.common.utils.funcutils import memorized, singleton
20
22
  from iker.common.utils.iterutils import batched, head_or_none
21
23
  from iker.common.utils.iterutils import dicttree
@@ -48,6 +50,8 @@ __all__ = [
48
50
  "tag_cache_file_path",
49
51
  "TagCache",
50
52
  "tag_cache",
53
+ "standard_clip_duration_us",
54
+ "populate_clip_ranges",
51
55
  ]
52
56
 
53
57
 
@@ -1093,3 +1097,52 @@ def tag_cache(*, identifier: str | None = None, file_path: str | None = None) ->
1093
1097
  if file_path is not None:
1094
1098
  return TagCache(file_path=file_path)
1095
1099
  return TagCache(file_path=tag_cache_file_path())
1100
+
1101
+
1102
+
1103
+ @singleton
1104
+ def standard_clip_duration_us() -> int:
1105
+ """Duration of clips to split samples into, in microseconds, which is fixed to 20 seconds for now."""
1106
+ return 20 * 1_000_000 # 20 seconds
1107
+
1108
+
1109
+
1110
+ def populate_clip_ranges(
1111
+ data_begin_dt: datetime.datetime,
1112
+ data_end_dt: datetime.datetime,
1113
+ *,
1114
+ clip_duration_us: int = None
1115
+ ) -> Generator[tuple[datetime.datetime, datetime.datetime, datetime.datetime, datetime.datetime]]:
1116
+ """
1117
+ Generate clip begin datetime, clip data begin datetime, and clip data end datetime for each clip slot that overlaps
1118
+ with the given data begin and end datetimes.
1119
+ The clip begin datetime is the beginning of the clip slot, the clip data begin datetime is the maximum of the clip
1120
+ begin datetime and the data begin datetime, and the clip data end datetime is the minimum of the clip end datetime
1121
+ and the data end datetime.
1122
+
1123
+ :param data_begin_dt: The beginning datetime of the data.
1124
+ :param data_end_dt: The end datetime of the data.
1125
+ :param clip_duration_us: The duration of each clip in microseconds. If not provided,
1126
+ the standard clip duration will be used.
1127
+ :return: A generator of tuples containing the clip begin datetime, clip data begin datetime,
1128
+ and clip data end datetime
1129
+ """
1130
+ clip_duration_us = clip_duration_us or standard_clip_duration_us()
1131
+
1132
+ if data_begin_dt == data_end_dt:
1133
+ clip_slot = math.floor(dt_to_ts_us(data_begin_dt) / clip_duration_us)
1134
+ clip_begin_dt = dt_from_ts_us(clip_slot * clip_duration_us)
1135
+ clip_end_dt = dt_from_ts_us((clip_slot + 1) * clip_duration_us)
1136
+ yield clip_begin_dt, clip_end_dt, data_begin_dt, data_end_dt
1137
+ return
1138
+
1139
+ begin_clip_slot = math.floor(dt_to_ts_us(data_begin_dt) / clip_duration_us)
1140
+ end_clip_slot = math.ceil(dt_to_ts_us(data_end_dt) / clip_duration_us)
1141
+
1142
+ for clip_slot in range(begin_clip_slot, end_clip_slot):
1143
+ clip_begin_dt = dt_from_ts_us(clip_slot * clip_duration_us)
1144
+ clip_end_dt = dt_from_ts_us((clip_slot + 1) * clip_duration_us)
1145
+ clip_data_begin_dt = max(clip_begin_dt, data_begin_dt)
1146
+ clip_data_end_dt = min(clip_end_dt, data_end_dt)
1147
+
1148
+ yield clip_begin_dt, clip_end_dt, clip_data_begin_dt, clip_data_end_dt
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: plexus-python-common
3
- Version: 1.0.63
3
+ Version: 1.0.64
4
4
  Classifier: Programming Language :: Python :: 3
5
5
  Classifier: Programming Language :: Python :: 3.12
6
6
  Classifier: Programming Language :: Python :: 3.13
@@ -21,9 +21,9 @@ plexus/common/utils/pathutils.py,sha256=hGJqSLj08tuOeZ7WeC5d4BtjnPI732BuntVQBQsq
21
21
  plexus/common/utils/s3utils.py,sha256=zlO4kGs-c2gUeOfPfiKIE5liQZsbYxqAZYCwA8kL0Lo,36017
22
22
  plexus/common/utils/sqlutils.py,sha256=D6kTBjhO5YlNRt3uFlPt6z3uH61m9ajEzPYmsI6NoFc,231
23
23
  plexus/common/utils/strutils.py,sha256=O9Inv4ffUTf6Xjc5ftoZwbIua1NeG7itCT9S3zjZxBc,16436
24
- plexus/common/utils/tagutils.py,sha256=Ga22WnUQxVbzFtGR8XyiRiZZnaddOhdHqBNVof40pqs,44514
24
+ plexus/common/utils/tagutils.py,sha256=7qBLW848fr64Jq8ogUUuUY15mT1KnNZbtEbb3tKosXQ,46930
25
25
  plexus/common/utils/testutils.py,sha256=GyrKOKfrl1Go8Q7tCZLybxYvVqyox1AtEFWzWoecNwg,6163
26
- plexus_python_common-1.0.63.dist-info/METADATA,sha256=x1Njz90tSzZxpbdTNZHMD2IPGqmSiZxFFzoSLa9Ae40,1481
27
- plexus_python_common-1.0.63.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
28
- plexus_python_common-1.0.63.dist-info/top_level.txt,sha256=ug_g7CVwaMQuas5UzAXbHUrQvKGCn8ezc6ZNvvRlJOE,7
29
- plexus_python_common-1.0.63.dist-info/RECORD,,
26
+ plexus_python_common-1.0.64.dist-info/METADATA,sha256=J8wWo3Q3ffOL79INfOk4lyJAcDolpsf3FXz5lIZ95eY,1481
27
+ plexus_python_common-1.0.64.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
28
+ plexus_python_common-1.0.64.dist-info/top_level.txt,sha256=ug_g7CVwaMQuas5UzAXbHUrQvKGCn8ezc6ZNvvRlJOE,7
29
+ plexus_python_common-1.0.64.dist-info/RECORD,,