halib 0.2.14__py3-none-any.whl → 0.2.16__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.
@@ -12,6 +12,8 @@ from plotly.subplots import make_subplots
12
12
  import plotly.graph_objects as go
13
13
  import plotly.express as px # for dynamic color scales
14
14
 
15
+ from contextlib import contextmanager
16
+
15
17
  from ...common.common import ConsoleLog
16
18
  from ...system.path import *
17
19
 
@@ -43,6 +45,20 @@ def check_enabled(func):
43
45
  # ==========================================
44
46
  # 2. The Class
45
47
  # ==========================================
48
+ class ContextScope:
49
+ """Helper to remember the current context name so you don't have to repeat it."""
50
+
51
+ def __init__(self, profiler, ctx_name):
52
+ self.profiler = profiler
53
+ self.ctx_name = ctx_name
54
+
55
+ @contextmanager
56
+ def step(self, step_name):
57
+ # Automatically passes the stored ctx_name + the new step_name
58
+ with self.profiler.measure(self.ctx_name, step_name):
59
+ yield
60
+
61
+
46
62
  class zProfiler:
47
63
  """A singleton profiler to measure execution time of contexts and steps.
48
64
 
@@ -52,13 +68,23 @@ class zProfiler:
52
68
  output_file (str): Path to save the profiling report.
53
69
  report_format (str): Output format for reports ("json" or "csv").
54
70
 
55
- Example:
71
+ Example (using context manager):
72
+ prof = zProfiler()
73
+ with prof.measure("my_context") as ctx:
74
+ with ctx.step("step1"):
75
+ time.sleep(0.1)
76
+ with ctx.step("step2"):
77
+ time.sleep(0.2)
78
+
79
+ Example (using raw methods):
56
80
  prof = zProfiler()
57
81
  prof.ctx_start("my_context")
58
82
  prof.step_start("my_context", "step1")
59
83
  time.sleep(0.1)
60
84
  prof.step_end("my_context", "step1")
61
85
  prof.ctx_end("my_context")
86
+
87
+
62
88
  """
63
89
 
64
90
  _instance = None
@@ -127,6 +153,24 @@ class zProfiler:
127
153
  return
128
154
  self.time_dict[ctx_name]["step_dict"][step_name][-1].append(time.perf_counter())
129
155
 
156
+ @contextmanager
157
+ def measure(self, ctx_name, step_name=None):
158
+ if step_name is None:
159
+ # --- Context Mode ---
160
+ self.ctx_start(ctx_name)
161
+ try:
162
+ # Yield the helper object initialized with the current context name
163
+ yield ContextScope(self, ctx_name)
164
+ finally:
165
+ self.ctx_end(ctx_name)
166
+ else:
167
+ # --- Step Mode ---
168
+ self.step_start(ctx_name, step_name)
169
+ try:
170
+ yield
171
+ finally:
172
+ self.step_end(ctx_name, step_name)
173
+
130
174
  def _step_dict_to_detail(self, ctx_step_dict):
