def test_compute_marker_counts_nuc_whole_cell_diff():
    cell_mask, channel_data = test_utils.create_test_extraction_data()

    # nuclear mask is smaller
    nuc_mask = \
        np.expand_dims(erosion(cell_mask[0, :, :, 0], selem=morph.disk(1)), axis=0)
    nuc_mask = np.expand_dims(nuc_mask, axis=-1)

    unequal_masks = np.concatenate((cell_mask, nuc_mask), axis=-1)
    segmentation_masks_unequal = test_utils.make_labels_xarray(
        label_data=unequal_masks, compartment_names=['whole_cell', 'nuclear'])

    input_images = test_utils.make_images_xarray(channel_data)

    # test utils output is 4D but tests require 3D
    segmentation_masks_unequal, input_images = segmentation_masks_unequal[
        0], input_images[0]

    segmentation_output_unequal = \
        marker_quantification.compute_marker_counts(input_images=input_images,
                                                    segmentation_masks=segmentation_masks_unequal,
                                                    nuclear_counts=True)

    # make sure nuclear segmentations are smaller
    assert np.all(
        segmentation_output_unequal.loc['nuclear', :, 'cell_size'].values <
        segmentation_output_unequal.loc['whole_cell', :, 'cell_size'].values)

    # check that channel 0 counts are same as cell size
    assert np.array_equal(
        segmentation_output_unequal.loc['nuclear', :, 'cell_size'].values,
        segmentation_output_unequal.loc['nuclear', :, 'chan0'].values)

    # check that channel 1 counts are 5x cell size
    assert np.array_equal(
        segmentation_output_unequal.loc['nuclear', :, 'cell_size'].values * 5,
        segmentation_output_unequal.loc['nuclear', :, 'chan1'].values)

    # check that channel 2 counts are the same as channel 1
    assert np.array_equal(
        segmentation_output_unequal.loc['nuclear', :, 'chan2'].values,
        segmentation_output_unequal.loc['nuclear', :, 'chan1'].values)

    # check that only cell1 is negative for channel 3
    assert segmentation_output_unequal.loc['nuclear', 1, 'chan3'] == 0
    assert np.all(segmentation_output_unequal.loc['nuclear', 2:, 'chan3'] > 0)

    # check that only cell2 is positive for channel 4
    assert segmentation_output_unequal.loc['nuclear', 2, 'chan4'] > 0
    assert np.all(segmentation_output_unequal.loc['nuclear', :1, 'chan4'] == 0)
    assert np.all(segmentation_output_unequal.loc['nuclear', 3:, 'chan4'] == 0)

    # check that cell sizes are correct
    sizes = [np.sum(nuc_mask == cell_id) for cell_id in [1, 2, 3, 5]]
    assert np.array_equal(
        sizes, segmentation_output_unequal.loc['nuclear', :, 'cell_size'])

    assert np.array_equal(
        segmentation_output_unequal.loc['nuclear', :, 'cell_size'],
        segmentation_output_unequal.loc['nuclear', :, 'area'])
def test_compute_marker_counts_diff_props():
    cell_mask, channel_data = test_utils.create_test_extraction_data()

    segmentation_masks = test_utils.make_labels_xarray(
        label_data=cell_mask, compartment_names=['whole_cell'])

    # test whole_cell and nuclear compartments with same data
    segmentation_masks_equal = test_utils.make_labels_xarray(
        label_data=np.concatenate((cell_mask, cell_mask), axis=-1),
        compartment_names=['whole_cell', 'nuclear'])

    input_images = test_utils.make_images_xarray(channel_data)

    segmentation_masks_equal, input_images = segmentation_masks_equal[
        0], input_images[0]

    # different object properties can be supplied
    regionprops_features = ['label', 'area']
    excluded_defaults = ['eccentricity']

    segmentation_output_specified = \
        marker_quantification.compute_marker_counts(input_images=input_images,
                                                    segmentation_masks=segmentation_masks_equal,
                                                    nuclear_counts=True,
                                                    regionprops_features=regionprops_features)

    assert np.all(
        np.isin(['label', 'area'],
                segmentation_output_specified.features.values))

    assert not np.any(
        np.isin(excluded_defaults,
                segmentation_output_specified.features.values))

    # these nuclei are all smaller than the cells, so we should get same result
    segmentation_output_specified_split = \
        marker_quantification.compute_marker_counts(input_images=input_images,
                                                    segmentation_masks=segmentation_masks_equal,
                                                    nuclear_counts=True,
                                                    regionprops_features=regionprops_features,
                                                    split_large_nuclei=True)

    assert np.all(
        segmentation_output_specified_split == segmentation_output_specified)
