Example #1
0
def fit2d_save(mask, filename, dir_path=None):
    """
    Compresses and wraps the mask for Fit2D use
    Parameters
    ----------
    mask: ndarray
        The mask
    filename: str
        The filename
    dir_path: str, optional
        Path to the destination file
    """
    saver = Fit2dMaskImage(data=~mask)
    saver.write(_create_file_path(dir_path, filename, '.msk'))
Example #2
0
def fit2d_save(mask, filename, dir_path=None):
    """
    Compresses and wraps the mask for Fit2D use
    Parameters
    ----------
    mask: ndarray
        The mask
    filename: str
        The filename
    dir_path: str, optional
        Path to the destination file
    """
    # Fit2d works with inverted masks
    mask = np.flipud(mask)
    mask = np.abs(mask - 1)
    b1, b2 = mask.shape
    b28 = b2 // 8
    c = np.zeros((b1, b28), dtype=np.uint8)
    # bitmap compression for Fit2D io
    # TODO: possibly parallizable
    for i in range(0, b1):
        j = 0
        while j < b2:
            c[i, math.floor(j / 8)] = mask[i, j]
            for k in range(1, 8):
                c[i, math.floor(j / 8)] += mask[i, j + k] * (2 ** k)
            j += 8
    maskname = _create_file_path(dir_path, filename, '.msk')
    with open(maskname, "wb") as fout:
        c.tofile(fout, "", "%x")
    # Giant long mask header, must be there for fit2d to work with the mask
    with open(maskname, 'rb') as mask:
        bodypt1 = mask.read(262144)
        bodypt2 = mask.read()

    body = [bodypt1, bodypt2]
    body2 = b"".join(body)
    total = [header, body2]
    total2 = b"".join(total)
    with open(maskname, "wb") as fout:
        fout.write(total2)