|
3 | 3 | from contextlib import suppress |
4 | 4 | from functools import partial |
5 | 5 | from operator import itemgetter |
| 6 | +import os.path |
6 | 7 |
|
7 | 8 | import numpy as np |
8 | 9 |
|
@@ -302,3 +303,75 @@ def from_product(cls, f, learner_type, learner_kwargs, combos): |
302 | 303 | learner = learner_type(function=partial(f, **combo), **learner_kwargs) |
303 | 304 | learners.append(learner) |
304 | 305 | return cls(learners, cdims=arguments) |
| 306 | + |
| 307 | + def save(self, folder, compress=True): |
| 308 | + """Save the data of the child learners into pickle files |
| 309 | + in a directory. |
| 310 | +
|
| 311 | + Parameters |
| 312 | + ---------- |
| 313 | + folder : str |
| 314 | + Directory in which the learners's data will be saved. |
| 315 | + compress : bool, default True |
| 316 | + Compress the data upon saving using 'gzip'. When saving |
| 317 | + using compression, one must load it with compression too. |
| 318 | +
|
| 319 | + Notes |
| 320 | + ----- |
| 321 | + The child learners need to have a 'fname' attribute in order to use |
| 322 | + this method. |
| 323 | +
|
| 324 | + Example |
| 325 | + ------- |
| 326 | + >>> def combo_fname(val): |
| 327 | + ... return '__'.join([f'{k}_{v}.p' for k, v in val.items()]) |
| 328 | + ... |
| 329 | + ... def f(x, a, b): return a * x**2 + b |
| 330 | + ... |
| 331 | + >>> learners = [] |
| 332 | + >>> for combo in adaptive.utils.named_product(a=[1, 2], b=[1]): |
| 333 | + ... l = Learner1D(functools.partial(f, combo=combo)) |
| 334 | + ... l.fname = combo_fname(combo) # 'a_1__b_1.p', 'a_2__b_1.p' etc. |
| 335 | + ... learners.append(l) |
| 336 | + ... learner = BalancingLearner(learners) |
| 337 | + ... # Run the learner |
| 338 | + ... runner = adaptive.Runner(learner) |
| 339 | + ... # Then save |
| 340 | + ... learner.save('data_folder') # use 'load' in the same way |
| 341 | + """ |
| 342 | + if len(self.learners) != len(set(l.fname for l in self.learners)): |
| 343 | + raise RuntimeError("The 'learner.fname's are not all unique.") |
| 344 | + |
| 345 | + for l in self.learners: |
| 346 | + l.save(os.path.join(folder, l.fname), compress=compress) |
| 347 | + |
| 348 | + def load(self, folder, compress=True): |
| 349 | + """Load the data of the child learners from pickle files |
| 350 | + in a directory. |
| 351 | +
|
| 352 | + Parameters |
| 353 | + ---------- |
| 354 | + folder : str |
| 355 | + Directory from which the learners's data will be loaded. |
| 356 | + compress : bool, default True |
| 357 | + If the data is compressed when saved, one must load it |
| 358 | + with compression too. |
| 359 | +
|
| 360 | + Notes |
| 361 | + ----- |
| 362 | + The child learners need to have a 'fname' attribute in order to use |
| 363 | + this method. |
| 364 | +
|
| 365 | + Example |
| 366 | + ------- |
| 367 | + See the example in the 'BalancingLearner.save' doc-string. |
| 368 | + """ |
| 369 | + for l in self.learners: |
| 370 | + l.load(os.path.join(folder, l.fname), compress=compress) |
| 371 | + |
| 372 | + def _get_data(self): |
| 373 | + return [l._get_data() for l in learner.learners] |
| 374 | + |
| 375 | + def _set_data(self, data): |
| 376 | + for l, _data in zip(self.learners, data): |
| 377 | + l._set_data(_data) |
0 commit comments