def test_compute_marker_counts_base():
    cell_mask, channel_data = test_utils.create_test_extraction_data()

    segmentation_masks = test_utils.make_labels_xarray(
        label_data=cell_mask, compartment_names=['whole_cell'])

    input_images = test_utils.make_images_xarray(channel_data)

    # test utils output is 4D but tests require 3D
    segmentation_masks, input_images = segmentation_masks[0], input_images[0]

    segmentation_output = \
        marker_quantification.compute_marker_counts(input_images=input_images,
                                                    segmentation_masks=segmentation_masks)

    # check that channel 0 counts are same as cell size
    assert np.array_equal(
        segmentation_output.loc['whole_cell', :, 'cell_size'].values,
        segmentation_output.loc['whole_cell', :, 'chan0'].values)

    # check that channel 1 counts are 5x cell size
    assert np.array_equal(
        segmentation_output.loc['whole_cell', :, 'cell_size'].values * 5,
        segmentation_output.loc['whole_cell', :, 'chan1'].values)

    # check that channel 2 counts are the same as channel 1
    assert np.array_equal(
        segmentation_output.loc['whole_cell', :, 'chan2'].values,
        segmentation_output.loc['whole_cell', :, 'chan1'].values)

    # check that only cell1 is negative for channel 3
    assert segmentation_output.loc['whole_cell', 1, 'chan3'] == 0
    assert np.all(segmentation_output.loc['whole_cell', 2:, 'chan3'] > 0)

    # check that only cell2 is positive for channel 4
    assert segmentation_output.loc['whole_cell', 2, 'chan4'] > 0
    assert np.all(segmentation_output.loc['whole_cell', :1, 'chan4'] == 0)
    assert np.all(segmentation_output.loc['whole_cell', 3:, 'chan4'] == 0)

    # check that cell sizes are correct
    sizes = [np.sum(cell_mask == cell_id) for cell_id in [1, 2, 3, 5]]
    assert np.array_equal(
        sizes, segmentation_output.loc['whole_cell', :, 'cell_size'])

    # check that regionprops size matches with cell size
    assert np.array_equal(
        segmentation_output.loc['whole_cell', :, 'cell_size'],
        segmentation_output.loc['whole_cell', :, 'area'])
def test_compute_marker_counts_equal_masks():
    cell_mask, channel_data = test_utils.create_test_extraction_data()

    segmentation_masks = test_utils.make_labels_xarray(
        label_data=cell_mask, compartment_names=['whole_cell'])

    # test whole_cell and nuclear compartments with same data
    segmentation_masks_equal = test_utils.make_labels_xarray(
        label_data=np.concatenate((cell_mask, cell_mask), axis=-1),
        compartment_names=['whole_cell', 'nuclear'])

    input_images = test_utils.make_images_xarray(channel_data)

    # test utils output is 4D but tests require 3D
    segmentation_masks_equal, input_images = segmentation_masks_equal[
        0], input_images[0]

    segmentation_output_equal = \
        marker_quantification.compute_marker_counts(input_images=input_images,
                                                    segmentation_masks=segmentation_masks_equal,
                                                    nuclear_counts=True)

    assert np.all(segmentation_output_equal[0].values ==
                  segmentation_output_equal[1].values)
