示例#1
0
    def get_step_parents(self, pipeline):
        """Return the possible step-parents associated with the parent"""
        step_parents = set()

        parent_name = self.x_name.value

        for module in pipeline.modules():
            if module.module_num == self.module_num:
                return list(step_parents)

            # Objects that are the parent of the parents
            grandparents = module.get_measurements(
                pipeline, parent_name, cellprofiler_core.measurement.C_PARENT)

            step_parents.update(grandparents)

            # Objects that are the children of the parents
            siblings = module.get_measurements(
                pipeline, parent_name,
                cellprofiler_core.measurement.C_CHILDREN)

            for sibling in siblings:
                match = re.match("^([^_]+)_Count", sibling)

                if match is not None:
                    sibling_name = match.groups()[0]

                    if parent_name in module.get_measurements(
                            pipeline, sibling_name,
                            cellprofiler_core.measurement.C_PARENT):
                        step_parents.add(sibling_name)

        return list(step_parents)
示例#2
0
def test_get_measurements():
    workspace, module = make_workspace(dict(image=numpy.zeros((20, 10), bool)),
                                       dict(image=numpy.zeros((20, 10), bool)))
    for wants_emd, features in (
        (
            True,
            list(cellprofiler.modules.measureimageoverlap.FTR_ALL) + [
                cellprofiler.modules.measureimageoverlap.
                FTR_EARTH_MOVERS_DISTANCE
            ],
        ),
        (False, cellprofiler.modules.measureimageoverlap.FTR_ALL),
    ):
        module.wants_emd.value = wants_emd
        mnames = module.get_measurements(
            workspace.pipeline,
            "Image",
            cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP,
        )
        assert len(mnames) == len(features)
        assert all(n in features for n in mnames)
        assert all(f in mnames for f in features)
        mnames = module.get_measurements(
            workspace.pipeline,
            "Foo",
            cellprofiler.modules.measureimageoverlap.C_IMAGE_OVERLAP,
        )
        assert len(mnames) == 0
        mnames = module.get_measurements(workspace.pipeline, "Image", "Foo")
        assert len(mnames) == 0
示例#3
0
    def validate_module(self, pipeline):
        """Validate the module's settings

        Relate will complain if the children and parents are related
        by a prior module or if a step-parent is named twice"""
        for module in pipeline.modules():
            if module == self:
                break

            parent_features = module.get_measurements(pipeline,
                                                      self.y_name.value,
                                                      "Parent")

            if self.x_name.value in parent_features:
                raise cellprofiler_core.setting.ValidationError(
                    "{} and {} were related by the {} module".format(
                        self.y_name.value, self.x_name.value,
                        module.module_name),
                    self.x_name,
                )

        if self.has_step_parents and self.wants_step_parent_distances:
            step_parents = set()
            for group in self.step_parent_names:
                if group.step_parent_name.value in step_parents:
                    raise cellprofiler_core.setting.ValidationError(
                        "{} has already been chosen".format(
                            group.step_parent_name.value),
                        group.step_parent_name,
                    )

                step_parents.add(group.step_parent_name.value)
