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 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_02_01_load_one(self): folder = "ExampleSpecklesImages" file_name = "1-162hrh2ax2.tif" path = os.path.dirname( maybe_download_example_image([folder], file_name)) cpprefs.set_default_image_directory(path) 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 test_10_01_scaling(self): '''Test loading an image scaled and unscaled''' folder = "ExampleSpecklesImages" file_name = "1-162hrh2ax2.tif" path = maybe_download_example_image([folder], file_name) csv_text = ("Image_PathName_MyFile,Image_FileName_MyFile\n" "%s,%s\n" % os.path.split(path)) 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 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_02_01_load_one(self): folder = "ExampleSpecklesImages" file_name = "1-162hrh2ax2.tif" path = os.path.dirname( maybe_download_example_image([folder], file_name)) cpprefs.set_default_image_directory(path) 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 test_01_01_get_image(self): from cellprofiler.modules.tests import maybe_download_example_image folder = "ExampleCometAssay" fn = "CometTails.tif" file_name = maybe_download_example_image([folder], fn) imageplus_obj = I.load_imageplus(file_name) pixels = IP.get_image(imageplus_obj.getProcessor()) pass
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_01_compare_to_matlab(self): path = os.path.split(__file__)[0] png_file = 'Channel2-01-A-01Mask.png' mat_file = 'texturemeasurements.mat' mask_file = os.path.join(path, png_file) if not os.path.isfile(mask_file): github_fmt = github_url + "/cellprofiler/modules/tests/%s" mask = load_using_bioformats_url(github_fmt % png_file) != 0 fin = urllib2.urlopen(github_fmt % mat_file) fd, tempmat = tempfile.mkstemp(".mat") fout = os.fdopen(fd, "wb") shutil.copyfileobj(fin, fout) fout.close() texture_measurements = loadmat(tempmat, struct_as_record=True) os.remove(tempmat) else: mask = load_image(mask_file) != 0 texture_measurements = loadmat( os.path.join(path, mat_file), struct_as_record=True) texture_measurements = texture_measurements['m'][0,0] folder = 'ExampleSBSImages' file_name = 'Channel1-01-A-01.tif' image_file = os.path.join( example_images_directory(), folder, file_name) maybe_download_example_image([folder], file_name) image = load_image(image_file, rescale=False).astype(float)/255.0 labels,count = scind.label(mask.astype(bool),np.ones((3,3),bool)) centers = scind.center_of_mass(np.ones(labels.shape), labels, np.arange(count)+1) centers = np.array(centers) X = 1 # the index of the X coordinate Y = 0 # the index of the Y coordinate order_python = np.lexsort((centers[:,X],centers[:,Y])) workspace, module = self.make_workspace(image, labels, convert = False) module.scale_groups[0].scale.value = 3 my_angle = M.H_HORIZONTAL module.scale_groups[0].angles.value = my_angle module.run(workspace) m = workspace.measurements tm_center_x = texture_measurements['Location_Center_X'][0,0][:,0] tm_center_y = texture_measurements['Location_Center_Y'][0,0][:,0] order_matlab = np.lexsort((tm_center_x,tm_center_y)) for measurement in M.F_HARALICK: mname = '%s_%s_%s_%d'%(M.TEXTURE, measurement, INPUT_IMAGE_NAME, 3) pymname = mname + "_" + M.H_TO_A[my_angle] pytm = m.get_current_measurement(INPUT_OBJECTS_NAME, pymname) tm = texture_measurements[mname][0,0][:,] error_count = 0 for i in range(count): matlab_val = tm[order_matlab[i]] python_val = pytm[order_python[i]] self.assertAlmostEqual(tm[order_matlab[i]], pytm[order_python[i]],7, "Measurement = %s, Loc=(%.2f,%.2f), Matlab=%f, Python=%f"% (mname, tm_center_x[order_matlab[i]], tm_center_y[order_matlab[i]], tm[order_matlab[i]], pytm[order_python[i]])) image_measurements =\ (('Texture_AngularSecondMoment_Cytoplasm_3', 0.5412), ('Texture_Contrast_Cytoplasm_3',0.1505), ('Texture_Correlation_Cytoplasm_3', 0.7740), ('Texture_Variance_Cytoplasm_3', 0.3330), ('Texture_InverseDifferenceMoment_Cytoplasm_3',0.9321), ('Texture_SumAverage_Cytoplasm_3',2.5684), ('Texture_SumVariance_Cytoplasm_3',1.1814), ('Texture_SumEntropy_Cytoplasm_3',0.9540), ('Texture_Entropy_Cytoplasm_3',1.0677), ('Texture_DifferenceVariance_Cytoplasm_3',0.1314), ('Texture_DifferenceEntropy_Cytoplasm_3',0.4147), ('Texture_InfoMeas1_Cytoplasm_3',-0.4236), ('Texture_InfoMeas2_Cytoplasm_3',0.6609)) for feature_name, value in image_measurements: py_feature_name = feature_name + "_" + M.H_TO_A[my_angle] pytm = m.get_current_image_measurement(py_feature_name) self.assertAlmostEqual(pytm, value,3, "%s failed. Python=%f, Matlab=%f" % (feature_name, pytm, value))
def test_02_01_compare_to_matlab(self): path = os.path.split(__file__)[0] png_file = 'Channel2-01-A-01Mask.png' mat_file = 'texturemeasurements.mat' mask_file = os.path.join(path, png_file) if not os.path.isfile(mask_file): github_fmt = github_url + "/cellprofiler/modules/tests/%s" mask = load_using_bioformats_url(github_fmt % png_file) != 0 fin = urllib2.urlopen(github_fmt % mat_file) fd, tempmat = tempfile.mkstemp(".mat") fout = os.fdopen(fd, "wb") shutil.copyfileobj(fin, fout) fout.close() texture_measurements = loadmat(tempmat, struct_as_record=True) os.remove(tempmat) else: mask = load_image(mask_file) != 0 texture_measurements = loadmat(os.path.join(path, mat_file), struct_as_record=True) texture_measurements = texture_measurements['m'][0, 0] folder = 'ExampleSBSImages' file_name = 'Channel1-01-A-01.tif' image_file = os.path.join(example_images_directory(), folder, file_name) maybe_download_example_image([folder], file_name) image = load_image(image_file, rescale=False).astype(float) / 255.0 labels, count = scind.label(mask.astype(bool), np.ones((3, 3), bool)) centers = scind.center_of_mass(np.ones(labels.shape), labels, np.arange(count) + 1) centers = np.array(centers) X = 1 # the index of the X coordinate Y = 0 # the index of the Y coordinate order_python = np.lexsort((centers[:, X], centers[:, Y])) workspace, module = self.make_workspace(image, labels, convert=False) module.scale_groups[0].scale.value = 3 my_angle = M.H_HORIZONTAL module.scale_groups[0].angles.value = my_angle module.run(workspace) m = workspace.measurements tm_center_x = texture_measurements['Location_Center_X'][0, 0][:, 0] tm_center_y = texture_measurements['Location_Center_Y'][0, 0][:, 0] order_matlab = np.lexsort((tm_center_x, tm_center_y)) for measurement in M.F_HARALICK: mname = '%s_%s_%s_%d' % (M.TEXTURE, measurement, INPUT_IMAGE_NAME, 3) pymname = mname + "_" + M.H_TO_A[my_angle] pytm = m.get_current_measurement(INPUT_OBJECTS_NAME, pymname) tm = texture_measurements[mname][0, 0][:, ] error_count = 0 for i in range(count): matlab_val = tm[order_matlab[i]] python_val = pytm[order_python[i]] self.assertAlmostEqual( tm[order_matlab[i]], pytm[order_python[i]], 7, "Measurement = %s, Loc=(%.2f,%.2f), Matlab=%f, Python=%f" % (mname, tm_center_x[order_matlab[i]], tm_center_y[order_matlab[i]], tm[order_matlab[i]], pytm[order_python[i]])) image_measurements =\ (('Texture_AngularSecondMoment_Cytoplasm_3', 0.5412), ('Texture_Contrast_Cytoplasm_3',0.1505), ('Texture_Correlation_Cytoplasm_3', 0.7740), ('Texture_Variance_Cytoplasm_3', 0.3330), ('Texture_InverseDifferenceMoment_Cytoplasm_3',0.9321), ('Texture_SumAverage_Cytoplasm_3',2.5684), ('Texture_SumVariance_Cytoplasm_3',1.1814), ('Texture_SumEntropy_Cytoplasm_3',0.9540), ('Texture_Entropy_Cytoplasm_3',1.0677), ('Texture_DifferenceVariance_Cytoplasm_3',0.1314), ('Texture_DifferenceEntropy_Cytoplasm_3',0.4147), ('Texture_InfoMeas1_Cytoplasm_3',-0.4236), ('Texture_InfoMeas2_Cytoplasm_3',0.6609)) for feature_name, value in image_measurements: py_feature_name = feature_name + "_" + M.H_TO_A[my_angle] pytm = m.get_current_image_measurement(py_feature_name) self.assertAlmostEqual( pytm, value, 3, "%s failed. Python=%f, Matlab=%f" % (feature_name, pytm, value))