def _delete_filedirs(self): if self.delete_all_previous_epochs_files: to_delete_filedirs = [ f for f in files.get_filedirs(self.complete_save_roodir) if files.get_dict_from_filedir(f)['id'] == self.id ] if len(to_delete_filedirs) > 0: epochs_to_delete = [ int(files.get_dict_from_filedir(f)['epoch']) for f in to_delete_filedirs ] prints.print_red( f'> (id={self.id}) deleting previous epochs={epochs_to_delete} in={self.complete_save_roodir}' ) files.delete_filedirs(to_delete_filedirs, verbose=0)
def load_model( self, target_id: str = None, target_epoch: int = None, ): if target_id is None: target_id = self.id if not target_id == self.id: self.id = target_id filedirs = files.get_filedirs(self.complete_save_roodir, fext=C_.SAVE_FEXT) if len(filedirs) == 0: prints.print_red( f'*** no files in {self.complete_save_roodir} ***') raise Exception(f'*** no files in {self.complete_save_roodir} ***') return False if target_epoch is None: # seach the last epoch with that id filedics = [ files.get_dict_from_filedir(filedir) for filedir in filedirs ] epochs = [ int(filedic['epoch']) for filedic in filedics if filedic['id'] == target_id ] if len(epochs) == 0: prints.print_red( f'*** no files with id {target_id} in {self.complete_save_roodir} ***' ) raise Exception( f'*** no files with id {target_id} in {self.complete_save_roodir} ***' ) return False epochs = sorted(epochs) target_epoch = epochs[-1] to_load_filedir = f'{self.complete_save_roodir}/id{C_.KEY_VALUE_SEP_CHAR}{target_id}{C_.KEY_KEY_SEP_CHAR}epoch{C_.KEY_VALUE_SEP_CHAR}{target_epoch}.{C_.SAVE_FEXT}' prints.print_blue(f'> loading model={to_load_filedir}') loaded_dic = torch.load(to_load_filedir, map_location=self.device) self.model.load_state_dict(loaded_dic['state_dict']) for lmonitor in self.lmonitors: #lmonitor.history_dict = loaded_dic['lmonitors'][lmonitor.name] pass return self.model
import fuzzytools.lists as lists from fuzzytools.files import load_pickle, save_pickle, get_dict_from_filedir from fuzzytools.cuteplots.utils import save_fig import matplotlib.pyplot as plt from lchandler.plots.lc import plot_lightcurve from fuzzytools.files import save_time_stamp methods = [ 'linear-fstw', 'bspline-fstw', 'spm-mle-fstw', 'spm-mle-estw', 'spm-mcmc-fstw', 'spm-mcmc-estw' ] if main_args.method == '.' else main_args.method methods = [methods] if isinstance(methods, str) else methods for method in methods: filedir = f'../../surveys-save/survey=alerceZTFv7.1~bands=gr~mode=onlySNe~method={method}.splcds' filedict = get_dict_from_filedir(filedir) rootdir = filedict['_rootdir'] cfilename = filedict['_cfilename'] lcdataset = load_pickle(filedir) lcset_info = lcdataset['raw'].get_info() print(lcdataset) lcset_names = lcdataset.get_lcset_names() for lcset_name in lcset_names: lcset = lcdataset[lcset_name] for lcobj_name in lcset.get_lcobj_names(): print( f'method={method} - lcset_name={lcset_name} - lcobj_name={lcobj_name}' ) figsize = (12, 5) fig, ax = plt.subplots(1, 1, figsize=figsize)