Skip to content

file

get_experiment_filename(PREFIX)

Returns the name of the experiment.

Parameters:

Name Type Description Default
PREFIX str

Prefix of the experiment.

required

Returns:

Name Type Description
filename str

Name of the experiment.

Examples:

>>> from spotpython.utils.file import get_experiment_name
>>> from spotpython.utils.init import fun_control_init
>>> fun_control = fun_control_init(PREFIX="branin")
>>> PREFIX = fun_control["PREFIX"]
>>> filename = get_experiment_filename(PREFIX)
Source code in spotpython/utils/file.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def get_experiment_filename(PREFIX):
    """Returns the name of the experiment.

    Args:
        PREFIX (str): Prefix of the experiment.

    Returns:
        filename (str): Name of the experiment.

    Examples:
        >>> from spotpython.utils.file import get_experiment_name
        >>> from spotpython.utils.init import fun_control_init
        >>> fun_control = fun_control_init(PREFIX="branin")
        >>> PREFIX = fun_control["PREFIX"]
        >>> filename = get_experiment_filename(PREFIX)
    """
    if PREFIX is None:
        return None
    else:
        filename = "spot_" + PREFIX + "_experiment.pickle"
    return filename

get_experiment_from_PREFIX(PREFIX, return_dict=True)

Setup the experiment based on the PREFIX provided and return the relevant configuration and control objects.

Parameters:

Name Type Description Default
PREFIX str

The prefix for the experiment filename.

required
return_dict bool

Whether to return the configuration and control objects as a dictionary. If False, a tuple is returned: “(config, fun_control, design_control, surrogate_control, optimizer_control).” Defaults to True.

True

Returns:

Name Type Description
dict dict

Dictionary containing the configuration and control objects.

Example

from spotpython.utils.file import get_experiment_from_PREFIX config = get_experiment_from_PREFIX(“100”)[“config”]

Source code in spotpython/utils/file.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
def get_experiment_from_PREFIX(PREFIX, return_dict=True) -> dict:
    """
    Setup the experiment based on the PREFIX provided and return the relevant configuration
    and control objects.

    Args:
        PREFIX (str):
            The prefix for the experiment filename.
        return_dict (bool, optional):
            Whether to return the configuration and control objects as a dictionary.
            If False, a tuple is returned:
            "(config, fun_control, design_control, surrogate_control, optimizer_control)."
            Defaults to True.

    Returns:
        dict: Dictionary containing the configuration and control objects.

    Example:
        >>> from spotpython.utils.file import get_experiment_from_PREFIX
        >>> config = get_experiment_from_PREFIX("100")["config"]

    """
    experiment_name = get_experiment_filename(PREFIX)
    spot_tuner, fun_control, design_control, surrogate_control, optimizer_control = load_experiment(experiment_name)
    config = get_tuned_architecture(spot_tuner, fun_control)
    if return_dict:
        return {
            "config": config,
            "fun_control": fun_control,
            "design_control": design_control,
            "surrogate_control": surrogate_control,
            "optimizer_control": optimizer_control,
        }
    else:
        return config, fun_control, design_control, surrogate_control, optimizer_control

load_and_run_spot_python_experiment(spot_pkl_name)

Loads and runs a spot experiment.

Parameters:

Name Type Description Default
spot_pkl_name str

The name of the spot experiment file.

required

Returns:

Name Type Description
tuple tuple

A tuple containing the spot tuner, fun control, design control, surrogate control, optimizer control, and the tensorboard process object (p_popen).

Notes

p_open is deprecated and should be removed in future versions. It returns None.

Examples:

>>> from spotpython.utils.file import load_and_run_spot_python_experiment
>>> spot_tuner = load_and_run_spot_python_experiment("spot_branin_experiment.pickle")
Source code in spotpython/utils/file.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
def load_and_run_spot_python_experiment(spot_pkl_name) -> tuple:
    """Loads and runs a spot experiment.

    Args:
        spot_pkl_name (str):
            The name of the spot experiment file.

    Returns:
        tuple: A tuple containing the spot tuner, fun control,
               design control, surrogate control, optimizer control,
               and the tensorboard process object (p_popen).

    Notes:
        p_open is deprecated and should be removed in future versions.
        It returns None.

    Examples:
        >>> from spotpython.utils.file import load_and_run_spot_python_experiment
        >>> spot_tuner = load_and_run_spot_python_experiment("spot_branin_experiment.pickle")

    """
    p_open = None
    (spot_tuner, fun_control, design_control, surrogate_control, optimizer_control) = load_experiment(spot_pkl_name)
    print("\nLoaded fun_control in spotRun():")
    # pprint.pprint(fun_control)
    print(gen_design_table(fun_control))
    setup_paths(fun_control["TENSORBOARD_CLEAN"])
    spot_tuner.init_spot_writer()
    # if fun_control["tensorboard_start"]:
    #     p_open = start_tensorboard()
    # else:
    #     p_open = None
    spot_tuner.run()
    # # tensorboard --logdir="runs/"
    # stop_tensorboard(p_open)
    print(gen_design_table(fun_control=fun_control, spot=spot_tuner))
    return spot_tuner, fun_control, design_control, surrogate_control, optimizer_control, p_open

