file_handler.py 1.16 KB
Newer Older
1
2
3
4
5
6
7
8
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""Utilities for file."""

from pathlib import Path
from datetime import datetime

9
import yaml
10
from omegaconf import OmegaConf
11

12
13
14

def create_output_dir():
    """Create a new output directory.
15
16
17
18
19
20
21

    Generate a new output directory name based on current time and create it on filesystem.

    Returns:
        str: Output directory name.
    """
    output_name = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
Yifan Xiong's avatar
Yifan Xiong committed
22
    output_path = Path('.', 'outputs', output_name).resolve()
23
24
25
26
    output_path.mkdir(mode=0o755, parents=True, exist_ok=True)
    return str(output_path)


27
def get_sb_config(config_file):
28
29
30
31
32
33
34
35
    """Read SuperBench config yaml.

    Read config file, use default config if None is provided.

    Args:
        config_file (str): config file path.

    Returns:
36
        OmegaConf: Config object, None if file does not exist.
37
    """
38
39
    default_config_file = Path(__file__).parent / '../../config/default.yaml'
    p = Path(config_file) if config_file else default_config_file
40
41
    if not p.is_file():
        return None
42
43
    with p.open() as fp:
        return OmegaConf.create(yaml.load(fp, Loader=yaml.SafeLoader))