|
import os |
|
import json |
|
import yaml |
|
import base64 |
|
import joblib |
|
from typing import Any |
|
from pathlib import Path |
|
from box import ConfigBox |
|
from ensure import ensure_annotations |
|
from box.exceptions import BoxValueError |
|
from kidney_classification import logger |
|
|
|
|
|
@ensure_annotations |
|
def read_yaml(path_to_yaml: Path) -> ConfigBox: |
|
"""reads yaml file and returns |
|
|
|
Args: |
|
path_to_yaml (str): path like input |
|
|
|
Raises: |
|
ValueError: if yaml file is empty |
|
e: empty file |
|
|
|
Returns: |
|
ConfigBox: ConfigBox type |
|
""" |
|
try: |
|
with open(path_to_yaml) as yaml_file: |
|
content = yaml.safe_load(yaml_file) |
|
logger.info(f"yaml file: {path_to_yaml} loaded successfully") |
|
return ConfigBox(content) |
|
except BoxValueError: |
|
raise ValueError("yaml file is empty") |
|
except Exception as e: |
|
raise e |
|
|
|
|
|
@ensure_annotations |
|
def create_directories(path_to_directories: list, verbose=True): |
|
"""create list of directories |
|
|
|
Args: |
|
path_to_directories (list): list of path of directories |
|
ignore_log (bool, optional): ignore if multiple dirs is to be created. Defaults to False. |
|
""" |
|
for path in path_to_directories: |
|
if not os.path.exists(path): |
|
os.makedirs(path) |
|
if verbose: |
|
logger.info(f"created directory at: {path}") |
|
else: |
|
continue |
|
|
|
|
|
def decodeImage(imgstring, fileName): |
|
imgdata = base64.b64decode(imgstring) |
|
with open(fileName, "wb") as f: |
|
f.write(imgdata) |
|
f.close() |
|
|
|
|
|
def encodeImageIntoBase64(croppedImagePath): |
|
with open(croppedImagePath, "rb") as f: |
|
return base64.b64encode(f.read()) |
|
|
|
|
|
@ensure_annotations |
|
def save_json(path: Path, data: dict): |
|
"""save json data |
|
|
|
Args: |
|
path (Path): path to json file |
|
data (dict): data to be saved in json file |
|
""" |
|
with open(path, "w") as f: |
|
json.dump(data, f, indent=4) |
|
|
|
logger.info(f"json file saved at: {path}") |
|
|