"""Top-level package for TIA Toolbox."""from__future__importannotationsimportimportlib.resourcesasimportlib_resourcesimportimportlib.utilimportsysfrompathlibimportPathfromtypingimportTYPE_CHECKING,TypedDictimportyamlifTYPE_CHECKING:# pragma: no coverfromloggingimportLogRecordfromtypesimportModuleType__author__="""TIA Centre"""__email__="tialab@dcs.warwick.ac.uk"__version__="1.5.1"# This will set the tiatoolbox external data# default to be the user home folder, should work on both Window and Unix/Linux# C:\Users\USER\.tiatoolbox# /home/USER/.tiatoolbox# Initialize internal logging facilities, such that models etc.# can have reporting mechanism, may need to change protocolimportlogging# We only create a logger if root has no handler to prevent overwriting use existing# logginglogging.captureWarnings(capture=True)ifnotlogging.getLogger().hasHandlers():formatter=logging.Formatter("|%(asctime)s.%(msecs)03d| [%(levelname)s] %(message)s",datefmt="%Y-%m-%d|%H:%M:%S",)stdout_handler=logging.StreamHandler(sys.stdout)stdout_handler.setFormatter(formatter)stdout_handler.addFilter(lambdarecord:record.levelno<=logging.INFO)stderr_handler=logging.StreamHandler()stderr_handler.setFormatter(formatter)stderr_handler.setLevel(logging.WARNING)logger=logging.getLogger()# get root loggerlogger.setLevel(logging.INFO)logger.addHandler(stdout_handler)logger.addHandler(stderr_handler)else:logger=logging.getLogger()
[docs]classDuplicateFilter(logging.Filter):"""Define an object to filter duplicate logs. The DuplicateFilter filters logs to avoid printing them multiple times while running code in a loop. """
class_RcParam(TypedDict):"""All the parameters in the rcParam dictionary should be defined here."""TIATOOLBOX_HOME:Pathpretrained_model_info:dict[str,dict]
[docs]defread_registry_files(path_to_registry:str|Path)->dict:"""Reads registry files using importlib_resources. Args: path_to_registry (str or Path): Path to registry files from tiatoolbox root. Returns: Contents of yaml file. """pretrained_files_registry_path=importlib_resources.as_file(importlib_resources.files("tiatoolbox")/str(path_to_registry),)withpretrained_files_registry_pathasregistry_file_path:registry_handle=Path.open(registry_file_path)returnyaml.safe_load(registry_handle)
# runtime context parametersrcParam:_RcParam={# noqa: N816"TIATOOLBOX_HOME":Path.home()/".tiatoolbox","pretrained_model_info":read_registry_files("data/pretrained_model.yaml",),# Load a dictionary of sample files data (names and urls)}def_lazy_import(name:str,module_location:Path)->ModuleType:spec=importlib.util.spec_from_file_location(name,module_location)ifspecisNoneorspec.loaderisNone:raiseModuleNotFoundError(name=name,path=str(module_location))loader=importlib.util.LazyLoader(spec.loader)spec.loader=loadermodule=importlib.util.module_from_spec(spec)sys.modules[name]=moduleloader.exec_module(module)returnmoduleif__name__=="__main__":print("tiatoolbox version:"+str(__version__))# noqa: T201location=Path(__file__).parentannotation=_lazy_import("annotation",location)models=_lazy_import("models",location)tiatoolbox=_lazy_import("tiatoolbox",location)tools=_lazy_import("tools",location)utils=_lazy_import("utils",location)visualization=_lazy_import("visualization",location)wsicore=_lazy_import("wsicore",location)