def test_01_01_default(self):
     s = cps.FloatRange("foo", (1, 15))
     self.assertEquals(s.min, 1)
     self.assertEquals(s.max, 15)
     self.assertEquals(s.min_text, "1.0")
     self.assertEquals(s.max_text, "15.0")
     s.test_valid(None)
 def test_02_03_good_max(self):
     s = cps.FloatRange("foo", (1, 15), maxval=20)
     for test_case in ("18", "20.00"):
         s.value_text = s.compose_max_text(test_case)
         self.assertEquals(s.max, float(test_case))
         self.assertEquals(s.max_text, test_case)
         s.test_valid(None)
 def test_00_00_init(self):
     x = cps.FloatRange("text", (1, 2), 1, 5)
     x.test_valid(None)
     self.assertEqual(x.text, "text")
     self.assertEqual(x.value, (1, 2))
     self.assertEqual(x.min, 1)
     self.assertEqual(x.max, 2)
     x.test_valid(None)
 def test_01_02_set_min(self):
     s = cps.FloatRange("foo", (1, 15))
     s.value_text = s.compose_min_text("2.10")
     self.assertEquals(s.min, 2.1)
     self.assertEquals(s.max, 15)
     self.assertEquals(s.min_text, "2.10")
     self.assertEquals(s.max_text, "15.0")
     s.test_valid(None)
 def test_01_05_set_max_bad(self):
     s = cps.FloatRange("foo", (1, 15))
     s.value_text = s.compose_max_text("a2")
     self.assertEquals(s.min, 1)
     self.assertEquals(s.max, 15)
     self.assertEquals(s.min_text, "1.0")
     self.assertEquals(s.max_text, "a2")
     self.assertRaises(cps.ValidationError, (lambda: s.test_valid(None)))
 def test_01_04_set_max(self):
     s = cps.FloatRange("foo", (1, 15))
     s.value_text = s.compose_max_text("016")
     self.assertEquals(s.min, 1)
     self.assertEquals(s.max, 16)
     self.assertEquals(s.min_text, "1.0")
     self.assertEquals(s.max_text, "016")
     s.test_valid(None)
 def test_02_02_neg_max(self):
     x = cps.FloatRange("text", (1, 2), 1, 5)
     x.value = (1, 6)
     self.assertRaises(ValueError, x.test_valid, None)
 def test_01_02_assign_string(self):
     x = cps.FloatRange("text", (1, 2), 1, 5)
     x.value = "2,5"
     self.assertEqual(x.min, 2)
     self.assertEqual(x.max, 5)
     x.test_valid(None)
 def test_01_01_assign_tuple(self):
     x = cps.FloatRange("text", (1, 2), 1, 5)
     x.value = (2, 5)
     self.assertEqual(x.min, 2)
     self.assertEqual(x.max, 5)
     x.test_valid(None)
    def create_settings(self):
        """Create the module settings

        create_settings is called at the end of initialization.
        """
        self.object = cps.ObjectNameSubscriber(
            'Select the object whose measurements will be displayed',
            cps.NONE,
            doc='''
            Choose the name of objects identified by some previous
            module (such as <b>IdentifyPrimaryObjects</b> or
            <b>IdentifySecondaryObjects</b>) whose measurements are to be displayed.'''
        )

        self.x_axis = cps.Measurement('Select the object measurement to plot',
                                      self.get_object,
                                      cps.NONE,
                                      doc='''
            Choose the object measurement made by a previous
            module to plot.''')

        self.bins = cps.Integer('Number of bins',
                                100,
                                1,
                                1000,
                                doc='''
            Enter the number of equally-spaced bins that you want
            used on the X-axis.''')

        self.xscale = cps.Choice(
            'Transform the data prior to plotting along the X-axis?',
            ['no', 'log'],
            None,
            doc='''
            The measurement data can be scaled with either a
            linear scale (<i>No</i>) or a <i>log</i> (base 10)
            scaling.
            <p>Log scaling is useful when one of the
            measurements being plotted covers a large range of
            values; a log scale can bring out features in the
            measurements that would not easily be seen if the
            measurement is plotted linearly.<p>''')

        self.yscale = cps.Choice('How should the Y-axis be scaled?',
                                 ['linear', 'log'],
                                 None,
                                 doc='''
            The Y-axis can be scaled either with either a <i>linear</i>
            scale or a <i>log</i> (base 10) scaling.
            <p>Log scaling is useful when one of the
            measurements being plotted covers a large range of
            values; a log scale can bring out features in the
            measurements that would not easily be seen if the
            measurement is plotted linearly.</p>''')

        self.title = cps.Text('Enter a title for the plot, if desired',
                              '',
                              doc='''
            Enter a title for the plot. If you leave this blank,
            the title will default
            to <i>(cycle N)</i> where <i>N</i> is the current image
            cycle being executed.''')

        self.wants_xbounds = cps.Binary(
            'Specify min/max bounds for the X-axis?',
            False,
            doc='''
            Select <i>%(YES)s</i> to specify minimum and maximum values for the
            plot on the X-axis. This is helpful if an outlier bin skews the
            plot such that the bins of interest are no longer visible.''' %
            globals())

        self.xbounds = cps.FloatRange('Minimum/maximum values for the X-axis')
    def create_settings(self):
        """Create your settings by subclassing this function

        create_settings is called at the end of initialization.

        You should create the setting variables for your module here:
            # Ask the user for the input image
            self.image_name = cellprofiler.settings.ImageNameSubscriber(...)
            # Ask the user for the name of the output image
            self.output_image = cellprofiler.settings.ImageNameProvider(...)
            # Ask the user for a parameter
            self.smoothing_size = cellprofiler.settings.Float(...)
        """
        self.objects_or_image = cps.Choice(
            "Display object or image measurements?", [OI_OBJECTS, OI_IMAGE],
            doc="""\
-  *%(OI_OBJECTS)s* displays measurements made on objects.
-  *%(OI_IMAGE)s* displays a single measurement made on an image.
""" % globals())

        self.objects_name = cps.ObjectNameSubscriber(
            "Select the input objects",
            cps.NONE,
            doc="""\
*(Used only when displaying object measurements)*

Choose the name of objects identified by some previous module (such as
**IdentifyPrimaryObjects** or **IdentifySecondaryObjects**).
""")

        def object_fn():
            if self.objects_or_image == OI_OBJECTS:
                return self.objects_name.value
            else:
                return cpmeas.IMAGE

        self.measurement = cps.Measurement("Measurement to display",
                                           object_fn,
                                           doc="""\
Choose the measurement to display. This will be a measurement made by
some previous module on either the whole image (if displaying a single
image measurement) or on the objects you selected.
""")

        self.wants_image = cps.Binary("Display background image?",
                                      True,
                                      doc="""\
Choose whether or not to display the measurements on
a background image. Usually, you will want to see the image
context for the measurements, but it may be useful to save
just the overlay of the text measurements and composite the
overlay image and the original image later. Choose "Yes" to
display the measurements on top of a background image or "No"
to display the measurements on a black background.""")

        self.image_name = cps.ImageNameSubscriber(
            "Select the image on which to display the measurements",
            cps.NONE,
            doc="""\
Choose the image to be displayed behind the measurements.
This can be any image created or loaded by a previous module.
If you have chosen not to display the background image, the image
will only be used to determine the dimensions of the displayed image.""")

        self.color_or_text = cps.Choice("Display mode", [CT_TEXT, CT_COLOR],
                                        doc="""\
*(Used only when displaying object measurements)*

Choose how to display the measurement information. If you choose
%(CT_TEXT)s, **DisplayDataOnImage** will display the numeric value on
top of each object. If you choose %(CT_COLOR)s, **DisplayDataOnImage**
will convert the image to grayscale, if necessary, and display the
portion of the image within each object using a hue that indicates the
measurement value relative to the other objects in the set using the
default color map.
""" % globals())

        self.colormap = cps.Colormap("Color map",
                                     doc="""\
*(Used only when displaying object measurements)*

This is the color map used as the color gradient for coloring the
objects by their measurement values. See `this page`_ for pictures
of the available colormaps.

.. _this page: http://matplotlib.org/users/colormaps.html
            """)
        self.text_color = cps.Color(
            "Text color",
            "red",
            doc=
            """This is the color that will be used when displaying the text."""
        )

        self.display_image = cps.ImageNameProvider(
            "Name the output image that has the measurements displayed",
            "DisplayImage",
            doc="""\
The name that will be given to the image with the measurements
superimposed. You can use this name to refer to the image in subsequent
modules (such as **SaveImages**).
""")

        self.font_size = cps.Integer(
            "Font size (points)",
            10,
            minval=1,
            doc="""Set the font size of the letters to be displayed.""")

        self.decimals = cps.Integer(
            "Number of decimals",
            2,
            minval=0,
            doc=
            """Set how many decimals to be displayed, for example 2 decimals for 0.01; 3 decimals for 0.001."""
        )

        self.saved_image_contents = cps.Choice("Image elements to save",
                                               [E_IMAGE, E_FIGURE, E_AXES],
                                               doc="""\
This setting controls the level of annotation on the image:

-  *%(E_IMAGE)s:* Saves the image with the overlaid measurement
   annotations.
-  *%(E_AXES)s:* Adds axes with tick marks and image coordinates.
-  *%(E_FIGURE)s:* Adds a title and other decorations.
""" % globals())

        self.offset = cps.Integer("Annotation offset (in pixels)",
                                  0,
                                  doc="""\
Add a pixel offset to the measurement. Normally, the text is
placed at the object (or image) center, which can obscure relevant features of
the object. This setting adds a specified offset to the text, in a random
direction.""")

        self.color_map_scale_choice = cps.Choice(
            "Color map scale", [CMS_USE_MEASUREMENT_RANGE, CMS_MANUAL],
            doc="""\
*(Used only when displaying object measurements as a colormap)*

**DisplayDataOnImage** assigns a color to each object’s measurement
value from a colormap when in colormap-mode, mapping the value to a
color along the colormap’s continuum. This mapping has implicit upper
and lower bounds to its range which are the extremes of the colormap.
This setting determines whether the extremes are the minimum and
maximum values of the measurement from among the objects in the
current image or manually-entered extremes.

-  *%(CMS_USE_MEASUREMENT_RANGE)s:* Use the full range of colors to
   get the maximum contrast within the image.
-  *%(CMS_MANUAL)s:* Manually set the upper and lower bounds so that
   images with different maxima and minima can be compared by a uniform
   color mapping.
""" % globals())
        self.color_map_scale = cps.FloatRange("Color map range",
                                              value=(0.0, 1.0),
                                              doc="""\
*(Used only when setting a manual colormap range)*

This setting determines the lower and upper bounds of the values for the
color map.
""")
 def test_02_04_bad_max(self):
     s = cps.FloatRange("foo", (1, 15), maxval=20)
     s.value_text = s.compose_max_text("21")
     self.assertEquals(s.max, 20)
     self.assertEquals(s.max_text, "21")
     self.assertRaises(cps.ValidationError, (lambda: s.test_valid(None)))
 def test_02_02_bad_min(self):
     s = cps.FloatRange("foo", (1, 15), minval=0)
     s.value_text = s.compose_min_text("-1")
     self.assertEquals(s.min, 0)
     self.assertEquals(s.min_text, "-1")
     self.assertRaises(cps.ValidationError, (lambda: s.test_valid(None)))
 def test_02_01_good_min(self):
     s = cps.FloatRange("foo", (1, 15), minval=0)
     for test_case in ("2", "0"):
         s.value_text = s.compose_min_text(test_case)
         s.test_valid(None)
 def test_02_03_neg_order(self):
     x = cps.FloatRange("text", (1, 2), 1, 5)
     x.value = (2, 1)
     self.assertRaises(ValueError, x.test_valid, None)
 def test_03_01_no_range(self):
     """Regression test a bug where the variable throws an exception if there is no range"""
     x = cps.FloatRange("text", (1, 2))
     x.test_valid(None)
