def load_checkpoint(ckpt_dir, epoch=None): if ckpt_dir is not None: if Path(ckpt_dir).is_file(): ckpt_path = Path(ckpt_dir) elif epoch is None: ckpt_path = Path(ckpt_dir) / 'model_best.pth.tar' else: ckpt_path = Path(ckpt_dir) / 'model_best_epoch{}.pth.tar'.format( epoch) else: raise ValueError('ckpt_dir must be not None') if ckpt_path.exists(): print("=> loading checkpoint '{}'".format(ckpt_path)) checkpoint = torch.load(str(ckpt_path)) print 'best_score', checkpoint['best_score'] print 'arch', checkpoint['arch'] print 'epoch', checkpoint['epoch'] if 'cur_score' in checkpoint: print 'cur_score', checkpoint['cur_score'] print("=> loaded checkpoint '{}' (epoch {})".format( ckpt_path, checkpoint['epoch'])) if epoch is None: out_path = ckpt_path.with_name('model_best_epoch{}.pth.tar'.format( checkpoint['epoch'])) if not out_path.exists(): shutil.copy(str(ckpt_path), str(out_path)) else: raise IOError("=> no checkpoint found at '{}'".format(ckpt_path)) checkpoint['path'] = ckpt_path return checkpoint
def get_path_params(self, name, include_set = None, exclude_set = None, need_const = True, need_nonconst = True, delimiter = '_'): dir_name = self.get_joined_params(Path(), include_set, exclude_set, need_const = need_const, delimiter = delimiter) name = Path(name) name = name.with_name(self.get_joined_params(name.stem, include_set, exclude_set, need_nonconst = need_nonconst, delimiter = delimiter) + name.suffix) if dir_name == Path(): path_const_params = self.data_path / name else: path_const_params = self.data_path / 'params' / dir_name / name if not path_const_params.parent.exists(): path_const_params.parent.mkdir(parents = True, exist_ok = True) return path_const_params
def increment_filename_version(path: Path, separator=' ') -> Path: stem = path.stem try: # try to find any existing counter splitstem = stem.split(separator) # raises ValueError on missing sep if len(splitstem) < 2: raise ValueError() counter = int(splitstem[-1]) stem = separator.join(splitstem[:-1]) except (ValueError, IndexError): # not found, we start with 1 counter = 1 return path.with_name('{stem}{sep}{cnt}{suffix}'.format( stem=stem, sep=separator, cnt=(counter + 1), suffix=path.suffix))
def find_unused_filename(path: Path) -> Path: """ we assume path already exists. This function then adds a counter to the filename until we find a unused filename. """ stem = path.stem try: splitstem = stem.split(' ') if len(splitstem) < 2: raise ValueError() count = int(splitstem[-1]) stem = ' '.join(splitstem[:-1]) except (ValueError, IndexError): count = 1 while True: count += 1 tmp_path = path.with_name('%s %s%s' % (stem, count, path.suffix)) if not tmp_path.exists(): return tmp_path
class Api: def __init__(self, taskplan_config="taskplan.json"): self.taskplan_metadata_path = Path(taskplan_config) self.taskplan_metadata_path = self.taskplan_metadata_path.with_name(self.taskplan_metadata_path.stem + "_metadata" + self.taskplan_metadata_path.suffix) if Path(self.taskplan_metadata_path).exists(): with open(self.taskplan_metadata_path) as f: metadata = json.load(f) else: metadata = {"project": {}, "scheduler": {}} self.project = Project.create_from_config_file(EventManager(), metadata["project"], taskplan_config, tasks_to_load=[], slim_mode=True) def load_task(self, task_path): task = self.project._load_saved_task(Path(task_path), task_path.startswith("tests")) self.project.configuration.renew_task_config(task) return task def load_config(self, project_name, config_uuid): project = self.project.project_by_name(project_name) return project.configuration.configs_by_uuid[config_uuid] def build_config(self, data, data_has_metadata=False): if not data_has_metadata: data = {"config": data} base_configs = [] if False: for param in self.project.configuration.get_params(): if param.has_metadata("deprecated_param_value"): for param_value in self.project.configuration.get_param_values(): if param.get_metadata("deprecated_param_value") == str(param_value.uuid): base_configs.append([param_value]) if param_value.has_metadata("template_deprecated"): base_configs[-1] += param_value.get_metadata("template_deprecated") base_configs = [] config = Configuration(data, base_configs) return config
from vtkplotlib import __file__ as _init_path DATA_FOLDER = Path(_init_path).with_name("data") MODELS_FOLDER = DATA_FOLDER / "models" def get_rabbit_stl(): return str(MODELS_FOLDER / "rabbit" / "rabbit.stl") ICONS_FOLDER = DATA_FOLDER / "icons" ICONS = {i.stem: str(i) for i in ICONS_FOLDER.glob("*.jpg")} _HOOKS_DIR = DATA_FOLDER.with_name("__PyInstaller") def _get_hooks_dir(): return [str(_HOOKS_DIR)] def assert_ok(): assert ICONS_FOLDER.is_dir() assert MODELS_FOLDER.is_dir() assert Path(get_rabbit_stl()).exists() assert _HOOKS_DIR.exists() if __name__ == "__main__": assert_ok()
class PathHandler(object): def __init__(self, path, mode="r", ALLOWED_ENCODING="ascii"): self.str_path = str(path) self.path = Path(path) self.ALLOWED_ENCODING = ALLOWED_ENCODING self.folder = self.path.parent self.name = self.path.name self.access_path = self.path if mode == "r": if not self.path.exists(): raise FileNotFoundError(self.str_path + " does not exist") elif mode == "w": if not self.folder.exists(): raise FileNotFoundError("The folder {} does not exist".format( self.path.parent)) elif mode == "i": pass else: raise ValueError( "Mode must be either 'r' or 'w'. Not '{}'".format(mode)) self.mode = mode # Test both the folder and the file name to see if they are safe to use. # Strangely Python 2.7 raises a UnicodeDecodeError for encoding rather # than an UnicodeEncodeError. try: str(self.folder).encode(ALLOWED_ENCODING) self.folder_ok = True except (UnicodeEncodeError, UnicodeDecodeError): self.folder_ok = False try: self.path.name.encode(ALLOWED_ENCODING) self.name_ok = True except (UnicodeEncodeError, UnicodeDecodeError): self.name_ok = False def __enter__(self): if not self.folder_ok: # print("Warning - non-ascii folder", self.folder) print("Setting the cwd to folder") self.old_folder = Path.cwd() os.chdir(str(self.folder)) self.access_path = Path(self.access_path.name) if not self.name_ok: # print("Warning - non-ascii file name '{}'. Will give file a temporary name then change back afterwards."\ # .format(self.name)) self.access_path.suffix.encode(self.ALLOWED_ENCODING) while True: self.access_path = self.access_path.with_name( str(np.random.randint(1 << 31, )) + self.access_path.suffix) if not self.access_path.exists(): break if self.mode == "r": os.rename(self.path, self.access_path) self.access_path = str(self.access_path) return self def __exit__(self, *spam): self.access_path = Path(self.access_path) if not self.name_ok and self.access_path.exists(): print("Reverting name changes") if self.path.exists(): # If the target path already exists then os.rename raises a # FileExistsError. So if the user runs the same piece of code # twice without deleting the output it'll fail. assert self.mode == "w" os.remove(self.path) os.rename(self.access_path, self.path.name) if not self.folder_ok: print("reverting to old working dir") os.chdir(str(self.old_folder))