示例#4
0
def test_metadata_row_and_column_and_well():
    csv_text = """"Metadata_Row","Metadata_Column","Metadata_Well"
"C","03","B14"
"""
    pipeline, module, filename = make_pipeline(csv_text)
    columns = module.get_measurement_columns(pipeline)
    assert any([
        c[0] == cellprofiler_core.measurement.IMAGE and c[1] == "Metadata_Row"
        and c[2] == "varchar(1)" for c in columns
    ])
    assert any([
        c[0] == cellprofiler_core.measurement.IMAGE
        and c[1] == "Metadata_Column" and c[2] == "varchar(2)" for c in columns
    ])
    assert any([
        c[0] == cellprofiler_core.measurement.IMAGE and c[1] == "Metadata_Well"
        and c[2] == "varchar(3)" for c in columns
    ])
    m = pipeline.run()
    features = module.get_measurements(
        pipeline,
        cellprofiler_core.measurement.IMAGE,
        cellprofiler_core.measurement.C_METADATA,
    )
    for feature, expected in (("Row", "C"), ("Column", "03"), ("Well", "B14")):
        assert feature in features
        value = m.get_current_image_measurement("_".join(
            (cellprofiler_core.measurement.C_METADATA, feature)))
        assert value == expected
    def test_means_of_distances(self):
        #
        # Regression test of issue #1409
        #
        # Make sure means of minimum and mean distances of children
        # are recorded properly
        #
        i, j = numpy.mgrid[0:14, 0:30]
        #
        # Make the objects different sizes to exercise more code
        #
        parent_labels = (i >= 7) * 1 + (j >= 15) * 2 + 1
        child_labels = numpy.zeros(i.shape)
        numpy.random.seed(0)
        # Take 12 random points and label them
        child_centers = numpy.random.permutation(numpy.prod(i.shape))[:12]
        child_centers = numpy.vstack(
            (i.flatten()[child_centers], j.flatten()[child_centers]))
        child_labels[child_centers[0], child_centers[1]] = numpy.arange(1, 13)
        parent_centers = numpy.array([[3, 7], [10, 7], [3, 22], [10, 22]],
                                     float)
        parent_indexes = parent_labels[child_centers[0], child_centers[1]] - 1
        expected = numpy.sqrt(
            numpy.sum((parent_centers[parent_indexes, :] -
                       child_centers.transpose())**2, 1))

        workspace, module = self.make_workspace(parent_labels, child_labels)
        assert isinstance(module, cellprofiler.modules.relateobjects.Relate)
        module.find_parent_child_distances.value = (
            cellprofiler.modules.relateobjects.D_CENTROID)
        module.wants_per_parent_means.value = True
        mnames = module.get_measurements(
            workspace.pipeline,
            PARENT_OBJECTS,
            "_".join(
                (cellprofiler.modules.relateobjects.C_MEAN, CHILD_OBJECTS)),
        )
        assert cellprofiler.modules.relateobjects.FF_CENTROID % PARENT_OBJECTS in mnames
        feat_mean = cellprofiler.modules.relateobjects.FF_MEAN % (
            CHILD_OBJECTS,
            cellprofiler.modules.relateobjects.FF_CENTROID % PARENT_OBJECTS,
        )
        mcolumns = module.get_measurement_columns(workspace.pipeline)
        assert any(
            [c[0] == PARENT_OBJECTS and c[1] == feat_mean for c in mcolumns])
        m = workspace.measurements
        m[CHILD_OBJECTS, M_LOCATION_CENTER_X, 1] = child_centers[1]
        m[CHILD_OBJECTS, M_LOCATION_CENTER_Y, 1] = child_centers[0]
        module.run(workspace)

        v = m[PARENT_OBJECTS, feat_mean, 1]

        plabel = m[CHILD_OBJECTS, "_".join((C_PARENT, PARENT_OBJECTS)), 1, ]

        assert len(v) == 4
        for idx in range(4):
            if numpy.any(plabel == idx + 1):
                assert (round(
                    abs(v[idx] - numpy.mean(expected[plabel == idx + 1])),
                    4) == 0)
    def test_get_measurements_other_other(self):
        module = (cellprofiler_core.module.image_segmentation.
                  _object_processing.ObjectProcessing())

        module.x_name.value = "Objects"

        actual = module.get_measurements(None, "foo", "bar")

        expected = []

        assert actual == expected
    def test_get_measurements_other_other(self):
        module = (cellprofiler_core.module.image_segmentation.
                  _image_segmentation.ImageSegmentation())

        module.x_name.value = "Image"

        actual = module.get_measurements(None, "foo", "bar")

        expected = []

        assert actual == expected
    def test_get_measurements_output_object_parent(self):
        module = (cellprofiler_core.module.image_segmentation.
                  _object_processing.ObjectProcessing())

        module.x_name.value = "Objects"

        actual = module.get_measurements(
            None, "ObjectProcessing",
            cellprofiler_core.constants.measurement.C_PARENT)

        expected = ["Objects"]

        assert actual == expected
    def test_get_measurements_output_object_number(self):
        module = (cellprofiler_core.module.image_segmentation.
                  _object_processing.ObjectProcessing())

        module.x_name.value = "Objects"

        actual = module.get_measurements(
            None, "ObjectProcessing",
            cellprofiler_core.constants.measurement.C_NUMBER)

        expected = [cellprofiler_core.constants.measurement.FTR_OBJECT_NUMBER]

        assert actual == expected
示例#10
0
    def test_get_measurements_input_object_children(self):
        module = (cellprofiler_core.module.image_segmentation.
                  _object_processing.ObjectProcessing())

        module.x_name.value = "Objects"

        actual = module.get_measurements(
            None, "Objects",
            cellprofiler_core.constants.measurement.C_CHILDREN)

        expected = ["%s_Count" % "ObjectProcessing"]

        assert actual == expected
示例#11
0
    def test_get_measurements_image_count(self):
        module = (cellprofiler_core.module.image_segmentation.
                  _object_processing.ObjectProcessing())

        module.x_name.value = "Objects"

        actual = module.get_measurements(
            None,
            cellprofiler_core.constants.measurement.IMAGE,
            cellprofiler_core.constants.measurement.C_COUNT,
        )

        expected = ["ObjectProcessing"]

        assert actual == expected
    def test_get_measurements_image_count(self):
        module = (cellprofiler_core.module.image_segmentation.
                  _image_segmentation.ImageSegmentation())

        module.x_name.value = "Image"

        actual = module.get_measurements(
            None,
            cellprofiler_core.constants.measurement.IMAGE,
            cellprofiler_core.constants.measurement.C_COUNT,
        )

        expected = ["ImageSegmentation"]

        assert actual == expected
示例#13
0
    def test_get_measurements_output_object_location(self):
        module = (cellprofiler_core.module.image_segmentation.
                  _object_processing.ObjectProcessing())

        module.x_name.value = "Objects"

        actual = module.get_measurements(
            None, "ObjectProcessing",
            cellprofiler_core.constants.measurement.C_LOCATION)

        expected = [
            cellprofiler_core.constants.measurement.FTR_CENTER_X,
            cellprofiler_core.constants.measurement.FTR_CENTER_Y,
            cellprofiler_core.constants.measurement.FTR_CENTER_Z,
        ]

        assert actual == expected
