def maybe_download_example_image(folders, file_name, shape=None): '''Download the given ExampleImages file if not in the directory folders - sequence of subfolders starting at ExampleImages file_name - name of file to fetch Image will be downloaded if not present to CP_EXAMPLEIMAGES directory. Returns the local path to the file which is often useful. ''' if shape is None: shape = (20, 30) local_path = os.path.join(*tuple([ example_images_directory()] + folders + [file_name])) if not os.path.exists(local_path): directory = os.path.join(*tuple([ example_images_directory()] + folders)) if not os.path.isdir(directory): os.makedirs(directory) r = np.random.RandomState() r.seed(np.frombuffer( hashlib.sha1("/".join(folders) + file_name).digest(), np.uint8)) img = (r.uniform(size=shape) * 255).astype(np.uint8) write_image(local_path, img, PT_UINT8) return local_path
def maybe_download_example_image(folders, file_name, shape=None): '''Download the given ExampleImages file if not in the directory folders - sequence of subfolders starting at ExampleImages file_name - name of file to fetch Image will be downloaded if not present to CP_EXAMPLEIMAGES directory. Returns the local path to the file which is often useful. ''' if shape is None: shape = (20, 30) local_path = os.path.join(*tuple([example_images_directory()] + folders + [file_name])) if not os.path.exists(local_path): directory = os.path.join(*tuple([example_images_directory()] + folders)) if not os.path.isdir(directory): os.makedirs(directory) r = np.random.RandomState() r.seed( np.frombuffer( hashlib.sha1("/".join(folders) + file_name).digest(), np.uint8)) img = (r.uniform(size=shape) * 255).astype(np.uint8) write_image(local_path, img, PT_UINT8) return local_path
def test_01_01_write_monochrome_8_bit_tif(self): r = np.random.RandomState() r.seed(101) img = r.randint(0, 256, (11, 33)).astype(np.uint8) path = self.get_tempfilename(".tif") W.write_image(path, img, OME.PT_UINT8) result = load_using_bioformats(path, rescale=False) np.testing.assert_array_equal(img, result)
def test_01_03_write_color_tiff(self): r = np.random.RandomState() r.seed(103) img = r.randint(0, 256, (9, 11, 3)) path = self.get_tempfilename(".tif") W.write_image(path, img, OME.PT_UINT8) result = load_using_bioformats(path, rescale = False) np.testing.assert_array_equal(img, result)
def test_01_03_write_color_tiff(self): r = np.random.RandomState() r.seed(103) img = r.randint(0, 256, (9, 11, 3)) path = self.get_tempfilename(".tif") W.write_image(path, img, OME.PT_UINT8) result = load_using_bioformats(path, rescale=False) np.testing.assert_array_equal(img, result)
def test_01_02_write_monchrome_16_bit__tiff(self): r = np.random.RandomState() r.seed(102) img = r.randint(0, 4096, size=(21, 24)) path = self.get_tempfilename(".tif") W.write_image(path, img, OME.PT_UINT16) result = load_using_bioformats(path, rescale=False) np.testing.assert_array_equal(img, result)
def test_02_01_write_movie(self): r = np.random.RandomState() r.seed(103) img = r.randint(0, 256, (7, 23, 11)) path = self.get_tempfilename(".tif") for i in range(img.shape[0]): W.write_image(path, img[i], OME.PT_UINT8, t=i, size_t = img.shape[0]) for i in range(img.shape[0]): result = load_using_bioformats(path, t=i, rescale = False) np.testing.assert_array_equal(img[i], result)
def test_02_01_write_movie(self): r = np.random.RandomState() r.seed(103) img = r.randint(0, 256, (7, 23, 11)) path = self.get_tempfilename(".tif") for i in range(img.shape[0]): W.write_image(path, img[i], OME.PT_UINT8, t=i, size_t=img.shape[0]) for i in range(img.shape[0]): result = load_using_bioformats(path, t=i, rescale=False) np.testing.assert_array_equal(img[i], result)
def do_save_image(self, workspace, filename, pixels, pixel_type, c=0, z=0, t=0, size_c=1, size_z=1, size_t=1, channel_names=None): '''Save image using bioformats workspace - the current workspace filename - save to this filename pixels - the image to save pixel_type - save using this pixel type c - the image's channel index z - the image's z index t - the image's t index sizeC - # of channels in the stack sizeZ - # of z stacks sizeT - # of timepoints in the stack channel_names - names of the channels (make up names if not present ''' write_image(filename, pixels, pixel_type, c=c, z=z, t=t, size_c=size_c, size_z=size_z, size_t=size_t, channel_names=channel_names)
def maybe_download_example_image(folders, file_name, shape=None): """Download the given ExampleImages file if not in the directory folders - sequence of subfolders starting at ExampleImages file_name - name of file to fetch Image will be downloaded if not present to CP_EXAMPLEIMAGES directory. Returns the local path to the file which is often useful. """ if shape is None: shape = (20, 30) local_path = os.path.join(*tuple([example_images_directory()] + folders + [file_name])) if not os.path.exists(local_path): directory = os.path.join(*tuple([example_images_directory()] + folders)) if not os.path.isdir(directory): os.makedirs(directory) random_state = numpy.random.RandomState() random_state.seed() image = (random_state.uniform(size=shape) * 255).astype(numpy.uint8) write_image(local_path, image, "uint8") return local_path
def make_12_bit_image(folder, filename, shape): """Create a 12-bit image of the desired shape folder - subfolder of example images directory filename - filename for image file shape - 2-tuple or 3-tuple of the dimensions of the image. The axis order is i, j, c or y, x, c """ r = np.random.RandomState() r.seed(np.frombuffer(hashlib.sha1("/".join([folder, filename])).digest(), np.uint8)) img = (r.uniform(size=shape) * 4095).astype(np.uint16) path = os.path.join(example_images_directory(), folder, filename) if not os.path.isdir(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) write_image(path, img, PT_UINT16) # # Now go through the file and find the TIF bits per sample IFD (#258) and # change it from 16 to 12. # with open(path, "rb") as fd: data = np.frombuffer(fd.read(), np.uint8).copy() offset = np.frombuffer(data[4:8].data, np.uint32)[0] nentries = np.frombuffer(data[offset : offset + 2], np.uint16)[0] ifds = [] # Get the IFDs we don't modify for idx in range(nentries): ifd = data[offset + 2 + idx * 12 : offset + 14 + idx * 12] code = ifd[0] + 256 * ifd[1] if code not in (258, 281): ifds.append(ifd) ifds += [ # 12 bits/sample np.array([2, 1, 3, 0, 1, 0, 0, 0, 12, 0, 0, 0], np.uint8), # max value = 4095 np.array([25, 1, 3, 0, 1, 0, 0, 0, 255, 15, 0, 0], np.uint8), ] ifds = sorted(ifds, cmp=(lambda a, b: cmp(a.tolist(), b.tolist()))) old_end = offset + 2 + nentries * 12 new_end = offset + 2 + len(ifds) * 12 diff = new_end - old_end # # Fix up the IFD offsets if greater than "offset" # for ifd in ifds: count = np.frombuffer(ifd[4:8].data, np.uint32)[0] if count > 4: ifd_off = np.array([np.frombuffer(ifd[8:12].data, np.uint32)[0]]) + diff if ifd_off > offset: ifd[8:12] = np.frombuffer(ifd_off.data, np.uint8) new_data = np.zeros(len(data) + diff, np.uint8) new_data[:offset] = data[:offset] new_data[offset] = len(ifds) % 256 new_data[offset + 1] = int(len(ifds) / 256) for idx, ifd in enumerate(ifds): new_data[offset + 2 + idx * 12 : offset + 14 + idx * 12] = ifd new_data[new_end:] = data[old_end:] with open(path, "wb") as fd: fd.write(new_data.data) return path
def make_12_bit_image(folder, filename, shape): '''Create a 12-bit image of the desired shape folder - subfolder of example images directory filename - filename for image file shape - 2-tuple or 3-tuple of the dimensions of the image. The axis order is i, j, c or y, x, c ''' r = np.random.RandomState() r.seed( np.frombuffer( hashlib.sha1("/".join([folder, filename])).digest(), np.uint8)) img = (r.uniform(size=shape) * 4095).astype(np.uint16) path = os.path.join(example_images_directory(), folder, filename) if not os.path.isdir(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) write_image(path, img, PT_UINT16) # # Now go through the file and find the TIF bits per sample IFD (#258) and # change it from 16 to 12. # with open(path, "rb") as fd: data = np.frombuffer(fd.read(), np.uint8).copy() offset = np.frombuffer(data[4:8].data, np.uint32)[0] nentries = np.frombuffer(data[offset:offset + 2], np.uint16)[0] ifds = [] # Get the IFDs we don't modify for idx in range(nentries): ifd = data[offset + 2 + idx * 12:offset + 14 + idx * 12] code = ifd[0] + 256 * ifd[1] if code not in (258, 281): ifds.append(ifd) ifds += [ # 12 bits/sample np.array([2, 1, 3, 0, 1, 0, 0, 0, 12, 0, 0, 0], np.uint8), # max value = 4095 np.array([25, 1, 3, 0, 1, 0, 0, 0, 255, 15, 0, 0], np.uint8) ] ifds = sorted(ifds, cmp=(lambda a, b: cmp(a.tolist(), b.tolist()))) old_end = offset + 2 + nentries * 12 new_end = offset + 2 + len(ifds) * 12 diff = new_end - old_end # # Fix up the IFD offsets if greater than "offset" # for ifd in ifds: count = np.frombuffer(ifd[4:8].data, np.uint32)[0] if count > 4: ifd_off = np.array([np.frombuffer(ifd[8:12].data, np.uint32)[0] ]) + diff if ifd_off > offset: ifd[8:12] = np.frombuffer(ifd_off.data, np.uint8) new_data = np.zeros(len(data) + diff, np.uint8) new_data[:offset] = data[:offset] new_data[offset] = len(ifds) % 256 new_data[offset + 1] = int(len(ifds) / 256) for idx, ifd in enumerate(ifds): new_data[offset + 2 + idx * 12:offset + 14 + idx * 12] = ifd new_data[new_end:] = data[old_end:] with open(path, "wb") as fd: fd.write(new_data.data) return path
def run_create_webpage(self, image_paths, thumb_paths = None, metadata = None, alter_fn = None): '''Run the create_webpage module, returning the resulting HTML document image_paths - list of path / filename tuples. The function will write an image to each of these and put images and measurements into the workspace for each. thumb_paths - if present a list of path / filename tuples. Same as above metadata - a dictionary of feature / string values alter_fn - function taking a CreateWebPage module, for you to alter the module's settings ''' np.random.seed(0) module = C.CreateWebPage() module.module_num = 1 module.orig_image_name.value = IMAGE_NAME module.web_page_file_name.value = DEFAULT_HTML_FILE if alter_fn is not None: alter_fn(module) pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) images = [ (IMAGE_NAME, image_paths)] if thumb_paths: images += [ (THUMB_NAME, thumb_paths)] self.assertEqual(len(image_paths), len(thumb_paths)) module.wants_thumbnails.value = True module.thumbnail_image_name.value = THUMB_NAME else: module.wants_thumbnails.value = False measurements = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, measurements, None, measurements, None, None) for i in range(len(image_paths)): image_number = i+1 if metadata is not None: for key in metadata.keys(): values = metadata[key] feature = cpmeas.C_METADATA + "_" + key measurements[cpmeas.IMAGE, feature, image_number] = values[i] for image_name, paths in images: pixel_data = np.random.uniform(size=(10,13)) path_name, file_name = paths[i] if path_name is None: path_name = cpprefs.get_default_image_directory() is_file = True elif path_name.lower().startswith("http"): is_file = False url = path_name + "/" + file_name if "?" in file_name: file_name = file_name.split("?", 1)[0] if is_file: full_path = os.path.abspath(os.path.join( self.directory, path_name, file_name)) url = pathname2url(full_path) path = os.path.split(full_path)[0] else: full_path = url path = path_name if is_file: write_image(full_path, pixel_data, PT_UINT8) path_feature = '_'.join((C_PATH_NAME, image_name)) file_feature = '_'.join((C_FILE_NAME, image_name)) url_feature = '_'.join((C_URL, image_name)) measurements[cpmeas.IMAGE, path_feature, image_number] = \ path measurements[cpmeas.IMAGE, file_feature, image_number] =\ file_name measurements[cpmeas.IMAGE, url_feature, image_number] = url module.post_run(workspace) return measurements
def run_as_data_tool(self): from cellprofiler.gui.editobjectsdlg import EditObjectsDialog import wx from wx.lib.filebrowsebutton import FileBrowseButton from cellprofiler.modules.namesandtypes import ObjectsImageProvider from bioformats import load_image with wx.Dialog(None) as dlg: dlg.Title = "Choose files for editing" dlg.Sizer = wx.BoxSizer(wx.VERTICAL) sub_sizer = wx.BoxSizer(wx.HORIZONTAL) dlg.Sizer.Add(sub_sizer, 0, wx.EXPAND | wx.ALL, 5) new_or_existing_rb = wx.RadioBox(dlg, style=wx.RA_VERTICAL, choices=("New", "Existing")) sub_sizer.Add(new_or_existing_rb, 0, wx.EXPAND) objects_file_fbb = FileBrowseButton( dlg, size=(300, -1), fileMask= "Objects file (*.tif, *.tiff, *.png, *.bmp, *.jpg)|*.tif;*.tiff;*.png;*.bmp;*.jpg", dialogTitle="Select objects file", labelText="Objects file:", ) objects_file_fbb.Enable(False) sub_sizer.AddSpacer(5) sub_sizer.Add(objects_file_fbb, 0, wx.ALIGN_TOP | wx.ALIGN_RIGHT) def on_radiobox(event): objects_file_fbb.Enable(new_or_existing_rb.GetSelection() == 1) new_or_existing_rb.Bind(wx.EVT_RADIOBOX, on_radiobox) image_file_fbb = FileBrowseButton( dlg, size=(300, -1), fileMask= "Objects file (*.tif, *.tiff, *.png, *.bmp, *.jpg)|*.tif;*.tiff;*.png;*.bmp;*.jpg", dialogTitle="Select guide image file", labelText="Guide image:", ) dlg.Sizer.Add(image_file_fbb, 0, wx.EXPAND | wx.ALL, 5) allow_overlap_checkbox = wx.CheckBox(dlg, -1, "Allow objects to overlap") allow_overlap_checkbox.Value = True dlg.Sizer.Add(allow_overlap_checkbox, 0, wx.EXPAND | wx.ALL, 5) buttons = wx.StdDialogButtonSizer() dlg.Sizer.Add(buttons, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.ALL, 5) buttons.Add(wx.Button(dlg, wx.ID_OK)) buttons.Add(wx.Button(dlg, wx.ID_CANCEL)) buttons.Realize() dlg.Fit() result = dlg.ShowModal() if result != wx.ID_OK: return self.allow_overlap.value = allow_overlap_checkbox.Value fullname = objects_file_fbb.GetValue() guidename = image_file_fbb.GetValue() if new_or_existing_rb.GetSelection() == 1: provider = ObjectsImageProvider("InputObjects", pathname2url(fullname), None, None) image = provider.provide_image(None) pixel_data = image.pixel_data shape = pixel_data.shape[:2] labels = [pixel_data[:, :, i] for i in range(pixel_data.shape[2])] else: labels = None # # Load the guide image # guide_image = load_image(guidename) if np.min(guide_image) != np.max(guide_image): guide_image = (guide_image - np.min(guide_image)) / ( np.max(guide_image) - np.min(guide_image)) if labels is None: shape = guide_image.shape[:2] labels = [np.zeros(shape, int)] with EditObjectsDialog(guide_image, labels, self.allow_overlap, self.object_name.value) as dialog_box: result = dialog_box.ShowModal() if result != wx.OK: return labels = dialog_box.labels n_frames = len(labels) with wx.FileDialog(None, style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as dlg: dlg.Path = fullname dlg.Wildcard = ("Object image file (*.tif,*.tiff)|*.tif;*.tiff|" "Ilastik project file (*.ilp)|*.ilp") result = dlg.ShowModal() fullname = dlg.Path if result == wx.ID_OK: if fullname.endswith(".ilp"): self.save_into_ilp(fullname, labels, guidename) else: from bioformats.formatwriter import write_image from bioformats.omexml import PT_UINT16 if os.path.exists(fullname): os.unlink(fullname) for i, l in enumerate(labels): write_image(fullname, l, PT_UINT16, t=i, size_t=len(labels))
def run_create_webpage(self, image_paths, thumb_paths=None, metadata=None, alter_fn=None): '''Run the create_webpage module, returning the resulting HTML document image_paths - list of path / filename tuples. The function will write an image to each of these and put images and measurements into the workspace for each. thumb_paths - if present a list of path / filename tuples. Same as above metadata - a dictionary of feature / string values alter_fn - function taking a CreateWebPage module, for you to alter the module's settings ''' np.random.seed(0) module = C.CreateWebPage() module.module_num = 1 module.orig_image_name.value = IMAGE_NAME module.web_page_file_name.value = DEFAULT_HTML_FILE if alter_fn is not None: alter_fn(module) pipeline = cpp.Pipeline() def callback(caller, event): self.assertFalse(isinstance(event, cpp.RunExceptionEvent)) pipeline.add_listener(callback) pipeline.add_module(module) images = [(IMAGE_NAME, image_paths)] if thumb_paths: images += [(THUMB_NAME, thumb_paths)] self.assertEqual(len(image_paths), len(thumb_paths)) module.wants_thumbnails.value = True module.thumbnail_image_name.value = THUMB_NAME else: module.wants_thumbnails.value = False measurements = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, measurements, None, measurements, None, None) for i in range(len(image_paths)): image_number = i + 1 if metadata is not None: for key in metadata.keys(): values = metadata[key] feature = cpmeas.C_METADATA + "_" + key measurements[cpmeas.IMAGE, feature, image_number] = values[i] for image_name, paths in images: pixel_data = np.random.uniform(size=(10, 13)) path_name, file_name = paths[i] if path_name is None: path_name = cpprefs.get_default_image_directory() is_file = True elif path_name.lower().startswith("http"): is_file = False url = path_name + "/" + file_name if "?" in file_name: file_name = file_name.split("?", 1)[0] if is_file: full_path = os.path.abspath(os.path.join( self.directory, path_name, file_name)) url = pathname2url(full_path) path = os.path.split(full_path)[0] else: full_path = url path = path_name if is_file: write_image(full_path, pixel_data, PT_UINT8) path_feature = '_'.join((C_PATH_NAME, image_name)) file_feature = '_'.join((C_FILE_NAME, image_name)) url_feature = '_'.join((C_URL, image_name)) measurements[cpmeas.IMAGE, path_feature, image_number] = \ path measurements[cpmeas.IMAGE, file_feature, image_number] = \ file_name measurements[cpmeas.IMAGE, url_feature, image_number] = url module.post_run(workspace) return measurements
def run_as_data_tool(self): from cellprofiler.gui.editobjectsdlg import EditObjectsDialog import wx from wx.lib.filebrowsebutton import FileBrowseButton from cellprofiler.modules.namesandtypes import ObjectsImageProvider from bioformats import load_image with wx.Dialog(None) as dlg: dlg.Title = "Choose files for editing" dlg.Sizer = wx.BoxSizer(wx.VERTICAL) box = wx.StaticBox(dlg, -1, "Choose or create new objects file") sub_sizer = wx.StaticBoxSizer(box, wx.HORIZONTAL) dlg.Sizer.Add(sub_sizer, 0, wx.EXPAND | wx.ALL, 5) new_or_existing_rb = wx.RadioBox(dlg, style=wx.RA_VERTICAL, choices = ("New", "Existing")) sub_sizer.Add(new_or_existing_rb, 0, wx.EXPAND) objects_file_fbb = FileBrowseButton( dlg, size=(300, -1), fileMask="Objects file (*.tif, *.tiff, *.png, *.bmp, *.jpg)|*.tif;*.tiff;*.png;*.bmp;*.jpg", dialogTitle="Select objects file", labelText="Objects file:") objects_file_fbb.Enable(False) sub_sizer.AddSpacer(5) sub_sizer.Add(objects_file_fbb, 0, wx.ALIGN_TOP | wx.ALIGN_RIGHT) def on_radiobox(event): objects_file_fbb.Enable(new_or_existing_rb.GetSelection() == 1) new_or_existing_rb.Bind(wx.EVT_RADIOBOX, on_radiobox) image_file_fbb = FileBrowseButton( dlg, size=(300, -1), fileMask="Objects file (*.tif, *.tiff, *.png, *.bmp, *.jpg)|*.tif;*.tiff;*.png;*.bmp;*.jpg", dialogTitle="Select guide image file", labelText="Guide image:") dlg.Sizer.Add(image_file_fbb, 0, wx.EXPAND | wx.ALL, 5) allow_overlap_checkbox = wx.CheckBox(dlg, -1, "Allow objects to overlap") allow_overlap_checkbox.Value = True dlg.Sizer.Add(allow_overlap_checkbox, 0, wx.EXPAND | wx.ALL, 5) buttons = wx.StdDialogButtonSizer() dlg.Sizer.Add(buttons, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.ALL, 5) buttons.Add(wx.Button(dlg, wx.ID_OK)) buttons.Add(wx.Button(dlg, wx.ID_CANCEL)) buttons.Realize() dlg.Fit() result = dlg.ShowModal() if result != wx.ID_OK: return self.allow_overlap.value = allow_overlap_checkbox.Value fullname = objects_file_fbb.GetValue() guidename = image_file_fbb.GetValue() if new_or_existing_rb.GetSelection() == 1: provider = ObjectsImageProvider( "InputObjects", pathname2url(fullname), None, None) image = provider.provide_image(None) pixel_data = image.pixel_data shape = pixel_data.shape[:2] labels = [pixel_data[:, :, i] for i in range(pixel_data.shape[2])] else: labels = None # # Load the guide image # guide_image = load_image(guidename) if np.min(guide_image) != np.max(guide_image): guide_image = ((guide_image - np.min(guide_image)) / (np.max(guide_image) - np.min(guide_image))) if labels is None: shape = guide_image.shape[:2] labels = [np.zeros(shape, int)] with EditObjectsDialog( guide_image, labels, self.allow_overlap, self.object_name.value) as dialog_box: result = dialog_box.ShowModal() if result != wx.OK: return labels = dialog_box.labels n_frames = len(labels) with wx.FileDialog(None, style = wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as dlg: dlg.Path = fullname dlg.Wildcard = ("Object image file (*.tif,*.tiff)|*.tif;*.tiff|" "Ilastik project file (*.ilp)|*.ilp") result = dlg.ShowModal() fullname = dlg.Path if result == wx.ID_OK: if fullname.endswith(".ilp"): self.save_into_ilp(fullname, labels, guidename) else: from bioformats.formatwriter import write_image from bioformats.omexml import PT_UINT16 if os.path.exists(fullname): os.unlink(fullname) for i, l in enumerate(labels): write_image(fullname, l, PT_UINT16, t = i, size_t = len(labels))