halib 0.1.67__py3-none-any.whl → 0.1.70__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.
@@ -0,0 +1,100 @@
1
+ import os
2
+ from rich.pretty import pprint
3
+ from abc import ABC, abstractmethod
4
+ from dataclass_wizard import YAMLWizard
5
+
6
+
7
+ class NamedConfig(ABC):
8
+ """
9
+ Base class for named configurations.
10
+ All configurations should have a name.
11
+ """
12
+
13
+ @abstractmethod
14
+ def get_name(self):
15
+ """
16
+ Get the name of the configuration.
17
+ This method should be implemented in subclasses.
18
+ """
19
+ pass
20
+
21
+
22
+ class ExpBaseConfig(ABC, YAMLWizard):
23
+ """
24
+ Base class for configuration objects.
25
+ What a cfg class must have:
26
+ 1 - a dataset cfg
27
+ 2 - a metric cfg
28
+ 3 - a method cfg
29
+ """
30
+
31
+ # Save to yaml fil
32
+ def save_to_outdir(
33
+ self, filename: str = "config.yaml", outdir=None, override: bool = False
34
+ ) -> None:
35
+ """
36
+ Save the configuration to the output directory.
37
+ """
38
+ if outdir is not None:
39
+ output_dir = outdir
40
+ else:
41
+ output_dir = self.get_outdir()
42
+ os.makedirs(output_dir, exist_ok=True)
43
+ assert (output_dir is not None) and (
44
+ os.path.isdir(output_dir)
45
+ ), f"Output directory '{output_dir}' does not exist or is not a directory."
46
+ file_path = os.path.join(output_dir, filename)
47
+ if os.path.exists(file_path) and not override:
48
+ pprint(
49
+ f"File '{file_path}' already exists. Use 'override=True' to overwrite."
50
+ )
51
+ else:
52
+ # method of YAMLWizard to_yaml_file
53
+ self.to_yaml_file(file_path)
54
+
55
+ @classmethod
56
+ @abstractmethod
57
+ # load from a custom YAML file
58
+ def from_custom_yaml_file(cls, yaml_file: str):
59
+ """Load a configuration from a custom YAML file."""
60
+ pass
61
+
62
+ @abstractmethod
63
+ def get_cfg_name(self):
64
+ """
65
+ Get the name of the configuration.
66
+ This method should be implemented in subclasses.
67
+ """
68
+ pass
69
+
70
+ @abstractmethod
71
+ def get_outdir(self):
72
+ """
73
+ Get the output directory for the configuration.
74
+ This method should be implemented in subclasses.
75
+ """
76
+ return None
77
+
78
+ @abstractmethod
79
+ def get_general_cfg(self):
80
+ """
81
+ Get the general configuration like output directory, log settings, SEED, etc.
82
+ This method should be implemented in subclasses.
83
+ """
84
+ pass
85
+
86
+ @abstractmethod
87
+ def get_dataset_cfg(self) -> NamedConfig:
88
+ """
89
+ Get the dataset configuration.
90
+ This method should be implemented in subclasses.
91
+ """
92
+ pass
93
+
94
+ @abstractmethod
95
+ def get_metric_cfg(self) -> NamedConfig:
96
+ """
97
+ Get the metric configuration.
98
+ This method should be implemented in subclasses.
99
+ """
100
+ pass
@@ -0,0 +1,99 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ from base_config import ExpBaseConfig
4
+ from perfcalc import PerfCalc
5
+ from metrics import MetricsBackend
6
+
7
+ # ! SEE https://github.com/hahv/base_exp for sample usage
8
+ class BaseExperiment(PerfCalc, ABC):
9
+ """
10
+ Base class for experiments.
11
+ Orchestrates the experiment pipeline using a pluggable metrics backend.
12
+ """
13
+
14
+ def __init__(self, config: ExpBaseConfig):
15
+ self.config = config
16
+ self.metric_backend = None
17
+
18
+ # -----------------------
19
+ # PerfCalc Required Methods
20
+ # -----------------------
21
+ def get_dataset_name(self):
22
+ return self.config.get_dataset_cfg().get_name()
23
+
24
+ def get_experiment_name(self):
25
+ return self.config.get_cfg_name()
26
+
27
+ def get_metric_backend(self):
28
+ if not self.metric_backend:
29
+ self.metric_backend = self.prepare_metrics(self.config.get_metric_cfg())
30
+ return self.metric_backend
31
+
32
+ # -----------------------
33
+ # Abstract Experiment Steps
34
+ # -----------------------
35
+ @abstractmethod
36
+ def init_general(self, general_cfg):
37
+ """Setup general settings like SEED, logging, env variables."""
38
+ pass
39
+
40
+ @abstractmethod
41
+ def prepare_dataset(self, dataset_cfg):
42
+ """Load/prepare dataset."""
43
+ pass
44
+
45
+ @abstractmethod
46
+ def prepare_metrics(self, metric_cfg) -> MetricsBackend:
47
+ """
48
+ Prepare the metrics for the experiment.
49
+ This method should be implemented in subclasses.
50
+ """
51
+ pass
52
+
53
+ @abstractmethod
54
+ def exec_exp(self, *args, **kwargs):
55
+ """Run experiment process, e.g.: training/evaluation loop.
56
+ Return: raw_metrics_data, and extra_data as input for calc_and_save_exp_perfs
57
+ """
58
+ pass
59
+
60
+ def eval_exp(self):
61
+ """Optional: re-run evaluation from saved results."""
62
+ pass
63
+
64
+ # -----------------------
65
+ # Main Experiment Runner
66
+ # -----------------------
67
+ def run_exp(self, do_calc_metrics=True, *args, **kwargs):
68
+ """
69
+ Run the whole experiment pipeline.
70
+ Params:
71
+ + 'outfile' to save csv file results,
72
+ + 'outdir' to set output directory for experiment results.
73
+ + 'return_df' to return a DataFrame of results instead of a dictionary.
74
+
75
+ Full pipeline:
76
+ 1. Init
77
+ 2. Dataset
78
+ 3. Metrics Preparation
79
+ 4. Save Config
80
+ 5. Execute
81
+ 6. Calculate & Save Metrics
82
+ """
83
+ self.init_general(self.config.get_general_cfg())
84
+ self.prepare_dataset(self.config.get_dataset_cfg())
85
+
86
+ # Save config before running
87
+ self.config.save_to_outdir()
88
+
89
+ # Execute experiment
90
+ results = self.exec_exp(*args, **kwargs)
91
+ if do_calc_metrics:
92
+ metrics_data, extra_data = results
93
+ # Calculate & Save metrics
94
+ perf_results = self.calc_and_save_exp_perfs(
95
+ raw_metrics_data=metrics_data, extra_data=extra_data, *args, **kwargs
96
+ )
97
+ return perf_results
98
+ else:
99
+ return results
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: halib
3
- Version: 0.1.67
3
+ Version: 0.1.70
4
4
  Summary: Small library for common tasks