示例#14
0
def test_02_get_zernike_measurements():
    module = (cellprofiler.modules.measureobjectintensitydistribution.
              MeasureObjectIntensityDistribution())
    for wants_zernikes, ftrs in (
        (
            cellprofiler.modules.measureobjectintensitydistribution.
            Z_MAGNITUDES,
            (cellprofiler.modules.measureobjectintensitydistribution.
             FF_ZERNIKE_MAGNITUDE, ),
        ),
        (
            cellprofiler.modules.measureobjectintensitydistribution.
            Z_MAGNITUDES_AND_PHASE,
            (
                cellprofiler.modules.measureobjectintensitydistribution.
                FF_ZERNIKE_MAGNITUDE,
                cellprofiler.modules.measureobjectintensitydistribution.
                FF_ZERNIKE_PHASE,
            ),
        ),
    ):
        module.wants_zernikes.value = wants_zernikes
        module.zernike_degree.value = 2

        module.images_list.value = "DNA, Cytoplasm, Actin"
        for i, object_name, center_name in (
            (0, "Nucleii", None),
            (1, "Cells", "Nucleii"),
            (2, "Cytoplasm", "Nucleii"),
        ):
            if i:
                module.add_object()
            module.objects[i].object_name.value = object_name
            if center_name is None:
                module.objects[i].center_choice.value = (
                    cellprofiler.modules.measureobjectintensitydistribution.
                    C_SELF)
            else:
                module.objects[i].center_choice.value = (
                    cellprofiler.modules.measureobjectintensitydistribution.
                    C_CENTERS_OF_OTHER)
                module.objects[i].center_object_name.value = center_name

        for object_name in "Nucleii", "Cells", "Cytoplasm":
            result = module.get_measurements(
                None,
                object_name,
                cellprofiler.modules.measureobjectintensitydistribution.
                M_CATEGORY,
            )
            for ftr in ftrs:
                assert ftr in result
                iresult = module.get_measurement_images(
                    None,
                    object_name,
                    cellprofiler.modules.measureobjectintensitydistribution.
                    M_CATEGORY,
                    ftr,
                )
                for image in "DNA", "Cytoplasm", "Actin":
                    assert image in iresult
                    sresult = module.get_measurement_scales(
                        None,
                        object_name,
                        cellprofiler.modules.
                        measureobjectintensitydistribution.M_CATEGORY,
                        ftr,
                        image,
                    )
                    for n, m in ((0, 0), (1, 1), (2, 0), (2, 2)):
                        assert "%d_%d" % (n, m) in sresult
示例#15
0
def test_01_get_measurements():
    module = (cellprofiler.modules.measureobjectintensitydistribution.
              MeasureObjectIntensityDistribution())
    module.images_list.value = "DNA, Cytoplasm, Actin"
    for i, object_name, center_name in (
        (0, "Nucleii", None),
        (1, "Cells", "Nucleii"),
        (2, "Cytoplasm", "Nucleii"),
    ):
        if i:
            module.add_object()
        module.objects[i].object_name.value = object_name
        if center_name is None:
            module.objects[i].center_choice.value = (
                cellprofiler.modules.measureobjectintensitydistribution.C_SELF)
        else:
            module.objects[i].center_choice.value = (
                cellprofiler.modules.measureobjectintensitydistribution.
                C_CENTERS_OF_OTHER)
            module.objects[i].center_object_name.value = center_name
    for i, bin_count in ((0, 4), (0, 5), (0, 6)):
        if i:
            module.add_bin_count()
        module.bin_counts[i].bin_count.value = bin_count

    for object_name in [x.object_name.value for x in module.objects]:
        assert tuple(module.get_categories(None, object_name)) == (
            cellprofiler.modules.measureobjectintensitydistribution.M_CATEGORY,
        )
        for feature in cellprofiler.modules.measureobjectintensitydistribution.F_ALL:
            assert feature in module.get_measurements(
                None,
                object_name,
                cellprofiler.modules.measureobjectintensitydistribution.
                M_CATEGORY,
            )
        for image_name in module.images_list.value:
            for (
                    feature
            ) in cellprofiler.modules.measureobjectintensitydistribution.F_ALL:
                assert image_name in module.get_measurement_images(
                    None,
                    object_name,
                    cellprofiler.modules.measureobjectintensitydistribution.
                    M_CATEGORY,
                    feature,
                )
            for bin_count in [x.bin_count.value for x in module.bin_counts]:
                for bin in range(1, bin_count + 1):
                    for (
                            feature
                    ) in cellprofiler.modules.measureobjectintensitydistribution.F_ALL:
                        assert "%dof%d" % (
                            bin,
                            bin_count,
                        ) in module.get_measurement_scales(
                            None,
                            object_name,
                            cellprofiler.modules.
                            measureobjectintensitydistribution.M_CATEGORY,
                            feature,
                            image_name,
                        )