def run(self, workspace): '''Run on an image set''' image_name = self.image_name.value m = workspace.measurements # # Get the path and file names from the measurements # path = m.get_current_image_measurement('PathName_%s' % image_name) file_name = m.get_current_image_measurement('FileName_%s' % image_name) # # Pull out the prefix, middle and suffix from the file name # prefix = file_name[:self.number_characters_prefix.value] if self.number_characters_suffix.value == 0: suffix = "" middle = file_name[self.number_characters_prefix.value:] else: suffix = file_name[-self.number_characters_suffix.value:] middle = file_name[self.number_characters_prefix.value: -self.number_characters_suffix.value] # # Possibly apply the renumbering rule # if self.action == A_RENUMBER: if not middle.isdigit(): raise ValueError( ('The middle of the filename, "%s", is "%s".\n' "It has non-numeric characters and can't be " "converted to a number") % ( file_name, middle )) format = '%0'+str(self.number_digits.value)+'d' middle = format % int(middle) elif self.action == A_DELETE: middle = "" else: raise NotImplementedError("Unknown action: %s" % self.action.value) # # Possibly apply the added text # if self.wants_text: middle += self.text_to_add.value new_file_name = prefix + middle + suffix if self.wants_to_replace_spaces: new_file_name = new_file_name.replace( " ", self.space_replacement.value) if self.show_window: workspace.display_data.old_file_name = file_name workspace.display_data.new_file_name = new_file_name if workspace.pipeline.test_mode: return # # Perform the actual renaming # # Most likely, we have a handle on the file open in BioFormats from bioformats.formatreader import clear_image_reader_cache clear_image_reader_cache() os.rename(os.path.join(path, file_name), os.path.join(path, new_file_name))
def exit_thread(self): from bioformats.formatreader import clear_image_reader_cache self.notify_socket.close() clear_image_reader_cache() J.detach() if self.with_stop_run_loop: stop_run_loop()
def OnClose(self, event): if event.CanVeto() and not self.pipeline_controller.check_close(): event.Veto() return try: self.__workspace.measurements.flush() except: logging.warning( "Failed to flush temporary measurements file during close", exc_info=True, ) try: from bioformats.formatreader import clear_image_reader_cache clear_image_reader_cache() except: logging.warning( "Failed to clear bioformats reader cache during close", exc_info=True, ) try: self.__preferences_view.close() except: logging.warning("Failed during close", exc_info=True) try: self.pipeline_controller.on_close() except: logging.warning("Failed to close the pipeline controller", exc_info=True) try: stop_validation_queue_thread() except: logging.warning("Failed to stop pipeline validation thread", exc_info=True) wx.GetApp().ExitMainLoop()
def run(self, workspace): '''Run on an image set''' image_name = self.image_name.value m = workspace.measurements # # Get the path and file names from the measurements # path = m.get_current_image_measurement('PathName_%s' % image_name) file_name = m.get_current_image_measurement('FileName_%s' % image_name) # # Pull out the prefix, middle and suffix from the file name # prefix = file_name[:self.number_characters_prefix.value] if self.number_characters_suffix.value == 0: suffix = "" middle = file_name[self.number_characters_prefix.value:] else: suffix = file_name[-self.number_characters_suffix.value:] middle = file_name[self.number_characters_prefix. value:-self.number_characters_suffix.value] # # Possibly apply the renumbering rule # if self.action == A_RENUMBER: if not middle.isdigit(): raise ValueError( ('The middle of the filename, "%s", is "%s".\n' "It has non-numeric characters and can't be " "converted to a number") % (file_name, middle)) format = '%0' + str(self.number_digits.value) + 'd' middle = format % int(middle) elif self.action == A_DELETE: middle = "" else: raise NotImplementedError("Unknown action: %s" % self.action.value) # # Possibly apply the added text # if self.wants_text: middle += self.text_to_add.value new_file_name = prefix + middle + suffix if self.wants_to_replace_spaces: new_file_name = new_file_name.replace(" ", self.space_replacement.value) if self.show_window: workspace.display_data.old_file_name = file_name workspace.display_data.new_file_name = new_file_name if workspace.pipeline.test_mode: return # # Perform the actual renaming # # Most likely, we have a handle on the file open in BioFormats from bioformats.formatreader import clear_image_reader_cache clear_image_reader_cache() os.rename(os.path.join(path, file_name), os.path.join(path, new_file_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 exit_thread(self): from bioformats.formatreader import clear_image_reader_cache self.notify_socket.close() clear_image_reader_cache() J.detach()