load_cifar10_data(data_dir='./data')

Loads the CIFAR10 dataset.

Parameters:

Name Type Description Default
data_dir str

Directory to save the data. Defaults to “./data”.

'./data'

Returns:

Name Type Description
trainset CIFAR10

Training dataset.

Examples:

>>> from spotpython.utils.file import load_cifar10_data
>>> trainset = load_cifar10_data(data_dir="./data")
Source code in spotpython/utils/file.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def load_cifar10_data(data_dir="./data"):
    """Loads the CIFAR10 dataset.

    Args:
        data_dir (str, optional): Directory to save the data. Defaults to "./data".

    Returns:
        trainset (torchvision.datasets.CIFAR10): Training dataset.

    Examples:
        >>> from spotpython.utils.file import load_cifar10_data
        >>> trainset = load_cifar10_data(data_dir="./data")

    """
    transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
    trainset = torchvision.datasets.CIFAR10(root=data_dir, train=True, download=True, transform=transform)
    testset = torchvision.datasets.CIFAR10(root=data_dir, train=False, download=True, transform=transform)
    return trainset, testset

load_core_model_from_file(coremodel, dirname='userModel')

Loads a core model from a python file.

Parameters:

Name Type Description Default
coremodel str

Name of the core model.

required
dirname str

Directory name. Defaults to “userModel”.

'userModel'

Returns:

Name Type Description
coremodel object

Core model.

Source code in spotpython/utils/file.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def load_core_model_from_file(coremodel, dirname="userModel"):
    """Loads a core model from a python file.

    Args:
        coremodel (str): Name of the core model.
        dirname (str, optional): Directory name. Defaults to "userModel".

    Returns:
        coremodel (object): Core model.

    """
    sys.path.insert(0, "./" + dirname)
    module = importlib.import_module(coremodel)
    core_model = getattr(module, coremodel)
    return core_model

load_dict_from_file(coremodel, dirname='userModel')

Loads a dictionary from a json file.

Parameters:

Name Type Description Default
coremodel str

Name of the core model.

required
dirname str

Directory name. Defaults to “userModel”.

'userModel'

Returns:

Name Type Description
dict dict

Dictionary with the core model.

Source code in spotpython/utils/file.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def load_dict_from_file(coremodel, dirname="userModel"):
    """Loads a dictionary from a json file.

    Args:
        coremodel (str): Name of the core model.
        dirname (str, optional): Directory name. Defaults to "userModel".

    Returns:
        dict (dict): Dictionary with the core model.

    """
    file_path = os.path.join(dirname, f"{coremodel}.json")
    if os.path.isfile(file_path):
        with open(file_path, "r") as f:
            dict_tmp = json.load(f)
            dict = dict_tmp[coremodel]
    else:
        print(f"The file {file_path} does not exist.")
        dict = None
    return dict

load_experiment(PKL_NAME=None, PREFIX=None)

Loads the experiment from a pickle file. If PKL_NAME is None and PREFIX is not None, the experiment is loaded based on the PREFIX using the get_experiment_filename function. If the spot tuner object and the fun control dictionary do not exist, an error is thrown. If the design control, surrogate control, and optimizer control dictionaries do not exist, a warning is issued and None is assigned to the corresponding variables.

Parameters:

Name Type Description Default
PKL_NAME str

Name of the pickle file. Defaults to None.

None
PREFIX str

Prefix of the experiment. Defaults to None.

None

Returns:

Name Type Description
spot_tuner object

The spot tuner object.

fun_control dict

The function control dictionary.

design_control dict

The design control dictionary.

surrogate_control dict

The surrogate control dictionary.

optimizer_control dict

The optimizer control dictionary.

Notes

The corresponding save_experiment function is part of the class spot.

