Skip to content

Commit

Permalink
Merge branch 'main' into ensemble_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaolong0728 committed Dec 2, 2024
2 parents 6832878 + a461ace commit 6542c1d
Show file tree
Hide file tree
Showing 12 changed files with 611 additions and 4 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/publish_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ jobs:
- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
- name: Build the package
run: poetry build
- name: Validate Version
run: |
latest_version=$(curl -s https://pypi.org/pypi/views-pipeline-core/json | jq -r .info.version)
new_version=$(python -c "import toml; print(toml.load('pyproject.toml')['tool']['poetry']['version'])")
python -c "from packaging.version import parse; assert parse('$new_version') > parse('$latest_version'), 'Version must be higher than $latest_version'"
- name: Publish to PyPI
run: poetry publish --username __token__ --skip-existing --password ${{ secrets.PYPI_TOKEN }}
run: poetry publish --build --username __token__ --password ${{ secrets.PYPI_TOKEN }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from typing import Dict
from utils import utils_script_gen
from pathlib import Path
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def generate(
script_dir: Path,
deployment_type: str = "shadow",
additional_settings: Dict[str, any] = None,
) -> bool:
"""
Generates a script that defines the `get_deployment_config` function for configuring the deployment status and settings.
Parameters:
script_dir (Path): The directory where the generated deployment configuration script will be saved.
This should be a valid writable path.
deployment_type (str, optional):
The type of deployment. Must be one of "shadow", "deployed", "baseline", or "deprecated".
Default is "shadow".
- "shadow": The deployment is shadowed and not yet active.
- "deployed": The deployment is active and in use.
- "baseline": The deployment is in a baseline state, for reference or comparison.
- "deprecated": The deployment is deprecated and no longer supported.
additional_settings (dict, optional):
A dictionary of additional settings to include in the deployment configuration.
These settings will be merged with the default configuration. Defaults to None.
Raises:
ValueError: If `deployment_type` is not one of the valid types.
Returns:
bool: True if the script was written and compiled successfully, False otherwise.
"""
valid_types = {"shadow", "deployed", "baseline", "deprecated"}
if deployment_type.lower() not in valid_types:
logging.error(
f"Invalid deployment_type: {deployment_type}. Must be one of {valid_types}."
)
raise ValueError(
f"Invalid deployment_type: {deployment_type}. Must be one of {valid_types}."
)

deployment_config = {"deployment_status": deployment_type.lower()}

# Merge additional settings if provided
if additional_settings and isinstance(additional_settings, dict):
deployment_config.update(additional_settings)

# Generate the script code
code = f"""\"\"\"
Deployment Configuration Script
This script defines the deployment configuration settings for the application.
It includes the deployment status and any additional settings specified.
Deployment Status:
- shadow: The deployment is shadowed and not yet active.
- deployed: The deployment is active and in use.
- baseline: The deployment is in a baseline state, for reference or comparison.
- deprecated: The deployment is deprecated and no longer supported.
Additional settings can be included in the configuration dictionary as needed.
\"\"\"
def get_deployment_config():
# Deployment settings
deployment_config = {deployment_config}
return deployment_config
"""
return utils_script_gen.save_script(script_dir, code)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from utils import utils_script_gen
from pathlib import Path


def generate(script_dir: Path) -> bool:
"""
Generates a script that defines a function for obtaining hyperparameter configurations
necessary for model training.
Parameters:
script_dir (Path):
The directory where the generated deployment configuration script will be saved.
This should be a valid writable path.
model_algorithm (str):
The architecture of the model to be used for training. This string will be included in the
hyperparameter configuration and can be modified to test different algorithms.
Returns:
bool:
True if the script was written and compiled successfully, False otherwise.
"""
code = """def get_hp_config():
hp_config = {
"steps": [*range(1, 36 + 1, 1)]
}
return hp_config
"""
return utils_script_gen.save_script(script_dir, code)
39 changes: 39 additions & 0 deletions views_pipeline_core/templates/ensemble/template_config_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from utils import utils_script_gen
from pathlib import Path


def generate(script_dir: Path, model_name: str) -> bool:
"""
Generates a script that defines the `get_meta_config` function for model metadata.
Parameters:
script_dir (Path):
The directory where the generated deployment configuration script will be saved.
This should be a valid writable path.
model_name (str):
The name of the model. This will be included in the metadata configuration.
Returns:
bool:
True if the script was written and compiled successfully, False otherwise.
"""
code = f"""def get_meta_config():
\"""
Contains the metadata for the model (model architecture, name, target variable, and level of analysis).
This config is for documentation purposes only, and modifying it will not affect the model, the training, or the evaluation.
Returns:
- meta_config (dict): A dictionary containing model meta configuration.
\"""
meta_config = {{
"name": "{model_name}", # Eg. "happy_kitten"
"models": [], # Eg. ["model1", "model2", "model3"]
"depvar": "ln_ged_sb_dep", # Eg. "ln_ged_sb_dep"
"level": "pgm", # Eg. "pgm", "cm"
"aggregation": "median", # Eg. "median", "mean"
"creator": "Your name here"
}}
return meta_config
"""
return utils_script_gen.save_script(script_dir, code)
55 changes: 55 additions & 0 deletions views_pipeline_core/templates/ensemble/template_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from utils import utils_script_gen
from pathlib import Path


def generate(script_dir: Path) -> bool:
"""
Generates a script that sets up the project paths, parses command-line arguments,
sets up logging, and executes a single model run.
Parameters:
script_dir (Path):
The directory where the generated script will be saved.
This should be a valid writable path.
Returns:
bool:
True if the script was written and compiled successfully, False otherwise.
"""
code = """import wandb
import sys
import warnings
from pathlib import Path
PATH = Path(__file__)
sys.path.insert(0, str(Path(
*[i for i in PATH.parts[:PATH.parts.index("views_pipeline") + 1]]) / "common_utils")) # PATH_COMMON_UTILS
from set_path import setup_project_paths
setup_project_paths(PATH)
from utils_cli_parser import parse_args, validate_arguments
from utils_logger import setup_logging
from execute_model_runs import execute_single_run
warnings.filterwarnings("ignore")
try:
from common_utils.ensemble_path import EnsemblePath
from common_utils.global_cache import GlobalCache
model_name = EnsemblePath.get_model_name_from_path(PATH)
GlobalCache["current_model"] = model_name
except ImportError as e:
warnings.warn(f"ImportError: {e}. Some functionalities (model separated log files) may not work properly.", ImportWarning)
except Exception as e:
warnings.warn(f"An unexpected error occurred: {e}.", RuntimeWarning)
logger = setup_logging("run.log")
if __name__ == "__main__":
wandb.login()
args = parse_args()
validate_arguments(args)
execute_single_run(args)
"""
return utils_script_gen.save_script(script_dir, code)
75 changes: 75 additions & 0 deletions views_pipeline_core/templates/model/template_config_deployment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from typing import Dict
from views_pipeline_core.templates.utils import save_script
from pathlib import Path
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def generate(
script_dir: Path,
deployment_type: str = "shadow",
additional_settings: Dict[str, any] = None,
) -> bool:
"""
Generates a script that defines the `get_deployment_config` function for configuring the deployment status and settings.
Parameters:
script_dir (Path): The directory where the generated deployment configuration script will be saved.
This should be a valid writable path.
deployment_type (str, optional):
The type of deployment. Must be one of "shadow", "deployed", "baseline", or "deprecated".
Default is "shadow".
- "shadow": The deployment is shadowed and not yet active.
- "deployed": The deployment is active and in use.
- "baseline": The deployment is in a baseline state, for reference or comparison.
- "deprecated": The deployment is deprecated and no longer supported.
additional_settings (dict, optional):
A dictionary of additional settings to include in the deployment configuration.
These settings will be merged with the default configuration. Defaults to None.
Raises:
ValueError: If `deployment_type` is not one of the valid types.
Returns:
bool: True if the script was written and compiled successfully, False otherwise.
"""
valid_types = {"shadow", "deployed", "baseline", "deprecated"}
if deployment_type.lower() not in valid_types:
logging.error(
f"Invalid deployment_type: {deployment_type}. Must be one of {valid_types}."
)
raise ValueError(
f"Invalid deployment_type: {deployment_type}. Must be one of {valid_types}."
)

deployment_config = {"deployment_status": deployment_type.lower()}

# Merge additional settings if provided
if additional_settings and isinstance(additional_settings, dict):
deployment_config.update(additional_settings)

# Generate the script code
code = f"""\"\"\"
Deployment Configuration Script
This script defines the deployment configuration settings for the application.
It includes the deployment status and any additional settings specified.
Deployment Status:
- shadow: The deployment is shadowed and not yet active.
- deployed: The deployment is active and in use.
- baseline: The deployment is in a baseline state, for reference or comparison.
- deprecated: The deployment is deprecated and no longer supported.
Additional settings can be included in the configuration dictionary as needed.
\"\"\"
def get_deployment_config():
# Deployment settings
deployment_config = {deployment_config}
return deployment_config
"""
return save_script(script_dir, code)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from views_pipeline_core.templates.utils import save_script
from pathlib import Path


def generate(script_dir: Path) -> bool:
"""
Generates a script that defines a function for obtaining hyperparameter configurations
necessary for model training.
Parameters:
script_dir (Path):
The directory where the generated deployment configuration script will be saved.
This should be a valid writable path.
Returns:
bool:
True if the script was written and compiled successfully, False otherwise.
"""
code = f"""
def get_hp_config():
\"""
Contains the hyperparameter configurations for model training.
This configuration is "operational" so modifying these settings will impact the model's behavior during the training.
Returns:
- hyperparameters (dict): A dictionary containing hyperparameters for training the model, which determine the model's behavior during the training phase.
\"""
hyperparameters = {{
'steps': [*range(1, 36 + 1, 1)],
# Add more hyperparameters as needed
}}
return hyperparameters
"""
return save_script(script_dir, code)
44 changes: 44 additions & 0 deletions views_pipeline_core/templates/model/template_config_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from views_pipeline_core.templates.utils import save_script
from pathlib import Path


def generate(script_dir: Path, model_name: str, model_algorithm: str) -> bool:
"""
Generates a script that defines the `get_meta_config` function for model metadata.
Parameters:
script_dir (Path):
The directory where the generated deployment configuration script will be saved.
This should be a valid writable path.
model_name (str):
The name of the model. This will be included in the metadata configuration.
model_algorithm (str):
The algorithm of the model. This string will also be included in the metadata configuration.
Returns:
bool:
True if the script was written and compiled successfully, False otherwise.
"""
code = f"""def get_meta_config():
\"""
Contains the meta data for the model (model algorithm, name, target variable, and level of analysis).
This config is for documentation purposes only, and modifying it will not affect the model, the training, or the evaluation.
Returns:
- meta_config (dict): A dictionary containing model meta configuration.
\"""
meta_config = {{
"name": "{model_name}",
"algorithm": "{model_algorithm}",
# Uncomment and modify the following lines as needed for additional metadata:
# "depvar": "ln_ged_sb_dep",
# "queryset": "escwa001_cflong",
# "level": "pgm",
# "creator": "Your name here"
}}
return meta_config
"""
return save_script(script_dir, code)
Loading

0 comments on commit 6542c1d

Please sign in to comment.