131
175
  """
132
176
  'ctx_step_dict': {
@@ -96,6 +96,34 @@ class VideoUtils:
96
96
  return dfmk["video_meta"].copy()
97
97
 
98
98
 
99
+ # -----------------------------
100
+ # FFmpeg Horizontal Stack
101
+ # -----------------------------
102
+ @staticmethod
103
+ def video_hstack(video_files, output_file):
104
+ """Horizontally stack multiple videos using FFmpeg."""
105
+ tmp_file = "video_list.txt"
106
+ try:
107
+ with open(tmp_file, "w") as f:
108
+ for video in video_files:
109
+ f.write(f"file '{video}'\n")
110
+
111
+ ffmpeg_cmd = (
112
+ f"ffmpeg -f concat -safe 0 -i {tmp_file} "
113
+ f'-filter_complex "[0:v][1:v][2:v]hstack=inputs={len(video_files)}[v]" '
114
+ f'-map "[v]" -c:v libx264 -preset fast -crf 22 {output_file}'
115
+ )
116
+
117
+ os.system(ffmpeg_cmd)
118
+ print(f"[INFO] Video stacked successfully: {output_file}")
119
+
120
+ except Exception as e:
121
+ print(f"[ERROR] Video stacking failed: {e}")
122
+ finally:
123
+ if os.path.exists(tmp_file):
124
+ os.remove(tmp_file)
125
+
126
+
99
127
  class VideoResolution(Enum):
100
128
  VR480p = "720x480"
101
129
  VR576p = "1280x720"
@@ -146,7 +174,6 @@ def trim_video(source, destination, start_time, end_time):
146
174
 
147
175
  progress_bar = None
148
176
 
149
-
150
177
  def on_progress(bytes_done, total_bytes):
151
178
  global progress_bar
152
179
  if progress_bar is None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: halib
3
- Version: 0.2.14
3
+ Version: 0.2.16
4
4
  Summary: Small library for common tasks
5
5
  Author: Hoang Van Ha
6
6
  Author-email: hoangvanhauit@gmail.com
@@ -41,6 +41,7 @@ Requires-Dist: tqdm
41
41
  Requires-Dist: tube_dl
42
42
  Requires-Dist: wandb
43
43
  Requires-Dist: ipynbname
44
+ Requires-Dist: typed-argument-parser
44
45
  Dynamic: author
45
46
  Dynamic: author-email
46
47
  Dynamic: classifier
@@ -53,8 +54,8 @@ Dynamic: summary
53
54
 
54
55
  # Helper package for coding and automation
55
56
 
56
- **Version 0.2.14**
57
- + update `exp.perf.profiler`: add `enabled` flag to enable/disable profiling dynamically
57
+ **Version 0.2.16**
58
+ + update `exp.perf.profiler`: add `enabled` flag to enable/disable profiling dynamically, also add `measure` context manager to simplify measuring code blocks.
58
59
 
59
60
  **Version 0.2.13**
60
61
  + reorganize packages with most changes in `research` package; also rename `research` to `exp` (package for experiment management and utilities)
@@ -35,7 +35,7 @@ halib/exp/perf/gpu_mon.py,sha256=vD41_ZnmPLKguuq9X44SB_vwd9JrblO4BDzHLXZhhFY,223
35
35
  halib/exp/perf/perfcalc.py,sha256=p7rhVShiie7DT_s50lbvbGftVCkrWE0tQGFLUEmTXi0,18326
36
36
  halib/exp/perf/perfmetrics.py,sha256=qRiNiCKGUSTLY7gPMVMuVHGAAyeosfGWup2eM4490aw,5485
37
37
  halib/exp/perf/perftb.py,sha256=IWElg3OB5dmhfxnY8pMZvkL2y_EnvLmEx3gJlpUR1Fs,31066
38
- halib/exp/perf/profiler.py,sha256=57OO_BEC6CWKDCmLNyoYyz5nAvy-q8Lyc61afJkD0f4,13451
38
+ halib/exp/perf/profiler.py,sha256=jWcOu4tujac-8eoRMmqLtOv71qIx-FAi9Ri_35Jj0W8,14817
39
39
  halib/exp/viz/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
40
40
  halib/exp/viz/plot.py,sha256=51FhD1mH4nthTrY3K4mrO5pxI5AzvHXpZy5_ToqkYHs,28580
41
41
  halib/filetype/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -43,7 +43,7 @@ halib/filetype/csvfile.py,sha256=dwWM1LglQr1SaSWq2KDl93fYzQn1638JGo_l1GbBhbk,646
43
43
  halib/filetype/ipynb.py,sha256=pd2LgmPa7ZbF0YlQJbeQZEsl6jHQUSoyVtkCT7WhU5Q,1657
44
44
  halib/filetype/jsonfile.py,sha256=2HcBqXYjLNvqFok3PHOgH59vlhDCZLZpt7ezvgx1TFM,474
45
45
  halib/filetype/textfile.py,sha256=3koEFyVme1SEHdud7TnjArHndoiqfMGfMdYY3NIFegM,397
46
- halib/filetype/videofile.py,sha256=wDyZp7Dh0ZuNgQUvt8gLTpy3Flx1jDr-QsO4-jzriGE,8104
46
+ halib/filetype/videofile.py,sha256=aYPWiCw-rVv2eVXe-3ycP6R9hwhvd-rLAdwXjB-EcOw,9103
47
47
  halib/filetype/yamlfile.py,sha256=buvj016n3wmEBDfeDX9lbljqDk5R5OlKHuXDKSfIKK0,2411
48
48
  halib/online/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
49
49
  halib/online/gdrive.py,sha256=RmF4y6UPxektkKIctmfT-pKWZsBM9FVUeld6zZmJkp0,7787
@@ -102,8 +102,8 @@ halib/utils/list.py,sha256=BM-8sRhYyqF7bh4p7TQtV7P_gnFruUCA6DTUOombaZg,337
102
102
  halib/utils/listop.py,sha256=Vpa8_2fI0wySpB2-8sfTBkyi_A4FhoFVVvFiuvW8N64,339
103
103
  halib/utils/tele_noti.py,sha256=-4WXZelCA4W9BroapkRyIdUu9cUVrcJJhegnMs_WpGU,5928
104
104
  halib/utils/video.py,sha256=zLoj5EHk4SmP9OnoHjO8mLbzPdtq6gQPzTQisOEDdO8,3261
105
- halib-0.2.14.dist-info/licenses/LICENSE.txt,sha256=qZssdna4aETiR8znYsShUjidu-U4jUT9Q-EWNlZ9yBQ,1100
106
- halib-0.2.14.dist-info/METADATA,sha256=jEayj2DXN6lkHvIzsVFf1cix3zp0aOmVT7y09H6UhFE,6950
107
- halib-0.2.14.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
108
- halib-0.2.14.dist-info/top_level.txt,sha256=7AD6PLaQTreE0Fn44mdZsoHBe_Zdd7GUmjsWPyQ7I-k,6
109
- halib-0.2.14.dist-info/RECORD,,
105
+ halib-0.2.16.dist-info/licenses/LICENSE.txt,sha256=qZssdna4aETiR8znYsShUjidu-U4jUT9Q-EWNlZ9yBQ,1100
106
+ halib-0.2.16.dist-info/METADATA,sha256=AmEmCz0eQJgwhd3rtube-XXu67XcqZOBzhbLQt5Xwp0,7059
107
+ halib-0.2.16.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
108
+ halib-0.2.16.dist-info/top_level.txt,sha256=7AD6PLaQTreE0Fn44mdZsoHBe_Zdd7GUmjsWPyQ7I-k,6
109
+ halib-0.2.16.dist-info/RECORD,,
File without changes