Skip to content

default_from_schema_no_validation

get_config_from_default_and_params(config_path=None, set_fn=None, config=None, verbose=False)

Use this function to create a valid config.json file from a schema-derived base config, a callback that sets your parameters of interest

Parameters:

Name Type Description Default
config_path string / path

Path to valid config.json

None
set_fn function

Callback that sets params with implicit schema enforcement.

None
config ReadOnlyDict

Read-only dict configuration object. Pass this XOR the config_path.

None
verbose bool

Flag to print debug statements

False

Returns:

Name Type Description
config ReadOnlyDict

read-only dict

Source code in emod_api/config/default_from_schema_no_validation.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def get_config_from_default_and_params(config_path: Union[str, os.PathLike, None] = None,
                                       set_fn=None,
                                       config: s2c.ReadOnlyDict = None,
                                       verbose: bool = False) -> s2c.ReadOnlyDict:
    """
    Use this function to create a valid config.json file from a schema-derived
    base config, a callback that sets your parameters of interest

    Parameters:
        config_path (string/path): Path to valid config.json
        set_fn (function): Callback that sets params with implicit schema enforcement.
        config: Read-only dict configuration object. Pass this XOR the config_path.
        verbose: Flag to print debug statements

    Returns:
        config: read-only dict
    """
    if not ((config_path is None) ^ (config is None)):
        raise Exception('Must specify either a default config_path or config, not neither or both.')

    if verbose:
        print(f"DEBUG: get_config_from_default_and_params invoked with "
              f"config_path: {config_path}, config: {config is not None}, {set_fn}.")

    # load default config from file if a path was given
    if config_path is not None:
        config = load_default_config_as_rod(config_path)
        if verbose:
            print("DEBUG: Calling set_fn.")

    # now that we have a config (either given or loaded from file), call the (possibly given) callback on it
    if set_fn is not None:
        config = set_fn(config)

    return config

get_default_config_from_schema(path_to_schema, schema_node=True, as_rod=False, output_filename=None)

This returns a default config object as defined from reading a schema file.

Parameters:

Name Type Description Default
output_filename str

if not None, the path to write the loaded config to

None
Source code in emod_api/config/default_from_schema_no_validation.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def get_default_config_from_schema(path_to_schema,
                                   schema_node=True,
                                   as_rod=False,
                                   output_filename=None):
    """
    This returns a default config object as defined from reading a schema file.

    Parameters:
        output_filename (str): if not None, the path to write the loaded config to
    """
    default_config = {"parameters": dict()}
    dc_param = default_config["parameters"]
    dc_param["schema"] = dict()

    with open(path_to_schema) as fid01:
        schema = json.load(fid01)

    for group in schema["config"]:
        _set_defaults_for_schema_group(dc_param, schema["config"][group], schema)

    if not schema_node:
        print("Removing schema node.")
        dc_param.pop("schema")

    if as_rod:
        default_config = json.loads(json.dumps(default_config), object_hook=s2c.ReadOnlyDict)

    if output_filename is not None:
        with open(output_filename, "w") as outfile:
            json.dump(default_config, outfile, sort_keys=True, indent=4)
        print(f"Wrote '{output_filename}' file.")

    return default_config

load_default_config_as_rod(config)

Parameters:

Name Type Description Default
config string / path

path to default or base config.json

required

Returns:

Type Description
ReadOnlyDict

config (as ReadOnlyDict) with schema ready for schema-verified param sets.

Source code in emod_api/config/default_from_schema_no_validation.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def load_default_config_as_rod(config) -> s2c.ReadOnlyDict:
    """
    Parameters:
        config (string/path): path to default or base config.json

    Returns:
        config (as ReadOnlyDict) with schema ready for schema-verified param sets.
    """
    if not os.path.exists(config):
        print(f"{config} not found.")
        return None
    config_rod = None
    with open(config) as conf:
        config_rod = json.load(conf, object_hook=s2c.ReadOnlyDict)
    return config_rod

write_config_from_default_and_params(config_path, set_fn, config_out_path, verbose=False)

Use this function to create a valid config.json file from a schema-derived base config, a callback that sets your parameters of interest, and an output path.

Parameters:

Name Type Description Default
config_path string / path

Path to valid config.json

required
set_fn function

Callback that sets params with implicit schema enforcement

required
config_out_path Union[str, PathLike]

Path to write new config.json

required
verbose bool

Flag to print debug statements

False

Returns:

Source code in emod_api/config/default_from_schema_no_validation.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
def write_config_from_default_and_params(config_path,
                                         set_fn,
                                         config_out_path: Union[str, os.PathLike],
                                         verbose: bool = False):
    """
    Use this function to create a valid config.json file from a schema-derived
    base config, a callback that sets your parameters of interest, and an output path.

    Parameters:
        config_path (string/path): Path to valid config.json
        set_fn (function): Callback that sets params with implicit schema enforcement
        config_out_path: Path to write new config.json
        verbose: Flag to print debug statements

    Returns:

    """
    if verbose:
        print(f"DEBUG: write_config_from_default_and_params invoked with {config_path}, {set_fn}, and {config_out_path}.")
    config = get_config_from_default_and_params(config_path=config_path, set_fn=set_fn)

    if verbose:
        print("DEBUG: Calling finalize.")
    config.parameters.finalize()

    if verbose:
        print("DEBUG: Writing output file.")
    with open(config_out_path, "w") as outfile:
        json.dump(config, outfile, sort_keys=True, indent=4)

write_default_from_schema(path_to_schema, output_filename='default_config.json', schema_node=True)

DEPRECATED: This function simply calls get_default_config_from_schema with specific arguments.

This function writes out a default config file as defined from reading a schema file. It's as good as the schema it's given. Note that this is designed to work with a schema from a disease-specific build, otherwise it may contain a lot of params from other disease types.

Source code in emod_api/config/default_from_schema_no_validation.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def write_default_from_schema(path_to_schema,
                              output_filename='default_config.json',
                              schema_node=True):
    """
    DEPRECATED: This function simply calls get_default_config_from_schema with specific arguments.

    This function writes out a default config file as defined from reading a schema file.
    It's as good as the schema it's given. Note that this is designed to work with a schema from
    a disease-specific build, otherwise it may contain a lot of params from other disease types.
    """
    warnings.warn("Calls to write_default_from_schema() should be updated to use get_default_config_from_schema()",
                  DeprecationWarning)
    get_default_config_from_schema(path_to_schema=path_to_schema, output_filename=output_filename,
                                   schema_node=schema_node)
    return output_filename