def test_03_02_pipeline_preferences(self): # # Walk the worker up through pipelines and preferences. # self.awthread = self.AWThread(self.announce_addr) self.awthread.start() self.set_work_socket() self.awthread.ex(self.awthread.aw.do_job, cpanalysis.WorkReply( image_set_numbers = [1], worker_runs_post_group = False, wants_dictionary = True)) # # The worker should ask for the pipeline and preferences next. # req = self.awthread.recv(self.work_socket) self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest) self.assertEqual(req.analysis_id, self.analysis_id) maybe_download_example_image(["ExampleSBSImages"], "Channel1-01-A-01.tif") maybe_download_example_image(["ExampleHT29"], "AS_09125_050116030001_D03f00d0.tif") input_dir = os.path.normcase( os.path.join(example_images_directory(), "ExampleSBSImages")) output_dir = os.path.normcase( os.path.join(example_images_directory(), "ExampleHT29")) cpprefs.set_default_image_directory(input_dir) input_dir = cpprefs.get_default_image_directory() cpprefs.set_default_output_directory(output_dir) output_dir = cpprefs.get_default_output_directory() preferences = {cpprefs.DEFAULT_IMAGE_DIRECTORY: cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY), cpprefs.DEFAULT_OUTPUT_DIRECTORY: cpprefs.config_read(cpprefs.DEFAULT_OUTPUT_DIRECTORY)} cpprefs.set_default_image_directory(example_images_directory()) cpprefs.set_default_output_directory(example_images_directory()) rep = cpanalysis.Reply( pipeline_blob = np.array(GOOD_PIPELINE), preferences = preferences) req.reply(rep) # # Get the next request so that we know the worker has # processed the preferences. # req = self.awthread.recv(self.work_socket) self.assertEqual(cpprefs.get_default_image_directory(), input_dir) self.assertEqual(cpprefs.get_default_output_directory(), output_dir) self.assertIn(self.analysis_id, self.awthread.aw.pipelines_and_preferences) pipe, prefs = self.awthread.aw.pipelines_and_preferences[ self.analysis_id] self.assertEqual(len(pipe.modules()), 7) # # Cancel and check for exit # req.reply(cpanalysis.ServerExited()) self.assertRaises(cpp.CancelledException, self.awthread.ecute)
def test_13_01_load_filename(self): # # Load a file, only specifying the FileName in the CSV # dir = os.path.join(example_images_directory(), "ExampleSBSImages") file_name = 'Channel2-01-A-01.tif' csv_text = '''"Image_FileName_DNA" "%s" '''% file_name pipeline, module, filename = self.make_pipeline(csv_text) assert isinstance(module, L.LoadData) module.image_directory.dir_choice = cps.ABSOLUTE_FOLDER_NAME module.image_directory.custom_path = dir m = cpmeas.Measurements(mode="memory") workspace = cpw.Workspace(pipeline, module, m, cpo.ObjectSet(), m, cpi.ImageSetList()) self.assertTrue(module.prepare_run(workspace)) self.assertEqual(m.get_measurement(cpmeas.IMAGE, "FileName_DNA", 1), file_name) path = m.get_measurement(cpmeas.IMAGE, "PathName_DNA", 1) self.assertEqual(path, dir) self.assertEqual(m.get_measurement(cpmeas.IMAGE, "URL_DNA", 1), L.pathname2url(os.path.join(dir, file_name))) module.prepare_group(workspace, {}, [1]) module.run(workspace) img = workspace.image_set.get_image("DNA", must_be_grayscale=True) self.assertEqual(tuple(img.pixel_data.shape), (640, 640))
def test_03_02_load_using_bioformats(self): path = os.path.join(example_images_directory(), 'ExampleSBSImages', 'Channel1-01-A-01.tif') data = F.load_using_bioformats(path, rescale=False) expected_0_10_0_10 = np.array( [[ 0, 7, 7, 6, 5, 8, 4, 2, 1, 2], [ 0, 8, 8, 7, 6, 10, 4, 2, 2, 2], [ 0, 9, 9, 7, 8, 8, 2, 1, 3, 2], [ 0, 10, 9, 8, 10, 6, 2, 2, 3, 2], [ 0, 10, 10, 10, 9, 4, 2, 2, 2, 2], [ 0, 9, 9, 10, 8, 3, 2, 4, 2, 2], [ 0, 9, 9, 10, 8, 2, 2, 4, 3, 2], [ 0, 9, 8, 9, 7, 4, 2, 2, 2, 2], [ 0, 10, 11, 9, 9, 4, 2, 2, 2, 2], [ 0, 12, 13, 12, 9, 4, 2, 2, 2, 2]], dtype=np.uint8) expected_n10_n10 = np.array( [[2, 1, 1, 1, 2, 2, 1, 2, 1, 2], [1, 2, 2, 2, 2, 1, 1, 1, 2, 1], [1, 1, 1, 2, 1, 2, 2, 2, 2, 1], [2, 2, 2, 2, 3, 2, 2, 2, 2, 1], [1, 2, 2, 1, 1, 1, 1, 1, 2, 2], [2, 1, 2, 2, 2, 1, 1, 2, 2, 2], [2, 2, 3, 2, 2, 1, 2, 2, 2, 1], [3, 3, 1, 2, 2, 2, 2, 3, 2, 2], [3, 2, 2, 2, 2, 2, 2, 2, 3, 3], [5, 2, 3, 3, 2, 2, 2, 3, 2, 2]], dtype=np.uint8) self.assertTrue(np.all(expected_0_10_0_10 == data[:10,:10])) self.assertTrue(np.all(expected_n10_n10 == data[-10:,-10:]))
def test_10_01_scaling(self): '''Test loading an image scaled and unscaled''' d = os.path.join(example_images_directory(), "ExampleSpecklesImages") file_name = "1-162hrh2ax2.tif" csv_text = ("Image_FileName_MyFile,Image_PathName_MyFile\n" "%s,%s\n" % (file_name, d)) c0_image = [] for rescale in (False, True): pipeline, module, filename = self.make_pipeline(csv_text) try: module.rescale.value = rescale def callback(workspace): imgset = workspace.image_set image = imgset.get_image("MyFile") pixels = image.pixel_data c0_image.append(pixels.copy()) c0 = C0() c0.callback = callback c0.module_num = 2 pipeline.add_module(c0) pipeline.run() finally: os.remove(filename) unscaled, scaled = c0_image np.testing.assert_almost_equal(unscaled * 65535. / 4095., scaled)
def test_01_06_get_temp_current_image(self): file_name = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel1-01-A-01.tif") ip = I.load_imageplus(file_name) W.set_temp_current_image(ip) ip_other = W.get_temp_current_image() self.assertEqual(ip.getID(), ip_other.getID())
def test_03_01_read_tif(self): path = os.path.join(example_images_directory(), 'ExampleSBSImages', 'Channel1-01-A-01.tif') ImageReader = F.make_image_reader_class() FormatTools = F.make_format_tools_class() reader = ImageReader() reader.setId(path) data = reader.openBytes(0) data.shape = (reader.getSizeY(), reader.getSizeX()) # # Data as read by cellprofiler.modules.loadimages.load_using_PIL # expected_0_10_0_10 = np.array( [[0, 7, 7, 6, 5, 8, 4, 2, 1, 2], [0, 8, 8, 7, 6, 10, 4, 2, 2, 2], [0, 9, 9, 7, 8, 8, 2, 1, 3, 2], [0, 10, 9, 8, 10, 6, 2, 2, 3, 2], [0, 10, 10, 10, 9, 4, 2, 2, 2, 2], [0, 9, 9, 10, 8, 3, 2, 4, 2, 2], [0, 9, 9, 10, 8, 2, 2, 4, 3, 2], [0, 9, 8, 9, 7, 4, 2, 2, 2, 2], [0, 10, 11, 9, 9, 4, 2, 2, 2, 2], [0, 12, 13, 12, 9, 4, 2, 2, 2, 2]], dtype=np.uint8) expected_n10_n10 = np.array( [[2, 1, 1, 1, 2, 2, 1, 2, 1, 2], [1, 2, 2, 2, 2, 1, 1, 1, 2, 1], [1, 1, 1, 2, 1, 2, 2, 2, 2, 1], [2, 2, 2, 2, 3, 2, 2, 2, 2, 1], [1, 2, 2, 1, 1, 1, 1, 1, 2, 2], [2, 1, 2, 2, 2, 1, 1, 2, 2, 2], [2, 2, 3, 2, 2, 1, 2, 2, 2, 1], [3, 3, 1, 2, 2, 2, 2, 3, 2, 2], [3, 2, 2, 2, 2, 2, 2, 2, 3, 3], [5, 2, 3, 3, 2, 2, 2, 3, 2, 2]], dtype=np.uint8) self.assertTrue(np.all(expected_0_10_0_10 == data[:10, :10])) self.assertTrue(np.all(expected_n10_n10 == data[-10:, -10:]))
def test_13_05_extra_lines_skip_rows(self): # # Regression test of issue #1211 - extra line at end / blank lines # Different code path from 13_04 # path = os.path.join(example_images_directory(), "ExampleSBSImages") file_names = ['Channel2-01-A-01.tif', 'Channel2-02-A-02.tif'] csv_text = '''"Image_FileName_DNA","Image_PathName_DNA" "%s","%s" "%s","%s" '''%(file_names[0], path, file_names[1], path) pipeline, module, filename = self.make_pipeline(csv_text) try: assert isinstance(module, L.LoadData) m = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, m, cpo.ObjectSet(), m, cpi.ImageSetList()) module.wants_rows.value = True module.row_range.min = 2 module.row_range.max = 3 self.assertTrue(module.prepare_run(workspace)) self.assertTrue(isinstance(m, cpmeas.Measurements)) self.assertEqual(m.image_set_count, 1) self.assertTrue('FileName_DNA' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m[cpmeas.IMAGE, 'FileName_DNA', 1], file_names[0]) finally: os.remove(filename)
def test_10_01_scaling(self): '''Test loading an image scaled and unscaled''' d = os.path.join(example_images_directory(), "ExampleSpecklesImages") file_name = "1-162hrh2ax2.tif" csv_text = ("Image_FileName_MyFile,Image_PathName_MyFile\n" "%s,%s\n" % (file_name, d)) c0_image = [] for rescale in (False, True): pipeline, module, filename = self.make_pipeline(csv_text) try: module.rescale.value = rescale def callback(workspace): imgset = workspace.image_set image = imgset.get_image("MyFile") pixels = image.pixel_data c0_image.append(pixels.copy()) c0 = C0() c0.callback = callback c0.module_num = 2 pipeline.add_module(c0) pipeline.run() finally: os.remove(filename) unscaled, scaled = c0_image np.testing.assert_almost_equal(unscaled * 65535. / 4095., scaled)
def test_01_03_get_image_by_id(self): file_name = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel1-01-A-01.tif") ip = I.load_imageplus(file_name) ip.show() ip_other = W.get_image_by_id(ip.getID()) self.assertEqual(ip_other.getID(), ip.getID())
def test_04_01_load_file(self): dir = os.path.join(example_images_directory(), "ExampleSBSImages") file_name = 'Channel2-01-A-01.tif' csv_text = '''"Image_FileName_DNA","Image_PathName_DNA" "%s","%s" '''%(file_name, dir) pipeline, module, filename = self.make_pipeline(csv_text) c0_ran = [False] def callback(workspace): imgset = workspace.image_set image = imgset.get_image("DNA") pixels = image.pixel_data self.assertEqual(pixels.shape[0],640) c0_ran[0] = True c0 = C0() c0.callback = callback c0.module_num = 2 pipeline.add_module(c0) try: m = pipeline.run() self.assertTrue(isinstance(m, cpmeas.Measurements)) self.assertTrue(c0_ran[0]) hexdigest = m.get_current_image_measurement('MD5Digest_DNA') self.assertEqual(hexdigest, "67880f6269fbf438d4b9c92256aa1d8f") self.assertTrue('PathName_DNA' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m.get_current_image_measurement('PathName_DNA'), dir) self.assertTrue('FileName_DNA' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m.get_current_image_measurement('FileName_DNA'), file_name) finally: os.remove(filename)
def setUpClass(self): from cellprofiler.modules.tests import \ example_images_directory, maybe_download_example_image self.root_dir = example_images_directory() self.file_name = maybe_download_example_image(["ExampleSBSImages"], "Channel1-01-A-01.tif") J.attach()
def test_13_05_extra_lines_skip_rows(self): # # Regression test of issue #1211 - extra line at end / blank lines # Different code path from 13_04 # path = os.path.join(example_images_directory(), "ExampleSBSImages") file_names = ['Channel2-01-A-01.tif', 'Channel2-02-A-02.tif'] csv_text = '''"Image_FileName_DNA","Image_PathName_DNA" "%s","%s" "%s","%s" ''' % (file_names[0], path, file_names[1], path) pipeline, module, filename = self.make_pipeline(csv_text) try: assert isinstance(module, L.LoadData) m = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, m, cpo.ObjectSet(), m, cpi.ImageSetList()) module.wants_rows.value = True module.row_range.min = 2 module.row_range.max = 3 self.assertTrue(module.prepare_run(workspace)) self.assertTrue(isinstance(m, cpmeas.Measurements)) self.assertEqual(m.image_set_count, 1) self.assertTrue( 'FileName_DNA' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m[cpmeas.IMAGE, 'FileName_DNA', 1], file_names[0]) finally: os.remove(filename)
def test_01_03_get_image_by_id(self): file_name = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel1-01-A-01.tif") ip = I.load_imageplus(file_name) ip.show() ip_other = W.get_image_by_id(ip.getID()) self.assertEqual(ip_other.getID(), ip.getID())
def setUpClass(self): from cellprofiler.modules.tests import \ example_images_directory, maybe_download_example_image self.root_dir = example_images_directory() self.file_name = maybe_download_example_image( ["ExampleSBSImages"], "Channel1-01-A-01.tif") J.attach()
def test_01_06_get_temp_current_image(self): file_name = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel1-01-A-01.tif") ip = I.load_imageplus(file_name) W.set_temp_current_image(ip) ip_other = W.get_temp_current_image() self.assertEqual(ip.getID(), ip_other.getID())
def test_01_02_get_id_list(self): file_name = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel1-01-A-01.tif") ip = I.load_imageplus(file_name) W.set_current_image(ip) id_list = W.get_id_list() self.assertTrue(ip.getID() in id_list)
def test_02_01_load_one(self): path = os.path.join(example_images_directory(), "ExampleSpecklesImages") cpprefs.set_default_image_directory(path) file_name = "1-162hrh2ax2.tif" workspace, module = self.make_workspace([file_name]) assert isinstance(module, L.LoadSingleImage) module.prepare_run(workspace) module.run(workspace) m = workspace.measurements self.assertTrue(isinstance(m, cpmeas.Measurements)) self.assertEqual(m.image_set_count, 1) f = m.get_all_measurements(cpmeas.IMAGE, "_".join((L.C_FILE_NAME, self.get_image_name(0)))) self.assertEqual(len(f), 1) self.assertEqual(f[0], file_name) p = m.get_all_measurements(cpmeas.IMAGE, "_".join((L.C_PATH_NAME, self.get_image_name(0)))) self.assertEqual(len(p), 1) self.assertEqual(p[0], path) s = m.get_all_measurements(cpmeas.IMAGE, "_".join((L.C_SCALING, self.get_image_name(0)))) self.assertEqual(len(s), 1) self.assertEqual(s[0], 4095) md = m.get_all_measurements(cpmeas.IMAGE, "_".join((L.C_MD5_DIGEST, self.get_image_name(0)))) self.assertEqual(len(md), 1) md5 = hashlib.md5() image = workspace.image_set.get_image(self.get_image_name(0)) md5.update(np.ascontiguousarray(image.pixel_data).data) self.assertEqual(md5.hexdigest(), md[0])
def get_measurements_for_good_pipeline(nimages=1, group_numbers=None): '''Get an appropriately initialized measurements structure for the good pipeline''' path = os.path.join(example_images_directory(), "ExampleSBSImages") m = cpmeas.Measurements() if group_numbers is None: group_numbers = [1] * nimages group_indexes = [1] last_group_number = group_numbers[0] group_index = 1 for group_number in group_numbers: if group_number == last_group_number: group_index += 1 else: group_index = 1 group_indexes.append(group_index) for i in range(1, nimages + 1): filename = ("Channel2-%02d-%s-%02d.tif" % (i, "ABCDEFGH"[int( (i - 1) / 12)], ((i - 1) % 12) + 1)) url = pathname2url(os.path.join(path, filename)) m[cpmeas.IMAGE, cpmeas.C_FILE_NAME + "_DNA", i] = filename m[cpmeas.IMAGE, cpmeas.C_PATH_NAME + "_DNA", i] = path m[cpmeas.IMAGE, cpmeas.C_URL + "_DNA", i] = url m[cpmeas.IMAGE, cpmeas.GROUP_NUMBER, i] = group_numbers[i - 1] m[cpmeas.IMAGE, cpmeas.GROUP_INDEX, i] = group_indexes[i - 1] pipeline = cpp.Pipeline() pipeline.loadtxt(StringIO(GOOD_PIPELINE)) pipeline.write_pipeline_measurement(m) return m
def test_13_04_extra_lines(self): # # Regression test of issue #1211 - extra line at end / blank lines # dir = os.path.join(example_images_directory(), "ExampleSBSImages") file_name = 'Channel2-01-A-01.tif' csv_text = '''"Image_FileName_DNA","Image_PathName_DNA" "%s","%s" ''' % (file_name, dir) pipeline, module, filename = self.make_pipeline(csv_text) try: assert isinstance(module, L.LoadData) m = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, m, cpo.ObjectSet(), m, cpi.ImageSetList()) self.assertTrue(module.prepare_run(workspace)) self.assertTrue(isinstance(m, cpmeas.Measurements)) self.assertEqual(m.image_set_count, 1) self.assertTrue( 'FileName_DNA' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m[cpmeas.IMAGE, 'FileName_DNA', 1], file_name) finally: os.remove(filename)
def test_13_01_load_filename(self): # # Load a file, only specifying the FileName in the CSV # dir = os.path.join(example_images_directory(), "ExampleSBSImages") file_name = 'Channel2-01-A-01.tif' csv_text = '''"Image_FileName_DNA" "%s" ''' % file_name pipeline, module, filename = self.make_pipeline(csv_text) assert isinstance(module, L.LoadData) module.image_directory.dir_choice = cps.ABSOLUTE_FOLDER_NAME module.image_directory.custom_path = dir m = cpmeas.Measurements(mode="memory") workspace = cpw.Workspace(pipeline, module, m, cpo.ObjectSet(), m, cpi.ImageSetList()) self.assertTrue(module.prepare_run(workspace)) self.assertEqual(m.get_measurement(cpmeas.IMAGE, "FileName_DNA", 1), file_name) path = m.get_measurement(cpmeas.IMAGE, "PathName_DNA", 1) self.assertEqual(path, dir) self.assertEqual(m.get_measurement(cpmeas.IMAGE, "URL_DNA", 1), L.pathname2url(os.path.join(dir, file_name))) module.prepare_group(workspace, {}, [1]) module.run(workspace) img = workspace.image_set.get_image("DNA", must_be_grayscale=True) self.assertEqual(tuple(img.pixel_data.shape), (640, 640))
def get_measurements_for_good_pipeline(nimages = 1, group_numbers = None): '''Get an appropriately initialized measurements structure for the good pipeline''' path = os.path.join(example_images_directory(), "ExampleSBSImages") m = cpmeas.Measurements() if group_numbers is None: group_numbers = [1] * nimages group_indexes = [1] last_group_number = group_numbers[0] group_index = 1 for group_number in group_numbers: if group_number == last_group_number: group_index += 1 else: group_index = 1 group_indexes.append(group_index) for i in range(1, nimages+1): filename = ("Channel2-%02d-%s-%02d.tif" % (i, "ABCDEFGH"[int((i-1) / 12)], ((i-1) % 12) + 1)) url = pathname2url(os.path.join(path, filename)) m[cpmeas.IMAGE, cpmeas.C_FILE_NAME + "_DNA", i] = filename m[cpmeas.IMAGE, cpmeas.C_PATH_NAME + "_DNA", i] = path m[cpmeas.IMAGE, cpmeas.C_URL+"_DNA", i] = url m[cpmeas.IMAGE, cpmeas.GROUP_NUMBER, i] = group_numbers[i-1] m[cpmeas.IMAGE, cpmeas.GROUP_INDEX, i] = group_indexes[i-1] pipeline = cpp.Pipeline() pipeline.loadtxt(StringIO(GOOD_PIPELINE)) pipeline.write_pipeline_measurement(m) return m
def setUpClass(cls): from cellprofiler.modules.tests \ import example_images_directory, maybe_download_sbs maybe_download_sbs() cls.root_dir = example_images_directory() J.attach()
def test_03_03_measurement_columns(self): '''Check that results of get_measurement_columns match the actual column names output''' data = 'eJztW91u2zYUph0naFqgSHexBesNd9d2iSA7SZcEQ2vPXjcPsRs0Xn8wbJgi0TEHWjQkKrU39N32GHuEXe4RJtqyJXFyJMuSJ6cSQEiH5seP5/DwHEqyWrXOWe0beCTJsFXr7HcxQfCcKKxLjf4pHFATD/dg3UAKQxqk+ilsUR3+YBFYPoDlw1P55PTgEFZk+QTEOArN1n37tHsMwJZ9vmOXovPTpiMXPIXLF4gxrF+Zm6AEdp36P+3yWjGwcknQa4VYyHQppvVNvUs7o8HspxbVLILaSt/b2D7aVv8SGebL7hTo/HyOh4hc4N+RoMK02St0jU1MdQfv9C/WzngpE3i5HXqfu3YoCHYo2eWhp563/x647UsBdnvgab/jyFjX8DXWLIVA3FeuZqPg/ckh/W34+tsAjXZtjDsOwW0J49ga21klCEfjLfjwBVBxxlsNwe0IvLx00JDtfztUVAb7ClN7SYw/DL8p4LlcR4SYEe0+T/9FcQcRcSUfrgSe7h3KUey9LejJ5Td24DDMHtKc+mX0jWPn5tnZj62IvKJ/v7NXR1y96yNGB0Qx+wvoPW99heGKPlwRtGk0vnm4MH3vCfpy+SUzLfgdoZcKmemblN3C4txnQj9cbqCuYhEGmzzIwQY2kMqoMVrKDxbFlSU58fi4JeCnxxS/7bFbNYQ36jzGyTOyJI+PvbJz4RlX2vEy6flbNF7aupfTjM/z9Et6nhaNH2U5Xnw/WlLvtOYnAHcUR7+nIJrf3wH++eFyvafoOiKVuPG0qTOkm5iNnPoo/dwV+uFyg0KdMmiZyO0nblyKmweT0n9RfjkgDiSpr+gv5Yi4uOtP9Os21VGaeSZoXl7wG03dvv1awk5fxbRT0P5oEX3fhvB9IejL5V+kJ/uPnp9//Yq+fyZ9+Xh8Xafk2U/y/snPf1Q+PF7ADnH3qUH5vvOeQtXeb5nOneAydumF8B8L/FzmdniHFMMxxOGHiWlaVGc91zjjuoYycmuSjGNx8uYbhK96/JnINX8AoKvz/HgR+y2RHyLls7h+E2THF9RAVwa1dG15vcP407q/Cto/xI3raeaDqPf7WdGvGjLOpPJBmnl6nfJB0vl8XeL/qvN+1uJEVtb7qvSTpaP/fZxpP39Jcj+2alxW9lFZm+e0909xcX/turiCgAt637RK+4xfTnEDDaL3E7Se6OVvSGVuR+uyLjzjhljX0CDF/tZlnd12XBWsZp1E7edjs9tt0TercTDXN8fluOzgwvYRnwD/uuIytRjBOvrPRiLN9b0jjIMXN35PRrFOds9a/Lxt+THHZQOXlfiS4/K4m+M+XlwV3Oznef7LcTkux+W424X7u+DixPcbXPa+N+ftf/XwBOWJJ8CfJ7isIkIGBuXfTRlSf/xxjykRqmiTr2ukM/uy6fnQhvMMQniqAk91Hg/WkM5wdzQwbDaL0b7CsCo1ndpzu7Y2reW8vRDeoPfzN/KaSKW6phijGefFtCYKX0Xgq8zj6yPFtAzE0JDZJ6k1ETsT0bWr10+2A/i88120pU8fPrh7k38B4Pcr19/+eR6Hb2OjWLgP/P/zuheCKwG/n4/9Gizm149uaD/VMavt/wXhfSus' # alternate data: 1 image, 1 object, tex scale=3, gabor angles=6 # data = 'eJztW91u2zYUph0nW1agSHexFesNd9d2iSA7TZMGQ2vPXjcPsWs0Xrti2DBGomMONGlIVGpv6Hvtco+yy13uESbasiVzciTLP7UzCSCkQ/Hjx3N4eA5lWbVS86z0FTzSdFgrNQ9ahGLYoEi0uNU5hV1uk94+LFsYCWxCzk5hjTP4nUNh/hDmj08LJ6eHBVjQ9ScgwZGp1m67p7+OAdhxzx+6Jevd2vbkTKBI+RwLQdilvQ1y4K5X/6dbXiGLoAuKXyHqYNunGNVXWYs3+93xrRo3HYrrqBNs7B51p3OBLftFawT0bjdID9Nz8htWVBg1e4mviE048/Be/2rtmJcLhVfaofGZb4eMYoecW+4F6mX7b4HfPhditzuB9nueTJhJrojpIApJB12ORyH70yP625robwtU6qUB7iQCt6OMY2dgZ4NiEo83M4HPgLw33mIEbk/hlaWJe+Lg6x4yBOwgYbQXMf4o/LaCl3IZU2rHtPs0/WfFHcbE5SZwOfB4/5Eex967ip5Sfu0GDstuY9Orn0ffJHaunp19X4vJq/r3G3d1JNW73Be8S5HdmUHvaesrCpedwGVBncfjm4aL0veWoq+UXwjbgd9QfoHoWN9511VUfPtUwUu5glvIoQJWZXCDFWJhQ3CrP9f8z4rLa/rC1ueOghsdI9yud17kvCXJK7qmD479vHcRGNesdngcE7fq+VLjoqtzfp75SqrfqudHjRN5fT4/Xda8xpmfmLijefSLinvB/d2eJ5fbiDFMC0nzTZUJzGwi+oFxRPXzkdKPlCscMi6gY2O/n1XN8yjfLUr/Wfn1kDiwSH2T7uPirL84fl3nDC8zv4TNy3P5QMncx6w57HSc0E5h+6BZ9P0hgu9zRV8p/6w9PLj/rPHlS/72qfbFg8F1mdOnP+oHT376vfDuwQx2SLofDcvzzbccGu5+1Pae+OaxSzuC/0Thl7K0wxuMLM8Qj94NTVPjTLR94wzqKqjv1ywyjiXJm68xuWzL3z6u5IM+M6b58YryQ6x8ltRvwuz4nFv40uIOM+fXO4p/Wc9RYfuHpHF9mfmgsGH6FSPGuah8sMw8vUn5YNH5fFPi/6rz/rrFiXVZ76vST9eO3vs4l/27yyL3Y6vGrcs+at3medn7p6S4P+76uIyCC3uvtEr7DF5CSQN14/cTtp74xa/YEH5Hm7IuAuOGhJm4u8T+NmWd3XRcEaxmncTt56bYbV3jQopLcSkuxb2vfcfHYDIuSpk7ghKG/7NRWGZ83lPGIYufj4aj2CS7r1v+u2n5PsWtB25d4kuKS+Nuivv/4orgej9P81+KS3EpLsXdLNzfGR+nvr+QcvC9uGz/S4AnLE88BJN5QsoGprRrcfn9k6V1Bh/p2BrlyBx+JaOduZfVwAczkqcbwVNUeIrTeIiJmSCtftdy2RzBO0gQQ6t6tQ23tjSqlbztCN6w9+/X8trY4MxEVn/MeT6qicNXUPgK0/g6GNmOhQXuCfek1YZicyj6dg36yW4IX3C+s670yb07H1znXwBM+pXvb/88S8K3tZXN3AaT/+O6FYHLgUk/H/g1mM2v71/TfqTjurb/F2wyHOE=' cpprefs.set_default_image_directory( os.path.join(example_images_directory(), "ExampleSBSImages")) fd = StringIO(zlib.decompress(base64.b64decode(data))) pipeline = cpp.Pipeline() pipeline.load(fd) module = pipeline.modules()[3] measurements = pipeline.run(image_set_end=1) for x in module.get_measurement_columns(pipeline): assert x[1] in measurements.get_feature_names( x[0] ), '%s does not match any measurement output by pipeline' % ( str(x)) for obname in measurements.get_object_names(): for m in measurements.get_feature_names(obname): if m.startswith(M.TEXTURE): assert ( obname, m, 'float' ) in module.get_measurement_columns( pipeline ), 'no entry matching %s in get_measurement_columns.' % ( (obname, m, 'float'))
def test_01_02_get_id_list(self): file_name = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel1-01-A-01.tif") ip = I.load_imageplus(file_name) W.set_current_image(ip) id_list = W.get_id_list() self.assertTrue(ip.getID() in id_list)
def test_04_01_load_file(self): dir = os.path.join(example_images_directory(), "ExampleSBSImages") file_name = 'Channel2-01-A-01.tif' csv_text = '''"Image_FileName_DNA","Image_PathName_DNA" "%s","%s" '''%(file_name, dir) pipeline, module, filename = self.make_pipeline(csv_text) c0_ran = [False] def callback(workspace): imgset = workspace.image_set image = imgset.get_image("DNA") pixels = image.pixel_data self.assertEqual(pixels.shape[0],640) c0_ran[0] = True c0 = C0() c0.callback = callback c0.module_num = 2 pipeline.add_module(c0) try: m = pipeline.run() self.assertTrue(isinstance(m, cpmeas.Measurements)) self.assertTrue(c0_ran[0]) hexdigest = m.get_current_image_measurement('MD5Digest_DNA') self.assertEqual(hexdigest, "67880f6269fbf438d4b9c92256aa1d8f") self.assertTrue('PathName_DNA' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m.get_current_image_measurement('PathName_DNA'), dir) self.assertTrue('FileName_DNA' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m.get_current_image_measurement('FileName_DNA'), file_name) finally: os.remove(filename)
def test_03_02_load_using_bioformats(self): path = os.path.join(example_images_directory(), 'ExampleSBSImages', 'Channel1-01-A-01.tif') data = F.load_using_bioformats(path, rescale=False) expected_0_10_0_10 = np.array( [[ 0, 7, 7, 6, 5, 8, 4, 2, 1, 2], [ 0, 8, 8, 7, 6, 10, 4, 2, 2, 2], [ 0, 9, 9, 7, 8, 8, 2, 1, 3, 2], [ 0, 10, 9, 8, 10, 6, 2, 2, 3, 2], [ 0, 10, 10, 10, 9, 4, 2, 2, 2, 2], [ 0, 9, 9, 10, 8, 3, 2, 4, 2, 2], [ 0, 9, 9, 10, 8, 2, 2, 4, 3, 2], [ 0, 9, 8, 9, 7, 4, 2, 2, 2, 2], [ 0, 10, 11, 9, 9, 4, 2, 2, 2, 2], [ 0, 12, 13, 12, 9, 4, 2, 2, 2, 2]], dtype=np.uint8) expected_n10_n10 = np.array( [[2, 1, 1, 1, 2, 2, 1, 2, 1, 2], [1, 2, 2, 2, 2, 1, 1, 1, 2, 1], [1, 1, 1, 2, 1, 2, 2, 2, 2, 1], [2, 2, 2, 2, 3, 2, 2, 2, 2, 1], [1, 2, 2, 1, 1, 1, 1, 1, 2, 2], [2, 1, 2, 2, 2, 1, 1, 2, 2, 2], [2, 2, 3, 2, 2, 1, 2, 2, 2, 1], [3, 3, 1, 2, 2, 2, 2, 3, 2, 2], [3, 2, 2, 2, 2, 2, 2, 2, 3, 3], [5, 2, 3, 3, 2, 2, 2, 3, 2, 2]], dtype=np.uint8) self.assertTrue(np.all(expected_0_10_0_10 == data[:10,:10])) self.assertTrue(np.all(expected_n10_n10 == data[-10:,-10:]))
def setUpClass(cls): from cellprofiler.modules.tests \ import example_images_directory, maybe_download_sbs maybe_download_sbs() cls.root_dir = example_images_directory() J.attach()
def test_03_08_load_mask(self): path = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel2-01-A-01.tif") workspace = self.run_workspace(path, N.LOAD_AS_MASK) image = workspace.image_set.get_image(IMAGE_NAME) pixel_data = image.pixel_data self.assertSequenceEqual(pixel_data.shape, (640, 640)) self.assertEqual(np.sum(~pixel_data), 627)
def test_03_03_initial_measurements(self): # # Walk to the initial measurements # self.awthread = self.AWThread(self.announce_addr) self.awthread.start() self.set_work_socket() self.awthread.ex( self.awthread.aw.do_job, cpanalysis.WorkReply(image_set_numbers=[1], worker_runs_post_group=False, wants_dictionary=True)) # # The worker should ask for the pipeline and preferences next. # req = self.awthread.recv(self.work_socket) self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest) self.assertEqual(req.analysis_id, self.analysis_id) input_dir = os.path.join(example_images_directory(), "ExampleSBSImages") cpprefs.set_default_image_directory(input_dir) preferences = { cpprefs.DEFAULT_IMAGE_DIRECTORY: cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY) } rep = cpanalysis.Reply(pipeline_blob=np.array(GOOD_PIPELINE), preferences=preferences) req.reply(rep) # # The worker asks for the initial measurements. # req = self.awthread.recv(self.work_socket) self.assertIsInstance(req, cpanalysis.InitialMeasurementsRequest) self.assertEqual(req.analysis_id, self.analysis_id) m = get_measurements_for_good_pipeline() try: req.reply(cpanalysis.Reply(buf=m.file_contents())) req = self.awthread.recv(self.work_socket) # # Check that they were installed # self.assertIn(self.analysis_id, self.awthread.aw.initial_measurements) cm = self.awthread.aw.initial_measurements[self.analysis_id] for object_name in m.get_object_names(): for feature_name in m.get_feature_names(object_name): self.assertTrue(cm.has_feature(object_name, feature_name)) self.assertEqual(cm[object_name, feature_name, 1], m[object_name, feature_name, 1]) # # Cancel and check for exit # req.reply(cpanalysis.ServerExited()) self.assertRaises(cpaw.CancelledException, self.awthread.ecute) finally: m.close()
def test_03_05_load_color_as_monochrome(self): path = os.path.join(example_images_directory(), "ExampleGrayToColor", "AS_09125_050116030001_D03f00d0.tif") workspace = self.run_workspace(path, N.LOAD_AS_GRAYSCALE_IMAGE) image = workspace.image_set.get_image(IMAGE_NAME) pixel_data = image.pixel_data self.assertSequenceEqual(pixel_data.shape, (512, 512)) self.assertTrue(np.all(pixel_data >= 0)) self.assertTrue(np.all(pixel_data <= 1))
def test_01_04_get_image_by_name(self): file_name = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel1-01-A-01.tif") ip = I.load_imageplus(file_name) title = W.make_unique_name("Whatever") ip.setTitle(title) self.assertEqual(ip.getTitle(), title) ip.show() ip_other = W.get_image_by_name(title) self.assertEqual(ip_other.getID(), ip.getID())
def test_03_07_load_raw(self): path = os.path.join(example_images_directory(), "ExampleSpecklesImages", "1-162hrh2ax2.tif") workspace = self.run_workspace(path, N.LOAD_AS_ILLUMINATION_FUNCTION) image = workspace.image_set.get_image(IMAGE_NAME) pixel_data = image.pixel_data self.assertSequenceEqual(pixel_data.shape, (1000, 1200)) self.assertTrue(np.all(pixel_data >= 0)) self.assertTrue(np.all(pixel_data <= 1. / 16.))
def test_03_08_load_mask(self): path = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel2-01-A-01.tif") workspace = self.run_workspace(path, N.LOAD_AS_MASK) image = workspace.image_set.get_image(IMAGE_NAME) pixel_data = image.pixel_data self.assertSequenceEqual(pixel_data.shape, (640, 640)) self.assertEqual(np.sum(~pixel_data), 627)
def test_03_03_initial_measurements(self): # # Walk to the initial measurements # self.awthread = self.AWThread(self.announce_addr) self.awthread.start() self.set_work_socket() self.awthread.ex(self.awthread.aw.do_job, cpanalysis.WorkReply( image_set_numbers = [1], worker_runs_post_group = False, wants_dictionary = True)) # # The worker should ask for the pipeline and preferences next. # req = self.awthread.recv(self.work_socket) self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest) self.assertEqual(req.analysis_id, self.analysis_id) input_dir = os.path.join(example_images_directory(), "ExampleSBSImages") cpprefs.set_default_image_directory(input_dir) preferences = {cpprefs.DEFAULT_IMAGE_DIRECTORY: cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY) } rep = cpanalysis.Reply( pipeline_blob = np.array(GOOD_PIPELINE), preferences = preferences) req.reply(rep) # # The worker asks for the initial measurements. # req = self.awthread.recv(self.work_socket) self.assertIsInstance(req, cpanalysis.InitialMeasurementsRequest) self.assertEqual(req.analysis_id, self.analysis_id) m = get_measurements_for_good_pipeline() try: req.reply(cpanalysis.Reply(buf = m.file_contents())) req = self.awthread.recv(self.work_socket) # # Check that they were installed # self.assertIn(self.analysis_id, self.awthread.aw.initial_measurements) cm = self.awthread.aw.initial_measurements[self.analysis_id] for object_name in m.get_object_names(): for feature_name in m.get_feature_names(object_name): self.assertTrue(cm.has_feature(object_name, feature_name)) self.assertEqual(cm[object_name, feature_name, 1], m[object_name, feature_name, 1]) # # Cancel and check for exit # req.reply(cpanalysis.ServerExited()) self.assertRaises(cpaw.CancelledException, self.awthread.ecute) finally: m.close()
def test_03_05_load_color_as_monochrome(self): path = os.path.join(example_images_directory(), "ExampleGrayToColor", "AS_09125_050116030001_D03f00d0.tif") workspace = self.run_workspace(path, N.LOAD_AS_GRAYSCALE_IMAGE) image = workspace.image_set.get_image(IMAGE_NAME) pixel_data = image.pixel_data self.assertSequenceEqual(pixel_data.shape, (512, 512)) self.assertTrue(np.all(pixel_data >= 0)) self.assertTrue(np.all(pixel_data <= 1))
def test_03_07_load_raw(self): path = os.path.join(example_images_directory(), "ExampleSpecklesImages", "1-162hrh2ax2.tif") workspace = self.run_workspace(path, N.LOAD_AS_ILLUMINATION_FUNCTION) image = workspace.image_set.get_image(IMAGE_NAME) pixel_data = image.pixel_data self.assertSequenceEqual(pixel_data.shape, (1000, 1200)) self.assertTrue(np.all(pixel_data >= 0)) self.assertTrue(np.all(pixel_data <= 1. / 16.))
def test_01_04_get_image_by_name(self): file_name = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel1-01-A-01.tif") ip = I.load_imageplus(file_name) title = W.make_unique_name("Whatever") ip.setTitle(title) self.assertEqual(ip.getTitle(), title) ip.show() ip_other = W.get_image_by_name(title) self.assertEqual(ip_other.getID(), ip.getID())
def test_03_02_load_monochrome_as_color(self): path = os.path.join(example_images_directory(), "ExampleGrayToColor", "AS_09125_050116030001_D03f00d0.tif") workspace = self.run_workspace(path, N.LOAD_AS_COLOR_IMAGE) image = workspace.image_set.get_image(IMAGE_NAME) pixel_data = image.pixel_data self.assertSequenceEqual(pixel_data.shape, (512, 512, 3)) self.assertTrue(np.all(pixel_data >= 0)) self.assertTrue(np.all(pixel_data <= 1)) np.testing.assert_equal(pixel_data[:, :, 0], pixel_data[:, :, 1]) np.testing.assert_equal(pixel_data[:, :, 0], pixel_data[:, :, 2])
def test_03_02_load_monochrome_as_color(self): path = os.path.join(example_images_directory(), "ExampleGrayToColor", "AS_09125_050116030001_D03f00d0.tif") workspace = self.run_workspace(path, N.LOAD_AS_COLOR_IMAGE) image = workspace.image_set.get_image(IMAGE_NAME) pixel_data = image.pixel_data self.assertSequenceEqual(pixel_data.shape, (512, 512, 3)) self.assertTrue(np.all(pixel_data >= 0)) self.assertTrue(np.all(pixel_data <= 1)) np.testing.assert_equal(pixel_data[:, :, 0], pixel_data[:, :, 1]) np.testing.assert_equal(pixel_data[:, :, 0], pixel_data[:, :, 2])
def setUpClass(cls): maybe_download_sbs() cls.test_folder = "loaddata" cls.test_path = os.path.join(example_images_directory(), cls.test_folder) cls.test_filename = "image.tif" cls.test_shape = (13, 15) path = maybe_download_example_image([cls.test_folder], cls.test_filename, shape=cls.test_shape) with open(path, "rb") as fd: cls.test_md5 = hashlib.md5(fd.read()).hexdigest()
def setUpClass(cls): maybe_download_sbs() cls.test_folder = "loaddata" cls.test_path = os.path.join( example_images_directory(), cls.test_folder) cls.test_filename = "image.tif" cls.test_shape = (13, 15) path = maybe_download_example_image([cls.test_folder], cls.test_filename, shape=cls.test_shape) with open(path, "rb") as fd: cls.test_md5 = hashlib.md5(fd.read()).hexdigest()
def test_02_02_scale(self): '''Load an image twice, as scaled and unscaled''' path = os.path.join(example_images_directory(), "ExampleSpecklesImages") cpprefs.set_default_image_directory(path) file_names = ["1-162hrh2ax2.tif", "1-162hrh2ax2.tif"] workspace, module = self.make_workspace(file_names) self.assertTrue(isinstance(module, L.LoadSingleImage)) module.file_settings[0].rescale.value = False module.file_settings[1].rescale.value = True module.run(workspace) unscaled, scaled = [workspace.image_set.get_image(self.get_image_name(i)).pixel_data for i in range(2)] np.testing.assert_almost_equal(unscaled * 65535. / 4095., scaled)
def test_02_02_scale(self): '''Load an image twice, as scaled and unscaled''' path = os.path.join(example_images_directory(), "ExampleSpecklesImages") cpprefs.set_default_image_directory(path) file_names = ["1-162hrh2ax2.tif", "1-162hrh2ax2.tif"] workspace, module = self.make_workspace(file_names) self.assertTrue(isinstance(module, L.LoadSingleImage)) module.file_settings[0].rescale.value = False module.file_settings[1].rescale.value = True module.run(workspace) unscaled, scaled = [workspace.image_set.get_image(self.get_image_name(i)).pixel_data for i in range(2)] np.testing.assert_almost_equal(unscaled * 65535. / 4095., scaled)
def test_09_01_load_bcb_file(self): dir = os.path.join(example_images_directory(), "ExampleSBSImages") file_name = 'Channel2-01-A-01.tif' csv_text = '''ELN_RUN_ID,CBIP_RUN_ID,ASSAY_PLATE_BARCODE,\ MX_PLATE_ID,ASSAY_WELL_POSITION,ASSAY_WELL_ROLE,SITE_X,SITE_Y,\ MICROSCOPE,SOURCE_DESCRIPTION,DATE_CREATED,FILE_PATH,FILE_NAME,\ CPD_PLATE_MAP_NAME,CPD_WELL_POSITION,BROAD_ID,\ CPD_MMOL_CONC,SOURCE_NAME,SOURCE_COMPOUND_NAME,CPD_SMILES "4012-10-W01-01-02","4254","BR00021547","20777","N01","COMPOUND",\ "2","2","GS IX Micro","DAPI","2010/03/19 06:01:12","%s",\ "%s","C-4012-00-D80-001_Rev3","N01",\ "BRD-K71194192-001-01-6","2.132352941","ChemBridge","",\ "Oc1ccnc(SCC(=O)Nc2ccc(Oc3ccccc3)cc2)n1" ''' % (dir, file_name) pipeline, module, filename = self.make_pipeline(csv_text) c0_ran = [False] def callback(workspace): imgset = workspace.image_set image = imgset.get_image("DAPI") pixels = image.pixel_data self.assertEqual(pixels.shape[0], 640) c0_ran[0] = True c0 = C0() c0.callback = callback c0.module_num = 2 pipeline.add_module(c0) try: m = pipeline.run() self.assertTrue(isinstance(m, cpmeas.Measurements)) self.assertTrue(c0_ran[0]) hexdigest = m.get_current_image_measurement('MD5Digest_DAPI') # # This appears to be tristable, depending on whether PIL or # Bioformats or Subimager loads it (???) # self.assertTrue( (hexdigest == u'f7d75e4ba3ca1385dfe09c111261359e') or (hexdigest == '1a37c43914c7ceb7d9cac503a5c1c767')) self.assertTrue( 'PathName_DAPI' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m.get_current_image_measurement('PathName_DAPI'), dir) self.assertTrue( 'FileName_DAPI' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m.get_current_image_measurement('FileName_DAPI'), file_name) finally: os.remove(filename)
def test_03_01_load_color(self): path = os.path.join(example_images_directory(), "ExampleColorToGray", "AS_09125_050116030001_D03f00_color.tif") workspace = self.run_workspace(path, N.LOAD_AS_COLOR_IMAGE) image = workspace.image_set.get_image(IMAGE_NAME) pixel_data = image.pixel_data self.assertSequenceEqual(pixel_data.shape, (512, 512, 3)) self.assertTrue(np.all(pixel_data >= 0)) self.assertTrue(np.all(pixel_data <= 1)) m = workspace.measurements self.assertEqual(m[cpmeas.IMAGE, C_MD5_DIGEST + "_" + IMAGE_NAME], "16729de931dc40f5ca19621598a3e7d6") self.assertEqual(m[cpmeas.IMAGE, C_HEIGHT + "_" + IMAGE_NAME], 512) self.assertEqual(m[cpmeas.IMAGE, C_WIDTH + "_" + IMAGE_NAME], 512)
def test_03_09_load_objects(self): path = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel2-01-A-01.tif") workspace = self.run_workspace(path, N.LOAD_AS_OBJECTS) o = workspace.object_set.get_objects(OBJECTS_NAME) assert isinstance(o, N.cpo.Objects) areas = o.areas self.assertEqual(areas[0], 9) self.assertEqual(areas[1], 321) self.assertEqual(areas[2], 2655) m = workspace.measurements self.assertEqual(m[cpmeas.IMAGE, C_MD5_DIGEST + "_" + OBJECTS_NAME], "67880f6269fbf438d4b9c92256aa1d8f") self.assertEqual(m[cpmeas.IMAGE, C_WIDTH + "_" + OBJECTS_NAME], 640)
def test_11_01_load_objects(self): r = np.random.RandomState() r.seed(1101) labels = r.randint(0,10, size=(30,20)).astype(np.uint8) handle, name = tempfile.mkstemp(".png") fd = os.fdopen(handle, "wb") img = PIL.Image.fromarray(labels, "L") img.save(fd, "PNG") fd.close() png_path, png_file = os.path.split(name) sbs_dir = os.path.join(example_images_directory(), "ExampleSBSImages") csv_text = """%s_%s,%s_%s,%s_DNA,%s_DNA %s,%s,Channel2-01-A-01.tif,%s """ % (L.C_OBJECTS_FILE_NAME, OBJECTS_NAME, L.C_OBJECTS_PATH_NAME, OBJECTS_NAME, L.C_FILE_NAME, L.C_PATH_NAME, png_file, png_path, sbs_dir) pipeline, module, csv_name = self.make_pipeline(csv_text) assert isinstance(pipeline, cpp.Pipeline) assert isinstance(module, L.LoadData) module.wants_images.value = True try: image_set_list = cpi.ImageSetList() measurements = cpmeas.Measurements() workspace = cpw.Workspace( pipeline, module, None, None, measurements, image_set_list) pipeline.prepare_run(workspace) key_names, g = pipeline.get_groupings(workspace) self.assertEqual(len(g), 1) module.prepare_group(workspace, g[0][0], g[0][1]) image_set = image_set_list.get_image_set(g[0][1][0]-1) object_set = cpo.ObjectSet() workspace = cpw.Workspace(pipeline, module, image_set, object_set, measurements, image_set_list) module.run(workspace) objects = object_set.get_objects(OBJECTS_NAME) self.assertTrue(np.all(objects.segmented == labels)) self.assertEqual(measurements.get_current_image_measurement( L.I.FF_COUNT % OBJECTS_NAME), 9) for feature in (L.I.M_LOCATION_CENTER_X, L.I.M_LOCATION_CENTER_Y, L.I.M_NUMBER_OBJECT_NUMBER): value = measurements.get_current_measurement( OBJECTS_NAME, feature) self.assertEqual(len(value), 9) finally: clear_image_reader_cache() os.remove(name) os.remove(csv_name)
def test_11_01_load_objects(self): r = np.random.RandomState() r.seed(1101) labels = r.randint(0,10, size=(30,20)).astype(np.uint8) handle, name = tempfile.mkstemp(".png") fd = os.fdopen(handle, "wb") img = PIL.Image.fromarray(labels, "L") img.save(fd, "PNG") fd.close() png_path, png_file = os.path.split(name) sbs_dir = os.path.join(example_images_directory(), "ExampleSBSImages") csv_text = """%s_%s,%s_%s,%s_DNA,%s_DNA %s,%s,Channel2-01-A-01.tif,%s """ % (L.C_OBJECTS_FILE_NAME, OBJECTS_NAME, L.C_OBJECTS_PATH_NAME, OBJECTS_NAME, L.C_FILE_NAME, L.C_PATH_NAME, png_file, png_path, sbs_dir) pipeline, module, csv_name = self.make_pipeline(csv_text) assert isinstance(pipeline, cpp.Pipeline) assert isinstance(module, L.LoadData) module.wants_images.value = True try: image_set_list = cpi.ImageSetList() measurements = cpmeas.Measurements() workspace = cpw.Workspace( pipeline, module, None, None, measurements, image_set_list) pipeline.prepare_run(workspace) key_names, g = pipeline.get_groupings(workspace) self.assertEqual(len(g), 1) module.prepare_group(workspace, g[0][0], g[0][1]) image_set = image_set_list.get_image_set(g[0][1][0]-1) object_set = cpo.ObjectSet() workspace = cpw.Workspace(pipeline, module, image_set, object_set, measurements, image_set_list) module.run(workspace) objects = object_set.get_objects(OBJECTS_NAME) self.assertTrue(np.all(objects.segmented == labels)) self.assertEqual(measurements.get_current_image_measurement( L.I.FF_COUNT % OBJECTS_NAME), 9) for feature in (L.I.M_LOCATION_CENTER_X, L.I.M_LOCATION_CENTER_Y, L.I.M_NUMBER_OBJECT_NUMBER): value = measurements.get_current_measurement( OBJECTS_NAME, feature) self.assertEqual(len(value), 9) finally: clear_image_reader_cache() os.remove(name) os.remove(csv_name)
def test_03_09_load_objects(self): path = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel2-01-A-01.tif") workspace = self.run_workspace(path, N.LOAD_AS_OBJECTS) o = workspace.object_set.get_objects(OBJECTS_NAME) assert isinstance(o, N.cpo.Objects) areas = o.areas self.assertEqual(areas[0], 9) self.assertEqual(areas[1], 321) self.assertEqual(areas[2], 2655) m = workspace.measurements self.assertEqual(m[cpmeas.IMAGE, C_MD5_DIGEST + "_" + OBJECTS_NAME], "67880f6269fbf438d4b9c92256aa1d8f") self.assertEqual(m[cpmeas.IMAGE, C_WIDTH + "_" + OBJECTS_NAME], 640)
def test_03_01_load_color(self): path = os.path.join(example_images_directory(), "ExampleColorToGray", "AS_09125_050116030001_D03f00_color.tif") workspace = self.run_workspace(path, N.LOAD_AS_COLOR_IMAGE) image = workspace.image_set.get_image(IMAGE_NAME) pixel_data = image.pixel_data self.assertSequenceEqual(pixel_data.shape, (512, 512, 3)) self.assertTrue(np.all(pixel_data >= 0)) self.assertTrue(np.all(pixel_data <= 1)) m = workspace.measurements self.assertEqual(m[cpmeas.IMAGE, C_MD5_DIGEST + "_" + IMAGE_NAME], "16729de931dc40f5ca19621598a3e7d6") self.assertEqual(m[cpmeas.IMAGE, C_HEIGHT + "_" + IMAGE_NAME], 512) self.assertEqual(m[cpmeas.IMAGE, C_WIDTH + "_" + IMAGE_NAME], 512)
def test_02_01_make_image_reader(self): path = os.path.join(example_images_directory(), 'ExampleSBSImages', 'Channel1-01-A-01.tif') ImageReader = F.make_image_reader_class() FormatTools = F.make_format_tools_class() reader = ImageReader() reader.setId(path) self.assertEqual(reader.getDimensionOrder(), "XYCZT") self.assertEqual(640, reader.getSizeX()) self.assertEqual(640, reader.getSizeY()) self.assertEqual(reader.getImageCount(), 1) self.assertEqual(reader.getSizeC(), 1) self.assertEqual(reader.getSizeT(), 1) self.assertEqual(reader.getSizeZ(), 1) self.assertEqual(reader.getPixelType(), FormatTools.UINT8) self.assertEqual(reader.getRGBChannelCount(), 1)
def test_02_01_make_image_reader(self): path = os.path.join(example_images_directory(), 'ExampleSBSImages', 'Channel1-01-A-01.tif') ImageReader = F.make_image_reader_class() FormatTools = F.make_format_tools_class() reader = ImageReader() reader.setId(path) self.assertEqual(reader.getDimensionOrder(), "XYCZT") self.assertEqual(640, reader.getSizeX()) self.assertEqual(640, reader.getSizeY()) self.assertEqual(reader.getImageCount(), 1) self.assertEqual(reader.getSizeC(), 1) self.assertEqual(reader.getSizeT(), 1) self.assertEqual(reader.getSizeZ(), 1) self.assertEqual(reader.getPixelType(), FormatTools.UINT8) self.assertEqual(reader.getRGBChannelCount(), 1)
def test_02_01_make_image_reader(self): path = os.path.join(example_images_directory(), "ExampleSBSImages", "Channel1-01-A-01.tif") ImageReader = F.make_image_reader_class() FormatTools = F.make_format_tools_class() reader = ImageReader() reader.setId(path) self.assertEqual(reader.getDimensionOrder(), "XYCZT") metadata = J.jdictionary_to_string_dictionary(reader.getMetadata()) self.assertEqual(int(metadata["ImageWidth"]), reader.getSizeX()) self.assertEqual(int(metadata["ImageLength"]), reader.getSizeY()) self.assertEqual(reader.getImageCount(), 1) self.assertEqual(reader.getSizeC(), 1) self.assertEqual(reader.getSizeT(), 1) self.assertEqual(reader.getSizeZ(), 1) self.assertEqual(reader.getPixelType(), FormatTools.UINT8) self.assertEqual(reader.getRGBChannelCount(), 1)
def get_measurements_for_good_pipeline(nimages = 1, group_numbers = None): '''Get an appropriately initialized measurements structure for the good pipeline''' path = os.path.join(example_images_directory(), "ExampleSBSImages") m = cpmeas.Measurements() if group_numbers is None: group_numbers = [1] * nimages group_indexes = [1] last_group_number = group_numbers[0] group_index = 1 for group_number in group_numbers: if group_number == last_group_number: group_index += 1 else: group_index = 1 group_indexes.append(group_index) for i in range(1, nimages+1): filename = ("Channel2-%02d-%s-%02d.tif" % (i, "ABCDEFGH"[int((i-1) / 12)], ((i-1) % 12) + 1)) url = pathname2url(os.path.join(path, filename)) m[cpmeas.IMAGE, cpmeas.C_FILE_NAME + "_DNA", i] = filename m[cpmeas.IMAGE, cpmeas.C_PATH_NAME + "_DNA", i] = path m[cpmeas.IMAGE, cpmeas.C_URL+"_DNA", i] = url m[cpmeas.IMAGE, cpmeas.GROUP_NUMBER, i] = group_numbers[i-1] m[cpmeas.IMAGE, cpmeas.GROUP_INDEX, i] = group_indexes[i-1] jblob = J.run_script(""" importPackage(Packages.org.cellprofiler.imageset); importPackage(Packages.org.cellprofiler.imageset.filter); var imageFile=new ImageFile(new java.net.URI(url)); var imageFileDetails = new ImageFileDetails(imageFile); var imageSeries=new ImageSeries(imageFile, 0); var imageSeriesDetails = new ImageSeriesDetails(imageSeries, imageFileDetails); var imagePlane=new ImagePlane(imageSeries, 0, ImagePlane.ALWAYS_MONOCHROME); var ipd = new ImagePlaneDetails(imagePlane, imageSeriesDetails); var stack = ImagePlaneDetailsStack.makeMonochromeStack(ipd); var stacks = java.util.Collections.singletonList(stack); var keys = java.util.Collections.singletonList(imageNumber); var imageSet = new ImageSet(stacks, keys); imageSet.compress(java.util.Collections.singletonList("DNA"), null); """, dict(url=url, imageNumber = str(i))) blob = J.get_env().get_byte_array_elements(jblob) m[cpmeas.IMAGE, M_IMAGE_SET, i, blob.dtype] = blob pipeline = cpp.Pipeline() pipeline.loadtxt(StringIO(GOOD_PIPELINE)) pipeline.write_pipeline_measurement(m) return m
def test_09_01_load_bcb_file(self): dir = os.path.join(example_images_directory(), "ExampleSBSImages") file_name = 'Channel2-01-A-01.tif' csv_text = '''ELN_RUN_ID,CBIP_RUN_ID,ASSAY_PLATE_BARCODE,\ MX_PLATE_ID,ASSAY_WELL_POSITION,ASSAY_WELL_ROLE,SITE_X,SITE_Y,\ MICROSCOPE,SOURCE_DESCRIPTION,DATE_CREATED,FILE_PATH,FILE_NAME,\ CPD_PLATE_MAP_NAME,CPD_WELL_POSITION,BROAD_ID,\ CPD_MMOL_CONC,SOURCE_NAME,SOURCE_COMPOUND_NAME,CPD_SMILES "4012-10-W01-01-02","4254","BR00021547","20777","N01","COMPOUND",\ "2","2","GS IX Micro","DAPI","2010/03/19 06:01:12","%s",\ "%s","C-4012-00-D80-001_Rev3","N01",\ "BRD-K71194192-001-01-6","2.132352941","ChemBridge","",\ "Oc1ccnc(SCC(=O)Nc2ccc(Oc3ccccc3)cc2)n1" '''%(dir,file_name) pipeline, module, filename = self.make_pipeline(csv_text) c0_ran = [False] def callback(workspace): imgset = workspace.image_set image = imgset.get_image("DAPI") pixels = image.pixel_data self.assertEqual(pixels.shape[0],640) c0_ran[0] = True c0 = C0() c0.callback = callback c0.module_num = 2 pipeline.add_module(c0) try: m = pipeline.run() self.assertTrue(isinstance(m, cpmeas.Measurements)) self.assertTrue(c0_ran[0]) hexdigest = m.get_current_image_measurement('MD5Digest_DAPI') # # This appears to be tristable, depending on whether PIL or # Bioformats or Subimager loads it (???) # self.assertTrue((hexdigest == u'f7d75e4ba3ca1385dfe09c111261359e') or (hexdigest == '1a37c43914c7ceb7d9cac503a5c1c767')) self.assertTrue('PathName_DAPI' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m.get_current_image_measurement('PathName_DAPI'), dir) self.assertTrue('FileName_DAPI' in m.get_feature_names(cpmeas.IMAGE)) self.assertEqual(m.get_current_image_measurement('FileName_DAPI'), file_name) finally: os.remove(filename)
def test_02_03_prepare_run(self): # regression test for issue #673 and #1161 # # If LoadSingleImage appears first, pathname data does not show # up in the measurements. # maybe_download_sbs() folder = "ExampleSBSImages" path = os.path.join(example_images_directory(), folder) filename = "Channel1-01-A-01.tif" pipeline = cpp.Pipeline() lsi = L.LoadSingleImage() lsi.module_num = 1 lsi.directory.dir_choice = cps.ABSOLUTE_FOLDER_NAME lsi.directory.custom_path = path lsi.file_settings[0].image_name.value = self.get_image_name(0) lsi.file_settings[0].file_name.value = filename pipeline.add_module(lsi) li = LI.LoadImages() li.module_num = 2 pipeline.add_module(li) li.match_method.value = LI.MS_EXACT_MATCH li.location.dir_choice = cps.ABSOLUTE_FOLDER_NAME li.location.custom_path = path li.images[0].common_text.value = "Channel2-" m = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, lsi, m, cpo.ObjectSet(), m, cpi.ImageSetList()) self.assertTrue(pipeline.prepare_run(workspace)) self.assertGreater(m.image_set_count, 1) pipeline.prepare_group(workspace, {}, m.get_image_numbers()) # # Skip to the second image set # m.next_image_set(2) lsi.run(workspace) # # Are the measurements populated? # m_file = "_".join((cpmeas.C_FILE_NAME, self.get_image_name(0))) self.assertEqual(m[cpmeas.IMAGE, m_file,2], filename) # # Can we retrieve the image? # pixel_data = m.get_image(self.get_image_name(0)).pixel_data self.assertEqual(tuple(pixel_data.shape), (640, 640))
def test_05_01_align_self(self): '''Align an image from the fly screen against itself. This is a regression test for the bug, IMG-284 ''' fly_file = '01_POS002_D.TIF' fly_dir = "ExampleFlyImages" path = os.path.join(example_images_directory(), fly_dir, fly_file) image = load_using_PIL(path) image = image[0:300,0:300] # make smaller so as to be faster workspace, module = self.make_workspace((image, image),(None,None)) module.alignment_method.value = A.M_MUTUAL_INFORMATION module.crop_mode.value = A.C_PAD module.run(workspace) m = workspace.measurements self.assertEqual(m.get_current_image_measurement('Align_Xshift_Aligned1'), 0) self.assertEqual(m.get_current_image_measurement('Align_Yshift_Aligned1'), 0)