peng-lab/BaSiCPy

load_model fails with TypeError: unsupported operand type(s) for /: 'PosixPath' and 'ModelPrivateAttr'

Closed this issue · 1 comments

https://github.com/peng-lab/BaSiCPy/blob/f4eac9bbca2a4cffec01c26aeb2837b80327905c/src/basicpy/basicpy.py#L801C22-L801C22

load_model being a classmethod, cls._profiles_fname and cls._settings_fname are not instantiated when called, so are not recognized as strings and can't be added to Path with / op.

yfukai commented

Hi, thanks @sooheon for finding this! I believe this is already fixed in the newest version by setting the filename outside the class. I'll close this for now but please feel free to reopen this!

def save_model(self, model_dir: PathLike, overwrite: bool = False) -> None:
"""Save current model to folder.
Args:
model_dir: path to model directory
Raises:
FileExistsError: if model directory already exists
"""
path = Path(model_dir)
try:
path.mkdir()
except FileExistsError:
if not overwrite:
raise FileExistsError("Model folder already exists.")
# save settings
with open(path / _SETTINGS_FNAME, "w") as fp:
# see pydantic docs for output options
fp.write(self.json())
# NOTE emit warning if profiles are all zeros? fit probably not run
# save profiles
profiles = np.array((self.flatfield, self.darkfield))
np.save(path / _PROFILES_FNAME, profiles)
@classmethod
def load_model(cls, model_dir: PathLike) -> BaSiC:
"""Create a new instance from a model folder."""
path = Path(model_dir)
if not path.exists():
raise FileNotFoundError("Model directory not found.")
with open(path / _SETTINGS_FNAME) as fp:
model = json.load(fp)
profiles = np.load(path / _PROFILES_FNAME)
model["flatfield"] = profiles[0]
model["darkfield"] = profiles[1]
return BaSiC(**model)