halib 0.1.99__py3-none-any.whl → 0.2.21__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.
Files changed (66) hide show
  1. halib/__init__.py +12 -6
  2. halib/common/__init__.py +0 -0
  3. halib/common/common.py +207 -0
  4. halib/common/rich_color.py +285 -0
  5. halib/exp/__init__.py +0 -0
  6. halib/exp/core/__init__.py +0 -0
  7. halib/exp/core/base_config.py +167 -0
  8. halib/exp/core/base_exp.py +147 -0
  9. halib/exp/core/param_gen.py +189 -0
  10. halib/exp/core/wandb_op.py +117 -0
  11. halib/exp/data/__init__.py +0 -0
  12. halib/exp/data/dataclass_util.py +41 -0
  13. halib/exp/data/dataset.py +208 -0
  14. halib/exp/data/torchloader.py +165 -0
  15. halib/exp/perf/__init__.py +0 -0
  16. halib/exp/perf/flop_calc.py +190 -0
  17. halib/exp/perf/gpu_mon.py +58 -0
  18. halib/exp/perf/perfcalc.py +440 -0
  19. halib/exp/perf/perfmetrics.py +137 -0
  20. halib/exp/perf/perftb.py +778 -0
  21. halib/exp/perf/profiler.py +507 -0
  22. halib/exp/viz/__init__.py +0 -0
  23. halib/exp/viz/plot.py +754 -0
  24. halib/filetype/csvfile.py +3 -9
  25. halib/filetype/ipynb.py +3 -5
  26. halib/filetype/jsonfile.py +0 -3
  27. halib/filetype/textfile.py +0 -1
  28. halib/filetype/videofile.py +119 -3
  29. halib/filetype/yamlfile.py +8 -16
  30. halib/online/projectmake.py +7 -6
  31. halib/online/tele_noti.py +165 -0
  32. halib/research/base_exp.py +75 -18
  33. halib/research/core/__init__.py +0 -0
  34. halib/research/core/base_config.py +144 -0
  35. halib/research/core/base_exp.py +157 -0
  36. halib/research/core/param_gen.py +108 -0
  37. halib/research/core/wandb_op.py +117 -0
  38. halib/research/data/__init__.py +0 -0
  39. halib/research/data/dataclass_util.py +41 -0
  40. halib/research/data/dataset.py +208 -0
  41. halib/research/data/torchloader.py +165 -0
  42. halib/research/dataset.py +1 -1
  43. halib/research/metrics.py +4 -0
  44. halib/research/mics.py +8 -2
  45. halib/research/perf/__init__.py +0 -0
  46. halib/research/perf/flop_calc.py +190 -0
  47. halib/research/perf/gpu_mon.py +58 -0
  48. halib/research/perf/perfcalc.py +363 -0
  49. halib/research/perf/perfmetrics.py +137 -0
  50. halib/research/perf/perftb.py +778 -0
  51. halib/research/perf/profiler.py +301 -0
  52. halib/research/perfcalc.py +57 -32
  53. halib/research/viz/__init__.py +0 -0
  54. halib/research/viz/plot.py +754 -0
  55. halib/system/_list_pc.csv +6 -0
  56. halib/system/filesys.py +60 -20
  57. halib/system/path.py +106 -0
  58. halib/utils/dict.py +9 -0
  59. halib/utils/list.py +12 -0
  60. halib-0.2.21.dist-info/METADATA +192 -0
  61. halib-0.2.21.dist-info/RECORD +109 -0
  62. halib-0.1.99.dist-info/METADATA +0 -209
  63. halib-0.1.99.dist-info/RECORD +0 -64
  64. {halib-0.1.99.dist-info → halib-0.2.21.dist-info}/WHEEL +0 -0
  65. {halib-0.1.99.dist-info → halib-0.2.21.dist-info}/licenses/LICENSE.txt +0 -0
  66. {halib-0.1.99.dist-info → halib-0.2.21.dist-info}/top_level.txt +0 -0