Exemple #17
0
    def create_settings(self):
        self.image_name = cps.ImageNameSubscriber(
            "Select the input image",
            cps.NONE,
            doc='''Select the image to be rescaled.''')

        self.rescaled_image_name = cps.ImageNameProvider(
            "Name the output image",
            "RescaledBlue",
            doc='''Enter the name of output rescaled image.''')

        self.rescale_method = cps.Choice('Rescaling method',
                                         choices=M_ALL,
                                         doc='''
            There are a number of options for rescaling the input image:
            <ul>
            <li><i>%(M_STRETCH)s:</i> Find the minimum and maximum values within the unmasked part of the image
            (or the whole image if there is no mask) and rescale every pixel so that
            the minimum has an intensity of zero and the maximum has an intensity of one.</li>
            <li><i>%(M_MANUAL_INPUT_RANGE)s:</i> Pixels are
            scaled from their user-specified original range to the range 0 to 1.
            Options are available to handle values outside of the original range.<br>
            To convert 12-bit images saved in 16-bit format to the correct range,
            use the range 0 to 0.0625. The value 0.0625 is equivalent
            to 2<sup>12</sup> divided by 2<sup>16</sup>, so it will convert a 16 bit image containing
            only 12 bits of data to the proper range.</li>
            <li><i>%(M_MANUAL_IO_RANGE)s:</i> Pixels are scaled from their original range to
            the new target range. Options are available to handle values outside
            of the original range.</li>
            <li><i>%(M_DIVIDE_BY_IMAGE_MINIMUM)s:</i> Divide the intensity value of each pixel
            by the image's minimum intensity value so that all pixel intensities are equal to or
            greater than 1. The rescaled image can serve as an illumination correction function in
            <b>CorrectIlluminationApply</b>.</li>
            <li><i>%(M_DIVIDE_BY_IMAGE_MAXIMUM)s:</i> Divide the intensity value of each pixel by the
            image's maximum intensity value so that all pixel intensities are less than or equal to 1.</li>
            <li><i>%(M_DIVIDE_BY_VALUE)s:</i> Divide the intensity value of each pixel by the value entered.</li>
            <li><i>%(M_DIVIDE_BY_MEASUREMENT)s:</i> The intensity value of each pixel is divided by some
            previously calculated measurement. This measurement can be the output of some other module
            or can be a value loaded by the <b>Metadata</b> module.</li>
            <li><i>%(M_SCALE_BY_IMAGE_MAXIMUM)s:</i> Scale an image so that its maximum value is the
            same as the maximum value within the reference image.</li>
            <li><i>%(M_CONVERT_TO_8_BIT)s:</i> Images in CellProfiler are normally stored as a floating
            point number in the range of 0 to 1. This option converts these images to class uint8,
            meaning an 8 bit integer in the range of 0 to 255,
            reducing the amount of memory required to store the image. <i>Warning:</i> Most
            CellProfiler modules require the incoming image to be in the standard 0
            to 1 range, so this conversion may cause downstream modules to behave
            in unexpected ways.</li>
            </ul>''' % globals())

        self.wants_automatic_low = cps.Choice(
            'Method to calculate the minimum intensity',
            LOW_ALL,
            doc="""
            <i>(Used only if "%(M_MANUAL_IO_RANGE)s" is selected)</i><br>
            This setting controls how the minimum intensity is determined.
            <ul>
            <li><i>%(CUSTOM_VALUE)s:</i> Enter the minimum intensity manually below.</li>
            <li><i>%(LOW_EACH_IMAGE)s</i>: use the lowest intensity in this image
            as the minimum intensity for rescaling</li>
            <li><i>%(LOW_ALL_IMAGES)s</i>: use the lowest intensity from all images
            in the image group or the experiment if grouping is not being used.
            <b>Note:</b> Choosing this option may have undesirable results for
            a large ungrouped experiment split into a number of batches. Each batch
            will open all images from the chosen channel at the start of the run.
            This sort of synchronized action may have a severe impact on your
            network file system.</li>
            </ul>
            """ % globals())

        self.wants_automatic_high = cps.Choice(
            'Method to calculate the maximum intensity',
            HIGH_ALL,
            doc="""
            <i>(Used only if "%(M_MANUAL_IO_RANGE)s" is selected)</i><br>
            This setting controls how the maximum intensity is determined.
            <ul>
            <li><i>%(CUSTOM_VALUE)s</i>: Enter the maximum intensity manually below.</li>
            <li><i>%(HIGH_EACH_IMAGE)s</i>: Use the highest intensity in this image
            as the maximum intensity for rescaling</li>
            <li><i>%(HIGH_ALL_IMAGES)s</i>: Use the highest intensity from all images
            in the image group or the experiment if grouping is not being used.
            <b>Note:</b> Choosing this option may have undesirable results for
            a large ungrouped experiment split into a number of batches. Each batch
            will open all images from the chosen channel at the start of the run.
            This sort of synchronized action may have a severe impact on your
            network file system.</li>
            </ul>
            """ % globals())

        self.source_low = cps.Float(
            'Lower intensity limit for the input image', 0)

        self.source_high = cps.Float(
            'Upper intensity limit for the input image', 1)

        self.source_scale = cps.FloatRange(
            'Intensity range for the input image', (0, 1))

        self.dest_scale = cps.FloatRange(
            'Intensity range for the output image', (0, 1))

        self.low_truncation_choice = cps.Choice(
            'Method to rescale pixels below the lower limit',
            [R_MASK, R_SET_TO_ZERO, R_SET_TO_CUSTOM, R_SCALE],
            doc='''
            <i>(Used only if "%(M_MANUAL_IO_RANGE)s" is selected)</i><br>
            There are several ways to handle values less than the lower limit of the intensity range:
            <ul>
            <li><i>%(R_MASK)s:</i> Creates a mask for the output image. All pixels below
            the lower limit will be masked out.</li>
            <li><i>%(R_SET_TO_ZERO)s:</i> Sets all pixels below the lower limit to zero.</li>
            <li><i>%(R_SET_TO_CUSTOM)s:</i> Sets all pixels below the lower limit to a custom
            value.</li>
            <li><i>%(R_SCALE)s:</i> Scales pixels with values below the lower limit
            using the same offset and divisor as other pixels. The results
            will be less than zero.</li>
            </ul>''' % globals())

        self.custom_low_truncation = cps.Float(
            "Custom value for pixels below lower limit",
            0,
            doc="""
            <i>(Used only if "%(M_MANUAL_IO_RANGE)s" and "%(R_SET_TO_CUSTOM)s are selected)</i><br>
            enter the custom value to be assigned to pixels with values below the lower limit."""
            % globals())

        self.high_truncation_choice = cps.Choice(
            'Method to rescale pixels above the upper limit',
            [R_MASK, R_SET_TO_ONE, R_SET_TO_CUSTOM, R_SCALE],
            doc="""
            <i>(Used only if "%(M_MANUAL_IO_RANGE)s" is selected)</i><br>
            There are several ways to handle values greater than the upper limit of the intensity range;
            Options are described in the Help for the equivalent lower limit question."""
            % globals())

        self.custom_high_truncation = cps.Float(
            "Custom value for pixels above upper limit",
            0,
            doc="""
            <i>(Used only if "%(M_MANUAL_IO_RANGE)s" and "%(R_SET_TO_CUSTOM)s are selected)</i><br>
            Enter the custom value to be assigned to pixels with values above the upper limit."""
            % globals())

        self.matching_image_name = cps.ImageNameSubscriber(
            "Select image to match in maximum intensity",
            cps.NONE,
            doc="""
            <i>(Used only if "%(M_SCALE_BY_IMAGE_MAXIMUM)s" is selected)</i><br>
            Select the image whose maximum you want the rescaled image to match."""
            % globals())

        self.divisor_value = cps.Float("Divisor value",
                                       1,
                                       minval=np.finfo(float).eps,
                                       doc="""
            <i>(Used only if "%(M_DIVIDE_BY_VALUE)s" is selected)</i><br>
            Enter the value to use as the divisor for the final image.""" %
                                       globals())

        self.divisor_measurement = cps.Measurement("Divisor measurement",
                                                   lambda: cpmeas.IMAGE,
                                                   doc="""
            <i>(Used only if "%(M_DIVIDE_BY_MEASUREMENT)s" is selected)</i><br>
            Select the measurement value to use as the divisor for the final image."""
                                                   % globals())