IBW Modules

For decoding and loading .ibw AFM file format into Python Numpy arrays.

get_ibw_channels(file_path: Path | str) -> list[str]

Extract a list of available channels and their corresponding dictionary key ids from the .ibw file.

Parameters:

Name Type Description Default
file_path Path or str

Path to the .ibw file.

required

Returns:

Type Description
list

List of available channels.

Source code in AFMReader/ibw.py
def get_ibw_channels(file_path: Path | str) -> list[str]:
    """
    Extract a list of available channels and their corresponding dictionary key ids from the `.ibw` file.

    Parameters
    ----------
    file_path : Path or str
        Path to the .ibw file.

    Returns
    -------
    list
        List of available channels.
    """
    file_path = Path(file_path)
    filename = file_path.stem
    scan = binarywave.load(file_path)
    logger.info(f"[{filename}] : Getting channels from : {file_path}")
    labels = []
    for label_list in scan["wave"]["labels"]:
        for label in label_list:
            if label:
                labels.append(label.decode())
    return labels

load_ibw(file_path: Path | str, channel: str) -> AFMScan

Load image from Asylum Research (Igor) .ibw files.

Parameters:

Name Type Description Default
file_path Path | str

Path to the .ibw file.

required
channel str

The channel to extract from the .ibw file.

required

Returns:

Type Description
AFMScan

An AFMScan object containing the image, its pixel to nanometre scaling value, and z-axis units.

Raises:

Type Description
FileNotFoundError

If the file is not found.

ValueError

If the channel is not found in the .ibw file.

Examples:

Load the image and pixel to nanometre scaling factor - 'HeightTracee' is the default channel name (the extra 'e' is not a typo!).

>>> from AFMReader.ibw import load_ibw
>>> afm_scan = load_ibw(file_path="./my_ibw_file.ibw", channel="HeightTracee")
>>> image = afm_scan.image
>>> pixel_to_nanometre_scaling_factor = afm_scan.pixel_to_nanometre_scaling
>>> z_units = afm_scan.z_units
Source code in AFMReader/ibw.py
def load_ibw(file_path: Path | str, channel: str) -> AFMScan:
    """
    Load image from Asylum Research (Igor) .ibw files.

    Parameters
    ----------
    file_path : Path | str
        Path to the .ibw file.
    channel : str
        The channel to extract from the .ibw file.

    Returns
    -------
    AFMScan
        An AFMScan object containing the image, its pixel to nanometre scaling value, and z-axis units.

    Raises
    ------
    FileNotFoundError
        If the file is not found.
    ValueError
        If the channel is not found in the .ibw file.

    Examples
    --------
    Load the image and pixel to nanometre scaling factor - 'HeightTracee' is the default channel name
    (the extra 'e' is not a typo!).

    >>> from AFMReader.ibw import load_ibw
    >>> afm_scan = load_ibw(file_path="./my_ibw_file.ibw", channel="HeightTracee")
    >>> image = afm_scan.image
    >>> pixel_to_nanometre_scaling_factor = afm_scan.pixel_to_nanometre_scaling
    >>> z_units = afm_scan.z_units
    """
    logger.info(f"Loading image from : {file_path}")
    file_path = Path(file_path)
    filename = file_path.stem
    # Check the file exists and raise an error if not
    if not file_path.is_file():
        logger.error(f"[{filename}] File not found : {file_path}")
        raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), file_path)
    try:
        scan = binarywave.load(file_path)
        logger.info(f"[{filename}] : Loaded image from : {file_path}")
        labels = []
        for label_list in scan["wave"]["labels"]:
            for label in label_list:
                if label:
                    labels.append(label.decode())
        channel_idx = labels.index(channel)

        pixel_to_nanometre_scaling, z_units = _ibw_pixel_to_nm_scaling(scan=scan, channel=channel)

        if z_units == "m":
            image = scan["wave"]["wData"][:, :, channel_idx].T * 1e9  # Convert from m to nm
            z_units = "nm"
        else:
            image = scan["wave"]["wData"][:, :, channel_idx].T
        image = np.flipud(image)
        logger.info(f"[{filename}] : Extracted channel {channel}")
    except FileNotFoundError:
        logger.error(f"[{filename}] File not found : {file_path}")
        raise
    except ValueError as e:
        logger.error(f"[{filename}] : '{channel}' not in {file_path.suffix} channel list: {labels}")
        raise ValueError(f"'{channel}' not in {file_path.suffix} channel list: {labels}") from e
    except Exception as e:
        logger.error(f"[{filename}] : {e}")
        raise e

    logger.info(f"[{filename}] : Extracted image.")
    return AFMScan(image=image, pixel_to_nanometre_scaling=pixel_to_nanometre_scaling, z_units=z_units)