def _generate_random_labels(self, labels_per_image, label_value): """ Inject random labels into the project file. (This is a special feature requested by the FlyEM proofreaders.) """ logger.info("Injecting {} labels of value {} into all images.".format( labels_per_image, label_value)) opTopLevelClassify = self.pcApplet.topLevelOperator label_names = copy.copy(opTopLevelClassify.LabelNames.value) while len(label_names) < label_value: label_names.append("Label {}".format(len(label_names) + 1)) opTopLevelClassify.LabelNames.setValue(label_names) for image_index in range(len(opTopLevelClassify.LabelImages)): logger.info("Injecting labels into image #{}".format(image_index)) # For reproducibility of label generation SEED = 1 numpy.random.seed([SEED, image_index]) label_input_slot = opTopLevelClassify.LabelInputs[image_index] label_output_slot = opTopLevelClassify.LabelImages[image_index] shape = label_output_slot.meta.shape random_labels = numpy.zeros(shape=shape, dtype=numpy.uint8) num_pixels = len(random_labels.flat) current_progress = -1 for sample_index in range(labels_per_image): flat_index = numpy.random.randint(0, num_pixels) # Don't overwrite existing labels # Keep looking until we find a blank pixel while random_labels.flat[flat_index]: flat_index = numpy.random.randint(0, num_pixels) random_labels.flat[flat_index] = label_value # Print progress every 10% progress = float(sample_index) // labels_per_image progress = 10 * (int(100 * progress) // 10) if progress != current_progress: current_progress = progress sys.stdout.write("{}% ".format(current_progress)) sys.stdout.flush() sys.stdout.write("100%\n") # Write into the operator label_input_slot[fullSlicing(shape)] = random_labels logger.info("Done injecting labels")
def _generate_random_labels(self, labels_per_image, label_value): """ Inject random labels into the project file. (This is a special feature requested by the FlyEM proofreaders.) """ logger.info( "Injecting {} labels of value {} into all images.".format( labels_per_image, label_value ) ) opTopLevelClassify = self.pcApplet.topLevelOperator label_names = copy.copy(opTopLevelClassify.LabelNames.value) while len(label_names) < label_value: label_names.append( "Label {}".format( len(label_names)+1 ) ) opTopLevelClassify.LabelNames.setValue( label_names ) for image_index in range(len(opTopLevelClassify.LabelImages)): logger.info( "Injecting labels into image #{}".format( image_index ) ) # For reproducibility of label generation SEED = 1 numpy.random.seed([SEED, image_index]) label_input_slot = opTopLevelClassify.LabelInputs[image_index] label_output_slot = opTopLevelClassify.LabelImages[image_index] shape = label_output_slot.meta.shape random_labels = numpy.zeros( shape=shape, dtype=numpy.uint8 ) num_pixels = len(random_labels.flat) current_progress = -1 for sample_index in range(labels_per_image): flat_index = numpy.random.randint(0,num_pixels) # Don't overwrite existing labels # Keep looking until we find a blank pixel while random_labels.flat[flat_index]: flat_index = numpy.random.randint(0,num_pixels) random_labels.flat[flat_index] = label_value # Print progress every 10% progress = float(sample_index) / labels_per_image progress = 10 * (int(100*progress)/10) if progress != current_progress: current_progress = progress sys.stdout.write( "{}% ".format( current_progress ) ) sys.stdout.flush() sys.stdout.write( "100%\n" ) # Write into the operator label_input_slot[fullSlicing(shape)] = random_labels logger.info( "Done injecting labels" )
def impl(): shell = self.shell workflow = shell.projectManager.workflow carvingApplet = workflow.carvingApplet gui = carvingApplet.getMultiLaneGui() op_carving = carvingApplet.topLevelOperator.getLane(0) # activate the carving applet shell.setSelectedAppletDrawer(2) # let the gui catch up QApplication.processEvents() self.waitForViews(gui.currentGui().editor.imageViews) # inject the labels op5 = OpReorderAxes(parent=op_carving.parent) opReader = OpInputDataReader(parent=op_carving.parent) try: opReader.FilePath.setValue(f"{self.reference_files['carving_label_file']}/exported_data") op5.AxisOrder.setValue(op_carving.WriteSeeds.meta.getAxisKeys()) op5.Input.connect(opReader.Output) label_data = op5.Output[:].wait() finally: op5.cleanUp() opReader.cleanUp() slicing = roi.fullSlicing(label_data.shape) op_carving.WriteSeeds[slicing] = label_data gui.currentGui().labelingDrawerUi.segment.click() QApplication.processEvents() op_carving.saveObjectAs("Object 1") op_carving.deleteObject("<not saved yet>") # export the mesh: req = gui.currentGui()._exportMeshes(["Object 1"], [self.output_obj_file]) req.wait() # compare meshes with open(self.output_obj_file, "r") as f: left = f.read() with open(self.reference_files["output_obj_file"], "r") as f: right = f.read() # TODO: might result in errors due to rounding on different systems assert left == right # export the completed segments layer layermatch = [ x.name.startswith("Completed segments (unicolor)") for x in gui.currentGui().editor.layerStack ] assert sum(layermatch) == 1, "Completed segments (unicolor) Layer expected." completed_segments_layer = gui.currentGui().editor.layerStack[layermatch.index(True)] opExport = get_export_operator(completed_segments_layer) try: opExport.OutputFilenameFormat.setValue(self.output_file) opExport.run_export() finally: opExport.cleanUp() assert os.path.exists(self.output_file) # compare completed segments with h5py.File(self.reference_files["output_file"], "r") as f_left: data_left = f_left["exported_data"][:] with h5py.File(self.output_file, "r") as f_right: data_right = f_right["exported_data"][:] numpy.testing.assert_array_almost_equal(data_left, data_right) # Save the project saveThread = self.shell.onSaveProjectActionTriggered() saveThread.join()