def make_workspace(self, images): """Make a workspace """ module = S.StackImages() pipeline = cpp.Pipeline() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cpw.Workspace( pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list, ) # setup the input images names = [INPUT_IMAGE_BASENAME + str(i) for i, img in enumerate(images)] for img, nam in zip(images, names): image_set.add(nam, cpi.Image(img)) # setup the input images settings module.stack_image_name.value = OUTPUT_IMAGE_NAME nimgs = len(images) while len(module.stack_channels) < nimgs: module.add_stack_channel_cb() for sc, imname in zip(module.stack_channels, names): sc.image_name.value = imname return workspace, module
def test_02_00_no_measurements(self): m = cpmeas.Measurements() m.add_measurement(OBJECT_NAME, M_FEATURES[0], np.array([], float)) rules = R.Rules() rules.rules += [ R.Rules.Rule( OBJECT_NAME, M_FEATURES[0], ">", 0, np.array([[1.0, -1.0], [-1.0, 1.0]]) ) ] score = rules.score(m) self.assertEqual(score.shape[0], 0) self.assertEqual(score.shape[1], 2)
def save_pipeline(self, workspace, outf=None): """Save the pipeline in Batch_data.mat Save the pickled image_set_list state in a setting and put this module in batch mode. if outf is not None, it is used as a file object destination. """ if outf is None: if self.wants_default_output_directory.value: path = cpprefs.get_default_output_directory() else: path = cpprefs.get_absolute_path( self.custom_output_directory.value) h5_path = os.path.join(path, F_BATCH_DATA_H5) else: h5_path = outf image_set_list = workspace.image_set_list pipeline = workspace.pipeline m = cpmeas.Measurements(copy=workspace.measurements, filename=h5_path) try: assert isinstance(pipeline, cpp.Pipeline) assert isinstance(m, cpmeas.Measurements) orig_pipeline = pipeline pipeline = pipeline.copy() # this use of workspace.frame is okay, since we're called from # prepare_run which happens in the main wx thread. target_workspace = cpw.Workspace(pipeline, None, None, None, m, image_set_list, workspace.frame) pipeline.prepare_to_create_batch(target_workspace, self.alter_path) bizarro_self = pipeline.module(self.module_num) bizarro_self.revision.value = int( re.sub(r"\.|rc\d{1}", "", cellprofiler.__version__)) if self.wants_default_output_directory: bizarro_self.custom_output_directory.value = self.alter_path( cpprefs.get_default_output_directory()) bizarro_self.default_image_directory.value = self.alter_path( cpprefs.get_default_image_directory()) bizarro_self.batch_mode.value = True pipeline.write_pipeline_measurement(m) orig_pipeline.write_pipeline_measurement(m, user_pipeline=True) # # Write the path mappings to the batch measurements # m.write_path_mappings([(mapping.local_directory.value, mapping.remote_directory.value) for mapping in self.mappings]) return h5_path finally: m.close()
def make_workspace(image, mask): """Make a workspace for testing FilterByObjectMeasurement""" module = S.SmoothMultichannel() pipeline = cpp.Pipeline() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) image_set.add(INPUT_IMAGE_NAME, cpi.Image(image, mask, scale=1)) module.image_name.value = INPUT_IMAGE_NAME module.filtered_image_name.value = OUTPUT_IMAGE_NAME return workspace, module
def test_02_01_score_one_positive(self): m = cpmeas.Measurements() m.add_measurement(OBJECT_NAME, M_FEATURES[0], np.array([1.5], float)) rules = R.Rules() rules.rules += [ R.Rules.Rule( OBJECT_NAME, M_FEATURES[0], ">", 0, np.array([[1.0, -0.5], [-2.0, 0.6]]) ) ] score = rules.score(m) self.assertEqual(score.shape[0], 1) self.assertEqual(score.shape[1], 2) self.assertAlmostEqual(score[0, 0], 1.0) self.assertAlmostEqual(score[0, 1], -0.5)
def test_03_01_score_two_rules(self): m = cpmeas.Measurements() m.add_measurement(OBJECT_NAME, M_FEATURES[0], np.array([1.5], float)) m.add_measurement(OBJECT_NAME, M_FEATURES[1], np.array([-1.5], float)) rules = R.Rules() rules.rules += [ R.Rules.Rule( OBJECT_NAME, M_FEATURES[0], ">", 0, np.array([[1.0, -0.5], [-2.0, 0.6]]) ), R.Rules.Rule( OBJECT_NAME, M_FEATURES[1], ">", 0, np.array([[1.5, -0.7], [-2.3, 0.9]]) ), ] score = rules.score(m) self.assertEqual(score.shape[0], 1) self.assertEqual(score.shape[1], 2) self.assertAlmostEqual(score[0, 0], 1.0 - 2.3) self.assertAlmostEqual(score[0, 1], -0.5 + 0.9)
def test_02_03_score_one_nan(self): m = cpmeas.Measurements() m.add_measurement(OBJECT_NAME, M_FEATURES[0], np.array([np.NaN], float)) rules = R.Rules() rules.rules += [ R.Rules.Rule( OBJECT_NAME, M_FEATURES[0], ">", 2.0, np.array([[1.0, -0.5], [-2.0, 0.6]]), ) ] score = rules.score(m) self.assertEqual(score.shape[0], 1) self.assertEqual(score.shape[1], 2) self.assertTrue(score[0, 0], -2) self.assertTrue(score[0, 1], 0.6)
def make_workspace(image, outlier_percentile): """Make a workspace """ module = C.ClipRange() pipeline = cpp.Pipeline() object_set = cpo.ObjectSet() image_set_list = cpi.ImageSetList() image_set = image_set_list.get_image_set(0) workspace = cpw.Workspace(pipeline, module, image_set, object_set, cpmeas.Measurements(), image_set_list) # setup the input images image_set.add(INPUT_IMAGE_NAME, cpi.Image(image)) # setup the input images settings module.x_name.value = INPUT_IMAGE_NAME module.y_name.value = OUTPUT_IMAGE_NAME module.outlier_percentile.value = outlier_percentile return workspace, module
def test_03_02_score_two_objects(self): m = cpmeas.Measurements() m.add_measurement(OBJECT_NAME, M_FEATURES[0], np.array([1.5, 2.5], float)) rules = R.Rules() rules.rules += [ R.Rules.Rule( OBJECT_NAME, M_FEATURES[0], "<", 2.0, np.array([[1.0, -0.5], [-2.0, 0.6]]), ) ] score = rules.score(m) self.assertEqual(score.shape[0], 2) self.assertEqual(score.shape[1], 2) self.assertAlmostEqual(score[0, 0], 1.0) self.assertAlmostEqual(score[0, 1], -0.5) self.assertAlmostEqual(score[1, 0], -2.0) self.assertAlmostEqual(score[1, 1], 0.6)
def merge_files(destination, sources, force_headless=False): is_headless = force_headless or get_headless() if not is_headless: import wx if len(sources) == 0: return if not is_headless: progress = wx.ProgressDialog( "Writing " + destination, "Loading " + sources[0], maximum=len(sources) * 4 + 1, style=wx.PD_CAN_ABORT | wx.PD_APP_MODAL | wx.PD_ELAPSED_TIME | wx.PD_REMAINING_TIME, ) count = 0 try: pipeline = cpp.Pipeline() has_error = [False] def callback(caller, event): if isinstance(event, cpp.event.LoadException): has_error = True wx.MessageBox( message="Could not load %s: %s" % (sources[0], event.error), caption="Failed to load %s" % sources[0], ) has_error[0] = True pipeline.add_listener(callback) pipeline.load(sources[0]) if has_error[0]: return if destination.lower().endswith(".h5"): mdest = cpmeas.Measurements(filename=destination, multithread=False) h5_dest = True else: mdest = cpmeas.Measurements(multithread=False) h5_dest = False for source in sources: if not is_headless: count += 1 keep_going, skip = progress.Update(count, "Loading " + source) if not keep_going: return if h5py.is_hdf5(source): msource = cpmeas.Measurements(filename=source, mode="r", multithread=False) else: msource = cpmeas.load_measurements(source) dest_image_numbers = mdest.get_image_numbers() source_image_numbers = msource.get_image_numbers() if len(dest_image_numbers) == 0 or len( source_image_numbers) == 0: offset_source_image_numbers = source_image_numbers else: offset_source_image_numbers = ( np.max(dest_image_numbers) - np.min(source_image_numbers) + source_image_numbers + 1) for object_name in msource.get_object_names(): if object_name in mdest.get_object_names(): destfeatures = mdest.get_feature_names(object_name) else: destfeatures = [] for feature in msource.get_feature_names(object_name): if object_name == cpmeas.EXPERIMENT: if not mdest.has_feature(object_name, feature): src_value = msource.get_experiment_measurement( feature) mdest.add_experiment_measurement( feature, src_value) continue src_values = msource.get_measurement( object_name, feature, image_set_number=source_image_numbers) mdest[object_name, feature, offset_source_image_numbers] = src_values destset = set(destfeatures) if not is_headless: keep_going, skip = progress.Update(count + 1, "Saving to " + destination) if not keep_going: return if not h5_dest: pipeline.save_measurements(destination, mdest) finally: if not is_headless: progress.Destroy()