def test_03_01_non_contiguous(self): '''make sure MeasureObjectAreaShape doesn't crash if fed non-contiguous objects''' module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape() module.object_groups[0].name.value = "SomeObjects" module.calculate_zernikes.value = True object_set = cellprofiler.object.ObjectSet() labels = numpy.zeros((10, 20), int) labels[1:9, 1:5] = 1 labels[4:6, 6:19] = 1 objects = cellprofiler.object.Objects() objects.segmented = labels object_set.add_objects(objects, "SomeObjects") module.module_num = 1 image_set_list = cellprofiler.image.ImageSetList() measurements = cellprofiler.measurement.Measurements() pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.RunExceptionEvent)) pipeline.add_listener(callback) workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set_list.get_image_set(0), object_set, measurements, image_set_list) module.run(workspace) values = measurements.get_current_measurement("SomeObjects", "AreaShape_Perimeter") self.assertEqual(len(values), 1) self.assertEqual(values[0], 54)
def load_pipeline(test_case, encoded_data): """Load a pipeline from base-64 encoded data test_case - an instance of unittest.TestCase encoded_data - a pipeline encoded using base-64 The magic incantation to do the above is the following: import base64 fd = open('my_PIPE.mat','rb') bindata = fd.read() fd.close() b64data = base64.b64encode(bindata) """ import cellprofiler.pipeline (matfd, matpath) = tempfile.mkstemp(".mat") matfh = os.fdopen(matfd, "wb") try: data = base64.b64decode(encoded_data) matfh.write(data) matfh.flush() pipeline = cellprofiler.pipeline.Pipeline() handles = scipy.io.matlab.mio.loadmat(matpath, struct_as_record=True) finally: matfh.close() def blowup(pipeline, event): if isinstance(event, (cellprofiler.pipeline.RunExceptionEvent, cellprofiler.pipeline.LoadExceptionEvent)): test_case.assertFalse(event.error.message) pipeline.add_listener(blowup) pipeline.create_from_handles(handles) return pipeline
def test_01_04_load_v4(self): data = """CellProfiler Pipeline: http://www.cellprofiler.org Version:3 DateRevision:20141015195823 GitHash:051040e ModuleCount:1 HasImagePlaneDetails:False MeasureImageOverlap:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:4|show_window:False|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Compare segmented objects, or foreground/background?:Foreground/background segmentation Select the image to be used as the ground truth basis for calculating the amount of overlap:Foo Select the image to be used to test for overlap:Bar Select the objects to be used as the ground truth basis for calculating the amount of overlap:Cell2_0 Select the objects to be tested for overlap against the ground truth:Cell2_1 Calculate earth mover\'s distance?:Yes Maximum # of points:101 Point selection method:Skeleton Maximum distance:102 Penalize missing pixels:Yes """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.measureimageoverlap.MeasureImageOverlap)) self.assertEqual(module.ground_truth, "Foo") self.assertEqual(module.test_img, "Bar")
def test_01_03_load_v3(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:3 DateRevision:20131210175632 GitHash:63ec479 ModuleCount:1 HasImagePlaneDetails:False MeasureImageOverlap:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:3|show_window:False|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True] Compare segmented objects, or foreground/background?:Foreground/background segmentation Select the image to be used as the ground truth basis for calculating the amount of overlap:Foo Select the image to be used to test for overlap:Bar Select the objects to be used as the ground truth basis for calculating the amount of overlap:Cell2_0 Select the objects to be tested for overlap against the ground truth:Cell2_1 """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.measureimageoverlap.MeasureImageOverlap)) self.assertEqual(module.ground_truth, "Foo") self.assertEqual(module.test_img, "Bar") # self.assertEqual(module.object_name_GT, "Cell2_0") # self.assertEqual(module.object_name_ID, "Cell2_1") self.assertFalse(module.wants_emd)
def test_01_02_load_v2(self): data = ('eJztVd1KwzAUTuv82QTxTi9zJV5oycTf3eimiAM3xQ3RK4lbOgJpM9J0bD6B' 'j+Jj+Cg+go9gMtKtrWOdeiPogZCc5PvOyfk4bWrl5mW5AvccBGvl5rZLGYHX' 'DEuXC68EfbkFTwXBkrQh90vwXFBYDjsQHcDifqmISmgX7iB0BL5nVrW2oqa3' 'RQAW1Lykhm2O5o1vxYb2G0RK6neCeZAD62b/VY1bLCh+ZOQWs5AE4xTRftV3' 'eXPQHR3VeDtkpI69OFhZPfQeiQiu3Ihojq9pn7AGfSKpEiLYDenRgHLf8E38' '9O4oL5epvFqHF3usgzVBh0JsX+MvwBifm4Bfi+FXjX9GXBwyCase7hB4RgVp' 'SS4Gw3goI56ViGcBx9zjMIO3BJL30H4R7R45raA3S965BH8O3CvtflP9WTw7' 'wbNBnf9At60i0vYT3U4yePlUXu03Ko2HNg9GXavj3H2xX+P3XUjhI4vw+X/e' 'n+c9g+n9Ff8eh/0Ipvf1Bkj2tfZbhLGu4PrdE443/DkHDuO4LUlfOpdq0VSL' 'z3XkJ8SP38dWq0JG/em6x3q8H38nnz0h33IGL2de3LR+s+i9OQUPUvgPhsiG' 'ig==') pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(zlib.decompress(base64.b64decode(data)))) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.loaddata.LoadText)) self.assertEqual(module.csv_file_name, "1049.csv") self.assertTrue(module.wants_images.value) self.assertTrue(module.wants_image_groupings.value) self.assertEqual(len(module.metadata_fields.selections), 1) self.assertEqual(module.metadata_fields.selections[0], "SBS_doses") self.assertEqual(module.image_directory.dir_choice, cellprofiler.setting.DEFAULT_INPUT_FOLDER_NAME) self.assertFalse(module.wants_rows.value)
def test_01_02_load_v2(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:3 DateRevision:20120209212234 ModuleCount:1 HasImagePlaneDetails:False Images:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:2|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)] :{"ShowFiltered"\x3A false} Filter choice:%s Filter:or (directory does startwith "foo") (file does contain "bar") """ for fc, fctext in ((cellprofiler.modules.images.FILTER_CHOICE_CUSTOM, "Custom"), (cellprofiler.modules.images.FILTER_CHOICE_IMAGES, "Images only"), (cellprofiler.modules.images.FILTER_CHOICE_NONE, "No filtering")): pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(cStringIO.StringIO(data % fctext)) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.images.Images)) self.assertEqual(module.filter_choice, fc) self.assertEqual(module.filter.value, 'or (directory does startwith "foo") (file does contain "bar")')
def set_file_chooser_path(self, path): file_names = [] for file_name in os.listdir(path): ext = os.path.splitext(file_name)[1].lower() if len(ext) > 0 and ext[1:] in cellprofiler.preferences.EXT_PIPELINE_CHOICES: file_names.append(file_name) self.file_chooser.DeleteAllItems() module_count = [None] def on_pipeline_event(caller, event): if isinstance(event, cellprofiler.pipeline.LoadExceptionEvent): module_count[0] = None elif isinstance(event, cellprofiler.pipeline.PipelineLoadedEvent): module_count[0] = len(caller.modules()) pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_listener(on_pipeline_event) for file_name in file_names: file_path = os.path.join(path, file_name) mtime = os.path.getmtime(file_path) mtime = datetime.datetime.fromtimestamp(mtime) mtime = mtime.ctime() pipeline.load(file_path) if module_count[0] is not None: index = self.file_chooser.InsertStringItem( sys.maxint, file_name) self.file_chooser.SetStringItem(index, FC_DATE_COLUMN, mtime) self.file_chooser.SetStringItem(index, FC_MODULE_COUNT_COLUMN, str(module_count[0])) self.file_chooser.SetColumnWidth(FC_FILENAME_COLUMN, wx.LIST_AUTOSIZE) self.file_chooser.SetColumnWidth(FC_DATE_COLUMN, wx.LIST_AUTOSIZE)
def test_04_02_load_saturation_blur(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:8925 FromMatlab:True MeasureImageSaturationBlur:[module_num:1|svn_version:\'8913\'|variable_revision_number:4|show_window:False|notes:\x5B\x5D] What did you call the image you want to check for saturation?:Image1 What did you call the image you want to check for saturation?:Image2 What did you call the image you want to check for saturation?:Image3 What did you call the image you want to check for saturation?:Do not use What did you call the image you want to check for saturation?:Do not use What did you call the image you want to check for saturation?:Do not use Do you want to also check the above images for image quality (called blur earlier)?:Yes If you chose to check images for image quality above, enter the window size of LocalFocusScore measurement (A suggested value is 2 times ObjectSize)?:25 """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] self.assertTrue(isinstance(module, M.MeasureImageQuality)) self.assertEqual(len(module.image_groups), 3) for i in range(3): group = module.image_groups[i] self.assertEqual(group.image_names, "Image%d" % (i + 1)) self.assertTrue(group.check_blur) self.assertEqual(group.scale_groups[0].scale, 25) self.assertTrue(group.check_saturation) self.assertFalse(group.calculate_threshold)
def test_load_v5(): data = r'''CellProfiler Pipeline: http://www.cellprofiler.org Version:3 DateRevision:20150414135713 GitHash:3bad577 ModuleCount:2 HasImagePlaneDetails:False EnhanceOrSuppressFeatures:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:5|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Select the input image:Dendrite Name the output image:EnhancedDendrite Select the operation:Enhance Feature size:10 Feature type:Neurites Range of hole sizes:1,10 Smoothing scale:2.0 Shear angle:0 Decay:0.95 Enhancement method:Tubeness Speed and accuracy:Slow / circular EnhanceOrSuppressFeatures:[module_num:2|svn_version:\'Unknown\'|variable_revision_number:5|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Select the input image:Axon Name the output image:EnhancedAxon Select the operation:Enhance Feature size:10 Feature type:Neurites Range of hole sizes:1,10 Smoothing scale:2.0 Shear angle:0 Decay:0.95 Enhancement method:Line structures Speed and accuracy:Fast / hexagonal ''' pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) assert len(pipeline.modules()) == 2 module = pipeline.modules()[0] assert isinstance(module, cellprofiler.modules.enhanceorsuppressfeatures.EnhanceOrSuppressFeatures) assert module.x_name == "Dendrite" assert module.y_name == "EnhancedDendrite" assert module.method == cellprofiler.modules.enhanceorsuppressfeatures.ENHANCE assert module.enhance_method == cellprofiler.modules.enhanceorsuppressfeatures.E_NEURITES assert module.smoothing == 2.0 assert module.object_size == 10 assert module.hole_size.min == 1 assert module.hole_size.max == 10 assert module.angle == 0 assert module.decay == .95 assert module.neurite_choice == cellprofiler.modules.enhanceorsuppressfeatures.N_TUBENESS assert module.speckle_accuracy == cellprofiler.modules.enhanceorsuppressfeatures.S_SLOW module = pipeline.modules()[1] assert isinstance(module, cellprofiler.modules.enhanceorsuppressfeatures.EnhanceOrSuppressFeatures) assert module.speckle_accuracy == cellprofiler.modules.enhanceorsuppressfeatures.S_FAST
def test_01_02_load_v1(self): data = ('eJztWF9v0zAQd7vuH0ioiAd4tPayFdYo7TZpq9C2siJRWEu1VWPTNMBdndZS' 'EleJM1bQJB75WHykfQTsLmkSE5a03RBITRWld7nf/e7OzsVOrdzcL7+CG4oK' 'a+VmXiM6hg0dMY1aRgmabBXuWRgx3IbULMFm14FvHR0W12Bho1TcLBXWYVFV' 't8B4R6pae8Qv8AkAc/y6wM+0e2vWlVOBU8iHmDFiduxZkAHPXP1Pfh4hi6CW' 'jo+Q7mDbp/D0VVOjzX5veKtG246O68gIGvOj7hgtbNnvNQ/o3m6QS6wfkq9Y' 'SsEzO8AXxCbUdPGuf1k75KVM4hV1aCz4dUhJdRB1yQb0wv4N8O0zEXV7HLDP' 'ujIx2+SCtB2kQ2KgzjAK4U+N8TcT8jcDKvXyALcbg8uCcBzibOJLln99ic4Z' 'NBA77ybx80DyI+QDbPNBaYtQEueRCvlJgTUXdxyDW5b4l4f8sNWHCGo8G2pB' 'qkHWxZBapENMXmdhkKhOUflVKDQpg46Nk+eXCfnJAFUpbiTBpUO4NKjT8eZF' 'QVUTzc+nUr5CrmANOTqDVTE5YYVYWNS0P1EcQdychPMOD7cI/DrvxvBFzes6' 'Rha2Gaxj0um2qDVO3Cf8qZwk7n+NL8m8GoVvM4ZvDoTHRch7OjVFs/ubdfX6' '433xyX2sEIG7Sz65r9R5SSfh+x7D9w6Ex1HIH1d2Gi/FAgVvKy9yn4T0Aev6' 'Af2yfVrON85ynmaP6o5hbp+q+a2zb4XV4tWN8SHhyIEyF5n3KPF3Y+LflOIX' 'sojhhPcIN7D1q1xeqGrUZF1XV3R1FdT3NZPEeTw/2rrivvpl1PttsAjpWNTp' '3T9/VL/2+SFfGuHeXT0/U9wUN8X9H7jdAG7aN6a4UXHXAZz8fpXXv8L+M7h9' 'vj0H4fkm5HO+pOlZVHyfsRRj8BHBVnSK2je7eGWf/60GNvRJ9rNLEs/Sn3is' 'wSZXudnrRtdrMcJ/MO80/2Xnb6+zXF+/7tc74/DNpH7nexiDy7iVErgfYLRx' 'XbnF3sttXPtf+3UFIg==') pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(zlib.decompress(base64.b64decode(data)))) self.assertEqual(len(pipeline.modules()), 2) module = pipeline.modules()[1] self.assertEqual(module.x_name, 'DNA') self.assertEqual(module.y_name, 'ResizedDNA') self.assertEqual(module.size_method, cellprofiler.modules.resize.R_BY_FACTOR) self.assertAlmostEqual(module.resizing_factor.value, .25) self.assertEqual(module.interpolation, cellprofiler.modules.resize.I_NEAREST_NEIGHBOR)
def test_01_04_load_v4(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:9524 LoadSingleImage:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:4|show_window:True|notes:\x5B\x5D] Folder containing the image file:Default Input Folder\x7CNone Filename of the image to load (Include the extension, e.g., .tif):foo.tif Name the image that will be loaded:DNA Rescale image?:No Filename of the image to load (Include the extension, e.g., .tif):bar.tif Name the image that will be loaded:Cytoplasm Rescale image?:Yes """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO(data)) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.loadsingleimage.LoadSingleImage)) self.assertEqual(len(module.file_settings), 2) fs = module.file_settings[0] self.assertEqual(fs.file_name, "foo.tif") self.assertEqual(fs.image_objects_choice, cellprofiler.modules.loadsingleimage.IO_IMAGES) self.assertEqual(fs.image_name, "DNA") self.assertFalse(fs.rescale) fs = module.file_settings[1] self.assertEqual(fs.file_name, "bar.tif") self.assertEqual(fs.image_name, "Cytoplasm") self.assertTrue(fs.rescale)
def test_load_v1(): data = ('eJztWNFO2zAUdUqBsUkr28v26Ee60aotQ4NqKu0oEtUIVLRiQohtpnXba' 'EkcOQlrNyHtcZ+0x33OHvcJs4NDUhMIbccmpqay2nt9zz3Xx0lsV600dy' 'qv4Wo2B9VKM9PRdAzrOnI6hBpFaDrLcJNi5OA2JGYRqsSEKhrAfB7mV4o' 'rheLqOizkcutgvEupqQ/Z19pjAObY9z3WEqJrVthKqHG7gR1HM7v2LEiC' 'p8L/g7UDRDV0ouMDpLvYDih8f83skObAuuhSSdvV8S4ywsHs2nWNE0ztv' 'Y4PFN11rY/1hvYZS0Pww/bxqWZrxBR4kV/2XvASR+LlOnyfD3RQJB24Lq' 'mQn8dvgyA+GaHbo1D8orA1s62dam0X6VAzUPeiCm8eYvLNS/m4rQ5qPI2' 'HL8fgFyU8b03cdzJbfdRyoIGcVu8meVJSnpRXx5bZQ2YLt4N6cjF5lKE8' 'CliZQAfBfiMd7kt4blcJNIkDXRsH8xFXf2IoTwLkX46H2yWXcXMSzr983' 'AII6oy7D59I4+V2FXeQqzvQmy1Y1ShuOYQOJqrjX+Kixj0zNO4ZcMietr' 'vK9zdwk75/bktX+T2R/4P6RPElh/iS7Pk08SR8X2P43oBhXbn9bmmj/op' 'vBHAp+zz9nltvsa7vk0+lo0qmfpz2PZtEdw2zdJTLrB9/yS8Xzs6DGxpD' 'es505LhHqb8XU/+aVD+3eQ2HGFFR2IuzdIa72AbG6QlfQfiqaBB4Jqnz5' '9xo6/e4POUYPaLWF2+x71LiWrfPH7XOB/yQbUGwdZfeS1PcFPc/4soh3P' 'Q5nuJGxS0qV6938jmDx38A199vz8Dw/cbtFttiWJTw/yVo1vAOz3ZWJ6h' '9fnrN7rCftdBBlvP0Y3i2JZ7tq3jw+aGOUNu1LIpt27Zw6yPvEce9PdoQ' 'PQ3RI+u5EMEf1iXBPqnk9fMg6x/My6+NcfgSymW+BzG4pFCS476B0eZ96' 'Zp4f2zjxv8G/FcCeg==') pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(zlib.decompress(base64.b64decode(data)))) assert len(pipeline.modules()) == 2 module = pipeline.modules()[1] assert module.module_name == 'EnhanceOrSuppressFeatures' assert module.x_name.value == 'MyImage' assert module.y_name.value == 'MyEnhancedImage' assert module.method.value == cellprofiler.modules.enhanceorsuppressfeatures.ENHANCE assert module.object_size == 17
def test_01_03_load_v3(self): data = ('eJztVUtPg0AQXihtfCTGgwePezIelICv2F5MqzE2kWqE+Dhu28WSLGwDS338' 'An+CP9Of4FKX8pAUbT2Y6AQyO7PzzTczLGA0rfNmC+6rGjSa1rbtEAwvCWI2' '9d0G9NgWPPYxYrgPqdeAN1yf4B7UD/jV0Hcbe/twR9PqYDaR2sYKV68LANS4' '5grIYqsqbCl1R7aJGXO8+6AKFLAu/G/8vka+g7oEXyMS4iChiP1tz6bW03Cy' 'ZdB+SHAHuelgLp3Q7WI/uLBjoNi+dB4xMZ1nnGshDrvCIydwqCfwIn/eO+Gl' 'LMdrDujDqc/LyeVvIdYbmIw/gax/PDc5mZtUMLellD+KPwNJvFIQv5aKXxX2' 'CbZRSBhse8OQwVNK+tiP82kl+aRMPgmoAndYglsA2ToiW9f26movGH2Ft5LB' 'V8Adn/Vv6r8MJ2dwMujQOea2pWuRzMObxtVyuFhi3KLQEe72m+dzVp5/3N/E' 'vYDp5yv9/o3PI5h+/jdA9v2J7B4mZOjT6L/oq+744x2ohKI+w49MPecLiy8+' '97FYkD9dj8xXSyX95/tO5vF2NAtfpYBvuQSniD9yfn5fmffmlHhQEP/dfqQf' 'qCvhUSY1feT/kHfzN4ip') pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(zlib.decompress(base64.b64decode(data)))) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.loaddata.LoadText)) self.assertEqual(module.csv_file_name, "1049.csv") self.assertTrue(module.wants_images.value) self.assertFalse(module.wants_image_groupings.value)
def test_04_01_extent(self): module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape() module.object_groups[0].name.value = "SomeObjects" module.calculate_zernikes.value = True object_set = cellprofiler.object.ObjectSet() labels = numpy.zeros((10, 20), int) # 3/4 of a square is covered labels[5:7, 5:10] = 1 labels[7:9, 5:15] = 1 objects = cellprofiler.object.Objects() objects.segmented = labels object_set.add_objects(objects, "SomeObjects") module.module_num = 1 image_set_list = cellprofiler.image.ImageSetList() measurements = cellprofiler.measurement.Measurements() pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.RunExceptionEvent)) pipeline.add_listener(callback) workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set_list.get_image_set(0), object_set, measurements, image_set_list) module.run(workspace) values = measurements.get_current_measurement( "SomeObjects", "_".join((cellprofiler.modules.measureobjectsizeshape.AREA_SHAPE, cellprofiler.modules.measureobjectsizeshape.F_EXTENT))) self.assertEqual(len(values), 1) self.assertAlmostEqual(values[0], .75)
def test_01_03_load_v3(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:3 DateRevision:20120112154631 ModuleCount:1 HasImagePlaneDetails:False Metadata:[module_num:2|svn_version:\'Unknown\'|variable_revision_number:3|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)] Extract metadata?:Yes Extraction method count:2 Metadata extraction method:Extract from file/folder names Metadata source:File name Regular expression:^Channel(?P<ChannelNumber>\x5B12\x5D)-(?P<Index>\x5B0-9\x5D+)-(?P<WellRow>\x5BA-H\x5D)-(?P<WellColumn>\x5B0-9\x5D{2}).tif$ Regular expression:(?P<Date>\x5B0-9\x5D{4}_\x5B0-9\x5D{2}_\x5B0-9\x5D{2})$ Extract metadata from:All images Select the filtering criteria:or (file does contain "Channel2") Metadata file location: Match file and image metadata:\x5B\x5D Case insensitive matching:No Metadata extraction method:Import from file Metadata source:Folder name Regular expression:^(?P<Plate>.*)_(?P<Well>\x5BA-P\x5D\x5B0-9\x5D{2})_s(?P<Site>\x5B0-9\x5D)_w(?P<ChannelNumber>\x5B0-9\x5D) Regular expression:Example(?P<Project>\x5B^\\\\\\\\\x5D+)Images Extract metadata from:Images matching a rule Select the filtering criteria:or (file does contain "") Metadata file location\x3A:/imaging/analysis/metadata.csv Match file and image metadata:\x5B{\'Image Metadata\'\x3A u\'ChannelNumber\', \'CSV Metadata\'\x3A u\'Wavelength\'}\x5D Case insensitive matching:Yes """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(cStringIO.StringIO(data)) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.metadata.Metadata)) self.assertTrue(module.wants_metadata) self.assertEqual(module.data_type_choice, cellprofiler.modules.metadata.DTC_TEXT) self.assertEqual(len(module.extraction_methods), 2) em0, em1 = module.extraction_methods self.assertEqual(em0.extraction_method, cellprofiler.modules.metadata.X_MANUAL_EXTRACTION) self.assertEqual(em0.source, cellprofiler.modules.metadata.XM_FILE_NAME) self.assertEqual(em0.file_regexp.value, r"^Channel(?P<ChannelNumber>[12])-(?P<Index>[0-9]+)-(?P<WellRow>[A-H])-(?P<WellColumn>[0-9]{2}).tif$") self.assertEqual(em0.folder_regexp.value, r"(?P<Date>[0-9]{4}_[0-9]{2}_[0-9]{2})$") self.assertEqual(em0.filter_choice, cellprofiler.modules.metadata.F_ALL_IMAGES) self.assertEqual(em0.filter, 'or (file does contain "Channel2")') self.assertFalse(em0.wants_case_insensitive) self.assertEqual(em1.extraction_method, cellprofiler.modules.metadata.X_IMPORTED_EXTRACTION) self.assertEqual(em1.source, cellprofiler.modules.metadata.XM_FOLDER_NAME) self.assertEqual(em1.filter_choice, cellprofiler.modules.metadata.F_FILTERED_IMAGES) self.assertEqual(em1.csv_location.get_dir_choice(), cellprofiler.setting.ABSOLUTE_FOLDER_NAME) self.assertEqual(em1.csv_location.get_custom_path(), "/imaging/analysis") self.assertEqual(em1.csv_filename.value, "metadata.csv") self.assertEqual(em1.csv_joiner.value, "[{'Image Metadata': u'ChannelNumber', 'CSV Metadata': u'Wavelength'}]") self.assertTrue(em1.wants_case_insensitive)
def test_01_02_load_v2(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:9524 LoadSingleImage:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:2|show_window:True|notes:\x5B\x5D] Folder containing the image file:Default Input Folder\x7CNone Filename of the image to load (Include the extension, e.g., .tif):foo.tif Name the image that will be loaded:DNA Filename of the image to load (Include the extension, e.g., .tif):bar.tif Name the image that will be loaded:Cytoplasm LoadSingleImage:[module_num:2|svn_version:\'Unknown\'|variable_revision_number:2|show_window:True|notes:\x5B\x5D] Folder containing the image file:Default Output Folder\x7CNone Filename of the image to load (Include the extension, e.g., .tif):baz.tif Name the image that will be loaded:GFP LoadSingleImage:[module_num:3|svn_version:\'Unknown\'|variable_revision_number:2|show_window:True|notes:\x5B\x5D] Folder containing the image file:Elsewhere...\x7CNone Filename of the image to load (Include the extension, e.g., .tif):baz.tif Name the image that will be loaded:GFP LoadSingleImage:[module_num:4|svn_version:\'Unknown\'|variable_revision_number:2|show_window:True|notes:\x5B\x5D] Folder containing the image file:URL\x7Chttps\x3A//svn.broadinstitute.org/CellProfiler/trunk/ExampleImages/ExampleSBSImages Filename of the image to load (Include the extension, e.g., .tif):Channel1-01-A-01.tif Name the image that will be loaded:DNA1 """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO(data)) self.assertEqual(len(pipeline.modules()), 4) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.loadsingleimage.LoadSingleImage)) self.assertEqual(len(module.file_settings), 2) fs = module.file_settings[0] self.assertEqual(fs.file_name, "foo.tif") self.assertEqual(fs.image_name, "DNA") fs = module.file_settings[1] self.assertEqual(fs.file_name, "bar.tif") self.assertEqual(fs.image_name, "Cytoplasm") module = pipeline.modules()[3] fs = module.file_settings[0] self.assertEqual( fs.file_name, "https://svn.broadinstitute.org/CellProfiler/trunk/ExampleImages/" "ExampleSBSImages/Channel1-01-A-01.tif") dir_choice = [ cellprofiler.setting.DEFAULT_INPUT_FOLDER_NAME, cellprofiler.setting.DEFAULT_OUTPUT_FOLDER_NAME, cellprofiler.setting.ABSOLUTE_FOLDER_NAME, cellprofiler.setting.URL_FOLDER_NAME] for i, module in enumerate(pipeline.modules()): self.assertTrue(isinstance(module, cellprofiler.modules.loadsingleimage.LoadSingleImage)) self.assertEqual(module.directory.dir_choice, dir_choice[i])
def test_01_03_load_v2(self): data = """CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:10865 MeasureTexture:[module_num:1|svn_version:\'1\'|variable_revision_number:2|show_window:True|notes:\x5B\x5D] Hidden:2 Hidden:2 Hidden:2 Select an image to measure:rawDNA Select an image to measure:rawGFP Select objects to measure:Cells Select objects to measure:Nuclei Texture scale to measure:3 Texture scale to measure:5 Measure Gabor features?:Yes Number of angles to compute for Gabor:6 MeasureTexture:[module_num:2|svn_version:\'1\'|variable_revision_number:2|show_window:True|notes:\x5B\x5D] Hidden:2 Hidden:2 Hidden:2 Select an image to measure:rawDNA Select an image to measure:rawGFP Select objects to measure:Cells Select objects to measure:Nuclei Texture scale to measure:3 Texture scale to measure:5 Measure Gabor features?:No Number of angles to compute for Gabor:6 """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) self.assertEqual(len(pipeline.modules()), 2) for i, wants_gabor in enumerate((True, False)): module = pipeline.modules()[i] self.assertTrue(isinstance(module, cellprofiler.modules.measuretexture.MeasureTexture)) self.assertEqual(len(module.image_groups), 2) self.assertEqual(module.image_groups[0].image_name, "rawDNA") self.assertEqual(module.image_groups[1].image_name, "rawGFP") self.assertEqual(len(module.object_groups), 2) self.assertEqual(module.object_groups[0].object_name, "Cells") self.assertEqual(module.object_groups[1].object_name, "Nuclei") self.assertEqual(len(module.scale_groups), 2) self.assertEqual(module.scale_groups[0].scale, 3) self.assertEqual(len(module.scale_groups[0].angles.get_selections()), 1) self.assertEqual(module.scale_groups[0].angles.get_selections()[0], cellprofiler.modules.measuretexture.H_HORIZONTAL) self.assertEqual(module.scale_groups[1].scale, 5) self.assertEqual(len(module.scale_groups[1].angles.get_selections()), 1) self.assertEqual(module.scale_groups[1].angles.get_selections()[0], cellprofiler.modules.measuretexture.H_HORIZONTAL) self.assertEqual(module.wants_gabor, wants_gabor) self.assertEqual(module.gabor_angles, 6)
def make_workspace(self, input_image, input_mask=None, reference_image=None, reference_mask=None, measurement=None): pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.RunExceptionEvent)) pipeline.add_listener(callback) object_set = cellprofiler.object.ObjectSet() image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) measurements = cellprofiler.measurement.Measurements() module_number = 1 module = cellprofiler.modules.rescaleintensity.RescaleIntensity() module.x_name.value = INPUT_NAME if isinstance(input_image, (tuple, list)): first_input_image = input_image[0] else: first_input_image = input_image if isinstance(input_mask, (tuple, list)): first_input_mask = input_mask[0] else: first_input_mask = input_mask if first_input_mask is None: image = cellprofiler.image.Image(first_input_image) else: image = cellprofiler.image.Image(first_input_image, first_input_mask) ii = cellprofiler.modules.injectimage.InjectImage(INPUT_NAME, input_image, input_mask) ii.module_num = module_number module_number += 1 pipeline.add_module(ii) image_set.add(INPUT_NAME, image) module.y_name.value = OUTPUT_NAME if reference_image is not None: module.matching_image_name.value = REFERENCE_NAME if reference_mask is None: image = cellprofiler.image.Image(reference_image) else: image = cellprofiler.image.Image(reference_image, mask=reference_mask) image_set.add(REFERENCE_NAME, image) ii = cellprofiler.modules.injectimage.InjectImage(REFERENCE_NAME, reference_image, reference_mask) ii.module_num = module_number module_number += 1 pipeline.add_module(ii) module.module_num = module_number pipeline.add_module(module) if measurement is not None: module.divisor_measurement.value = MEASUREMENT_NAME measurements.add_image_measurement(MEASUREMENT_NAME, measurement) workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set, object_set, measurements, image_set_list) return workspace, module
def test_01_01_load_v1(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:9524 LoadSingleImage:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:1|show_window:True|notes:\x5B\x5D] Folder containing the image file:Default Input Folder Name of the folder containing the image file:path1 Filename of the image to load (Include the extension, e.g., .tif):foo.tif Name the image that will be loaded:DNA Filename of the image to load (Include the extension, e.g., .tif):bar.tif Name the image that will be loaded:Cytoplasm LoadSingleImage:[module_num:2|svn_version:\'Unknown\'|variable_revision_number:1|show_window:True|notes:\x5B\x5D] Folder containing the image file:Default Output Folder Name of the folder containing the image file:path2 Filename of the image to load (Include the extension, e.g., .tif):baz.tif Name the image that will be loaded:GFP LoadSingleImage:[module_num:3|svn_version:\'Unknown\'|variable_revision_number:1|show_window:True|notes:\x5B\x5D] Folder containing the image file:Custom folder Name of the folder containing the image file:path3 Filename of the image to load (Include the extension, e.g., .tif):baz.tif Name the image that will be loaded:GFP LoadSingleImage:[module_num:4|svn_version:\'Unknown\'|variable_revision_number:1|show_window:True|notes:\x5B\x5D] Folder containing the image file:Custom with metadata Name of the folder containing the image file:path4 Filename of the image to load (Include the extension, e.g., .tif):baz.tif Name the image that will be loaded:GFP """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO(data)) self.assertEqual(len(pipeline.modules()), 4) dir_choice = [ cellprofiler.setting.DEFAULT_INPUT_FOLDER_NAME, cellprofiler.setting.DEFAULT_OUTPUT_FOLDER_NAME, cellprofiler.setting.ABSOLUTE_FOLDER_NAME, cellprofiler.setting.ABSOLUTE_FOLDER_NAME] for i, module in enumerate(pipeline.modules()): self.assertTrue(isinstance(module, cellprofiler.modules.loadsingleimage.LoadSingleImage)) self.assertEqual(module.directory.dir_choice, dir_choice[i]) self.assertEqual(module.directory.custom_path, "path%d" % (i + 1)) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.loadsingleimage.LoadSingleImage)) self.assertEqual(len(module.file_settings), 2) fs = module.file_settings[0] self.assertEqual(fs.file_name, "foo.tif") self.assertEqual(fs.image_name, "DNA") fs = module.file_settings[1] self.assertEqual(fs.file_name, "bar.tif") self.assertEqual(fs.image_name, "Cytoplasm")
def test_01_05_load_v4(): data = r'''CellProfiler Pipeline: http://www.cellprofiler.org Version:2 DateRevision:20120516145742 EnhanceOrSuppressFeatures:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:4|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)] Select the input image:Dendrite Name the output image:EnhancedDendrite Select the operation:Enhance Feature size:10 Feature type:Neurites Range of hole sizes:1,10 Smoothing scale:2.0 Shear angle:0 Decay:0.95 Enhancement method:Tubeness EnhanceOrSuppressFeatures:[module_num:2|svn_version:\'Unknown\'|variable_revision_number:4|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)] Select the input image:Axon Name the output image:EnhancedAxon Select the operation:Enhance Feature size:10 Feature type:Neurites Range of hole sizes:1,10 Smoothing scale:2.0 Shear angle:0 Decay:0.95 Enhancement method:Line structures ''' pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) assert len(pipeline.modules()) == 2 module = pipeline.modules()[0] assert isinstance(module, cellprofiler.modules.enhanceorsuppressfeatures.EnhanceOrSuppressFeatures) assert module.x_name == "Dendrite" assert module.y_name == "EnhancedDendrite" assert module.method == cellprofiler.modules.enhanceorsuppressfeatures.ENHANCE assert module.enhance_method == cellprofiler.modules.enhanceorsuppressfeatures.E_NEURITES assert module.smoothing == 2.0 assert module.object_size == 10 assert module.hole_size.min == 1 assert module.hole_size.max == 10 assert module.angle == 0 assert module.decay == .95 assert module.neurite_choice == cellprofiler.modules.enhanceorsuppressfeatures.N_TUBENESS module = pipeline.modules()[1] assert isinstance(module, cellprofiler.modules.enhanceorsuppressfeatures.EnhanceOrSuppressFeatures) assert module.neurite_choice == cellprofiler.modules.enhanceorsuppressfeatures.N_GRADIENT
def test_04_01_load_objects(self): r = numpy.random.RandomState() r.seed(41) labels = numpy.random.randint(0, 10, size=(30, 40)) filename = "myobjects.png" directory = tempfile.mkdtemp() cellprofiler.preferences.set_default_image_directory(directory) pilimage = PIL.Image.fromarray(labels.astype(numpy.uint8), "L") pilimage.save(os.path.join(directory, filename)) del pilimage try: module = cellprofiler.modules.loadsingleimage.LoadSingleImage() module.module_num = 1 module.directory.set_dir_choice(cellprofiler.modules.loadsingleimage.DEFAULT_INPUT_FOLDER_NAME) fs = module.file_settings[0] fs.file_name.value = filename fs.image_objects_choice.value = cellprofiler.modules.loadsingleimage.IO_OBJECTS fs.objects_name.value = OBJECTS_NAME pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) m = cellprofiler.measurement.Measurements() object_set = cellprofiler.object.ObjectSet() image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set, object_set, m, image_set_list) module.prepare_run(workspace) module.run(workspace) o = object_set.get_objects(OBJECTS_NAME) numpy.testing.assert_equal(labels, o.segmented) self.assertEqual(m.get_current_image_measurement( "_".join((cellprofiler.measurement.C_COUNT, OBJECTS_NAME))), 9) self.assertEqual(m.get_current_image_measurement( "_".join((cellprofiler.modules.loadsingleimage.C_OBJECTS_FILE_NAME, OBJECTS_NAME))), filename) self.assertEqual(m.get_current_image_measurement( "_".join((cellprofiler.modules.loadsingleimage.C_OBJECTS_PATH_NAME, OBJECTS_NAME))), directory) for feature in (cellprofiler.measurement.M_LOCATION_CENTER_X, cellprofiler.measurement.M_LOCATION_CENTER_Y, cellprofiler.measurement.M_NUMBER_OBJECT_NUMBER): values = m.get_current_measurement(OBJECTS_NAME, feature) self.assertEqual(len(values), 9) finally: try: os.remove(os.path.join(directory, filename)) os.rmdir(directory) except: print("Failed to delete directory " + directory)
def test_01_03_load_v3(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:10104 Resize:[module_num:1|svn_version:\'10104\'|variable_revision_number:3|show_window:True|notes:\x5B\x5D] Select the input image:DNA Name the output image:ResizedDNA Select resizing method:Resize by specifying desired final dimensions Resizing factor:0.25 Width of the final image, in pixels:141 Height of the final image, in pixels:169 Interpolation method:Bilinear Additional image count:1 Select the additional image?:Actin Name the output image:ResizedActin Resize:[module_num:2|svn_version:\'10104\'|variable_revision_number:3|show_window:True|notes:\x5B\x5D] Select the input image:DNA Name the output image:ResizedDNA Select resizing method:Resize by specifying desired final dimensions Resizing factor:0.25 Width of the final image, in pixels:100 Height of the final image, in pixels:100 Interpolation method:Bicubic Additional image count:0 """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) self.assertEqual(len(pipeline.modules()), 2) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.resize.Resize)) self.assertEqual(module.x_name, "DNA") self.assertEqual(module.y_name, "ResizedDNA") self.assertEqual(module.size_method, cellprofiler.modules.resize.R_TO_SIZE) self.assertAlmostEqual(module.resizing_factor.value, 0.25) self.assertEqual(module.specific_width, 141) self.assertEqual(module.specific_height, 169) self.assertEqual(module.interpolation, cellprofiler.modules.resize.I_BILINEAR) self.assertEqual(module.additional_image_count.value, 1) additional_image = module.additional_images[0] self.assertEqual(additional_image.input_image_name, "Actin") self.assertEqual(additional_image.output_image_name, "ResizedActin") module = pipeline.modules()[1] self.assertTrue(isinstance(module, cellprofiler.modules.resize.Resize)) self.assertEqual(module.interpolation, cellprofiler.modules.resize.I_BICUBIC)
def test_test_load_v3(): data = r'''CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:10999 EnhanceOrSuppressFeatures:[module_num:1|svn_version:\'10591\'|variable_revision_number:3|show_window:True|notes:\x5B\x5D] Select the input image:DNA Name the output image:EnhancedTexture Select the operation:Enhance Feature size:10 Feature type:Texture Range of hole sizes:1,10 Smoothing scale:3.5 Shear angle:45 Decay:0.90 EnhanceOrSuppressFeatures:[module_num:2|svn_version:\'10591\'|variable_revision_number:3|show_window:True|notes:\x5B\x5D] Select the input image:EnhancedTexture Name the output image:EnhancedDIC Select the operation:Enhance Feature size:10 Feature type:DIC Range of hole sizes:1,10 Smoothing scale:1.5 Shear angle:135 Decay:0.99 ''' pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) assert len(pipeline.modules()) == 2 module = pipeline.modules()[0] assert isinstance(module, cellprofiler.modules.enhanceorsuppressfeatures.EnhanceOrSuppressFeatures) assert module.x_name == "DNA" assert module.y_name == "EnhancedTexture" assert module.method == cellprofiler.modules.enhanceorsuppressfeatures.ENHANCE assert module.enhance_method == cellprofiler.modules.enhanceorsuppressfeatures.E_TEXTURE assert module.smoothing == 3.5 assert module.object_size == 10 assert module.hole_size.min == 1 assert module.hole_size.max == 10 assert module.angle == 45 assert module.decay == .9 assert module.speckle_accuracy == cellprofiler.modules.enhanceorsuppressfeatures.S_SLOW module = pipeline.modules()[1] assert isinstance(module, cellprofiler.modules.enhanceorsuppressfeatures.EnhanceOrSuppressFeatures) assert module.enhance_method == cellprofiler.modules.enhanceorsuppressfeatures.E_DIC
def test_01_05_load_v5(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:3 DateRevision:300 GitHash: ModuleCount:1 HasImagePlaneDetails:False Metadata:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:4|show_window:False|notes:\x5B\'The Metadata module optionally allows you to extract information describing your images (i.e, metadata) which will be stored along with your measurements. This information can be contained in the file name and/or location, or in an external file.\'\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Extract metadata?:Yes Metadata data type:Text Metadata types:{} Extraction method count:2 Metadata extraction method:Import from file Metadata source:File name Regular expression to extract from file name:^(?P<Plate>.*)_(?P<Well>\x5BA-P\x5D\x5B0-9\x5D{2})_s(?P<Site>\x5B0-9\x5D)_w(?P<ChannelNumber>\x5B0-9\x5D) Regular expression to extract from folder name:(?P<Date>\x5B0-9\x5D{4}_\x5B0-9\x5D{2}_\x5B0-9\x5D{2})$ Extract metadata from:All images Select the filtering criteria:and (file does contain "") Metadata file location:/imaging/analysis/metadata.csv Match file and image metadata:\x5B{\'Image Metadata\'\x3A u\'FileLocation\', \'CSV Metadata\'\x3A u\'None\'}\x5D Use case insensitive matching?:No Metadata extraction method:Import from file Metadata source:File name Regular expression to extract from file name:^(?P<Plate>.*)_(?P<Well>\x5BA-P\x5D\x5B0-9\x5D{2})_s(?P<Site>\x5B0-9\x5D)_w(?P<ChannelNumber>\x5B0-9\x5D) Regular expression to extract from folder name:(?P<Date>\x5B0-9\x5D{4}_\x5B0-9\x5D{2}_\x5B0-9\x5D{2})$ Extract metadata from:All images Select the filtering criteria:and (file does contain "") Metadata file location:https\x3A//cellprofiler.org/metadata.csv Match file and image metadata:\x5B{\'Image Metadata\'\x3A u\'FileLocation\', \'CSV Metadata\'\x3A u\'None\'}\x5D Use case insensitive matching?:No """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(cStringIO.StringIO(data)) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] em0, em1 = module.extraction_methods self.assertEqual(em0.csv_location.get_dir_choice(), cellprofiler.setting.ABSOLUTE_FOLDER_NAME) self.assertEqual(em0.csv_location.get_custom_path(), "/imaging/analysis") self.assertEqual(em0.csv_filename.value, "metadata.csv") self.assertEqual(em1.csv_location.get_dir_choice(), cellprofiler.setting.URL_FOLDER_NAME) self.assertEqual(em1.csv_location.get_custom_path(), "https://cellprofiler.org") self.assertEqual(em1.csv_filename.value, "metadata.csv")
def test_load_v1(): with open("./tests/resources/modules/images/v1.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[0] assert isinstance(module, cellprofiler.modules.images.Images) assert module.filter_choice == cellprofiler.modules.images.FILTER_CHOICE_CUSTOM assert (module.filter.value == 'or (directory does startwith "foo") (file does contain "bar")')
def make_workspace(labels, image, mask=None, intensity_image=None, wants_graph=False): m = cellprofiler.measurement.Measurements() image_set_list = cellprofiler.image.ImageSetList() m.add_measurement(cellprofiler.measurement.IMAGE, cellprofiler.measurement.GROUP_NUMBER, 1) m.add_measurement(cellprofiler.measurement.IMAGE, cellprofiler.measurement.GROUP_INDEX, 1) image_set = m img = cellprofiler.image.Image(image, mask) image_set.add(IMAGE_NAME, img) object_set = cellprofiler.object.ObjectSet() o = cellprofiler.object.Objects() o.segmented = labels object_set.add_objects(o, OBJECT_NAME) module = cellprofiler.modules.measureobjectskeleton.MeasureObjectSkeleton() module.image_name.value = IMAGE_NAME module.seed_objects_name.value = OBJECT_NAME if intensity_image is not None: img = cellprofiler.image.Image(intensity_image) image_set.add(INTENSITY_IMAGE_NAME, img) module.intensity_image_name.value = INTENSITY_IMAGE_NAME if wants_graph: module.wants_objskeleton_graph.value = True module.directory.dir_choice = cellprofiler.setting.ABSOLUTE_FOLDER_NAME module.directory.custom_path = temp_dir module.edge_file_name.value = EDGE_FILE module.vertex_file_name.value = VERTEX_FILE module.set_module_num(1) pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) pipeline.add_module(module) workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set, object_set, m, image_set_list) return workspace, module
def test_load_v3(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:3 DateRevision:300 GitHash: ModuleCount:1 HasImagePlaneDetails:False MeasureImageAreaOccupied:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:3|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False] Hidden:3 Measure the area occupied in a binary image, or in objects?:Binary Image Select objects to measure:None Retain a binary image of the object regions?:No Name the output binary image:Stain Select a binary image to measure:DNA Measure the area occupied in a binary image, or in objects?:Objects Select objects to measure:Cells Retain a binary image of the object regions?:Yes Name the output binary image:Stain Select a binary image to measure:None Measure the area occupied in a binary image, or in objects?:Objects Select objects to measure:Nuclei Retain a binary image of the object regions?:No Name the output binary image:Stain Select a binary image to measure:None """ def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_listener(callback) pipeline.load(six.StringIO(data)) module = pipeline.modules()[0] assert module.count.value == 3 assert module.operands[0].operand_choice == "Binary Image" assert module.operands[1].operand_choice == "Objects" assert module.operands[1].operand_objects == "Cells" assert module.operands[2].operand_choice == "Objects" assert module.operands[2].operand_objects == "Nuclei"
def make_workspace(image_measurements, object_measurements): """Make a workspace with a FlagImage module and the given measurements image_measurements - a sequence of single image measurements. Use image_measurement_name(i) to get the name of the i th measurement object_measurements - a seequence of sequences of object measurements. These are stored under object, OBJECT_NAME with measurement name object_measurement_name(i) for the i th measurement. returns module, workspace """ module = cellprofiler.modules.flagimage.FlagImage() measurements = cellprofiler.measurement.Measurements() for i in range(len(image_measurements)): measurements.add_image_measurement( image_measurement_name(i), image_measurements[i] ) for i in range(len(object_measurements)): measurements.add_measurement( OBJECT_NAME, object_measurement_name(i), numpy.array(object_measurements[i]) ) flag = module.flags[0] assert isinstance(flag, cellprofiler.setting.SettingsGroup) flag.category.value = MEASUREMENT_CATEGORY flag.feature_name.value = MEASUREMENT_FEATURE module.set_module_num(1) pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set, cellprofiler.object.ObjectSet(), measurements, image_set_list, ) return module, workspace
def test_01_01_load_matlab(self): data = ('eJzzdQzxcXRSMNUzUPB1DNFNy8xJ1VEIyEksScsvyrVSCHAO9/TTUXAuSk0s' 'SU1RyM+zUgjJKFXwKs1RMDJWMDS1MjWyAjKMDAwsFUgGDIyevvwMDAwBTAwM' 'FXPeTr/rd9tA5Li/V1CCc8fG2bFvlKbHMHh3a73dJKLGm3lazcFjctljNbWt' 'U26F2gUk6h9/KpfYv+H4DN6fW5zm1mWsFejTeW1ht7/4T3+y+m2Ggv3cF568' 'ZVvPW6HjlpTdqvWGW8a1pWoho9Ue71/vK/c6MTrWyscr2h9TTFF9a7/4+ptP' 'ZVPu/jp158ZxMykx9s9Jxw/b8R3Kv74yuei3tWTy/6Y7iRrTS25Y7lNUTDT5' '6P+jKF9sumvQndy4/znPrDIm8Vmtz+f+m1Qj/uljVWuedetHa8+9Rx6/eHBZ' 'wnKipctEYwEZ74OR2oXXHlyxa0v9sfLZ76hZ6zf4s3UEvdnOvyYs926cp0zi' '/McT7GvClwTN3qxauK3Ir3/9pbJYv6t7PtX0bar5d/2zRU551s9MVXveKONn' 'M48Fvwz5esdvpmdkyrHET4+4zrmzpsc+4tidGvl7z7l39x/+m2v5h/uj+NLm' 'ruCt9TckWC16fKSF8y+kO879meMhk/i9w18j/ewq936LwvkW3Fcfr/zWaj/T' '/q1dwZYnNfbaP/JV9m3gLD3JW5j8ZHZKx543X+a5T3fT9GPO2RYxl1/0Z6no' 'Hl9P9sRqxl28sk8ec93b8vPU7z/8615pvdR87m6/oNMm9Hf0/D+14THln/fx' '83df4Y6wiYr1i427ajnls8+/rEcXi5+dqLqaG/9I3/xfcb7u25d/fW5YTfm+' 'Wc9nf35N4glbbZkdTYt/xeV4ySTa/9Nf21b/8U6mVkl2QfZ1g57Ic/9X/V9r' '9ZHH9/KTylV/viz5//x3PP/0D/+5L6/t38v5+PKmVY//M5ac/+oEAKjKSvo=') pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(zlib.decompress(base64.b64decode(data)))) self.assertEqual(len(pipeline.modules()), 4) # # 4 modules - first resizes by factor, second resizes to a fixed size # interpolation is different for each # for module in pipeline.modules()[1:]: self.assertTrue(isinstance(module, cellprofiler.modules.resize.Resize)) module = pipeline.modules()[1] self.assertEqual(module.x_name, 'DNA') self.assertEqual(module.y_name, 'ResizedDNA') self.assertEqual(module.size_method, cellprofiler.modules.resize.R_BY_FACTOR) self.assertAlmostEqual(module.resizing_factor.value, .25) self.assertEqual(module.interpolation, cellprofiler.modules.resize.I_NEAREST_NEIGHBOR) module = pipeline.modules()[2] self.assertEqual(module.size_method, cellprofiler.modules.resize.R_TO_SIZE) self.assertEqual(module.specific_width.value, 150) self.assertEqual(module.specific_height.value, 150) self.assertEqual(module.interpolation, cellprofiler.modules.resize.I_BILINEAR) module = pipeline.modules()[3] self.assertEqual(module.interpolation, cellprofiler.modules.resize.I_BICUBIC)
def make_workspace(ground_truth, test, dimensions=2): """Make a workspace with a ground-truth image and a test image ground_truth and test are dictionaries with the following keys: image - the pixel data mask - (optional) the mask data crop_mask - (optional) a cropping mask returns a workspace and module """ module = cellprofiler.modules.measureimageoverlap.MeasureImageOverlap() module.set_module_num(1) module.ground_truth.value = GROUND_TRUTH_IMAGE_NAME module.test_img.value = TEST_IMAGE_NAME module.wants_emd.value = True pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) for name, d in ((GROUND_TRUTH_IMAGE_NAME, ground_truth), (TEST_IMAGE_NAME, test)): image = cellprofiler.image.Image( d["image"], mask=d.get("mask"), crop_mask=d.get("crop_mask"), dimensions=dimensions, ) image_set.add(name, image) workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set, cellprofiler.object.ObjectSet(), cellprofiler.measurement.Measurements(), image_set_list, ) return workspace, module
def test_load_v3(): with open("./tests/resources/modules/measureimagequality/v3.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[0] assert isinstance( module, cellprofiler.modules.measureimagequality.MeasureImageQuality ) assert len(module.image_groups) == 2 group = module.image_groups[0] thr = group.threshold_groups[0] assert group.image_names == "Alpha" assert group.check_blur assert group.scale_groups[0].scale == 25 assert group.check_saturation assert group.calculate_threshold assert thr.threshold_method == centrosome.threshold.TM_OTSU assert round(abs(thr.object_fraction.value - 0.2), 7) == 0 assert thr.two_class_otsu == cellprofiler.modules.identify.O_THREE_CLASS assert ( thr.use_weighted_variance == cellprofiler.modules.identify.O_WEIGHTED_VARIANCE ) assert thr.assign_middle_to_foreground == cellprofiler.modules.identify.O_FOREGROUND group = module.image_groups[1] thr = group.threshold_groups[0] assert group.image_names == "Beta" assert not group.check_blur assert group.scale_groups[0].scale == 15 assert not group.check_saturation assert not group.calculate_threshold assert thr.threshold_method == centrosome.threshold.TM_MOG assert round(abs(thr.object_fraction.value - 0.3), 7) == 0 assert thr.two_class_otsu == cellprofiler.modules.identify.O_TWO_CLASS assert thr.use_weighted_variance == cellprofiler.modules.identify.O_ENTROPY assert thr.assign_middle_to_foreground == cellprofiler.modules.identify.O_BACKGROUND
def test_04_02_object_outlines(self): labels = numpy.zeros((30, 40), int) labels[10:15, 20:30] = 1 expected_outlines = labels != 0 expected_outlines[11:14, 21:29] = False filename = "myobjects.png" directory = tempfile.mkdtemp() cellprofiler.preferences.set_default_image_directory(directory) pilimage = PIL.Image.fromarray(labels.astype(numpy.uint8), "L") pilimage.save(os.path.join(directory, filename)) del pilimage try: module = cellprofiler.modules.loadsingleimage.LoadSingleImage() module.module_num = 1 module.directory.set_dir_choice(cellprofiler.modules.loadsingleimage.DEFAULT_INPUT_FOLDER_NAME) fs = module.file_settings[0] fs.file_name.value = filename fs.image_objects_choice.value = cellprofiler.modules.loadsingleimage.IO_OBJECTS fs.objects_name.value = OBJECTS_NAME fs.wants_outlines.value = True fs.outlines_name.value = OUTLINES_NAME pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) m = cellprofiler.measurement.Measurements() object_set = cellprofiler.object.ObjectSet() image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set, object_set, m, image_set_list) module.prepare_run(workspace) module.run(workspace) outlines = image_set.get_image(OUTLINES_NAME) numpy.testing.assert_equal(outlines.pixel_data, expected_outlines) finally: try: os.remove(os.path.join(directory, filename)) os.rmdir(directory) except: print("Failed to delete directory " + directory)
def test_zernikes_are_different(): """Regression test of IMG-773""" numpy.random.seed(32) labels = numpy.zeros((40, 20), int) # # Make two "objects" composed of random foreground/background # labels[1:19, 1:19] = (numpy.random.uniform(size=(18, 18)) > 0.5).astype(int) labels[21:39, 1:19] = (numpy.random.uniform(size=(18, 18)) > 0.5).astype(int) * 2 objects = cellprofiler.object.Objects() objects.segmented = labels object_set = cellprofiler.object.ObjectSet() object_set.add_objects(objects, "SomeObjects") module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape() module.object_groups[0].name.value = "SomeObjects" module.calculate_zernikes.value = True module.set_module_num(1) image_set_list = cellprofiler.image.ImageSetList() measurements = cellprofiler.measurement.Measurements() pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set_list.get_image_set(0), object_set, measurements, image_set_list, ) module.run(workspace) features = [ x[1] for x in module.get_measurement_columns(pipeline) if x[0] == "SomeObjects" and x[1].startswith("AreaShape_Zernike") ] for feature in features: values = measurements.get_current_measurement("SomeObjects", feature) assert len(values) == 2 assert values[0] != values[1]
def test_01_03_combine_channels(): numpy.random.seed(13) image = numpy.random.uniform(size=(20, 10, 5)) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, cellprofiler.image.Image(image)) module = cellprofiler.modules.colortogray.ColorToGray() module.module_num = 1 module.image_name.value = IMAGE_NAME module.combine_or_split.value = cellprofiler.modules.colortogray.COMBINE module.grayscale_name.value = OUTPUT_IMAGE_F % 1 module.rgb_or_channels.value = cellprofiler.modules.colortogray.CH_CHANNELS module.add_channel() module.add_channel() channel_indexes = numpy.array([2, 0, 3]) factors = numpy.random.uniform(size=3) divisor = numpy.sum(factors) expected = numpy.zeros((20, 10)) for i, channel_index in enumerate(channel_indexes): module.channels[i].channel_choice.value = channel_index + 1 module.channels[i].contribution.value_text = "%.10f" % factors[i] expected += image[:, :, channel_index] * factors[i] / divisor pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) pipeline.add_module(module) workspace = Workspace( pipeline, module, image_set, cellprofiler.object.ObjectSet(), cellprofiler.measurement.Measurements(), image_set_list, ) module.run(workspace) pixels = image_set.get_image(module.grayscale_name.value).pixel_data assert pixels.ndim == 2 assert tuple(pixels.shape) == (20, 10) numpy.testing.assert_almost_equal(expected, pixels)
def test_01_06_load_v6(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:10536 LoadData:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:6|show_window:True|notes:\x5B\x5D] Input data file location:Elsewhere...\x7Cx\x3A\\projects\\NightlyBuild\\trunk\\ExampleImages\\ExampleSBSImages Name of the file:1049_Metadata.csv Load images based on this data?:Yes Base image location:Default Input Folder\x7C. Process just a range of rows?:No Rows to process:1,100000 Group images by metadata?:Yes Select metadata fields for grouping:Column,Row Rescale intensities?:Yes """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse( isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] self.assertTrue( isinstance(module, cellprofiler.modules.loaddata.LoadData)) self.assertEqual(module.csv_file_name, "1049_Metadata.csv") self.assertEqual(module.csv_directory.dir_choice, cellprofiler.setting.ABSOLUTE_FOLDER_NAME) self.assertEqual( module.csv_directory.custom_path, r"x:\projects\NightlyBuild\trunk\ExampleImages\ExampleSBSImages") self.assertTrue(module.wants_images) self.assertEqual(module.image_directory.dir_choice, cellprofiler.setting.DEFAULT_INPUT_FOLDER_NAME) self.assertTrue(module.rescale) self.assertTrue(module.wants_image_groupings) self.assertFalse(module.wants_rows) self.assertEqual(module.row_range.min, 1) self.assertEqual(module.row_range.max, 100000) self.assertEqual(len(module.metadata_fields.selections), 2) self.assertEqual(module.metadata_fields.selections[0], "Column") self.assertEqual(module.metadata_fields.selections[1], "Row")
def test_divide(): """Test correction by division""" numpy.random.seed(0) image = numpy.random.uniform(size=(10, 10)).astype(numpy.float32) illum = numpy.random.uniform(size=(10, 10)).astype(numpy.float32) expected = image / illum pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_listener(error_callback) input_module = cellprofiler.modules.injectimage.InjectImage( "InputImage", image) input_module.set_module_num(1) pipeline.add_module(input_module) illum_module = cellprofiler.modules.injectimage.InjectImage( "IllumImage", illum) illum_module.set_module_num(2) pipeline.add_module(illum_module) module = cellprofiler.modules.correctilluminationapply.CorrectIlluminationApply( ) module.set_module_num(3) pipeline.add_module(module) image = module.images[0] image.image_name.value = "InputImage" image.illum_correct_function_image_name.value = "IllumImage" image.corrected_image_name.value = "OutputImage" image.divide_or_subtract.value = ( cellprofiler.modules.correctilluminationapply.DOS_DIVIDE) image.rescale_option = cellprofiler.modules.correctilluminationapply.RE_NONE image_set_list = cellprofiler.image.ImageSetList() measurements = cellprofiler.measurement.Measurements() workspace = cellprofiler.workspace.Workspace(pipeline, None, None, None, measurements, image_set_list) pipeline.prepare_run(workspace) input_module.prepare_group(workspace, {}, [1]) illum_module.prepare_group(workspace, {}, [1]) module.prepare_group(workspace, {}, [1]) image_set = image_set_list.get_image_set(0) object_set = cellprofiler.object.ObjectSet() workspace = cellprofiler.workspace.Workspace(pipeline, input_module, image_set, object_set, measurements, image_set_list) input_module.run(workspace) illum_module.run(workspace) module.run(workspace) output_image = workspace.image_set.get_image("OutputImage") assert numpy.all(output_image.pixel_data == expected)
def test_load_v2(): with open("./tests/resources/modules/groups/v2.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[0] assert isinstance(module, cellprofiler.modules.groups.Groups) assert module.wants_groups assert module.grouping_metadata_count.value == 1 g0 = module.grouping_metadata[0] assert g0.metadata_choice == "Plate"
def test_load_v3(): with open("./tests/resources/modules/identifytertiaryobjects/v3.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.loadtxt(six.moves.StringIO(data)) module = pipeline.modules()[0] assert module.secondary_objects_name.value == "IdentifySecondaryObjects" assert module.primary_objects_name.value == "IdentifyPrimaryObjects" assert module.subregion_objects_name.value == "IdentifyTertiaryObjects" assert module.shrink_primary.value
def test_load_v1(): with open("./tests/resources/modules/measureimageoverlap/v1.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[0] assert isinstance( module, cellprofiler.modules.measureimageoverlap.MeasureImageOverlap) assert module.ground_truth == "GroundTruth" assert module.test_img == "Segmentation"
def test_01_04_split_channels(): numpy.random.seed(13) image = numpy.random.uniform(size=(20, 10, 5)) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, cellprofiler.image.Image(image)) module = cellprofiler.modules.colortogray.ColorToGray() module.module_num = 1 module.image_name.value = IMAGE_NAME module.combine_or_split.value = cellprofiler.modules.colortogray.SPLIT module.rgb_or_channels.value = cellprofiler.modules.colortogray.CH_CHANNELS module.add_channel() module.add_channel() module.add_channel() module.add_channel() channel_indexes = numpy.array([1, 4, 2]) for i, channel_index in enumerate(channel_indexes): module.channels[i].channel_choice.value = channel_index + 1 module.channels[i].image_name.value = OUTPUT_IMAGE_F % i pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) pipeline.add_module(module) workspace = Workspace( pipeline, module, image_set, cellprofiler.object.ObjectSet(), cellprofiler.measurement.Measurements(), image_set_list, ) module.run(workspace) for i, channel_index in enumerate(channel_indexes): pixels = image_set.get_image( module.channels[i].image_name.value).pixel_data assert pixels.ndim == 2 assert tuple(pixels.shape) == (20, 10) numpy.testing.assert_almost_equal(image[:, :, channel_index], pixels)
def make_obj_workspace(self, ground_truth_obj, id_obj, ground_truth, id): '''make a workspace to test comparing objects''' ''' ground truth object and ID object are dictionaires w/ the following keys''' '''i - i component of pixel coordinates j - j component of pixel coordinates l - label ''' module = cellprofiler.modules.measureobjectoverlap.MeasureObjectOverlap( ) module.module_num = 1 module.object_name_GT.value = GROUND_TRUTH_OBJ module.object_name_ID.value = ID_OBJ module.wants_emd.value = True pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse( isinstance(event, cellprofiler.pipeline.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) for name, d in ((GROUND_TRUTH_OBJ_IMAGE_NAME, ground_truth), (ID_OBJ_IMAGE_NAME, id)): image = cellprofiler.image.Image(d["image"], mask=d.get("mask"), crop_mask=d.get("crop_mask")) image_set.add(name, image) object_set = cellprofiler.object.ObjectSet() for name, d in ((GROUND_TRUTH_OBJ, ground_truth_obj), (ID_OBJ, id_obj)): object = cellprofiler.object.Objects() if d.shape[1] == 3: object.ijv = d else: object.segmented = d object_set.add_objects(object, name) workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set, object_set, cellprofiler.measurement.Measurements(), image_set_list) return workspace, module
def test_load_v7(): with open("./tests/resources/modules/filterobjects/v7.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[-1] assert isinstance(module, cellprofiler.modules.filterobjects.FilterObjects) assert module.y_name == "MyFilteredObjects" assert module.x_name == "MyObjects" assert module.mode == cellprofiler.modules.filterobjects.MODE_MEASUREMENTS assert module.filter_choice == cellprofiler.modules.filterobjects.FI_LIMITS assert (module.per_object_assignment == cellprofiler.modules.filterobjects.PO_PARENT_WITH_MOST_OVERLAP) assert (module.rules_directory.dir_choice == cellprofiler.preferences.DEFAULT_INPUT_FOLDER_NAME) assert module.rules_directory.custom_path == "./rules" assert module.rules_file_name == "myrules.txt" assert module.rules_class == "1" assert module.measurement_count.value == 2 assert module.additional_object_count.value == 2 assert module.measurements[ 0].measurement == "Intensity_LowerQuartileIntensity_DNA" assert module.measurements[0].wants_minimum assert not module.measurements[0].wants_maximum assert round(abs(module.measurements[0].min_limit.value - 0.2), 7) == 0 assert round(abs(module.measurements[0].max_limit.value - 1.5), 7) == 0 assert module.measurements[ 1].measurement == "Intensity_UpperQuartileIntensity_DNA" assert not module.measurements[1].wants_minimum assert module.measurements[1].wants_maximum assert round(abs(module.measurements[1].min_limit.value - 0.9), 7) == 0 assert round(abs(module.measurements[1].max_limit.value - 1.8), 7) == 0 for group, name in zip(module.additional_objects, ("Cells", "Cytoplasm")): assert group.object_name == name assert group.target_name == "Filtered%s" % name
def test_01_00_load_matlab(self): data = ('eJzzdQzxcXRSMNUzUPB1DNFNy8xJ1VEIyEksScsvyrVSCHAO9/TTUXAuSk0s' 'SU1RyM+zUggpTVXwTSxSMDRTMDSxMjW3MrJQMDIwNFAgGTAwevryMzAwrGZk' 'YKiY8zbM1v+wgcDeFpapDEuFuIOvchpu3WDY9MJBVaNj0boTqn6pmp2LaxRO' '6Sc86S9JT3mSnrjd77VExqRLnF8XBX+ZU/1+nl78OqYDt80OqFmHR7wy4A1O' '8PXqq7bo67Tv8TF44LCmfsObxRMWNHb/PuFbwLLZ47r1Puf37gffXDLdKixe' 'PlFdfPMLtXsM7Rd7JwsdfRr9qeeuXOXBCb1b3vDZT+wIiP/Qum+X1Wvv5KX5' 'U5+utpzfvOxM4/mjk65V/jU887pX/tk2xavXJT5Fv/Dfc1lm3syHvoWbnwZo' '/dE7bJ/DG6DxI93yT2zr+Y1vF7M/WqiYd+yI5orNi18U3Hk3rzzG/GPLmaDi' 'FKnWZwGNOf+7rsz/rF/84zfX/MfHA32YxV3j0qSOPkvUrJLZnnl4eaFy5xHu' 'QJd074sPfyh9ZT1aGvY0fe3ma5FLPq+c/ltuuu3zn2s07Sr97t1L9Ji8wLFG' 'mn31lcjfv+//u/fw+/OZybKbzhfH3bddqn88XOSm7TbHZGu9dwlLrT79LzM+' 'vv8c32mtb/OvObxbf5Cz5rnoy3SJp5Vs1se+FtZu+t9c9P15hmOt1Olr9YzH' 'iy8IAQDsQ/za') pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO(zlib.decompress(base64.b64decode(data)))) self.assertEqual(len(pipeline.modules()), 2) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.loadsingleimage.LoadSingleImage)) self.assertEqual(module.directory.dir_choice, cellprofiler.setting.DEFAULT_INPUT_SUBFOLDER_NAME) self.assertEqual(module.directory.custom_path, "./foo") self.assertEqual(len(module.file_settings), 2) fs = module.file_settings[0] self.assertEqual(fs.file_name, "dna_image.tif") self.assertEqual(fs.image_name, "DNA") fs = module.file_settings[1] self.assertEqual(fs.file_name, "cytoplasm_image.tif") self.assertEqual(fs.image_name, "Cytoplasm") module = pipeline.modules()[1] self.assertTrue(isinstance(module, cellprofiler.modules.loadsingleimage.LoadSingleImage)) self.assertEqual(module.directory.dir_choice, cellprofiler.setting.DEFAULT_OUTPUT_SUBFOLDER_NAME) self.assertEqual(module.directory.custom_path, "./bar") self.assertEqual(len(module.file_settings), 1) fs = module.file_settings[0] self.assertEqual(fs.file_name, "DNAIllum.tif") self.assertEqual(fs.image_name, "DNAIllum")
def make_workspace(measurement, labels=None, image=None): object_set = cellprofiler.object.ObjectSet() module = cellprofiler.modules.displaydataonimage.DisplayDataOnImage() module.set_module_num(1) module.image_name.value = INPUT_IMAGE_NAME module.display_image.value = OUTPUT_IMAGE_NAME module.objects_name.value = OBJECTS_NAME m = cellprofiler.measurement.Measurements() if labels is None: module.objects_or_image.value = cellprofiler.modules.displaydataonimage.OI_IMAGE m.add_image_measurement(MEASUREMENT_NAME, measurement) if image is None: image = numpy.zeros((50, 120)) else: module.objects_or_image.value = ( cellprofiler.modules.displaydataonimage.OI_OBJECTS) o = cellprofiler.object.Objects() o.segmented = labels object_set.add_objects(o, OBJECTS_NAME) m.add_measurement(OBJECTS_NAME, MEASUREMENT_NAME, numpy.array(measurement)) y, x = centrosome.cpmorphology.centers_of_labels(labels) m.add_measurement(OBJECTS_NAME, "Location_Center_X", x) m.add_measurement(OBJECTS_NAME, "Location_Center_Y", y) if image is None: image = numpy.zeros(labels.shape) module.measurement.value = MEASUREMENT_NAME pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(INPUT_IMAGE_NAME, cellprofiler.image.Image(image)) workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set, object_set, m, image_set_list) return workspace, module
def run_module(image, mask=None, fn=None): """Run the FlipAndRotate module image - pixel data to be transformed mask - optional mask on the pixel data fn - function with signature, "fn(module)" that will be called with the FlipAndRotate module returns an Image object containing the flipped/rotated/masked/cropped image and the angle measurement. """ img = cellprofiler.image.Image(image, mask) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) image_set.add(IMAGE_NAME, img) module = cellprofiler.modules.flipandrotate.FlipAndRotate() module.image_name.value = IMAGE_NAME module.output_name.value = OUTPUT_IMAGE module.set_module_num(1) if fn is not None: fn(module) pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) def error_callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent) pipeline.add_listener(error_callback) measurements = cellprofiler.measurement.Measurements() workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set, cellprofiler.object.ObjectSet(), measurements, image_set_list, ) module.run(workspace) feature = cellprofiler.modules.flipandrotate.M_ROTATION_F % OUTPUT_IMAGE assert feature in measurements.get_feature_names( cellprofiler.measurement.IMAGE) angle = measurements.get_current_image_measurement(feature) output_image = image_set.get_image(OUTPUT_IMAGE) return output_image, angle
def test_load_v2(self): with open("./tests/resources/modules/loadsingleimage/v2.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(six.moves.StringIO(data)) assert len(pipeline.modules()) == 4 module = pipeline.modules()[0] assert isinstance(module, cellprofiler.modules.loadsingleimage.LoadSingleImage) assert len(module.file_settings) == 2 fs = module.file_settings[0] assert fs.file_name == "foo.tif" assert fs.image_name == "DNA" fs = module.file_settings[1] assert fs.file_name == "bar.tif" assert fs.image_name == "Cytoplasm" module = pipeline.modules()[3] fs = module.file_settings[0] assert ( fs.file_name == "https://svn.broadinstitute.org/CellProfiler/trunk/ExampleImages/" "ExampleSBSImages/Channel1-01-A-01.tif") dir_choice = [ cellprofiler.setting.DEFAULT_INPUT_FOLDER_NAME, cellprofiler.setting.DEFAULT_OUTPUT_FOLDER_NAME, cellprofiler.setting.ABSOLUTE_FOLDER_NAME, cellprofiler.setting.URL_FOLDER_NAME, ] for i, module in enumerate(pipeline.modules()): assert isinstance( module, cellprofiler.modules.loadsingleimage.LoadSingleImage) assert module.directory.dir_choice == dir_choice[i]
def test_04_01_extent(self): module = cellprofiler.modules.measureobjectsizeshape.MeasureObjectAreaShape() module.object_groups[0].name.value = "SomeObjects" module.calculate_zernikes.value = True object_set = cellprofiler.object.ObjectSet() labels = numpy.zeros((10, 20), int) # 3/4 of a square is covered labels[5:7, 5:10] = 1 labels[7:9, 5:15] = 1 objects = cellprofiler.object.Objects() objects.segmented = labels object_set.add_objects(objects, "SomeObjects") module.module_num = 1 image_set_list = cellprofiler.image.ImageSetList() measurements = cellprofiler.measurement.Measurements() pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module) def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.RunExceptionEvent)) pipeline.add_listener(callback) workspace = cellprofiler.workspace.Workspace( pipeline, module, image_set_list.get_image_set(0), object_set, measurements, image_set_list, ) module.run(workspace) values = measurements.get_current_measurement( "SomeObjects", "_".join( ( cellprofiler.modules.measureobjectsizeshape.AREA_SHAPE, cellprofiler.modules.measureobjectsizeshape.F_EXTENT, ) ), ) self.assertEqual(len(values), 1) self.assertAlmostEqual(values[0], 0.75)
def test_load_v3(): with open( "./tests/resources/modules/measureobjectintensitydistribution/v3.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[0] assert isinstance( module, cellprofiler.modules.measureobjectintensitydistribution. MeasureObjectIntensityDistribution, ) assert module.image_count.value == 2 assert module.object_count.value == 3 assert module.bin_counts_count.value == 2 assert module.images[0].image_name == "EnhancedGreen" assert module.images[1].image_name == "OrigBlue" assert module.objects[0].object_name == "Nuclei" assert (module.objects[0].center_choice == cellprofiler.modules.measureobjectintensitydistribution.C_SELF) assert module.objects[0].center_object_name == "Cells" assert (module.objects[1].center_choice == cellprofiler.modules. measureobjectintensitydistribution.C_CENTERS_OF_OTHER) assert module.objects[1].center_object_name == "Cells" assert (module.objects[2].center_choice == cellprofiler.modules. measureobjectintensitydistribution.C_EDGES_OF_OTHER) assert module.objects[2].center_object_name == "Cells" assert module.bin_counts[0].bin_count == 4 assert not module.bin_counts[0].wants_scaled assert module.bin_counts[0].maximum_radius == 200 assert module.bin_counts[1].bin_count == 5 assert module.bin_counts[1].wants_scaled assert module.bin_counts[1].maximum_radius == 50 assert len(module.heatmaps) == 0
def test_01_06_load_v6(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:10536 LoadData:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:6|show_window:True|notes:\x5B\x5D] Input data file location:Elsewhere...\x7Cx\x3A\\projects\\NightlyBuild\\trunk\\ExampleImages\\ExampleSBSImages Name of the file:1049_Metadata.csv Load images based on this data?:Yes Base image location:Default Input Folder\x7C. Process just a range of rows?:No Rows to process:1,100000 Group images by metadata?:Yes Select metadata fields for grouping:Column,Row Rescale intensities?:Yes """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.loaddata.LoadData)) self.assertEqual(module.csv_file_name, "1049_Metadata.csv") self.assertEqual(module.csv_directory.dir_choice, cellprofiler.setting.ABSOLUTE_FOLDER_NAME) self.assertEqual(module.csv_directory.custom_path, r"x:\projects\NightlyBuild\trunk\ExampleImages\ExampleSBSImages") self.assertTrue(module.wants_images) self.assertEqual(module.image_directory.dir_choice, cellprofiler.setting.DEFAULT_INPUT_FOLDER_NAME) self.assertTrue(module.rescale) self.assertTrue(module.wants_image_groupings) self.assertFalse(module.wants_rows) self.assertEqual(module.row_range.min, 1) self.assertEqual(module.row_range.max, 100000) self.assertEqual(len(module.metadata_fields.selections), 2) self.assertEqual(module.metadata_fields.selections[0], "Column") self.assertEqual(module.metadata_fields.selections[1], "Row")
def test_load_v2(): with open("./tests/resources/modules/align/v2.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(six.moves.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[0] assert isinstance(module, cellprofiler.modules.align.Align) assert module.alignment_method == cellprofiler.modules.align.M_MUTUAL_INFORMATION assert module.crop_mode == cellprofiler.modules.align.C_CROP assert module.first_input_image == "Image1" assert module.second_input_image == "Image2" assert module.first_output_image, "AlignedImage1" assert module.second_output_image, "AlignedImage2"
def test_load_v3(): with open("./tests/resources/modules/metadata/v3.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[0] assert isinstance(module, cellprofiler.modules.metadata.Metadata) assert module.wants_metadata assert module.data_type_choice == cellprofiler.modules.metadata.DTC_TEXT assert len(module.extraction_methods) == 2 em0, em1 = module.extraction_methods assert em0.extraction_method == cellprofiler.modules.metadata.X_MANUAL_EXTRACTION assert em0.source == cellprofiler.modules.metadata.XM_FILE_NAME assert ( em0.file_regexp.value == r"^Channel(?P<ChannelNumber>[12])-(?P<Index>[0-9]+)-(?P<WellRow>[A-H])-(?P<WellColumn>[0-9]{2}).tif$" ) assert em0.folder_regexp.value == r"(?P<Date>[0-9]{4}_[0-9]{2}_[0-9]{2})$" assert em0.filter_choice == cellprofiler.modules.metadata.F_ALL_IMAGES assert em0.filter == 'or (file does contain "Channel2")' assert not em0.wants_case_insensitive assert em1.extraction_method == cellprofiler.modules.metadata.X_IMPORTED_EXTRACTION assert em1.source == cellprofiler.modules.metadata.XM_FOLDER_NAME assert em1.filter_choice == cellprofiler.modules.metadata.F_FILTERED_IMAGES assert (em1.csv_location.get_dir_choice() == cellprofiler.setting.ABSOLUTE_FOLDER_NAME) assert em1.csv_location.get_custom_path() == "/imaging/analysis" assert em1.csv_filename.value == "metadata.csv" assert ( em1.csv_joiner.value == "[{'Image Metadata': u'ChannelNumber', 'CSV Metadata': u'Wavelength'}]" ) assert em1.wants_case_insensitive
def load_pipeline(test_case, encoded_data): """Load a pipeline from base-64 encoded data test_case - an instance of unittest.TestCase encoded_data - a pipeline encoded using base-64 The magic incantation to do the above is the following: import base64 fd = open('my_PIPE.mat','rb') bindata = fd.read() fd.close() b64data = base64.b64encode(bindata) """ import cellprofiler.pipeline (matfd, matpath) = tempfile.mkstemp(".mat") matfh = os.fdopen(matfd, "wb") try: data = base64.b64decode(encoded_data) matfh.write(data) matfh.flush() pipeline = cellprofiler.pipeline.Pipeline() handles = scipy.io.matlab.mio.loadmat(matpath, struct_as_record=True) finally: matfh.close() def blowup(pipeline, event): if isinstance( event, ( cellprofiler.pipeline.RunExceptionEvent, cellprofiler.pipeline.LoadExceptionEvent, ), ): if test_case is not None: test_case.assertFalse(event.error.message) else: assert not event.error.message pipeline.add_listener(blowup) pipeline.create_from_handles(handles) return pipeline
def make_obj_workspace(self, ground_truth_obj, id_obj, ground_truth, id): '''make a workspace to test comparing objects''' ''' ground truth object and ID object are dictionaires w/ the following keys''' '''i - i component of pixel coordinates j - j component of pixel coordinates l - label ''' module = cellprofiler.modules.measureobjectoverlap.MeasureObjectOverlap() module.module_num = 1 module.object_name_GT.value = GROUND_TRUTH_OBJ module.object_name_ID.value = ID_OBJ module.wants_emd.value = True pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) image_set_list = cellprofiler.image.ImageSetList() image_set = image_set_list.get_image_set(0) for name, d in ((GROUND_TRUTH_OBJ_IMAGE_NAME, ground_truth), (ID_OBJ_IMAGE_NAME, id)): image = cellprofiler.image.Image(d["image"], mask=d.get("mask"), crop_mask=d.get("crop_mask")) image_set.add(name, image) object_set = cellprofiler.object.ObjectSet() for name, d in ((GROUND_TRUTH_OBJ, ground_truth_obj), (ID_OBJ, id_obj)): object = cellprofiler.object.Objects() if d.shape[1] == 3: object.ijv = d else: object.segmented = d object_set.add_objects(object, name) workspace = cellprofiler.workspace.Workspace(pipeline, module, image_set, object_set, cellprofiler.measurement.Measurements(), image_set_list) return workspace, module
def test_load_v3(): with open("./tests/resources/modules/measuregranularity/v3.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(six.moves.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[0] assert isinstance( module, cellprofiler.modules.measuregranularity.MeasureGranularity) assert len(module.images) == 2 for image_setting, image_name, subsample_size, bsize, elsize, glen, objs in ( (module.images[0], "DNA", 0.25, 0.25, 10, 16, ("Nuclei", "Cells")), ( module.images[1], "Actin", 0.33, 0.50, 12, 20, ("Nuclei", "Cells", "Cytoplasm"), ), ): # assertTrue(isinstance(image_setting, M.MeasureGranularity)) assert image_setting.image_name == image_name assert round(abs(image_setting.subsample_size.value - subsample_size), 7) == 0 assert round(abs(image_setting.image_sample_size.value - bsize), 7) == 0 assert image_setting.element_size.value == elsize assert image_setting.granular_spectrum_length.value == glen assert len(image_setting.objects) == len(objs) assert image_setting.object_count.value == len(objs) assert all( [ob.objects_name.value in objs for ob in image_setting.objects])
def test_load_v1(): with open("./tests/resources/modules/measureobjectskeleton/v1.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(six.moves.StringIO(data)) assert len(pipeline.modules()) == 1 module = pipeline.modules()[-1] assert isinstance( module, cellprofiler.modules.measureobjectskeleton.MeasureObjectSkeleton) assert module.image_name == "DNA" assert module.seed_objects_name == "Nucs" assert module.wants_branchpoint_image assert module.branchpoint_image_name == "BPImg"
def test_01_02_load_v1(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:9169 MeasureImageOverlap:[module_num:1|svn_version:\'9000\'|variable_revision_number:1|show_window:True|notes:\x5B\x5D] Which image do you want to use as the basis for calculating the amount of overlap? :GroundTruth Which image do you want to compare for overlap?:Segmentation """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)) pipeline.add_listener(callback) pipeline.load(StringIO.StringIO(data)) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.measureimageoverlap.MeasureImageOverlap)) self.assertEqual(module.ground_truth, "GroundTruth") self.assertEqual(module.test_img, "Segmentation")
def test_load_v3(): with open("./tests/resources/modules/measureimageoverlap/v3.pipeline", "r") as fd: data = fd.read() pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) module = pipeline.modules()[0] assert isinstance( module, cellprofiler.modules.measureimageoverlap.MeasureImageOverlap) assert module.ground_truth == "Foo" assert module.test_img == "Bar" # assertEqual(module.object_name_GT, "Cell2_0") # assertEqual(module.object_name_ID, "Cell2_1") assert not module.wants_emd
def test_01_04_load_v4(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:9722 LoadData:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:4|show_window:True|notes:\x5B\x5D] Input data file location:Default Input Folder\x7C. Name of the file:1049_Metadata.csv Load images based on this data?:Yes Base image location:Default Input Folder\x7C. Process just a range of rows?:No Rows to process:10,36 Group images by metadata?:No Select metadata fields for grouping:Well """ pipeline = cellprofiler.pipeline.Pipeline() def callback(caller, event): self.assertFalse( isinstance(event, cellprofiler.pipeline.LoadExceptionEvent) ) pipeline.add_listener(callback) pipeline.load(io.StringIO(data)) self.assertEqual(len(pipeline.modules()), 1) module = pipeline.modules()[0] self.assertTrue(isinstance(module, cellprofiler.modules.loaddata.LoadData)) self.assertEqual(module.csv_file_name, "1049_Metadata.csv") self.assertEqual( module.csv_directory.dir_choice, cellprofiler.setting.DEFAULT_INPUT_FOLDER_NAME, ) self.assertTrue(module.wants_images) self.assertTrue(module.rescale) self.assertFalse(module.wants_image_groupings) self.assertFalse(module.wants_rows) self.assertEqual(module.row_range.min, 10) self.assertEqual(module.row_range.max, 36) self.assertEqual(len(module.metadata_fields.selections), 1) self.assertEqual(module.metadata_fields.selections[0], "Well")