Skip to content

afmslicer.processing

Run AFMSlicer modules.

This module provides entry points for running AFMSlicer as a command line programme.

filter_scan(topostats_object, config=None)

Filter an image and save to ''.topostats''.

Runs just the first key step of flattening images to remove noise, tilt and optionally scars saving to .topostats for subsequent processing and analyses.

Parameters:

Name Type Description Default
topostats_object TopoStats

A TopoStats object.

required
config dict

Dictionary of configuration options for running the Filter stage.

None

Returns:

Type Description
tuple[str, bool]

Returns a tuple of topostats_object.filename and a bool of whether it successfully filtered.

Source code in src/afmslicer/processing.py
def filter_scan(
    topostats_object: TopoStats,
    config: dict[str, Any] | None = None,
) -> tuple[str, bool]:
    """
    Filter an image and save to ''.topostats''.

    Runs just the first key step of flattening images to remove noise, tilt and optionally scars saving to
    ``.topostats`` for subsequent processing and analyses.

    Parameters
    ----------
    topostats_object : TopoStats
        A TopoStats object.
    config : dict, optional
        Dictionary of configuration options for running the Filter stage.

    Returns
    -------
    tuple[str, bool]
        Returns a tuple of ``topostats_object.filename`` and a ``bool`` of whether it successfully filtered.
    """
    config = deepcopy(topostats_object.config) if config is None else config
    if "run" in config:
        config.pop("run")
    output_dir = (
        Path("output") if config["output_dir"] is None else Path(config["output_dir"])
    )
    output_dir.mkdir(exist_ok=True)
    if "filter" in config:
        filter_config = config["filter"]
        with contextlib.suppress(KeyError):
            filter_config.pop("run")
    else:
        with contextlib.suppress(KeyError):
            config.pop("output_dir")
        filter_config = config
    # Flatten Image
    try:
        filters = SlicingFilter(topostats_object, **filter_config)
        filters.filter_image()
        # Save the topostats object to .topostats file.
        # save_topostats_file(
        #     output_dir=output_dir,
        #     filename=str(topostats_object.filename),
        #     topostats_object=topostats_object,
        # )
        logger.info(f"[{topostats_object.filename}] : Filtering complete 😻")
        return (topostats_object.filename, True)
    except KeyError as e:
        raise KeyError() from e
    except Exception:  # noqa: BLE001, pylint: disable=broad-exception-caught
        logger.info(f"[{topostats_object.filename}] : Filtering failed 😿")
        return (topostats_object.filename, False)

process_scan(topostats_object, config=None)

Process a single image, filtering, slicing and calculating statistics.

Parameters:

Name Type Description Default
topostats_object dict[str, Union[NDArray, Path, float]]

A dictionary with keys 'image', 'img_path' and 'pixel_to_nm_scaling' containing a file or frames' image, it's path and it's pixel to namometre scaling value.

required
config dict

Dictionary of configuration options for processing images.

None

Returns:

Type Description
tuple(str, bool)

Tuple of filename and whether it processed correctly.

Source code in src/afmslicer/processing.py
def process_scan(
    topostats_object: TopoStats,
    config: dict[str, Any] | None = None,
) -> tuple[str, AFMSlicer]:
    """
    Process a single image, filtering, slicing and calculating statistics.

    Parameters
    ----------
    topostats_object : dict[str, Union[npt.NDArray, Path, float]]
        A dictionary with keys 'image', 'img_path' and 'pixel_to_nm_scaling' containing a file or frames' image, it's
        path and it's pixel to namometre scaling value.
    config : dict
        Dictionary of configuration options for processing images.

    Returns
    -------
    tuple(str, bool)
        Tuple of filename and whether it processed correctly.
    """
    config = deepcopy(topostats_object.config) if config is None else config
    topostats_object.filename = topostats_object.filename.split(".")[0]
    _, _ = filter_scan(topostats_object=topostats_object, config=config)
    filename, afmslicer_object = slicer_scan(
        topostats_object=topostats_object, config=config
    )
    return (filename, afmslicer_object)

slicer_scan(topostats_object, config=None)

Flatten images and save to ''.topostats''.

Runs just the first key step of flattening images to remove noise, tilt and optionally scars saving to ''.topostats'' for subsequent processing and analyses.

Parameters:

Name Type Description Default
topostats_object TopoStats

A TopoStats object.

required
config dict

Dictionary of configuration options for running the Slicing stage.

None

Returns:

Type Description
tuple[str, AFMSlicer]

Returns a tuple of the AFMSlicer.filename and the processed AFMSlicer object.

Source code in src/afmslicer/processing.py
def slicer_scan(
    topostats_object: TopoStats,
    config: dict[str, Any] | None = None,
) -> tuple[str, AFMSlicer]:
    """
    Flatten images and save to ''.topostats''.

    Runs just the first key step of flattening images to remove noise, tilt and optionally scars saving to
    ''.topostats'' for subsequent processing and analyses.

    Parameters
    ----------
    topostats_object : TopoStats
        A TopoStats object.
    config : dict
        Dictionary of configuration options for running the Slicing stage.

    Returns
    -------
    tuple[str, AFMSlicer]
        Returns a tuple of the ``AFMSlicer.filename`` and the processed ``AFMSlicer`` object.
    """
    config = deepcopy(topostats_object.config) if config is None else config
    output_dir = (
        Path("output") if config["output_dir"] is None else Path(config["output_dir"])
    )
    output_dir.mkdir(exist_ok=True)

    # Slice Image
    try:
        if isinstance(topostats_object, AFMSlicer):
            topostats_object.slice_image()
        else:
            afmslicer_object = AFMSlicer(
                image=topostats_object.image,
                image_original=topostats_object.image_original,
                filename=topostats_object.filename,
                img_path=topostats_object.img_path,
                pixel_to_nm_scaling=topostats_object.pixel_to_nm_scaling,
                **config,
                config=config,
            )
            afmslicer_object.slice_image()
        logger.info(f"[{afmslicer_object.filename}] Slicing complete 😻")
        return (afmslicer_object.filename, afmslicer_object)

        # Save the topostats object to .topostats file.
        # save_topostats_file(
        #     output_dir=output_dir,
        #     topostats_object=topostats_object,
        #     topostats_version=__release__,
        # )
    except ValidationError:
        raise
    except Exception:  # noqa: BLE001, pylint: disable=broad-exception-caught
        logger.info(f"[{topostats_object.filename}] Slicing failed 😿")
        return (topostats_object.filename, topostats_object)