def test_01_01_assign(self): x=cps.CustomChoice("text",["foo","bar"],"bar") self.assertTrue(x == "bar") x.value = "foo" self.assertTrue(x == "foo") x.value = "bar" self.assertTrue(x == "bar")
def test_00_00_init(self): x=cps.CustomChoice("text",["choice"]) x.test_valid(None) self.assertEqual(x.text,"text") self.assertEqual(x.value,"choice") self.assertEqual(len(x.choices),1) self.assertEqual(x.choices[0],"choice")
def test_01_02_assign_other(self): x=cps.CustomChoice("text",["foo","bar"],"bar") x.value = "other" self.assertTrue(x == "other") self.assertEqual(len(x.choices),3) self.assertEqual(x.choices[0],"other")
def create_settings(self): self.delimiter = cps.CustomChoice( 'Select or enter the column delimiter', DELIMITERS, doc=""" What delimiter do you want to use? This is the character that separates columns in a file. The two default choices are tab and comma, but you can type in any single character delimiter you would prefer. Be sure that the delimiter you choose is not a character that is present within your data (for example, in file names).""" ) self.prepend_output_filename = cps.Binary( "Prepend the output file name to the data file names?", True, doc=""" This can be useful if you want to run a pipeline multiple times without overwriting the old results.""") self.directory = cps.DirectoryPath( "Output file location", dir_choices=[ DEFAULT_OUTPUT_FOLDER_NAME, DEFAULT_INPUT_FOLDER_NAME, ABSOLUTE_FOLDER_NAME, DEFAULT_INPUT_SUBFOLDER_NAME, DEFAULT_OUTPUT_SUBFOLDER_NAME ], doc="""This setting lets you choose the folder for the output files. %(IO_FOLDER_CHOICE_HELP_TEXT)s <p>%(IO_WITH_METADATA_HELP_TEXT)s %(USING_METADATA_TAGS_REF)s<br> For instance, if you have a metadata tag named "Plate", you can create a per-plate folder by selecting one of the subfolder options and then specifying the subfolder name as "\g<Plate>". The module will substitute the metadata values for the current image set for any metadata tags in the folder name. %(USING_METADATA_HELP_REF)s.</p>""" % globals()) self.add_metadata = cps.Binary( "Add image metadata columns to your object data file?", False, doc= """"Image_Metadata_" columns are normally exported in the Image data file, but if you check this box they will also be exported with the Object data file(s).""" ) self.excel_limits = cps.Binary( "Limit output to a size that is allowed in Excel?", False, doc=""" If your output has more than 256 columns, a window will open which allows you to select the columns you'd like to export. If your output exceeds 65,000 rows, you can still open the .csv in Excel, but not all rows will be visible.""" ) self.pick_columns = cps.Binary( "Select the columns of measurements to export?", False, doc=""" Checking this setting will open up a window that allows you to select the columns to export.""" ) self.columns = cps.MeasurementMultiChoice( "Press button to select measurements to export", doc= """<i>(Used only when selecting the columns of measurements to export)</i><br>This setting controls the columns to be exported. Press the button and check the measurements or categories to export""") self.wants_aggregate_means = cps.Binary( "Calculate the per-image mean values for object measurements?", False, doc=""" <b>ExportToSpreadsheet</b> can calculate population statistics over all the objects in each image and save that value as an aggregate measurement in the Image file. For instance, if you are measuring the area of the Nuclei objects and you check the box for this option, <b>ExportToSpreadsheet</b> will create a column in the Image file called "Mean_Nuclei_AreaShape_Area". <p>You may not want to use <b>ExportToSpreadsheet</b> to calculate these measurements if your pipeline generates a large number of per-object measurements; doing so might exceed Excel's limits on the number of columns (256). """ ) self.wants_aggregate_medians = cps.Binary( "Calculate the per-image median values for object measurements?", False) self.wants_aggregate_std = cps.Binary( "Calculate the per-image standard deviation values for object measurements?", False) self.wants_genepattern_file = cps.Binary( "Create a GenePattern GCT file?", False, doc=""" Create a GCT file compatible with <a href="http://www.broadinstitute.org/cancer/software/genepattern/">GenePattern</a>. The GCT file format is a tab-delimited text file format that describes a gene expression dataset; the specifics of the format are described <a href="http://www.broadinstitute.org/cancer/software/genepattern/tutorial/gp_fileformats.html#gct">here</a>. By converting your measurements into a GCT file, you can make use of GenePattern's data visualization and clustering methods. <p>Each row in the GCT file represents (ordinarily) a gene and each column represents a sample (in this case, a per-image set of measurements). In addition to any other spreadsheets desired, checking this box will produce a GCT file with the extension .gct, prepended with the text selection above. If per-image aggregate measurements are requested above, those measurements are included in the GCT file as well.</p>""") self.how_to_specify_gene_name = cps.Choice( "Select source of sample row name", GP_NAME_OPTIONS, GP_NAME_METADATA, doc=""" <i>(Used only if a GenePattern file is requested)</i><br> The first column of the GCT file is the unique identifier for each sample, which is ordinarily the gene name. This information may be specified in one of two ways: <ul> <li><i>Metadata:</i> If you used <b>LoadData</b> or <b>LoadImages</b> to input your images, you may use a per-image data measurement (such as metadata) that corresponds to the identifier for this column. %(USING_METADATA_HELP_REF)s.</li> <li><i>Image filename:</i> If the gene name is not available, the image filename can be used as a surrogate identifier.</li> </ul>""" % globals()) self.gene_name_column = cps.Measurement( "Select the metadata to use as the identifier", lambda: cpmeas.IMAGE, doc=""" <i>(Used only if a GenePattern file is requested and metadata is used to name each row)</i><br> Choose the measurement that corresponds to the identifier, such as metadata from <b>LoadData</b>'s input file. %(USING_METADATA_HELP_REF)s.""" % globals()) self.use_which_image_for_gene_name = cps.ImageNameSubscriber( "Select the image to use as the identifier", "None", doc=""" <i>(Used only if a GenePattern file is requested and image filename is used to name each row)</i><br> Select which image whose filename will be used to identify each sample row.""" ) self.wants_everything = cps.Binary( "Export all measurements, using default file names?", True, doc="""Check this setting to export every measurement. <b>ExportToSpreadsheet</b> will create one file per object type, as well as per-image, per-experiment and object relationships, if relevant. It will use the object name as the file name, optionally prepending the output file name if specified above. Leave this box unchecked to specify which objects should be exported or to override the automatic names.""") self.object_groups = [] self.add_object_group() self.add_button = cps.DoSomething("", "Add another data set", self.add_object_group)
def create_threshold_settings(self, methods=TM_METHODS): '''Create settings related to thresholding''' self.threshold_method = cps.Choice('Select the thresholding method', methods, doc=""" The intensity threshold affects the decision of whether each pixel will be considered foreground (regions of interest) or background. A stringent threshold will result in only bright regions being identified, with tight lines around them, whereas a lenient threshold will include dim regions and the lines between regions and background will be more loose. You can have the threshold automatically calculated using several methods, or you can enter an absolute number between 0 and 1 for the threshold. To help determine the choice of threshold manually, you can inspect the pixel intensities in an image of your choice. %(HELP_ON_PIXEL_INTENSITIES)s""" % globals() + """ Both options have advantages. An absolute number treats every image identically, but is not robust with regard to slight changes in lighting/staining conditions between images. An automatically calculated threshold adapts to changes in lighting/staining conditions between images and is usually more robust/accurate, but it can occasionally produce a poor threshold for unusual/artifactual images. It also takes a small amount of time to calculate. <p>The threshold that is used for each image is recorded as a measurement in the output file, so if you are surprised by unusual measurements from one of your images, you might check whether the automatically calculated threshold was unusually high or low compared to the other images. <p>There are seven methods for finding thresholds automatically: <ul><li><i>Otsu:</i> This method is probably best if you are not able to make certain assumptions about every images in your experiment, especially if the percentage of the image covered by regions of interest varies substantially from image to image. Our implementation takes into account the maximum and minimum values in the image and log-transforming the image prior to calculating the threshold. For this reason, please note that negative-valued pixels are ignored in this computation, so caution should be used in using image offsets (such as by using <b>ImageMath</b>). <p>If you know that the percentage of each image that is foreground does not vary much from image to image, the MoG method can be better, especially if the foreground percentage is not near 50%.</li> <li><i>Mixture of Gaussian (MoG):</i>This function assumes that the pixels in the image belong to either a background class or a foreground class, using an initial guess of the fraction of the image that is covered by foreground. This method is our own version of a Mixture of Gaussians algorithm (<i>O. Friman, unpublished</i>). Essentially, there are two steps: <ol><li>First, a number of Gaussian distributions are estimated to match the distribution of pixel intensities in the image. Currently three Gaussian distributions are fitted, one corresponding to a background class, one corresponding to a foreground class, and one distribution for an intermediate class. The distributions are fitted using the Expectation-Maximization algorithm, a procedure referred to as Mixture of Gaussians modeling. </li> <li>When the three Gaussian distributions have been fitted, a decision is made whether the intermediate class more closely models the background pixels or foreground pixels, based on the estimated fraction provided by the user.</li></ol></li> <li><i>Background:</i> This method is simple and appropriate for images in which most of the image is background. It finds the mode of the histogram of the image, which is assumed to be the background of the image, and chooses a threshold at twice that value (which you can adjust with a Threshold Correction Factor; see below). The calculation includes those pixels between 2% and 98% of the intensity range. This thresholding method can be helpful if your images vary in overall brightness, but the objects of interest are consistently N times brighter than the background level of the image. </li> <li><i>Robust background:</i> Much like the Background method, this method is also simple and assumes that the background distribution approximates a Gaussian by trimming the brightest and dimmest 5% of pixel intensities. It then calculates the mean and standard deviation of the remaining pixels and calculates the threshold as the mean + 2 times the standard deviation. This thresholding method can be helpful if the majority of the image is background, and the results are often comparable or better than the Background method.</li> <li><i>Ridler-Calvard:</i> This method is simple and its results are often very similar to Otsu's. According to Sezgin and Sankur's paper (<i>Journal of Electronic Imaging</i>, 2004), Otsu's overall quality on testing 40 nondestructive testing images is slightly better than Ridler's (average error: Otsu, 0.318; Ridler, 0.401). Ridler-Calvard chooses an initial threshold and then iteratively calculates the next one by taking the mean of the average intensities of the background and foreground pixels determined by the first threshold, repeating this until the threshold converges.</li> <li><i>Kapur:</i> This method computes the threshold of an image by log-transforming its values, then searching for the threshold that maximizes the sum of entropies of the foreground and background pixel values, when treated as separate distributions.</li> <li><i>Maximum correlation:</i>This is an implementation of the method described in Padmanabhan et al, 2010. It computes the maximum correlation between the binary mask created by thresholding and the thresholded image and is somewhat similar mathematically to Otsu. The authors claim superior results when thresholding images of neurites and other images that have sparse foreground densities.</li> </ul> <p>You can also choose between <i>Global</i>, <i>Adaptive</i>, and <i>Per-object</i> thresholding for the automatic methods: <ul> <li><i>Global:</i> One threshold is calculated for the entire image (fast)</li> <li><i>Adaptive:</i> The calculated threshold varies across the image. This method is a bit slower but may be more accurate near edges of regions of interest, or where illumination variation is significant (though in the latter case, using the <b>CorrectIllumination</b> modules is preferable).</li> <li><i>Per-object:</i> If you are using this module to find child objects located <i>within</i> parent objects, the per-object method will calculate a distinct threshold for each parent object. This is especially helpful, for example, when the background brightness varies substantially among the parent objects. <br><i>Important:</i> the per-object method requires that you run an <b>IdentifyPrimaryObjects</b> module to identify the parent objects upstream in the pipeline. After the parent objects are identified in the pipeline, you must then also run a <b>Crop</b> module with the following inputs: <ul> <li>The input image is the image containing the sub-objects to be identified.</li> <li>Select <i>Objects</i> as the shape to crop into.</li> <li>Select the parent objects (e.g., <i>Nuclei</i>) as the objects to use as a cropping mask.</li> </ul> Finally, in the <b>IdentifyPrimaryObjects</b> module, select the cropped image as input image.</li></ul> <p>Selecting <i>manual thresholding</i> allows you to enter a single value between 0 and 1 as the threshold value. This setting can be useful when you are certain what the cutoff should be and it does not vary from image to image in the experiment. If you are using this module to find objects in an image that is already binary (where the foreground is 1 and the background is 0), a manual value of 0.5 will identify the objects. <p>Selecting thresholding via a <i>binary image</i> will use a selected binary image as a mask for the input image. The most typical approach to produce a binary image is to use the <b>ApplyThreshold</b> module (image as input, image as output) or the <b>ConvertObjectsToImage</b> module (objects as input, image as output); both have options to produce a binary image. Note that unlike <b>MaskImage</b>, the binary image will not be stored permanently as a mask. Also, even though no algorithm is actually used to find the threshold in this case, the final threshold value is reported as the Otsu threshold calculated for the foreground region. <p>Selecting thresholding via <i>measurement</i> will use an image measurement previously calculated in order to threshold the image. Like manual thresholding, this setting can be useful when you are certain what the cutoff should be. The difference in this case is that the desired threshold does vary from image to image in the experiment but can be measured using a Measurement module.</p> <p>References <ul> <li>Sezgin M, Sankur B (2004) "Survey over image thresholding techniques and quantitative performance evaluation." <i>Journal of Electronic Imaging</i>, 13(1), 146-165</li> <li>Padmanabhan K, Eddy WF, Crowley JC (2010) "A novel algorithm for optimal image thresholding of biological data" <i>Journal of Neuroscience Methods</i> 193, 380-384.</li> </ul></p> """) self.threshold_correction_factor = cps.Float( 'Threshold correction factor', 1, doc="""\ When the threshold is calculated automatically, it may consistently be too stringent or too lenient. You may need to enter an adjustment factor that you empirically determine is suitable for your images. The number 1 means no adjustment, 0 to 1 makes the threshold more lenient and greater than 1 (e.g., 1.3) makes the threshold more stringent. For example, the Otsu automatic thresholding inherently assumes that 50% of the image is covered by objects. If a larger percentage of the image is covered, the Otsu method will give a slightly biased threshold that may have to be corrected using this setting.""") self.threshold_range = cps.FloatRange( 'Lower and upper bounds on threshold', (0, 1), minval=0, maxval=1, doc="""\ Enter the minimum and maximum allowable threshold, in the range [0,1]. This is helpful as a safety precaution when the threshold is calculated automatically. For example, if there are no objects in the field of view, the automatic threshold might be calculated as unreasonably low. In such cases, the lower bound you enter here will override the automatic threshold.""" ) self.object_fraction = cps.CustomChoice( 'Approximate fraction of image covered by objects?', [ '0.01', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '0.99' ], doc="""\ <i>(Used only when applying the MoG thresholding method)</i><br> Enter an estimate of how much of the image is covered with objects, which is used to estimate the distribution of pixel intensities.""") self.manual_threshold = cps.Float("Manual threshold", value=0.0, minval=0.0, maxval=1.0, doc="""\ <i>(Used only if Manual selected for thresholding method)</i><br> Enter the value that will act as an absolute threshold for the images, in the range of [0,1].""" ) self.thresholding_measurement = cps.Measurement( "Select the measurement to threshold with", lambda: cpmeas.IMAGE, doc=""" <i>(Used only if Measurement is selected for thresholding method)</i><br> Choose the image measurement that will act as an absolute threshold for the images.""" ) self.binary_image = cps.ImageNameSubscriber("Select binary image", "None", doc=""" <i>(Used only if Binary image selected for thresholding method)</i><br> What is the binary thresholding image?""") self.two_class_otsu = cps.Choice( 'Two-class or three-class thresholding?', [O_TWO_CLASS, O_THREE_CLASS], doc=""" <i>(Used only for the Otsu thresholding method)</i> <br> Select <i>Two</i> if the grayscale levels are readily distinguishable into only two classes: foreground (i.e., objects) and background. Select <i>Three</i> if the grayscale levels fall instead into three classes. You will then be asked whether the middle intensity class should be added to the foreground or background class in order to generate the final two-class output. Note that whether two- or three-class thresholding is chosen, the image pixels are always finally assigned two classes: foreground and background. <p>For example, three-class thresholding may be useful for images in which you have nuclear staining along with low-intensity non-specific cell staining. Where two-class thresholding might incorrectly assign this intermediate staining to the nuclei objects for some cells, three-class thresholding allows you to assign it to the foreground or background as desired. However, in extreme cases where either there are almost no objects or the entire field of view is covered with objects, three-class thresholding may perform worse than two-class.""" ) self.use_weighted_variance = cps.Choice( 'Minimize the weighted variance or the entropy?', [O_WEIGHTED_VARIANCE, O_ENTROPY]) self.assign_middle_to_foreground = cps.Choice( 'Assign pixels in the middle intensity class to the foreground ' 'or the background?', [O_FOREGROUND, O_BACKGROUND], doc=""" <i>(Used only for three-class thresholding)</i><br> Choose whether you want the pixels with middle grayscale intensities to be assigned to the foreground class or the background class.""") self.adaptive_window_method = cps.Choice( "Method to calculate adaptive window size", [FI_IMAGE_SIZE, FI_CUSTOM], doc=""" <i>(Used only if an adaptive thresholding method is used)</i><br> The adaptive method breaks the image into blocks, computing the threshold for each block. There are two ways to compute the block size: <ul> <li><i>%(FI_IMAGE_SIZE)s:</i> The block size is one-tenth of the image dimensions, or 50 x 50 pixels, whichever is bigger.</li> <li><i>%(FI_CUSTOM)s:</i> The block size is specified by the user.</li> </ul>""" % globals()) self.adaptive_window_size = cps.Integer('Size of adaptive window', 10, doc=""" <i>(Used only if an adaptive thresholding method with a %(FI_CUSTOM)s window size are selected)</i><br> Enter the window for the adaptive method. For example, you may want to use a multiple of the largest expected object size.""" % globals())