halib/__init__.py CHANGED
@@ -19,11 +19,11 @@ __all__ = [
19
19
  "os",
20
20
  "pd",
21
21
  "plt",
22
- "pprint",
23
22
  "pprint_box",
24
23
  "pprint_local_path",
24
+ "pprint_stack_trace",
25
+ "pprint",
25
26
  "px",
26
- "pprint_local_path",
27
27
  "rcolor_all_str",
28
28
  "rcolor_palette_all",
29
29
  "rcolor_palette",
@@ -32,10 +32,10 @@ __all__ = [
32
32
  "rprint",
33
33
  "sns",
34
34
  "tcuda",
35
+ "time",
35
36
  "timebudget",
36
37
  "tqdm",
37
38
  "warnings",
38
- "time",
39
39
  ]
40
40
  import warnings
41
41
 
@@ -56,8 +56,7 @@ from .filetype.yamlfile import load_yaml
56
56
  from .system import cmd
57
57
  from .system import filesys as fs
58
58
  from .filetype import csvfile
59
- from .cuda import tcuda
60
- from .common import (
59
+ from .common.common import (
61
60
  console,
62
61
  console_log,
63
62
  ConsoleLog,
@@ -65,6 +64,8 @@ from .common import (
65
64
  norm_str,
66
65
  pprint_box,
67
66
  pprint_local_path,
67
+ pprint_stack_trace,
68
+ tcuda,
68
69
  )
69
70
 
70
71
  # for log
@@ -76,7 +77,12 @@ from timebudget import timebudget
76
77
  import omegaconf
77
78
  from omegaconf import OmegaConf
78
79
  from omegaconf.dictconfig import DictConfig
79
- from .rich_color import rcolor_str, rcolor_palette, rcolor_palette_all, rcolor_all_str
80
+ from .common.rich_color import (
81
+ rcolor_str,
82
+ rcolor_palette,
83
+ rcolor_palette_all,
84
+ rcolor_all_str,
85
+ )
80
86
 
81
87
  # for visualization
82
88
  import seaborn as sns
File without changes
halib/common/common.py ADDED
@@ -0,0 +1,207 @@
1
+ import os
2
+ import sys
3
+ import re
4
+ import arrow
5
+ import importlib
6
+
7
+ import rich
8
+ from rich import print
9
+ from rich.panel import Panel
10
+ from rich.console import Console
11
+ from rich.pretty import pprint, Pretty
12
+
13
+ from pathlib import Path, PureWindowsPath
14
+ from typing import Optional
15
+ from loguru import logger
16
+
17
+
18
+ console = Console()
19
+
20
+
21
+ def seed_everything(seed=42):
22
+ import random
23
+ import numpy as np
24
+
25
+ random.seed(seed)
26
+ np.random.seed(seed)
27
+ # import torch if it is available
28
+ try:
29
+ import torch
30
+
31
+ torch.manual_seed(seed)
32
+ torch.cuda.manual_seed(seed)
33
+ torch.cuda.manual_seed_all(seed)
34
+ torch.backends.cudnn.deterministic = True
35
+ torch.backends.cudnn.benchmark = False
36
+ except ImportError:
37
+ pprint("torch not imported, skipping torch seed_everything")
38
+ pass
39
+
40
+
41
+ def now_str(sep_date_time="."):
42
+ assert sep_date_time in [
43
+ ".",
44
+ "_",
45
+ "-",
46
+ ], "sep_date_time must be one of '.', '_', or '-'"
47
+ now_string = arrow.now().format(f"YYYYMMDD{sep_date_time}HHmmss")
48
+ return now_string
49
+
50
+
51
+ def norm_str(in_str):
52
+ # Replace one or more whitespace characters with a single underscore
53
+ norm_string = re.sub(r"\s+", "_", in_str)
54
+ # Remove leading and trailing spaces
55
+ norm_string = norm_string.strip()
56
+ return norm_string
57
+
58
+
59
+ def pprint_box(obj, title="", border_style="green"):
60
+ """
61
+ Pretty print an object in a box.
62
+ """
63
+ rich.print(
64
+ Panel(Pretty(obj, expand_all=True), title=title, border_style=border_style)
65
+ )
66
+
67
+
68
+ def console_rule(msg, do_norm_msg=True, is_end_tag=False):
69
+ msg = norm_str(msg) if do_norm_msg else msg
70
+ if is_end_tag:
71
+ console.rule(f"</{msg}>")
72
+ else:
73
+ console.rule(f"<{msg}>")
74
+
75
+
76
+ def console_log(func):
77
+ def wrapper(*args, **kwargs):
78
+ console_rule(func.__name__)
79
+ result = func(*args, **kwargs)
80
+ console_rule(func.__name__, is_end_tag=True)
81
+ return result
82
+
83
+ return wrapper
84
+
85
+
86
+ class ConsoleLog:
87
+ def __init__(self, message):
88
+ self.message = message
89
+
90
+ def __enter__(self):
91
+ console_rule(self.message)
92
+ return self
93
+
94
+ def __exit__(self, exc_type, exc_value, traceback):
95
+ console_rule(self.message, is_end_tag=True)
96
+ if exc_type is not None:
97
+ print(f"An exception of type {exc_type} occurred.")
98
+ print(f"Exception message: {exc_value}")
99
+
100
+
101
+ def linux_to_wins_path(path: str) -> str:
102
+ """
103
+ Convert a Linux-style WSL path (/mnt/c/... or /mnt/d/...) to a Windows-style path (C:\...).
104
+ """
105
+ # Handle only /mnt/<drive>/... style
106
+ if (
107
+ path.startswith("/mnt/")
108
+ and len(path) > 6
109
+ and path[5].isalpha()
110
+ and path[6] == "/"
111
+ ):
112
+ drive = path[5].upper() # Extract drive letter
113
+ win_path = f"{drive}:{path[6:]}" # Replace "/mnt/c/" with "C:/"
114
+ else:
115
+ win_path = path # Return unchanged if not a WSL-style path
116
+ # Normalize to Windows-style backslashes
117
+ return str(PureWindowsPath(win_path))
118
+
119
+
120
+ DEFAULT_STACK_TRACE_MSG = "Caused by halib.common.common.pprint_stack_trace"
121
+
122
+
123
+ def pprint_stack_trace(
124
+ msg: str = DEFAULT_STACK_TRACE_MSG,
125
+ e: Optional[Exception] = None,
126
+ force_stop: bool = False,
127
+ ):
128
+ """
129
+ Print the current stack trace or the stack trace of an exception.
130
+ """
131
+ try:
132
+ if e is not None:
133
+ raise e
134
+ else:
135
+ raise Exception("pprint_stack_trace called")
136
+ except Exception as e:
137
+ # attach the exception trace to a warning
138
+ global DEFAULT_STACK_TRACE_MSG
139
+ if len(msg.strip()) == 0:
140
+ msg = DEFAULT_STACK_TRACE_MSG
141
+ logger.opt(exception=e).warning(msg)
142
+ if force_stop:
143
+ console.rule("[red]Force Stop Triggered in <halib.common.pprint_stack_trace>[/red]")
144
+ sys.exit(1)
145
+
146
+ def pprint_local_path(
147
+ local_path: str, get_wins_path: bool = False, tag: str = ""
148
+ ) -> str:
149
+ """
150
+ Pretty-print a local path with emoji and clickable file:// URI.
151
+
152
+ Args:
153
+ local_path: Path to file or directory (Linux or Windows style).
154
+ get_wins_path: If True on Linux, convert WSL-style path to Windows style before printing.
155
+ tag: Optional console log tag.
156
+
157
+ Returns:
158
+ The file URI string.
159
+ """
160
+ p = Path(local_path).resolve()
161
+ type_str = "📄" if p.is_file() else "📁" if p.is_dir() else "❓"
162
+
163
+ if get_wins_path and os.name == "posix":
164
+ # Try WSL → Windows conversion
165
+ converted = linux_to_wins_path(str(p))
166
+ if converted != str(p): # Conversion happened
167
+ file_uri = str(PureWindowsPath(converted).as_uri())
168
+ else:
169
+ file_uri = p.as_uri()
170
+ else:
171
+ file_uri = p.as_uri()
172
+
173
+ content_str = f"{type_str} [link={file_uri}]{file_uri}[/link]"
174
+
175
+ if tag:
176
+ with ConsoleLog(tag):
177
+ console.print(content_str)
178
+ else:
179
+ console.print(content_str)
180
+
181
+ return file_uri
182
+
183
+
184
+ def tcuda():
185
+ NOT_INSTALLED = "Not Installed"
186
+ GPU_AVAILABLE = "GPU(s) Available"
187
+ ls_lib = ["torch", "tensorflow"]
188
+ lib_stats = {lib: NOT_INSTALLED for lib in ls_lib}
189
+ for lib in ls_lib:
190
+ spec = importlib.util.find_spec(lib)
191
+ if spec:
192
+ if lib == "torch":
193
+ import torch
194
+
195
+ lib_stats[lib] = str(torch.cuda.device_count()) + " " + GPU_AVAILABLE
196
+ elif lib == "tensorflow":
197
+ import tensorflow as tf
198
+
199
+ lib_stats[lib] = (
200
+ str(len(tf.config.list_physical_devices("GPU")))
201
+ + " "
202
+ + GPU_AVAILABLE
203
+ )
204
+ console.rule("<CUDA Library Stats>")
205
+ pprint(lib_stats)
206
+ console.rule("</CUDA Library Stats>")
207
+ return lib_stats
@@ -0,0 +1,285 @@
1
+ from rich.console import Console
2
+ from rich.pretty import pprint
3
+ from rich.table import Table
4
+ from rich.text import Text
5
+ from rich.panel import Panel
6
+
7
+ # List of colors
8
+ # ! https://rich.readthedocs.io/en/stable/appendix/colors.html
9
+ all_colors = [
10
+ "black",
11
+ "red",
12
+ "green",
13
+ "yellow",
14
+ "blue",
15
+ "magenta",
16
+ "cyan",
17
+ "white",
18
+ "bright_black",
19
+ "bright_red",
20
+ "bright_green",
21
+ "bright_yellow",
22
+ "bright_blue",
23
+ "bright_magenta",
24
+ "bright_cyan",
25
+ "bright_white",
26
+ "grey0",
27
+ "navy_blue",
28
+ "dark_blue",
29
+ "blue3",
30
+ "blue1",
31
+ "dark_green",
32
+ "deep_sky_blue4",
33
+ "dodger_blue3",
34
+ "dodger_blue2",
35
+ "green4",
36
+ "spring_green4",
37
+ "turquoise4",
38
+ "deep_sky_blue3",
39
+ "dodger_blue1",
40
+ "dark_cyan",
41
+ "light_sea_green",
42
+ "deep_sky_blue2",
43
+ "deep_sky_blue1",
44
+ "green3",
45
+ "spring_green3",
46
+ "cyan3",
47
+ "dark_turquoise",
48
+ "turquoise2",
49
+ "green1",
50
+ "spring_green2",
51
+ "spring_green1",
52
+ "medium_spring_green",
53
+ "cyan2",
54
+ "cyan1",
55
+ "purple4",
56
+ "purple3",
57
+ "blue_violet",
58
+ "grey37",
59
+ "medium_purple4",
60
+ "slate_blue3",
61
+ "royal_blue1",
62
+ "chartreuse4",
63
+ "pale_turquoise4",
64
+ "steel_blue",
65
+ "steel_blue3",
66
+ "cornflower_blue",
67
+ "dark_sea_green4",
68
+ "cadet_blue",
69
+ "sky_blue3",
70
+ "chartreuse3",
71
+ "sea_green3",
72
+ "aquamarine3",
73
+ "medium_turquoise",
74
+ "steel_blue1",
75
+ "sea_green2",
76
+ "sea_green1",
77
+ "dark_slate_gray2",
78
+ "dark_red",
79
+ "dark_magenta",
80
+ "orange4",
81
+ "light_pink4",
82
+ "plum4",
83
+ "medium_purple3",
84
+ "slate_blue1",
85
+ "wheat4",
86
+ "grey53",
87
+ "light_slate_grey",
88
+ "medium_purple",
89
+ "light_slate_blue",
90
+ "yellow4",
91
+ "dark_sea_green",
92
+ "light_sky_blue3",
93
+ "sky_blue2",
94
+ "chartreuse2",
95
+ "pale_green3",
96
+ "dark_slate_gray3",
97
+ "sky_blue1",
98
+ "chartreuse1",
99
+ "light_green",
100
+ "aquamarine1",
101
+ "dark_slate_gray1",
102
+ "deep_pink4",
103
+ "medium_violet_red",
104
+ "dark_violet",
105
+ "purple",
106
+ "medium_orchid3",
107
+ "medium_orchid",
108
+ "dark_goldenrod",
109
+ "rosy_brown",
110
+ "grey63",
111
+ "medium_purple2",
112
+ "medium_purple1",
113
+ "dark_khaki",
114
+ "navajo_white3",
115
+ "grey69",
116
+ "light_steel_blue3",
117
+ "light_steel_blue",
118
+ "dark_olive_green3",
119
+ "dark_sea_green3",
120
+ "light_cyan3",
121
+ "light_sky_blue1",
122
+ "green_yellow",
123
+ "dark_olive_green2",
124
+ "pale_green1",
125
+ "dark_sea_green2",
126
+ "pale_turquoise1",
127
+ "red3",
128
+ "deep_pink3",
129
+ "magenta3",
130
+ "dark_orange3",
131
+ "indian_red",
132
+ "hot_pink3",
133
+ "hot_pink2",
134
+ "orchid",
135
+ "orange3",
136
+ "light_salmon3",
137
+ "light_pink3",
138
+ "pink3",
139
+ "plum3",
140
+ "violet",
141
+ "gold3",
142
+ "light_goldenrod3",
143
+ "tan",
144
+ "misty_rose3",
145
+ "thistle3",
146
+ "plum2",
147
+ "yellow3",
148
+ "khaki3",
149
+ "light_yellow3",
150
+ "grey84",
151
+ "light_steel_blue1",
152
+ "yellow2",
153
+ "dark_olive_green1",
154
+ "dark_sea_green1",
155
+ "honeydew2",
156
+ "light_cyan1",
157
+ "red1",
158
+ "deep_pink2",
159
+ "deep_pink1",
160
+ "magenta2",
161
+ "magenta1",
162
+ "orange_red1",
163
+ "indian_red1",
164
+ "hot_pink",
165
+ "medium_orchid1",
166
+ "dark_orange",
167
+ "salmon1",
168
+ "light_coral",
169
+ "pale_violet_red1",
170
+ "orchid2",
171
+ "orchid1",
172
+ "orange1",
173
+ "sandy_brown",
174
+ "light_salmon1",
175
+ "light_pink1",
176
+ "pink1",
177
+ "plum1",
178
+ "gold1",
179
+ "light_goldenrod2",
180
+ "navajo_white1",
181
+ "misty_rose1",
182
+ "thistle1",
183
+ "yellow1",
184
+ "light_goldenrod1",
185
+ "khaki1",
186
+ "wheat1",
187
+ "cornsilk1",
188
+ "grey100",
189
+ "grey3",
190
+ "grey7",
191
+ "grey11",
192
+ "grey15",
193
+ "grey19",
194
+ "grey23",
195
+ "grey27",
196
+ "grey30",
197
+ "grey35",
198
+ "grey39",
199
+ "grey42",
200
+ "grey46",
201
+ "grey50",
202
+ "grey54",
203
+ "grey58",
204
+ "grey62",
205
+ "grey66",
206
+ "grey70",
207
+ "grey74",
208
+ "grey78",
209
+ "grey82",
210
+ "grey85",
211
+ "grey89",
212
+ "grey93",
213
+ ]
214
+
215
+ basic_colors = [
216
+ "black",
217
+ "red",
218
+ "green",
219
+ "yellow",
220
+ "blue",
221
+ "magenta",
222
+ "cyan",
223
+ "white",
224
+ "bright_black",
225
+ "bright_red",
226
+ "bright_green",
227
+ "bright_yellow",
228
+ "bright_blue",
229
+ "bright_magenta",
230
+ "bright_cyan",
231
+ "bright_white",
232
+ ]
233
+
234
+ def rcolor_all_str():
235
+ pprint(all_colors)
236
+
237
+ def rcolor_basic_str():
238
+ pprint(basic_colors)
239
+
240
+ def rcolor_str(in_str, color="white"):
241
+ assert color in all_colors, f"color must be one of {all_colors}"
242
+ return f"[{color}]{in_str}[/{color}]"
243
+
244
+ def rcolor_palette(color_list):
245
+ # make sure all colors are valid (in all_colors)
246
+ for color in color_list:
247
+ assert (
248
+ color in all_colors
249
+ ), f"color must be a valid color. call <rcolor_all_str()> or <rcolor_palette_all()> to see all valid colors"
250
+ # Initialize console
251
+ console = Console()
252
+
253
+ # Create a table with horizontal lines and six columns
254
+ table = Table(show_header=True, header_style="bold magenta", show_lines=True)
255
+
256
+ # Define the columns
257
+ table.add_column("Color Name 1", style="bold")
258
+ table.add_column("Sample 1", style="bold")
259
+ table.add_column("Color Name 2", style="bold")
260
+ table.add_column("Sample 2", style="bold")
261
+ table.add_column("Color Name 3", style="bold")
262
+ table.add_column("Sample 3", style="bold")
263
+
264
+ # Adjust the number of rows needed for the table
265
+ num_colors = len(color_list)
266
+ num_rows = (num_colors + 2) // 3 # Ceiling division to ensure all colors fit
267
+
268
+ # Add rows to the table
269
+ for i in range(num_rows):
270
+ color1 = color_list[i * 3] if i * 3 < num_colors else ""
271
+ color2 = color_list[i * 3 + 1] if i * 3 + 1 < num_colors else ""
272
+ color3 = color_list[i * 3 + 2] if i * 3 + 2 < num_colors else ""
273
+ filled_rect1 = Text(" " * 10, style=f"on {color1}") if color1 else ""
274
+ filled_rect2 = Text(" " * 10, style=f"on {color2}") if color2 else ""
275
+ filled_rect3 = Text(" " * 10, style=f"on {color3}") if color3 else ""
276
+ table.add_row(color1, filled_rect1, color2, filled_rect2, color3, filled_rect3)
277
+
278
+ # Print the table
279
+ console.print(table)
280
+
281
+ def rcolor_palette_basic():
282
+ rcolor_palette(basic_colors)
283
+
284
+ def rcolor_palette_all():
285
+ rcolor_palette(all_colors)
halib/exp/__init__.py ADDED
File without changes
File without changes