Example #1
0
def file_handler_factory(file_path: str, database) -> FileHandlerBase:
    # Build unique class list
    file_handlers_list = get_module_classes(
        package=ipso_phen.ipapi.file_handlers,
        class_inherits_from=FileHandlerBase,
        remove_abstract=True,
    )
    file_handlers_list = [
        fh
        for fh in file_handlers_list
        if inspect.isclass(fh) and callable(getattr(fh, "probe", None))
    ]

    # Create objects
    best_score = 0
    best_class = None
    for cls in file_handlers_list:
        if cls.probe(file_path, database) > best_score:
            best_score = cls.probe(file_path, database)
            best_class = cls

    if best_class:
        return best_class(file_path=file_path, database=database)
    else:
        return FileHandlerDefault(file_path=file_path, database=database)
Example #2
0
    def __init__(self, **kwargs):
        self.ipt_list = []
        self.use_cases = [WIP_CASE]
        self._log_callback = None

        """Build list containing a single instance of all available tools"""
        # Build unique class list
        ipt_classes_list = get_module_classes(
            package=ipt,
            class_inherits_from=IptBase,
            remove_abstract=True,
        )

        # Create objects
        logger.info("Loading image processing modules")
        error_count: int = 0
        load_count: int = 0
        for cls in ipt_classes_list:
            try:
                op = cls()
                self.ipt_list.append(op)
                self.use_cases.extend(op.use_case)
            except Exception as e:
                logger.exception(f'Failed to add "{cls.__name__}" because "{repr(e)}"')
                error_count += 1
            else:
                logger.info(f'Loaded "{cls.__name__}"')
                load_count += 1
        self.use_cases = sorted(list(set(self.use_cases)))
        self.ipt_list.sort(key=lambda x: (x.order, x.name))
        if error_count == 0:
            logger.info(f"Loaded {load_count} modules")
        else:
            logger.warning(f"Loaded {load_count} modules, {error_count} errors")
Example #3
0
def get_ipt_class(class_name: str) -> Union[type, None]:
    ipt_classes_list = get_module_classes(
        package=ipt, class_inherits_from=IptBase, remove_abstract=True
    )
    for cls_ in ipt_classes_list:
        if inspect.isclass(cls_) and (cls_.__name__ == class_name):
            return cls_
    else:
        return None
Example #4
0
 def test_objects(self):
     """IPT Holder: Check objects are created"""
     for cls in get_module_classes(package=ipt,
                                   class_inherits_from=IptBase,
                                   remove_abstract=True):
         self.assertIsInstance(
             cls(),
             cls=cls,
             msg=f"Failed to create ipt of type {cls.__name__}",
         )
Example #5
0
def ipo_factory(
    file_path,
    options=None,
    force_abstract: bool = False,
    data_base=None,
    scale_factor=1,
):
    if force_abstract:
        return BaseImageProcessor(
            file_path,
            options,
            database=data_base,
            scale_factor=scale_factor,
        )
    else:
        # Build unique class list
        ipt_classes_list = get_module_classes(
            package=class_pipelines,
            class_inherits_from=BaseImageProcessor,
            remove_abstract=True,
        )

        # Create temporary image wrapper to detect experiment
        fh = file_handler_factory(file_path, data_base)

        # Select able class
        ipt_classes_list = list(set(ipt_classes_list))
        for cls in ipt_classes_list:
            if callable(getattr(cls, "can_process", None)) and cls.can_process(
                    dict(experiment=fh.experiment,
                         robot=fh.__class__.__name__)):
                return cls(
                    file_path,
                    options,
                    database=data_base,
                    scale_factor=scale_factor,
                )

        return BaseImageProcessor(
            file_path,
            options,
            database=data_base,
            scale_factor=scale_factor,
        )