def create_settings(self): """Create the settings during initialization """ self.directory = cps.DirectoryPath( "Input image file location", support_urls=True, doc="""\ Choose the folder containing the image(s) to be loaded. Generally, it is best to store the image you want to load in either the Default Input or Output Folder, so that the correct image is loaded into the pipeline and typos are avoided. {IO_FOLDER_CHOICE_HELP_TEXT} {IO_WITH_METADATA_HELP_TEXT} """.format( **{ "IO_FOLDER_CHOICE_HELP_TEXT": IO_FOLDER_CHOICE_HELP_TEXT, "IO_WITH_METADATA_HELP_TEXT": IO_WITH_METADATA_HELP_TEXT })) self.file_settings = [] self.add_file(can_remove=False) self.add_button = cps.DoSomething("", "Add another image", self.add_file)
def test_04_05_alter_absolute_path(self): s = cps.DirectoryPath("whatever") s.dir_choice = cps.ABSOLUTE_FOLDER_NAME s.custom_path = os.path.join(self.root_directory, "..", "1", "2") s.alter_for_create_batch_files(TestDirectoryPath.fn_alter_path) self.assertEqual(s.get_absolute_path(), os.path.join(self.root_directory, "2altered"))
def test_02_04_get_output_subfolder_path(self): s = cps.DirectoryPath("whatever") s.dir_choice = cps.DEFAULT_OUTPUT_SUBFOLDER_NAME s.custom_path = "0" self.assertEqual( s.get_absolute_path(), os.path.join(cpprefs.get_default_output_directory(), "0"))
def add_dose_value(self, can_remove=True): '''Add a dose value measurement to the list can_delete - set this to False to keep from showing the "remove" button for images that must be present.''' group = cps.SettingsGroup() group.append("measurement", cps.Measurement("Select the image measurement describing the treatment dose", lambda: cpmeas.IMAGE, doc="""\ The V and Z’ factors, metrics of assay quality, and the EC50, indicating dose-response, are calculated by this module based on each image being specified as a particular treatment dose. Choose a measurement that gives the dose of some treatment for each of your images. See the help for the previous setting for details.""")) group.append("log_transform", cps.Binary( "Log-transform the dose values?", False, doc='''\ Select *%(YES)s* if you have dose-response data and you want to log-transform the dose values before fitting a sigmoid curve. Select *%(NO)s* if your data values indicate only positive vs. negative controls. ''' % globals())) group.append('wants_save_figure', cps.Binary( '''Create dose-response plots?''', False, doc='''Select *%(YES)s* if you want to create and save dose-response plots. You will be asked for information on how to save the plots.''' % globals())) group.append('figure_name', cps.Text( "Figure prefix", "", doc='''\ *(Used only when creating dose-response plots)* CellProfiler will create a file name by appending the measurement name to the prefix you enter here. For instance, if you specify a prefix of “Dose\_”, when saving a file related to objects you have chosen (for example, *Cells*) and a particular measurement (for example, *AreaShape_Area*), CellProfiler will save the figure as *Dose_Cells_AreaShape_Area.m*. Leave this setting blank if you do not want a prefix. ''' )) group.append('pathname', cps.DirectoryPath( "Output file location", dir_choices=[ cps.DEFAULT_OUTPUT_FOLDER_NAME, cps.DEFAULT_INPUT_FOLDER_NAME, cps.ABSOLUTE_FOLDER_NAME, cps.DEFAULT_OUTPUT_SUBFOLDER_NAME, cps.DEFAULT_INPUT_SUBFOLDER_NAME], doc="""\ *(Used only when creating dose-response plots)* This setting lets you choose the folder for the output files. %(IO_FOLDER_CHOICE_HELP_TEXT)s %(IO_WITH_METADATA_HELP_TEXT)s """ % globals())) group.append("divider", cps.Divider()) group.append("remover", cps.RemoveSettingButton("", "Remove this dose measurement", self.dose_values, group)) self.dose_values.append(group)
def test_01_02_split_and_join(self): gibberish = "aqwura[oijs|fd" for dir_choice in cps.DirectoryPath.DIR_ALL + [cps.NO_FOLDER_NAME]: s = cps.DirectoryPath("whatever") value = s.join_parts(dir_choice, gibberish) out_dir_choice = s.dir_choice custom_path = s.custom_path self.assertEqual(dir_choice, out_dir_choice) self.assertEqual(custom_path, gibberish)
def test_01_03_is_custom_choice(self): for dir_choice, expected in ((cps.DEFAULT_INPUT_FOLDER_NAME, False), (cps.DEFAULT_INPUT_SUBFOLDER_NAME, True), (cps.DEFAULT_OUTPUT_FOLDER_NAME, False), (cps.DEFAULT_OUTPUT_SUBFOLDER_NAME, True), (cps.ABSOLUTE_FOLDER_NAME, True), (cps.URL_FOLDER_NAME, True)): s = cps.DirectoryPath("whatever") s.dir_choice = dir_choice self.assertEqual(s.is_custom_choice, expected)
def test_04_04_alter_output_subfolder_path(self): s = cps.DirectoryPath("whatever") s.dir_choice = cps.DEFAULT_OUTPUT_SUBFOLDER_NAME s.custom_path = "0" def fn_alter_path(path, **kwargs): self.assertEqual(path, "0") return "5" s.alter_for_create_batch_files(fn_alter_path) self.assertEqual( s.get_absolute_path(), os.path.join(cpprefs.get_default_output_directory(), "5"))
def create_settings(self): """Create the settings during initialization """ self.directory = cps.DirectoryPath( "Input image file location", support_urls=True, doc=''' Select the folder containing the image(s) to be loaded. Generally, it is best to store the image you want to load in either the Default Input or Output Folder, so that the correct image is loaded into the pipeline and typos are avoided. %(IO_FOLDER_CHOICE_HELP_TEXT)s <p>%(IO_WITH_METADATA_HELP_TEXT)s %(USING_METADATA_TAGS_REF)s For instance, if you have a "Plate" metadata tag, and your single files are organized in subfolders named with the "Plate" tag, you can select one of the subfolder options and then specify a subfolder name of "\g<Plate>" to get the files from the subfolder associated with that image's 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.file_settings = [] self.add_file(can_remove=False) self.add_button = cps.DoSomething("", "Add another image", self.add_file)
def test_03_07_metadata(self): m = cpmeas.Measurements() m.add_image_measurement("Metadata_Path", "2") s = cps.DirectoryPath("whatever", allow_metadata=True) for dir_choice, expected in ( (cps.DEFAULT_INPUT_SUBFOLDER_NAME, os.path.join(cpprefs.get_default_image_directory(), "0", "2")), (cps.DEFAULT_OUTPUT_SUBFOLDER_NAME, os.path.join(cpprefs.get_default_output_directory(), "0", "2")), (cps.ABSOLUTE_FOLDER_NAME, os.path.join(self.root_directory, "2")), (cps.URL_FOLDER_NAME, "http://www.cellprofiler.org/2")): s.dir_choice = dir_choice if dir_choice in (cps.DEFAULT_INPUT_SUBFOLDER_NAME, cps.DEFAULT_OUTPUT_SUBFOLDER_NAME): s.custom_path = "0" + os.path.sep.replace('\\', '\\\\') + "\\g<Path>" elif dir_choice == cps.ABSOLUTE_FOLDER_NAME: s.custom_path = self.root_directory + os.path.sep.replace( '\\', '\\\\') + "\\g<Path>" else: s.custom_path = "http://www.cellprofiler.org/\\g<Path>" self.assertEqual(s.get_absolute_path(m), expected)
def test_02_06_get_url(self): s = cps.DirectoryPath("whatever") s.dir_choice = cps.URL_FOLDER_NAME s.custom_path = "http://www.cellprofiler.org" self.assertEqual(s.get_absolute_path(), "http://www.cellprofiler.org")
def test_02_05_get_absolute_path(self): s = cps.DirectoryPath("whatever") s.dir_choice = cps.ABSOLUTE_FOLDER_NAME s.custom_path = os.path.join(self.root_directory, "..", "1", "2") self.assertEqual(s.get_absolute_path(), os.path.join(self.root_directory, "2"))
def create_settings(self): '''Create the settings for the ExportToCellH5 module''' self.directory = cps.DirectoryPath("Output file location", doc=""" This setting lets you choose the folder for the output files. %(IO_FOLDER_CHOICE_HELP_TEXT)s """ % globals()) def get_directory_fn(): '''Get the directory for the CellH5 file''' return self.directory.get_absolute_path() def set_directory_fn(path): dir_choice, custom_path = self.directory.get_parts_from_path(path) self.directory.join_parts(dir_choice, custom_path) self.file_name = cps.FilenameText("Output file name", "DefaultOut.ch5", get_directory_fn=get_directory_fn, set_directory_fn=set_directory_fn, metadata=True, browse_msg="Choose CellH5 file", mode=cps.FilenameText.MODE_APPEND, exts=[("CellH5 file (*.cellh5)", "*.ch5"), ("HDF5 file (*.h5)", "*.h5"), ("All files (*.*", "*.*")], doc=""" This setting lets you name your CellH5 file. If you choose an existing file, CellProfiler will add new data to the file or overwrite existing locations. <p>%(IO_WITH_METADATA_HELP_TEXT)s %(USING_METADATA_TAGS_REF)s. For instance, if you have a metadata tag named "Plate", you can create a per-plate folder by selecting one 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.overwrite_ok = cps.Binary( "Overwrite existing data without warning?", False, doc=""" Select <i>%(YES)s</i> to automatically overwrite any existing data for a site. Select <i>%(NO)s</i> to be prompted first. If you are running the pipeline on a computing cluster, select <i>%(YES)s</i> unless you want execution to stop because you will not be prompted to intervene. Also note that two instances of CellProfiler cannot write to the same file at the same time, so you must ensure that separate names are used on a cluster. """ % globals()) self.repack = cps.Binary("Repack after analysis", True, doc=""" This setting determines whether CellProfiler in multiprocessing mode repacks the data at the end of analysis. If you select <i>%(YES)s</i>, CellProfiler will combine all of the satellite files into a single file upon completion. This option requires some extra temporary disk space and takes some time at the end of analysis, but results in a single file which may occupy less disk space. If you select <i>%(NO)s</i>, CellProfiler will create a master file using the name that you give and this file will have links to individual data files that contain the actual data. Using the data generated by this option requires that you keep the master file and the linked files together when copying them to a new folder. """ % globals()) self.plate_metadata = cps.Choice("Plate metadata", [], value="Plate", choices_fn=self.get_metadata_choices, doc=""" This is the metadata tag that identifies the plate name of the images for the current cycle. Choose <i>None</i> if your assay does not have metadata for plate name. If your assay is slide-based, you can use a metadata item that identifies the slide as the choice for this setting and set the well and site metadata items to <i>None</i>.""") self.well_metadata = cps.Choice( "Well metadata", [], value="Well", choices_fn=self.get_metadata_choices, doc="""This is the metadata tag that identifies the well name for the images in the current cycle. Choose <i>None</i> if your assay does not have metadata for the well.""") self.site_metadata = cps.Choice( "Site metadata", [], value="Site", choices_fn=self.get_metadata_choices, doc="""This is the metadata tag that identifies the site name for the images in the current cycle. Choose <i>None</i> if your assay doesn't divide wells up into sites or if this tag is not required for other reasons.""") self.divider = cps.Divider() self.wants_to_choose_measurements = cps.Binary("Choose measurements?", False, doc=""" This setting lets you choose between exporting all measurements or just the ones that you choose. Select <i>%(YES)s</i> to pick the measurements to be exported. Select <i>%(NO)s</i> to automatically export all measurements available at this stage of the pipeline. """ % globals()) self.measurements = cps.MeasurementMultiChoice( "Measurements to export", doc=""" <i>(Used only if choosing measurements.)</i> <br> This setting lets you choose individual measurements to be exported. Check the measurements you want to export. """) self.objects_to_export = [] self.add_objects_button = cps.DoSomething("Add objects to export", "Add objects", self.add_objects) self.images_to_export = [] self.add_image_button = cps.DoSomething("Add an image to export", "Add image", self.add_image) self.objects_count = cps.HiddenCount(self.objects_to_export) self.images_count = cps.HiddenCount(self.images_to_export)
def test_02_02_get_default_output_folder_path(self): s = cps.DirectoryPath("whatever") s.dir_choice = cps.DEFAULT_OUTPUT_FOLDER_NAME self.assertEqual(s.get_absolute_path(), cpprefs.get_default_output_directory())
def create_settings(self): self.input_type = cps.Choice("Select the type of input", [IF_IMAGE], IF_IMAGE) self.image_name = cps.ImageNameSubscriber("Select the image to save", cps.NONE, doc=""" <i>(Used only if "%(IF_IMAGE)s", "%(IF_MASK)s" or "%(IF_CROPPING)s" are selected to save)</i><br> Select the image you want to save.""" % globals()) self.input_object_name = cps.ObjectNameSubscriber( "Select the objects to save", cps.NONE) self.objects_name = cps.ObjectNameSubscriber( "Select the objects to crop and save", cps.NONE, doc=""" Select the objects that you want to save.""" % globals()) self.file_name_method = cps.Choice( "Select method for constructing file names", [ FN_FROM_IMAGE, ], doc=""" Several choices are available for constructing the image file name: <ul> <li><i>%(FN_FROM_IMAGE)s:</i> The filename will be constructed based on the original filename of an input image specified in <b>NamesAndTypes</b>. You will have the opportunity to prefix or append additional text. <p>If you have metadata associated with your images, you can append an text to the image filename using a metadata tag. This is especially useful if you want your output given a unique label according to the metadata corresponding to an image group. The name of the metadata to substitute can be provided for each image for each cycle using the <b>Metadata</b> module. %(USING_METADATA_TAGS_REF)s%(USING_METADATA_HELP_REF)s.</p></li> </ul>""" % globals()) self.file_image_name = cps.FileImageNameSubscriber( "Select image name for file prefix", cps.NONE, doc=""" <i>(Used only when "%(FN_FROM_IMAGE)s" is selected for contructing the filename)</i><br> Select an image loaded using <b>NamesAndTypes</b>. The original filename will be used as the prefix for the output filename.""" % globals()) self.wants_file_name_suffix = cps.Binary( "Append a suffix to the image file name?", False, doc=""" Select <i>%(YES)s</i> to add a suffix to the image's file name. Select <i>%(NO)s</i> to use the image name as-is.""" % globals()) self.file_name_suffix = cps.Text("Text to append to the image name", "", metadata=True, doc=""" <i>(Used only when constructing the filename from the image filename)</i><br> Enter the text that should be appended to the filename specified above.""" ) self.file_format = cps.Choice("Saved file format", [FF_TIFF], value=FF_TIFF, doc=""" <i>(Used only when saving non-movie files)</i><br> Select the image or movie format to save the image(s). Most common image formats are available; MAT-files are readable by MATLAB.""") self.pathname = SaveImagesDirectoryPath("Output file location", self.file_image_name, doc=""" <i>(Used only when saving non-movie files)</i><br> This setting lets you choose the folder for the output files. %(IO_FOLDER_CHOICE_HELP_TEXT)s <p>An additional option is the following: <ul> <li><i>Same folder as image</i>: Place the output file in the same folder that the source image is located.</li> </ul></p> <p>%(IO_WITH_METADATA_HELP_TEXT)s %(USING_METADATA_TAGS_REF)s. For instance, if you have a metadata tag named "Plate", you can create a per-plate folder by selecting one 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> <p>If the subfolder does not exist when the pipeline is run, CellProfiler will create it.</p> <p>If you are creating nested subfolders using the sub-folder options, you can specify the additional folders separated with slashes. For example, "Outlines/Plate1" will create a "Plate1" folder in the "Outlines" folder, which in turn is under the Default Input/Output Folder. The use of a forward slash ("/") as a folder separator will avoid ambiguity between the various operating systems.</p>""" % globals()) self.bit_depth = cps.Choice( "Image bit depth", [BIT_DEPTH_8, BIT_DEPTH_16, BIT_DEPTH_FLOAT], doc=""" <i>(Used only when saving files in a non-MAT format)</i><br> Select the bit-depth at which you want to save the images. <i>%(BIT_DEPTH_FLOAT)s</i> saves the image as floating-point decimals with 32-bit precision in its raw form, typically scaled between 0 and 1. <b>%(BIT_DEPTH_16)s and %(BIT_DEPTH_FLOAT)s images are supported only for TIF formats. Currently, saving images in 12-bit is not supported.</b>""" % globals()) self.object_extension = cps.Integer("Object extension", value=1, doc=""" How many pixels should the bounding box of the objects be extended before cropping""") self.overwrite = cps.Binary( "Overwrite existing files without warning?", False, doc=""" Select <i>%(YES)s</i> to automatically overwrite a file if it already exists. Select <i>%(NO)s</i> to be prompted for confirmation first. <p>If you are running the pipeline on a computing cluster, select <i>%(YES)s</i> since you will not be able to intervene and answer the confirmation prompt.</p>""" % globals()) self.when_to_save = cps.Choice("When to save", [WS_FIRST_CYCLE], doc="""<a name='when_to_save'> <i>(Used only when saving non-movie files)</i><br> Specify at what point during pipeline execution to save file(s). </a> <ul> <li><i>%(WS_EVERY_CYCLE)s:</i> Useful for when the image of interest is created every cycle and is not dependent on results from a prior cycle.</li> </ul> """ % globals()) self.update_file_names = cps.Binary( "Record the file and path information to the saved image?", False, doc=""" Select <i>%(YES)s</i> to store filename and pathname data for each of the new files created via this module as a per-image measurement. <p>Instances in which this information may be useful include: <ul> <li>Exporting measurements to a database, allowing access to the saved image. If you are using the machine-learning tools or image viewer in CellProfiler Analyst, for example, you will want to enable this setting if you want the saved images to be displayed along with the original images.</li> <li>Allowing downstream modules (e.g., <b>CreateWebPage</b>) to access the newly saved files.</li> </ul></p>""" % globals()) self.create_subdirectories = cps.Binary( "Create subfolders in the output folder?", False, doc=""" Select <i>%(YES)s</i> to create subfolders to match the input image folder structure.""" % globals()) self.root_dir = cps.DirectoryPath("Base image folder", doc=""" <i>Used only if creating subfolders in the output folder</i> In subfolder mode, <b>SaveImages</b> determines the folder for an image file by examining the path of the matching input file. The path that SaveImages uses is relative to the image folder chosen using this setting. As an example, input images might be stored in a folder structure of "images%(sep)s<i>experiment-name</i>%(sep)s <i>date</i>%(sep)s<i>plate-name</i>". If the image folder is "images", <b>SaveImages</b> will store images in the subfolder, "<i>experiment-name</i>%(sep)s<i>date</i>%(sep)s<i>plate-name</i>". If the image folder is "images%(sep)s<i>experiment-name</i>", <b>SaveImages</b> will store images in the subfolder, <i>date</i>%(sep)s<i>plate-name</i>". """ % dict(sep=os.path.sep))
def test_04_07_no_folder(self): s = cps.DirectoryPath("whatever") s.dir_choice = cps.NO_FOLDER_NAME s.custom_path = "gibberish" self.assertEqual(s.get_absolute_path(), '')
def test_04_02_alter_output_folder_path(self): s = cps.DirectoryPath("whatever") s.dir_choice = cps.DEFAULT_OUTPUT_FOLDER_NAME s.alter_for_create_batch_files(TestDirectoryPath.fn_alter_path) self.assertEqual(s.get_absolute_path(), cpprefs.get_default_output_directory())
def add_dose_value(self, can_remove=True): '''Add a dose value measurement to the list can_delete - set this to False to keep from showing the "remove" button for images that must be present.''' group = cps.SettingsGroup() group.append("measurement", cps.Measurement("Select the image measurement describing the treatment dose", lambda: cpmeas.IMAGE, doc=""" The V and Z' factor, a measure of assay quality, and the EC50, indicating dose/response, are calculated by this module based on each image being specified as a particular treatment dose. Choose a measurement that gives the dose of some treatment for each of your images. <p> The typical way to provide this information in the pipeline is to create a comma-delimited text file (CSV) outside of CellProfiler and then load that file into the pipeline using <b>Metadata</b> or the <b>LoadData</b>. In that case, choose the measurement that matches the column header of the measurement in the CSV input file. See <b>LoadData</b> help for an example text file. """)) group.append("log_transform", cps.Binary( "Log-transform the dose values?", False, doc=''' Select <i>%(YES)s</i> if you have dose-response data and you want to log-transform the dose values before fitting a sigmoid curve. <p>Select <i>%(NO)s</i> if your data values indicate only positive vs. negative controls.</p>''' % globals())) group.append('wants_save_figure', cps.Binary( '''Create dose/response plots?''', False, doc='''<a name='wants_save_figure'></a> Select <i>%(YES)s</i> if you want to create and save dose response plots. You will be asked for information on how to save the plots.''' % globals())) group.append('figure_name', cps.Text( "Figure prefix", "", doc=''' <i>(Used only when creating dose/response plots)</i><br> CellProfiler will create a file name by appending the measurement name to the prefix you enter here. For instance, if you have objects named, "Cells", the "AreaShape_Area measurement", and a prefix of "Dose_", CellProfiler will save the figure as <i>Dose_Cells_AreaShape_Area.m</i>. Leave this setting blank if you do not want a prefix.''' )) group.append('pathname', cps.DirectoryPath( "Output file location", dir_choices=[ cps.DEFAULT_OUTPUT_FOLDER_NAME, cps.DEFAULT_INPUT_FOLDER_NAME, cps.ABSOLUTE_FOLDER_NAME, cps.DEFAULT_OUTPUT_SUBFOLDER_NAME, cps.DEFAULT_INPUT_SUBFOLDER_NAME], doc=""" <i>(Used only when creating dose/response plots)</i><br> 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 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())) group.append("divider", cps.Divider()) group.append("remover", cps.RemoveSettingButton("", "Remove this dose measurement", self.dose_values, group)) self.dose_values.append(group)
def add_measurement(self, flag_settings, can_delete=True): measurement_settings = flag_settings.measurement_settings group = cps.SettingsGroup() group.append("divider1", cps.Divider(line=False)) group.append( "source_choice", cps.Choice( "Flag is based on", S_ALL, doc="""\ - *%(S_IMAGE)s:* A per-image measurement, such as intensity or granularity. - *%(S_AVERAGE_OBJECT)s:* The average of all object measurements in the image. - *%(S_ALL_OBJECTS)s:* All the object measurements in an image, without averaging. In other words, if *any* of the objects meet the criteria, the image will be flagged. - *%(S_RULES)s:* Use a text file of rules produced by CellProfiler Analyst. With this option, you will have to ensure that this pipeline produces every measurement in the rules file upstream of this module. - *%(S_CLASSIFIER)s:* Use a classifier built by CellProfiler Analyst. """ % globals(), ), ) group.append( "object_name", cps.ObjectNameSubscriber( "Select the object to be used for flagging", cps.NONE, doc="""\ *(Used only when flag is based on an object measurement)* Select the objects whose measurements you want to use for flagging. """, ), ) def object_fn(): if group.source_choice == S_IMAGE: return cpmeas.IMAGE return group.object_name.value group.append( "rules_directory", cps.DirectoryPath( "Rules file location", doc="""\ *(Used only when flagging using "%(S_RULES)s")* Select the location of the rules file that will be used for flagging images. %(IO_FOLDER_CHOICE_HELP_TEXT)s """ % globals(), ), ) def get_directory_fn(): """Get the directory for the rules file name""" return group.rules_directory.get_absolute_path() def set_directory_fn(path): dir_choice, custom_path = group.rules_directory.get_parts_from_path( path) group.rules_directory.join_parts(dir_choice, custom_path) group.append( "rules_file_name", cps.FilenameText( "Rules file name", "rules.txt", get_directory_fn=get_directory_fn, set_directory_fn=set_directory_fn, doc="""\ *(Used only when flagging using "%(S_RULES)s")* The name of the rules file, most commonly from CellProfiler Analyst's Classifier. This file should be a plain text file containing the complete set of rules. Each line of this file should be a rule naming a measurement to be made on an image, for instance: IF (Image_ImageQuality_PowerLogLogSlope_DNA < -2.5, [0.79, -0.79], [-0.94, 0.94]) The above rule will score +0.79 for the positive category and -0.94 for the negative category for images whose power log slope is less than -2.5 pixels and will score the opposite for images whose slope is larger. The filter adds positive and negative and flags the images whose positive score is higher than the negative score. """ % globals(), ), ) def get_rules_class_choices(group=group): """Get the available choices from the rules file""" try: if group.source_choice == S_CLASSIFIER: return self.get_bin_labels(group) elif group.source_choice == S_RULES: rules = self.get_rules(group) nclasses = len(rules.rules[0].weights[0]) return [str(i) for i in range(1, nclasses + 1)] else: return ["None"] rules = self.get_rules(group) nclasses = len(rules.rules[0].weights[0]) return [str(i) for i in range(1, nclasses + 1)] except: return [str(i) for i in range(1, 3)] group.append( "rules_class", cps.MultiChoice( "Class number", choices=["1", "2"], doc="""\ *(Used only when flagging using "%(S_RULES)s")* Select which classes to flag when filtering. The CellProfiler Analyst Classifier user interface lists the names of the classes in order. By default, these are the positive (class 1) and negative (class 2) classes. **FlagImage** uses the first class from CellProfiler Analyst if you choose “1”, etc. Please note the following: - The flag is set if the image falls into the selected class. - You can make multiple class selections. If you do so, the module will set the flag if the image falls into any of the selected classes. """ % globals(), ), ) group.rules_class.get_choices = get_rules_class_choices group.append( "measurement", cps.Measurement( "Which measurement?", object_fn, doc="""Choose the measurement to be used as criteria.""", ), ) group.append( "wants_minimum", cps.Binary( "Flag images based on low values?", True, doc="""\ Select *Yes* to flag images with measurements below the specified cutoff. If the measurement evaluates to Not-A-Number (NaN), then the image is not flagged. """ % globals(), ), ) group.append( "minimum_value", cps.Float("Minimum value", 0, doc="""Set a value as a lower limit."""), ) group.append( "wants_maximum", cps.Binary( "Flag images based on high values?", True, doc="""\ Select *Yes* to flag images with measurements above the specified cutoff. If the measurement evaluates to Not-A-Number (NaN), then the image is not flagged. """ % globals(), ), ) group.append( "maximum_value", cps.Float("Maximum value", 1, doc="""Set a value as an upper limit."""), ) if can_delete: group.append( "remover", cps.RemoveSettingButton("", "Remove this measurement", measurement_settings, group), ) group.append("divider2", cps.Divider(line=True)) measurement_settings.append(group)
def test_01_05_get_parts_from_output_folder_path(self): s = cps.DirectoryPath("whatever") dir_choice, custom_path = s.get_parts_from_path( cpprefs.get_default_output_directory()) self.assertEqual(dir_choice, cps.DEFAULT_OUTPUT_FOLDER_NAME)
def test_01_07_get_parts_from_output_subfolder_path(self): s = cps.DirectoryPath("whatever") dir_choice, custom_path = s.get_parts_from_path( os.path.join(cpprefs.get_default_output_directory(), "2")) self.assertEqual(dir_choice, cps.DEFAULT_OUTPUT_SUBFOLDER_NAME) self.assertEqual(custom_path, "2")
def test_04_06_alter_url(self): s = cps.DirectoryPath("whatever") s.dir_choice = cps.URL_FOLDER_NAME s.custom_path = "http://www.cellprofiler.org" s.alter_for_create_batch_files(TestDirectoryPath.fn_alter_path) self.assertEqual(s.get_absolute_path(), "http://www.cellprofiler.org")
def test_01_08_get_parts_from_abspath(self): s = cps.DirectoryPath("whatever") dir_choice, custom_path = s.get_parts_from_path(self.root_directory) self.assertEqual(dir_choice, cps.ABSOLUTE_FOLDER_NAME) self.assertEqual(custom_path, self.root_directory)
def create_settings(self): '''Create the UI settings for the module''' self.seed_objects_name = cps.ObjectNameSubscriber( "Select the seed objects", cps.NONE, doc="""\ Select the previously identified objects that you want to use as the seeds for measuring branches and distances. Branches and trunks are assigned per seed object. Seed objects are typically not single points/pixels but instead are usually objects of varying sizes.""") self.image_name = cps.ImageNameSubscriber( "Select the skeletonized image", cps.NONE, doc="""\ Select the skeletonized image of the dendrites and/or axons as produced by the **Morph** module’s *Skel* operation.""") self.wants_branchpoint_image = cps.Binary( "Retain the branchpoint image?", False, doc="""\ Select "*%(YES)s*" if you want to save the color image of branchpoints and trunks. This is the image that is displayed in the output window for this module.""" % globals()) self.branchpoint_image_name = cps.ImageNameProvider( "Name the branchpoint image", "BranchpointImage", doc="""\ *(Used only if a branchpoint image is to be retained)* Enter a name for the branchpoint image here. You can then use this image in a later module, such as **SaveImages**.""") self.wants_to_fill_holes = cps.Binary("Fill small holes?", True, doc="""\ The algorithm reskeletonizes the image and this can leave artifacts caused by small holes in the image prior to skeletonizing. These holes result in false trunks and branchpoints. Select "*%(YES)s*" to fill in these small holes prior to skeletonizing.""" % globals()) self.maximum_hole_size = cps.Integer("Maximum hole size", 10, minval=1, doc="""\ *(Used only when filling small holes)* This is the area of the largest hole to fill, measured in pixels. The algorithm will fill in any hole whose area is this size or smaller.""") self.wants_objskeleton_graph = cps.Binary( "Export the skeleton graph relationships?", False, doc="""\ Select "*%(YES)s*" to produce an edge file and a vertex file that gives the relationships between vertices (trunks, branchpoints and endpoints).""" % globals()) self.intensity_image_name = cps.ImageNameSubscriber("Intensity image", cps.NONE, doc="""\ Select the image to be used to calculate the total intensity along the edges between the vertices (trunks, branchpoints, and endpoints).""" ) self.directory = cps.DirectoryPath( "File output directory", doc= 'Select the directory you want to save the graph relationships to.', dir_choices=[ cps.DEFAULT_OUTPUT_FOLDER_NAME, cps.DEFAULT_INPUT_FOLDER_NAME, cps.ABSOLUTE_FOLDER_NAME, cps.DEFAULT_OUTPUT_SUBFOLDER_NAME, cps.DEFAULT_INPUT_SUBFOLDER_NAME ]) self.directory.dir_choice = cps.DEFAULT_OUTPUT_FOLDER_NAME self.vertex_file_name = cps.Text("Vertex file name", "vertices.csv", doc="""\ *(Used only when exporting graph relationships)* Enter the name of the file that will hold the edge information. You can use metadata tags in the file name. Each line of the file is a row of comma-separated values. The first row is the header; this names the file’s columns. Each subsequent row represents a vertex in the skeleton graph: either a trunk, a branchpoint or an endpoint. The file has the following columns: - *image\_number:* The image number of the associated image. - *vertex\_number:* The number of the vertex within the image. - *i:* The I coordinate of the vertex. - *j:* The J coordinate of the vertex. - *label:* The label of the seed object associated with the vertex. - *kind:* The vertex type, with the following choices: - **T:** Trunk - **B:** Branchpoint - **E:** Endpoint """) self.edge_file_name = cps.Text("Edge file name", "edges.csv", doc="""\ *(Used only when exporting graph relationships)* Enter the name of the file that will hold the edge information. You can use metadata tags in the file name. Each line of the file is a row of comma-separated values. The first row is the header; this names the file’s columns. Each subsequent row represents an edge or connection between two vertices (including between a vertex and itself for certain loops). Note that vertices include trunks, branchpoints, and endpoints. The file has the following columns: - *image\_number:* The image number of the associated image. - *v1:* The zero-based index into the vertex table of the first vertex in the edge. - *v2:* The zero-based index into the vertex table of the second vertex in the edge. - *length:* The number of pixels in the path connecting the two vertices, including both vertex pixels. - *total\_intensity:* The sum of the intensities of the pixels in the edge, including both vertex pixel intensities. """)
def create_settings(self): self.mode = cps.Choice("Classify or train?", [MODE_CLASSIFY, MODE_TRAIN]) self.advanced_or_automatic = cps.Choice( "Configuration mode", [AA_AUTOMATIC, AA_ADVANCED], doc="""Do you want to automatically choose the training parameters or use the defaults?""") self.radius = cps.Integer("Radius", DEFAULT_RADIUS, 1) self.n_features = cps.Integer( "Number of features", DEFAULT_N_FEATURES, 1, doc="""The classifier runs a feature reduction set. This creates <i>Eigentextures</i> which are representative texture patches found throughout the image. The module scores each patch around a pixel according to how much it has each of these textures and those scores are fed into the final classifier. Raise the number of features if some of the textures or edges of your classes are misclassified. Lower the number of features to improve processing time or to reduce overfitting if you have a smaller amount of ground truth. """) self.n_estimators = cps.Integer( "Number of estimators", DEFAULT_N_ESTIMATORS, 1, doc="""The classifier uses a voting scheme where it trains this many estimators. It purposefully does a bad job training and makes up for this deficit by having many poor classification judges. This protects against overfitting by not relying on having a single classifier that is very good at classifying the ground truth, but mistakenly uses irrelevant information to do so. Raise the number of estimators if the classifier is making obvious mistakes with unwarranted certainty. Lower the number of estimators to improve processing speed.""") self.min_samples_per_leaf = cps.Integer( "Minimum samples per leaf", DEFAULT_MIN_SAMPLES_PER_LEAF, 1, doc="""This setting determines the minimum number of ground truth pixels that the classifier will use to split a decision tree. There must be at least this number of example pixels in each branch for the classifier to have confidence that the split is real and not just an artifact of an irrelevant measurement. Lower this setting if the classifier does a good job on most of the pixels but does not draw sharp distinctions between one class and another at the border between the classes (e.g. at the edges of cells). Raise this setting if the classifier misclassifies pixels that are clearly not the right class - this is overtraining. """) self.path = cps.DirectoryPath("Classifier folder") def get_directory_fn(): '''Get the directory for the file name''' return self.path.get_absolute_path() def set_directory_fn(path): dir_choice, custom_path = self.path.get_parts_from_path(path) self.path.join_parts(dir_choice, custom_path) self.filename = cps.FilenameText( "Classifier file", "Classifier.cpclassifier", get_directory_fn=get_directory_fn, set_directory_fn=set_directory_fn, exts=[("Pixel classifier (*.cpclassifier)", "*.cpclassifier"), ("All files (*.*)", "*.*")]) self.gt_source = cps.Choice("Ground truth source", [SRC_OBJECTS, SRC_ILASTIK], doc=""" The ground truth data can either be taken from objects or can be the exported TIF "labels" output of Ilastik. """) self.labels_image = cps.ImageNameSubscriber("Ilastik labels image", "labels.tif", doc=""" <i>Used only if the ground truth source is "Ilastik"</i> <br> This image should be the exported labels image from Ilastik. """) self.wants_background_class = cps.Binary( "Do you want a background class?", True) self.background_class_name = cps.Text("Background class name", "Background") self.object_classes = [] self.object_class_count = cps.HiddenCount(self.object_classes, "Object class count") self.add_objects(False) self.add_objects_button = cps.DoSomething("Add another class", "Add", self.add_objects) self.label_classes = [] self.label_class_count = cps.HiddenCount(self.label_classes, "Label class count") self.add_labels(False) self.add_labels_button = cps.DoSomething("Add another class", "Add", self.add_labels) self.images = [] self.image_count = cps.HiddenCount(self.images, "Image count") self.add_image(False) self.add_image_button = cps.DoSomething("Add another image", "Add", self.add_image) self.outputs = [] self.output_count = cps.HiddenCount(self.outputs, "Output count") self.add_output(False) self.add_output_button = cps.DoSomething("Add another output", "Add", self.add_output)