def test_compute_marker_counts():
    cell_mask, channel_data = _create_test_extraction_data()

    # create xarray containing segmentation mask
    coords = [range(40), range(40), ['whole_cell']]
    dims = ['rows', 'cols', 'compartments']
    segmentation_masks = xr.DataArray(np.expand_dims(cell_mask, axis=-1), coords=coords, dims=dims)

    # create xarray with channel data
    coords = [range(40), range(40), ['chan0', 'chan1', 'chan2', 'chan3', 'chan4']]
    dims = ['rows', 'cols', 'channels']
    input_images = xr.DataArray(channel_data, coords=coords, dims=dims)

    segmentation_output = \
        marker_quantification.compute_marker_counts(input_images=input_images,
                                                    segmentation_masks=segmentation_masks)

    # check that channel 0 counts are same as cell size
    assert np.array_equal(segmentation_output.loc['whole_cell', :, 'cell_size'].values,
                          segmentation_output.loc['whole_cell', :, 'chan0'].values)

    # check that channel 1 counts are 5x cell size
    assert np.array_equal(segmentation_output.loc['whole_cell', :, 'cell_size'].values * 5,
                          segmentation_output.loc['whole_cell', :, 'chan1'].values)

    # check that channel 2 counts are the same as channel 1
    assert np.array_equal(segmentation_output.loc['whole_cell', :, 'chan2'].values,
                          segmentation_output.loc['whole_cell', :, 'chan1'].values)

    # check that only cell1 is negative for channel 3
    assert segmentation_output.loc['whole_cell', :, 'chan3'][1] == 0
    assert np.all(segmentation_output.loc['whole_cell', :, 'chan3'][2:] > 0)

    # check that only cell2 is positive for channel 4
    assert segmentation_output.loc['whole_cell', :, 'chan4'][2] > 0
    assert np.all(segmentation_output.loc['whole_cell', :, 'chan4'][:2] == 0)
    assert np.all(segmentation_output.loc['whole_cell', :, 'chan4'][3:] == 0)

    # check that cell sizes are correct
    sizes = [np.sum(cell_mask == cell_id) for cell_id in [1, 2, 3, 5]]
    assert np.array_equal(sizes, segmentation_output.loc['whole_cell', 1:, 'cell_size'])

    # check that regionprops size matches with cell size
    assert np.array_equal(segmentation_output.loc['whole_cell', 1:, 'cell_size'],
                          segmentation_output.loc['whole_cell', 1:, 'area'])

    # test whole_cell and nuclear compartments with same data
    equal_masks = np.stack((cell_mask, cell_mask), axis=-1)
    # create xarray containing segmentation mask
    coords = [range(40), range(40), ['whole_cell', 'nuclear']]
    dims = ['rows', 'cols', 'compartments']
    segmentation_masks_equal = xr.DataArray(equal_masks, coords=coords, dims=dims)

    segmentation_output_equal = \
        marker_quantification.compute_marker_counts(input_images=input_images,
                                                    segmentation_masks=segmentation_masks_equal,
                                                    nuclear_counts=True)

    assert np.all(segmentation_output_equal[0].values == segmentation_output_equal[1].values)

    # test with different sized nuclear and whole_cell masks

    # nuclear mask is smaller
    nuc_mask = erosion(cell_mask, selem=morph.disk(1))

    unequal_masks = np.stack((cell_mask, nuc_mask), axis=-1)
    coords = [range(40), range(40), ['whole_cell', 'nuclear']]
    dims = ['rows', 'cols', 'compartments']
    segmentation_masks_unequal = xr.DataArray(unequal_masks, coords=coords, dims=dims)

    segmentation_output_unequal = \
        marker_quantification.compute_marker_counts(input_images=input_images,
                                                    segmentation_masks=segmentation_masks_unequal,
                                                    nuclear_counts=True)

    # make sure nuclear segmentations are smaller
    assert np.all(segmentation_output_unequal.loc['nuclear', 1:, 'cell_size'].values <
                  segmentation_output_unequal.loc['whole_cell', 1:, 'cell_size'].values)

    # check that channel 0 counts are same as cell size
    assert np.array_equal(segmentation_output_unequal.loc['nuclear', :, 'cell_size'].values,
                          segmentation_output_unequal.loc['nuclear', :, 'chan0'].values)

    # check that channel 1 counts are 5x cell size
    assert np.array_equal(segmentation_output_unequal.loc['nuclear', :, 'cell_size'].values * 5,
                          segmentation_output_unequal.loc['nuclear', :, 'chan1'].values)

    # check that channel 2 counts are the same as channel 1
    assert np.array_equal(segmentation_output_unequal.loc['nuclear', :, 'chan2'].values,
                          segmentation_output_unequal.loc['nuclear', :, 'chan1'].values)

    # check that only cell1 is negative for channel 3
    assert segmentation_output_unequal.loc['nuclear', :, 'chan3'][1] == 0
    assert np.all(segmentation_output_unequal.loc['nuclear', :, 'chan3'][2:] > 0)

    # check that only cell2 is positive for channel 4
    assert segmentation_output_unequal.loc['nuclear', :, 'chan4'][2] > 0
    assert np.all(segmentation_output_unequal.loc['nuclear', :, 'chan4'][:2] == 0)
    assert np.all(segmentation_output_unequal.loc['nuclear', :, 'chan4'][3:] == 0)

    # check that cell sizes are correct
    sizes = [np.sum(nuc_mask == cell_id) for cell_id in [1, 2, 3, 5]]
    assert np.array_equal(sizes, segmentation_output_unequal.loc['nuclear', 1:, 'cell_size'])

    assert np.array_equal(segmentation_output_unequal.loc['nuclear', 1:, 'cell_size'],
                          segmentation_output_unequal.loc['nuclear', 1:, 'area'])
예제 #6
0
io.imsave(os.path.join(plot_dir, 'Figure_4e_labels.tif'), labels)

current_DNA = io.imread(os.path.join(plot_dir, 'Figure_4e_DNA.tif'))
current_Membrane = io.imread(os.path.join(plot_dir, 'Figure_4e_Membrane.tif'))
current_label = io.imread(os.path.join(plot_dir, 'Figure_4e_labels.tif'))
current_boundary = find_boundaries(current_label, mode='inner')
DNA_xr = xr.DataArray(np.expand_dims(current_DNA, axis=-1),
                      coords=[range(512), range(512), ['DNA']],
                      dims=['rows', 'cols', 'channels'])

label_xr = xr.DataArray(np.expand_dims(current_label, axis=-1),
                        coords=[range(512),
                                range(512), ['whole_cell']],
                        dims=['rows', 'cols', 'compartments'])

marker_counts = compute_marker_counts(input_images=DNA_xr,
                                      segmentation_labels=label_xr)

overlay_img = np.full_like(current_label, 255, dtype='uint8')
overlay_img[current_label > 0] = 0

anucleated_cells = marker_counts.loc['whole_cell',
                                     marker_counts.loc['whole_cell', :,
                                                       'DNA'].values < 10,
                                     'label']

anucleated_mask = np.isin(current_label, anucleated_cells.values)

overlay_img[anucleated_mask] = 130
overlay_img[current_boundary] = 255

io.imsave(os.path.join(plot_dir, 'Figure_4e_DNA_cropped.tif'),