Ejemplo n.º 1
0
def test_output_schema_element():
    """test that attempts to keep the TypedDict and subschema element in sync
    """
    s = DenseROI(id=1,
                 x=23,
                 y=34,
                 width=128,
                 height=128,
                 valid_roi=True,
                 mask_matrix=[[True, True], [False, True]],
                 max_correction_up=12,
                 max_correction_down=12,
                 max_correction_left=12,
                 max_correction_right=12,
                 mask_image_plane=0,
                 exclusion_labels=['small_size', 'motion_border'])

    # does this example have exactly the keys specified in DenseROI?
    assert set(list(s.keys())) == set(list(DenseROI.__annotations__.keys()))

    # can't really validate the above, but we can check against our
    # output schema
    # validate the object with a marshmallow load()
    subschema = DenseROISchema()
    subschema.load(s)
    assert subschema.dump(s) == s
def test_morphological_transform(mask_matrix, expect_None):
    d = DenseROI(id=1,
                 x=23,
                 y=34,
                 width=128,
                 height=128,
                 valid_roi=True,
                 mask_matrix=mask_matrix,
                 max_correction_up=12,
                 max_correction_down=12,
                 max_correction_left=12,
                 max_correction_right=12,
                 mask_image_plane=0,
                 exclusion_labels=['small_size', 'motion_border'])
    if expect_None:
        assert rois_utils.morphological_transform(d, (50, 50)) is None
        return

    morphed = rois_utils.morphological_transform(d, (50, 50))
    DenseROISchema().validate(morphed)

    for k in ['id', 'valid_roi', 'mask_image_plane', 'exclusion_labels']:
        assert morphed[k] == d[k]
    for k in d.keys():
        if 'max_correction' in k:
            assert morphed[k] == d[k]
def test_dense_to_extract():
    d = DenseROI(id=1,
                 x=23,
                 y=34,
                 width=128,
                 height=128,
                 valid_roi=True,
                 mask_matrix=[[True, True], [False, True]],
                 max_correction_up=12,
                 max_correction_down=12,
                 max_correction_left=12,
                 max_correction_right=12,
                 mask_image_plane=0,
                 exclusion_labels=['small_size', 'motion_border'])
    e = rois_utils.dense_to_extract(d)
    for k in ['id', 'x', 'y', 'width', 'height']:
        assert e[k] == d[k]
    assert e['mask'] == d['mask_matrix']
    assert e['valid'] == d['valid_roi']
Ejemplo n.º 4
0
def _coo_mask_to_LIMS_compatible_format(
        coo_mask: coo_matrix) -> Union[DenseROI, None]:
    """
    This functions transforms ROI mask data from COO format
    to the LIMS expected format.
    Parameters
    ----------
    coo_mask: coo_matrix
        The coo roi matrix to be converted

    Returns
    -------
    DenseROI
       or None if the coo_mask is empty

    """
    bounds = roi_bounds(coo_mask)
    if bounds is None:
        return None

    height = bounds[1] - bounds[0]
    width = bounds[3] - bounds[2]
    mask_matrix = crop_roi_mask(coo_mask).toarray()
    mask_matrix = np.array(mask_matrix, dtype=bool)
    compatible_roi = DenseROI(
        x=int(bounds[2]),
        y=int(bounds[0]),
        width=int(width),
        height=int(height),
        mask_matrix=mask_matrix.tolist(),
        # following are placeholders
        valid_roi=True,
        mask_image_plane=0,
        exclusion_labels=[],
        id=-1,
        max_correction_up=-1,
        max_correction_down=-1,
        max_correction_left=-1,
        max_correction_right=-1)
    return compatible_roi
def mock_compatible_path(tmp_path):
    cpath = tmp_path / "myrois.json"
    rois = []
    for i in range(10):
        rois.append(
            DenseROI(id=i + 1,
                     x=23,
                     y=34,
                     width=128,
                     height=128,
                     valid_roi=True,
                     mask_matrix=[[True, True], [False, True]],
                     max_correction_up=12,
                     max_correction_down=12,
                     max_correction_left=12,
                     max_correction_right=12,
                     mask_image_plane=0,
                     exclusion_labels=['small_size', 'motion_border']))

    with open(cpath, "w") as f:
        json.dump(rois, f)
    yield cpath