Examples:

>>> from spotpython.utils.file import load_experiment
>>> spot_tuner, fun_control, design_control, _, _ = load_experiment("spot_0_experiment.pickle")
Source code in spotpython/utils/file.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
148
149
150
151
152
153
154
155
def load_experiment(PKL_NAME=None, PREFIX=None):
    """
    Loads the experiment from a pickle file.
    If PKL_NAME is None and PREFIX is not None, the experiment is loaded based on the PREFIX
    using the get_experiment_filename function.
    If the spot tuner object and the fun control dictionary do not exist, an error is thrown.
    If the design control, surrogate control, and optimizer control dictionaries do not exist, a warning is issued
    and `None` is assigned to the corresponding variables.

    Args:
        PKL_NAME (str): Name of the pickle file. Defaults to None.
        PREFIX (str, optional): Prefix of the experiment. Defaults to None.

    Returns:
        spot_tuner (object): The spot tuner object.
        fun_control (dict): The function control dictionary.
        design_control (dict): The design control dictionary.
        surrogate_control (dict): The surrogate control dictionary.
        optimizer_control (dict): The optimizer control dictionary.

    Notes:
        The corresponding save_experiment function is part of the class spot.

    Examples:
        >>> from spotpython.utils.file import load_experiment
        >>> spot_tuner, fun_control, design_control, _, _ = load_experiment("spot_0_experiment.pickle")

    """
    if PKL_NAME is None and PREFIX is not None:
        PKL_NAME = get_experiment_filename(PREFIX)
    with open(PKL_NAME, "rb") as handle:
        experiment = pickle.load(handle)
        print(f"Loaded experiment from {PKL_NAME}")
    # assign spot_tuner and fun_control only if they exist otherwise throw an error
    if "spot_tuner" not in experiment:
        raise ValueError("The spot tuner object does not exist in the pickle file.")
    if "fun_control" not in experiment:
        raise ValueError("The fun control dictionary does not exist in the pickle file.")
    spot_tuner = experiment["spot_tuner"]
    fun_control = experiment["fun_control"]
    # assign the rest of the dictionaries if they exist otherwise assign None
    if "design_control" not in experiment:
        design_control = None
        # issue a warning
        print("The design control dictionary does not exist in the pickle file. Returning None.")
    else:
        design_control = experiment["design_control"]
    if "surrogate_control" not in experiment:
        surrogate_control = None
        # issue a warning
        print("The surrogate control dictionary does not exist in the pickle file. Returning None.")
    else:
        surrogate_control = experiment["surrogate_control"]
    if "optimizer_control" not in experiment:
        # issue a warning
        print("The optimizer control dictionary does not exist in the pickle file. Returning None.")
        optimizer_control = None
    else:
        optimizer_control = experiment["optimizer_control"]
    return spot_tuner, fun_control, design_control, surrogate_control, optimizer_control

load_pickle(filename)

Loads a pickle file. Add .pkl to the filename.

Parameters:

Name Type Description Default
filename str

Name of the pickle file.

required

Returns:

Type Description
object

Loaded object.

Examples:

>>> from spotpython.utils.file import load_pickle
>>> obj = load_pickle(filename="obj.pkl")
Source code in spotpython/utils/file.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def load_pickle(filename: str):
    """Loads a pickle file.
        Add .pkl to the filename.

    Args:
        filename (str): Name of the pickle file.

    Returns:
        (object): Loaded object.

    Examples:
        >>> from spotpython.utils.file import load_pickle
        >>> obj = load_pickle(filename="obj.pkl")
    """
    filename = filename + ".pkl"
    with open(filename, "rb") as f:
        obj = pickle.load(f)
    return obj

save_pickle(obj, filename)

Saves an object as a pickle file. Add .pkl to the filename.

Parameters:

Name Type Description Default
obj object

Object to be saved.

required
filename str

Name of the pickle file.

required

Examples:

>>> from spotpython.utils.file import save_pickle
>>> save_pickle(obj, filename="obj.pkl")
Source code in spotpython/utils/file.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def save_pickle(obj, filename: str):
    """Saves an object as a pickle file.
        Add .pkl to the filename.

    Args:
        obj (object): Object to be saved.
        filename (str): Name of the pickle file.

    Examples:
        >>> from spotpython.utils.file import save_pickle
        >>> save_pickle(obj, filename="obj.pkl")
    """
    filename = filename + ".pkl"
    with open(filename, "wb") as f:
        pickle.dump(obj, f)