66from pathlib import Path
77
88import fire
9+ import omegaconf
910import pandas as pd
1011from omegaconf import OmegaConf
1112from platformdirs import user_cache_dir
1516
1617logger = get_logger ("ConfigIndexer" )
1718
19+ cache_path = Path (user_cache_dir ("carps" )) / "index.csv"
1820
1921config_folder = Path (__file__ ).parent .parent / "configs"
2022config_folder_task = config_folder / "task"
@@ -41,23 +43,30 @@ def index_configs(extra_task_paths: list[str] | None = None, extra_optimizer_pat
4143 """
4244 register_extra_paths (extra_task_paths , extra_optimizer_paths )
4345
46+ index_list = []
4447 for path , key in PATH_KEY_ZIP .items ():
4548 paths = list (path .glob ("**/*.yaml" ))
4649
4750 table_list = []
4851 for fn in track (paths , total = len (paths ), description = f"Gathering for { key } ..." ):
4952 cfg = OmegaConf .load (fn )
50- value = cfg .get (key )
53+ try :
54+ value = cfg .get (key , None )
55+ except omegaconf .errors .InterpolationToMissingValueError :
56+ cfg_dict = OmegaConf .to_container (cfg = cfg , resolve = False )
57+ value = cfg_dict .get (key , None )
58+ if value is None :
59+ continue
5160 table_list .append (
5261 {
5362 "config_fn" : str (fn ),
5463 key : value ,
5564 }
5665 )
5766 table = pd .DataFrame (table_list )
58- _key = "task" if "task" in str ( paths [ 0 ]) else "optimizer"
59- indexpath = Path ( str ( paths [ 0 ]). split ( _key )[ 0 ] + _key )
60- table .to_csv (indexpath / "index.csv" , index = False )
67+ index_list . append ( table )
68+ index_df = pd . concat ( index_list )
69+ index_df .to_csv (cache_path , index = False )
6170
6271
6372def create_table (key : str , paths : list [Path ], target : Path ) -> None :
@@ -107,28 +116,26 @@ def register_extra_paths(extra_task_paths: list[str] | None, extra_optimizer_pat
107116 PATH_KEY_ZIP [Path (task_path_str )] = "task_id"
108117
109118
110- def get_index_config (path : Path ) -> pd .DataFrame :
111- """Index all task and optimizer configs.
112-
113- Create `index.csv` containing the config filename `config_fn` and the
114- `task_id` or `optimizer_id` for all task and optimizer configs.
115- Replaces old indexing api by using caching directory
119+ def get_index () -> pd .DataFrame :
120+ """Get index of carps compatible tasks and optimizers.
116121
117- Parameters:
118- ----------
119- path: path the old index file would have been
122+ Reads from the cache directory.
120123
121- returns: pd.DataFrame containing the index
124+ Returns:
125+ -------
126+ pd.DataFrame
127+ The index with `task_id` and `optimizer_id` with the corresponding
128+ config filename.
122129 """
123- path_dashed = str ( path . parent ). replace ( "/" , "-" )
124-
125- paths = list ( path . parent . glob ( "**/*.yaml" ))
126- paths_hash = hash_inputs ( paths )[: 12 ]
127-
128- cache_path = Path ( user_cache_dir ( "carps" )) / f"index- { path_dashed } - { paths_hash } .csv "
129- if not cache_path . is_file ():
130+ if not cache_path . is_file () or True :
131+ logger . info (
132+ f"Index file not found at { cache_path } . Reindex config. Attention! "
133+ "If you have configs in your package, manually run "
134+ "python -m carps.utils.index_configs --extra_task_paths=... "
135+ "--extra_optimizer_paths=... "
136+ )
130137 cache_path .parent .mkdir (exist_ok = True , parents = True )
131- create_table ( PATH_KEY_ZIP [ path . parent ], paths , cache_path )
138+ index_configs ( )
132139
133140 return pd .read_csv (cache_path )
134141
0 commit comments