@@ -317,70 +317,57 @@ def from_product(cls, f, learner_type, learner_kwargs, combos):
317317 learners .append (learner )
318318 return cls (learners , cdims = arguments )
319319
320- def save (self , folder , compress = True ):
320+ def save (self , fname , compress = True ):
321321 """Save the data of the child learners into pickle files
322322 in a directory.
323323
324324 Parameters
325325 ----------
326- folder : str
327- Directory in which the learners's data will be saved.
326+ fname: callable
327+ Given a learner, returns a filename into which to save the data
328328 compress : bool, default True
329329 Compress the data upon saving using `gzip`. When saving
330330 using compression, one must load it with compression too.
331331
332- Notes
333- -----
334- The child learners need to have a 'fname' attribute in order to use
335- this method.
336-
337332 Example
338333 -------
339- >>> def combo_fname(val ):
340- ... return '__'.join([f'{k}_{v}.p' for k, v in val.items()])
341- ...
342- ... def f(x, a, b): return a * x**2 + b
343- ...
344- >>> learners = []
345- >>> for combo in adaptive.utils.named_product(a=[1, 2], b=[1]):
346- ... l = Learner1D(functools.partial(f, combo=combo ))
347- ... l.fname = combo_fname(combo) # 'a_1__b_1.p', 'a_2__b_1.p' etc.
348- ... learners.append(l)
349- ... learner = BalancingLearner(learners)
350- ... # Run the learner
351- ... runner = adaptive.Runner(learner)
352- ... # Then save
353- ... learner.save('data_folder' ) # use 'load' in the same way
334+ >>> def combo_fname(learner ):
335+ ... val = learner.function.keywords # because functools.partial
336+ ... fname = '__'.join([f'{k}_{v}.pickle' for k, v in val])
337+ ... return 'data_folder/' + fname
338+ >>>
339+ >>> def f(x, a, b): return a * x**2 + b
340+ >>>
341+ >>> learners = [ Learner1D(functools.partial(f, ** combo), (-1, 1 ))
342+ ... for combo in adaptive.utils.named_product(a=[1, 2], b=[1]]
343+ >>>
344+ >>> learner = BalancingLearner(learners)
345+ >>> # Run the learner
346+ >>> runner = adaptive.Runner(learner)
347+ >>> # Then save
348+ >>> learner.save(combo_fname ) # use 'load' in the same way
354349 """
355- if len (self .learners ) != len (set (l .fname for l in self .learners )):
356- raise RuntimeError ("The 'learner.fname's are not all unique." )
357-
358350 for l in self .learners :
359- l .save (os . path . join ( folder , l . fname ), compress = compress )
351+ l .save (fname ( l ), compress = compress )
360352
361- def load (self , folder , compress = True ):
353+ def load (self , fname , compress = True ):
362354 """Load the data of the child learners from pickle files
363355 in a directory.
364356
365357 Parameters
366358 ----------
367- folder : str
368- Directory from which the learners's data will be loaded.
359+ fname: callable
360+ Given a learner, returns a filename into which to save the data
369361 compress : bool, default True
370362 If the data is compressed when saved, one must load it
371363 with compression too.
372364
373- Notes
374- -----
375- The child learners need to have a 'fname' attribute in order to use
376- this method.
377-
378365 Example
379366 -------
380367 See the example in the `BalancingLearner.save` doc-string.
381368 """
382369 for l in self .learners :
383- l .load (os . path . join ( folder , l . fname ), compress = compress )
370+ l .load (fname ( l ), compress = compress )
384371
385372 def _get_data (self ):
386373 return [l ._get_data () for l in learner .learners ]
0 commit comments