5
5
  Author: Hoang Van Ha
6
6
  Author-email: hoangvanhauit@gmail.com
@@ -52,6 +52,11 @@ Dynamic: summary
52
52
 
53
53
  Helper package for coding and automation
54
54
 
55
+ **Version 0.1.70**
56
+
57
+ + `research/base_exp`: add base experiment class to handle common experiment tasks, including performance calculation and saving results.
58
+
59
+
55
60
  **Version 0.1.67**
56
61
 
57
62
  + now use `uv` for venv management
@@ -28,6 +28,8 @@ halib/online/gdrive_mkdir.py,sha256=wSJkQMJCDuS1gxQ2lHQHq_IrJ4xR_SEoPSo9n_2WNFU,
28
28
  halib/online/gdrive_test.py,sha256=hMWzz4RqZwETHp4GG4WwVNFfYvFQhp2Boz5t-DqwMo0,1342
29
29
  halib/online/projectmake.py,sha256=Zrs96WgXvO4nIrwxnCOletL4aTBge-EoF0r7hpKO1w8,4034
30
30
  halib/research/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
31
+ halib/research/base_config.py,sha256=AIjVzl2ZJ9b8yIGb2X5EZwLmyGJ_9wNWqrib1nU3Wj0,2831
32
+ halib/research/base_exp.py,sha256=mIyxVDKb40lngrEgW3_hUV5KPon25nxQeaWCUU_AWjQ,3185
31
33
  halib/research/benchquery.py,sha256=FuKnbWQtCEoRRtJAfN-zaN-jPiO_EzsakmTOMiqi7GQ,4626
32
34
  halib/research/dataset.py,sha256=QU0Hr5QFb8_XlvnOMgC9QJGIpwXAZ9lDd0RdQi_QRec,6743
33
35
  halib/research/metrics.py,sha256=Xgv0GUGo-o-RJaBOmkRCRpQJaYijF_1xeKkyYU_Bv4U,5249
@@ -49,8 +51,8 @@ halib/utils/gpu_mon.py,sha256=vD41_ZnmPLKguuq9X44SB_vwd9JrblO4BDzHLXZhhFY,2233
49
51
  halib/utils/listop.py,sha256=Vpa8_2fI0wySpB2-8sfTBkyi_A4FhoFVVvFiuvW8N64,339
50
52
  halib/utils/tele_noti.py,sha256=-4WXZelCA4W9BroapkRyIdUu9cUVrcJJhegnMs_WpGU,5928
51
53
  halib/utils/video.py,sha256=ZqzNVPgc1RZr_T0OlHvZ6SzyBpL7O27LtB86JMbBuR0,3059
52
- halib-0.1.67.dist-info/licenses/LICENSE.txt,sha256=qZssdna4aETiR8znYsShUjidu-U4jUT9Q-EWNlZ9yBQ,1100
53
- halib-0.1.67.dist-info/METADATA,sha256=Zk22ct5W95qBzGkz0tNepuAdfUwPJTbVO7Nb4L_hFTQ,5541
54
- halib-0.1.67.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
55
- halib-0.1.67.dist-info/top_level.txt,sha256=7AD6PLaQTreE0Fn44mdZsoHBe_Zdd7GUmjsWPyQ7I-k,6
56
- halib-0.1.67.dist-info/RECORD,,
54
+ halib-0.1.70.dist-info/licenses/LICENSE.txt,sha256=qZssdna4aETiR8znYsShUjidu-U4jUT9Q-EWNlZ9yBQ,1100
55
+ halib-0.1.70.dist-info/METADATA,sha256=C5ei-WhAmt6SuG-vR8pIQd2Uat5HrlT20RX9JP3D0Q4,5706
56
+ halib-0.1.70.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
57
+ halib-0.1.70.dist-info/top_level.txt,sha256=7AD6PLaQTreE0Fn44mdZsoHBe_Zdd7GUmjsWPyQ7I-k,6
58
+ halib-0.1.70.dist-info/RECORD,,
File without changes