Skip to content

Commit 60ec181

Browse files
author
Sunil Thaha
committed
fix(config): use predictable paths and proper typings
This modifies config to make use of proper python typing for config and to make use of predictable paths for directories Changes include: * adding new RESOURCE_DIR config for resources dir * add typing info to get_config to return proper types * fix casing to follow pep8 Signed-off-by: Sunil Thaha <[email protected]>
1 parent dbe7f55 commit 60ec181

File tree

12 files changed

+156
-125
lines changed

12 files changed

+156
-125
lines changed

src/kepler_model/estimate/estimator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import logging
33
import os
4+
import pathlib
45
import shutil
56
import signal
67
import socket
@@ -188,11 +189,11 @@ def sig_handler(signum, frame) -> None:
188189
@click.option(
189190
"--config-dir",
190191
"-c",
191-
type=click.Path(exists=False, dir_okay=True, file_okay=False),
192+
type=click.Path(exists=False, dir_okay=True, file_okay=False, path_type=pathlib.Path),
192193
default=CONFIG_PATH,
193194
required=False,
194195
)
195-
def run(log_level: str, machine_spec: str, config_dir: str) -> int:
196+
def run(log_level: str, machine_spec: str, config_dir: pathlib.Path) -> int:
196197
level = getattr(logging, log_level.upper())
197198
logging.basicConfig(
198199
level=level,

src/kepler_model/server/model_server.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import enum
33
import logging
44
import os
5+
import pathlib
56
import shutil
67
import sys
78

@@ -16,7 +17,7 @@
1617
MODEL_SERVER_MODEL_LIST_PATH,
1718
MODEL_SERVER_MODEL_REQ_PATH,
1819
download_path,
19-
getConfig,
20+
get_config,
2021
initial_pipeline_urls,
2122
model_toppath,
2223
set_config_dir,
@@ -102,7 +103,7 @@ class ModelListParam(enum.Enum):
102103

103104

104105
###########################################
105-
MODEL_SERVER_PORT = int(getConfig("MODEL_SERVER_PORT", "8100"))
106+
MODEL_SERVER_PORT = get_config("MODEL_SERVER_PORT", 8100)
106107

107108
# pipelineName and nodeCollection are global dict values set at initial state (load_init_pipeline)
108109
## pipelineName: map of energy_source to target pipeline name
@@ -190,9 +191,9 @@ def select_best_model(
190191
continue
191192
else:
192193
response = get_archived_file(valid_group_path, model_name)
193-
if not os.path.exists(response):
194+
if response.exists() is False:
194195
# archived model file does not exists
195-
logger.warning(f"archive failed: {response}")
196+
logger.warning(f"archive file for {model_name}: {response} does not exist")
196197
continue
197198
if best_cadidate is None or best_cadidate[ERROR_KEY] > metadata[ERROR_KEY]:
198199
best_cadidate = metadata
@@ -341,8 +342,7 @@ def unpack_zip_files(root_folder):
341342
zip_file_path = os.path.join(folder, file)
342343
extract_to = os.path.splitext(zip_file_path)[0] # Extract to same location as the ZIP file
343344
# Make sure the destination folder exists, if not, create it
344-
if not os.path.exists(extract_to):
345-
os.makedirs(extract_to)
345+
os.makedirs(extract_to, exist_ok=True)
346346
shutil.unpack_archive(zip_file_path, extract_to)
347347
weight_file = os.path.join(folder, file.replace(".zip", ".json"))
348348
if os.path.exists(weight_file):
@@ -435,11 +435,11 @@ def fill_machine_spec():
435435
@click.option(
436436
"--config-dir",
437437
"-c",
438-
type=click.Path(exists=False, dir_okay=True, file_okay=False),
438+
type=click.Path(exists=False, dir_okay=True, file_okay=False, path_type=pathlib.Path),
439439
default=CONFIG_PATH,
440440
required=False,
441441
)
442-
def run(log_level: str, config_dir: str) -> int:
442+
def run(log_level: str, config_dir: pathlib.Path) -> int:
443443
level = getattr(logging, log_level.upper())
444444
logging.basicConfig(
445445
level=level,

src/kepler_model/train/exporter/validator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, models_path, output_type, feature_group, energy_source, pipel
2222
self.feature_group = feature_group
2323
self.model_name = model_name
2424
self.source_model_group_path = get_model_group_path(models_path, output_type, feature_group, energy_source, assure=False, pipeline_name=pipeline_name)
25-
self.source_model_path = os.path.join(self.source_model_group_path, self.model_name)
25+
self.source_model_path = self.source_model_group_path / self.model_name
2626
self.source_model_zip = get_archived_file(self.source_model_group_path, self.model_name)
2727
self.metadata = metadata
2828
self.node_type = metadata["node_type"]

src/kepler_model/train/online_trainer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
from kepler_model.train.pipeline import NewPipeline
77
from kepler_model.train.profiler.profiler import load_all_profiles
88
from kepler_model.train.prom.prom_query import PrometheusClient
9-
from kepler_model.util.config import getConfig
9+
from kepler_model.util.config import get_config
1010
from kepler_model.util.loader import default_train_output_pipeline
1111
from kepler_model.util.prom_types import PROM_QUERY_INTERVAL, get_valid_feature_group_from_queries
1212
from kepler_model.util.train_types import FeatureGroups, PowerSourceMap
1313

14-
SAMPLING_INTERVAL = PROM_QUERY_INTERVAL
15-
SAMPLING_INTERVAL = getConfig("SAMPLING_INTERVAL", SAMPLING_INTERVAL)
16-
SAMPLING_INTERVAL = int(SAMPLING_INTERVAL)
14+
SAMPLING_INTERVAL = get_config("SAMPLING_INTERVAL", PROM_QUERY_INTERVAL)
1715

1816

1917
default_trainers = ["GradientBoostingRegressorTrainer"]

src/kepler_model/train/pipeline.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ def print_pipeline_process_end(self, energy_source, feature_group, abs_data, dyn
209209
print_bounded_multiline_message(messages)
210210

211211
def archive_pipeline(self):
212-
save_path = os.path.join(model_toppath, self.name)
212+
save_path = model_toppath / self.name
213213
archived_file = get_archived_file(model_toppath, self.name)
214-
self.print_log("archive pipeline :" + archived_file)
215-
self.print_log("save_path :" + save_path)
216-
shutil.make_archive(save_path, "zip", save_path)
214+
self.print_log(f"archive pipeline : {archived_file}")
215+
self.print_log(f"save_path : {save_path}")
216+
shutil.make_archive(str(save_path), "zip", save_path)
217217

218218

219219
def initial_trainers(trainer_names, node_level, pipeline_name, target_energy_sources, valid_feature_groups):

src/kepler_model/train/profiler/profiler.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
import json
1717
import os
18+
import pathlib
1819
from urllib.request import urlopen
1920

2021
import joblib
2122
import pandas as pd
2223

24+
from kepler_model.util import config
2325
from kepler_model.util.extract_types import component_to_col
2426
from kepler_model.util.loader import default_node_type
2527
from kepler_model.util.prom_types import generate_dataframe_from_response, node_info_column, node_info_query
@@ -30,23 +32,13 @@
3032
max_watt_key = "max_watt"
3133
unit_num_key = "#unit"
3234

33-
resource_path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "resource")
34-
default_profile_top_path = os.path.join(resource_path, "profiles")
35-
36-
if not os.path.exists(resource_path):
37-
os.mkdir(resource_path)
38-
35+
default_profile_top_path = config.RESOURCE_DIR / "profiles"
3936
profiler_registry = "https://raw.githubusercontent.com/sustainable-computing-io/kepler-model-db/main/profiles"
4037

4138

42-
def prepare_profile_path(profile_top_path):
43-
if not os.path.exists(profile_top_path):
44-
os.mkdir(profile_top_path)
45-
46-
profile_path = os.path.join(profile_top_path, "profile")
47-
if not os.path.exists(profile_path):
48-
os.mkdir(profile_path)
49-
39+
def prepare_profile_path(profile_top_path: str | pathlib.Path) -> pathlib.Path:
40+
profile_path = pathlib.Path(profile_top_path) / "profile"
41+
os.makedirs(profile_path, exist_ok=True)
5042
return profile_path
5143

5244

src/kepler_model/train/trainer/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ def archive_model(self, node_type):
238238
save_path = self._get_save_path(node_type)
239239
model_name, _ = self._model_filename(node_type)
240240
archived_file = get_archived_file(self.group_path, model_name)
241-
self.print_log("archive model :" + archived_file)
242-
self.print_log("save_path :" + save_path)
243-
shutil.make_archive(save_path, "zip", save_path)
241+
self.print_log(f"archive model : {archived_file}")
242+
self.print_log(f"save_path : {save_path}")
243+
shutil.make_archive(str(save_path), "zip", save_path)
244244
weight_dict = self.get_weight_dict(node_type)
245245
if weight_dict is not None:
246246
save_weight(save_path, weight_dict)

src/kepler_model/util/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# commonly-used definitions
2-
from .config import getConfig, model_toppath
2+
from .config import get_config, model_toppath
33
from .loader import (
44
class_to_json,
55
default_train_output_pipeline,
@@ -47,7 +47,7 @@
4747
"save_metadata",
4848
"save_scaler",
4949
"save_weight",
50-
"getConfig",
50+
"get_config",
5151
"model_toppath",
5252
"SYSTEM_FEATURES",
5353
"COUNTER_FEAUTRES",

0 commit comments

Comments
 (0)