niklib.configs package#

Submodules#

niklib.configs.core module#

class niklib.configs.core.JsonConfigHandler[source]#

Bases: object

Initializes a class or method based on a predefined json config file

In the highest level of configs, there are keys that contain the name of method or property of a Callable object. These needs to be passed to the parse() method of the niklib.configs.core.JsonConfigHandler class.

Here is the convention of constructing json config file:
  • name the json file itself as whatever you wish. But better to be

something that represents the class object you are trying to config. e.g. assume we have flaml_automl object hence we name the json file as example_flaml_automl_configs.json. - in highest level, if args of a method need to be defined, use method_[name_of_the_method_of_your_class]. e.g. let’s say flaml_automl has fit method, then key in json would be named method_fit. - values of each key are the arguments of that key. E.g. let’s say method_fit has args task and max_iter. Then the value for method_fit would be [YOUR_CLASS_NAME]_[ARG]: value. E.g. FLAML_AUTOML_TASK: "classification"` or FLAML_AUTOML_MAX_ITER: 100.

Here is a full example:

```json
{
“method_fit”: {

“FLAML_AUTOML_METRIC”: “log_loss”, “FLAML_AUTOML_TASK”: “classification”, “FLAML_AUTOML_MAX_ITER”: 100, “FLAML_SPLIT_RATIO”: 0.1, “FLAML_AUTOML_MEM_THRES”: 19327352832, “FLAML_AUTOML_N_CONCURRENT_TRIALS”: 1, “FLAML_AUTOML_AUTO_AUGMENT”: false, “FLAML_AUTOML_EARLY_STOP”: false, “FLAML_AUTOML_VERBOSE”: 5

}

}

```

After defining this file, you can added it as a constant to the root of the package hosting the class for this config. E.g. this example was for flaml model which is in niklib.models.trainers.aml_flaml. So, you can fill niklib.models.trainers.__init__ with a constant to the path of this json. In the end, you can using this way:

>>> from niklib.configs import JsonConfigHandler
>>> from niklib.models.trainers.aml_flaml import AutoML
>>> configs = JsonConfigHandler.parse(EXAMPLE_FLAML_AUTOML_CONFIGS, AutoML)
>>> configs['method_fit']['task']
classification
>>> configs['method_fit']['max_iter']
100

Note

This class has hardcoded and customized definitions for different target values which can be found in the each packages data dir such as niklib.models.trainers.EXAMPLE_FLAML_AUTOML_CONFIGS.

__init__()[source]#
static load(filename)[source]#
Return type:

Any

parse(filename, target)[source]#

Takes a json file path and parse it for a particular class or method

Note

See class description about valid json configs and the way they are constructed.

Parameters:
  • filename (Union[Path, str]) – Path to JSON file

  • target (str) – Target class or method name to parse the configs for

Raises:

ValueError – If the target class or method is not supported or implemented

Returns:

A dictionary containing the configs for each possible method or property of the target class or method.

Return type:

dict

as_mlflow_artifact(target_path)[source]#

Saves the configs to the MLFlow artifact directory

Parameters:

target_path (Union[Path, str]) – Path to the MLFlow artifact directory. The name of the file will be same as original config file, hence, only provide path to dir.

Return type:

None

Module contents#

niklib.configs.SAMPLE = PosixPath('/home/runner/work/niklib/niklib/niklib/configs/data/sample.csv')#

Some sample data for demo

Note

The data has been obtained from http://example.com