[docs]classMethodsDatabaseQuery:""" Implements the `MethodQuery` protocol from `httomo`. """def__init__(self,module_path:str,method_name:str):self.module_path=module_pathself.method_name=method_namedef_get_method_info(self,attr:str):"""Get the information about the given method associated with `attr` that is stored in the relevant YAML file in `httomo/methods_database/packages/` Parameters ---------- module_path : str The full module path of the method, including the top-level package name. Ie, `httomolib.misc.images.save_to_images`. method_name : str The name of the method function. attr : str The name of the piece of information about the method being requested (for example, "pattern"). Returns ------- The requested piece of information about the method. """method_path=f"{self.module_path}.{self.method_name}"split_method_path=method_path.split(".")package_name=split_method_path[0]# open the library file for the packageext_package_path=""ifpackage_name!="httomo":ext_package_path=f"backends/{package_name}/"else:ext_package_path=""yaml_info_path=Path(YAML_DIR,str(ext_package_path),f"{package_name}.yaml")ifnotyaml_info_path.exists():err_str=f"The YAML file {yaml_info_path} doesn't exist."raiseFileNotFoundError(err_str)withopen(yaml_info_path,"r")asf:info=yaml.safe_load(f)forkeyinsplit_method_path[1:]:try:info=info[key]exceptKeyError:raiseKeyError(f"The key {key} is not present ({method_path})")try:returninfo[attr]exceptKeyError:raiseKeyError(f"The attribute {attr} is not present on {method_path}")
[docs]defget_pattern(self)->Pattern:p=self._get_method_info("pattern")assertpin["projection","sinogram","all"],(f"The pattern {p} that is listed for the method "f"{self.module_path}.{self.method_name} is invalid.")ifp=="projection":returnPattern.projectionifp=="sinogram":returnPattern.sinogramreturnPattern.all
[docs]defget_implementation(self)->Literal["cpu","gpu","gpu_cupy"]:p=self._get_method_info("implementation")assertpin["gpu","gpu_cupy","cpu",],f"The implementation arch {p} listed for method {self.module_path}.{self.method_name} is invalid"returnp
[docs]defget_memory_gpu_params(self,)->Optional[GpuMemoryRequirement]:p=self._get_method_info("memory_gpu")ifpisNoneorp=="None":returnNoneiftype(p)==list:# convert to dict firstd:dict=dict()foriteminp:d|=itemelse:d=preturnGpuMemoryRequirement(multiplier=d["multiplier"],method=d["method"])