def normalize(C): # briefly transform to msi to be able to apply standard normalizer msi = Msi() msi.set_image(C.copy()) standard_normalizer.normalize(msi) # back to array format used here: return msi.get_image()
def run(self): tiff_ring_reader = TiffRingReader() nr_filters = len(sc.other["RECORDED_WAVELENGTHS"]) # analyze all the first image files image_files = get_image_files_from_folder(self.flatfield_folder) image_files = filter(lambda image_name: "F0" in image_name, image_files) # helper function to take maximum of two images def maximum_of_two_images(image_1, image_name_2): image_2 = tiff_ring_reader.read( os.path.join(self.flatfield_folder, image_name_2), nr_filters)[0].get_image() return np.maximum(image_1, image_2) # now reduce to maximum of all the single images flat_maximum = reduce(lambda x, y: maximum_of_two_images(x, y), image_files, 0) msi = Msi(image=flat_maximum) msi.set_wavelengths(sc.other["RECORDED_WAVELENGTHS"]) # write flatfield as nrrd writer = NrrdWriter(msi) writer.write(self.output().path)
def test_add_dummy_wavelengths_automatically(self): msi_no_wavelengths_set = Msi() msi_no_wavelengths_set.set_image(self.msi.get_image()) nr_wavelengths = msi_no_wavelengths_set.get_image().shape[-1] np.testing.assert_equal(msi_no_wavelengths_set.get_wavelengths(), np.arange(nr_wavelengths), "correct dummy wavelength values set")
def getFakeMsi(): # build a fake multispectral image with 5 dimensions. image = np.concatenate((np.ones((5, 5, 1)), np.ones( (5, 5, 1)) * 2, np.ones((5, 5, 1)) * 3, np.ones( (5, 5, 1)) * 4, np.ones((5, 5, 1)) * 5), axis=-1) msi = Msi(image) msi.set_wavelengths(np.array([5, 4, 3, 2, 1])) return msi
def getFakeMsi(): # build a fake multispectral image with 5 dimensions. image = np.concatenate((np.ones((5, 5, 1)), np.ones((5, 5, 1)) * 2, np.ones((5, 5, 1)) * 3, np.ones((5, 5, 1)) * 4, np.ones((5, 5, 1)) * 5), axis=-1) msi = Msi(image) msi.set_wavelengths(np.array([5, 4, 3, 2, 1])) return msi
def read(self, file_to_read): # our spectrometer like to follow german standards in files, we need # to switch to english ones transformed="" replacements = {',': '.', '\r\n': ''} with open(file_to_read) as infile: for line in infile: for src, target in replacements.iteritems(): line = line.replace(src, target) transformed = "\n".join([transformed, line]) for num, line in enumerate(transformed.splitlines(), 1): if ">>>>>Begin" in line: break for num_end, line in enumerate(transformed.splitlines(), 1): if ">>>>>End" in line: num_end -= 1 break string_only_spectrum = "\n".join(transformed.splitlines()[num:num_end]) data_vector = np.fromstring(string_only_spectrum, sep="\t").reshape(-1, 2) msi = Msi(data_vector[:, 1], {'wavelengths': data_vector[:, 0] * 10 ** -9}) return msi
def read(self, file_to_read, resize_factor=None, sorting_function=sort_by_filter): """ read the msi from tiffs. The fileToRead is a string prefix, all files starting with this prefix will be summarized to one msi. they will be sorted as specified in the sorting_function Args: sorting_function: the function which defines the sorting of the strings that match the prefix. Pass none if normal lexicographical sorting is wished file_to_read: the prefix of the tiff file which shall be read """ if resize_factor is None: resize_factor = TiffReader.RESIZE_FACTOR path, file_prefix = os.path.split(file_to_read) files = os.listdir(path) files_to_read = [ os.path.join(path, f) for f in files if file_prefix[2:] in f ] files_to_read.sort(cmp=sorting_function) image_array = [ self.to_image(f, resize_factor=resize_factor) for f in files_to_read ] image = reduce(lambda x, y: np.dstack((x, y)), image_array) msi = Msi(image) return msi
def read(self, fileToRead): """ read the nrrd image. TODO: properties are not correctly extracted from nrrd.""" image = None try: reader = sitk.ImageFileReader() reader.SetFileName(fileToRead) image = reader.Execute() image = sitk.GetArrayFromImage(image) except RuntimeError as re: # image could not be read logging.warning("image " + fileToRead + " could not be loaded: " + str(re)) # rethrow exception after logging raise # if image is too low dimensional singleton dimensions # are added when saving. Done because sitk can only handle dimensions # 2,3,4. This removes these singleton dimensions again. squeezed_image = np.squeeze(image) msi = Msi(squeezed_image) return msi
def read(self, fileToRead, n, resize_factor=None, segmentation=None): """ read the msi from tiffs. The fileToRead is the first file to read, then n files will be read to one msi from a sorted file list segmentation: tiff filename of the segmentation. If none, it will be tried to get a segmentation from npy files with filenames like the tiff files + _seg.tiff. If this fails, no segmentation will be assumed """ if resize_factor is None: resize_factor = TiffRingReader.RESIZE_FACTOR path, file_name = os.path.split(fileToRead) files = os.listdir(path) files_in_folder = [ os.path.join(path, f) for f in files if os.path.isfile(os.path.join(path, f)) and f.endswith('.tiff') ] files_in_folder.sort() position = files_in_folder.index(fileToRead) # take n images from found position image_array = [ self.to_image(f, resize_factor) for f in files_in_folder[position:position + n] ] image = reduce(lambda x, y: np.dstack((x, y)), image_array) # in case of 1 dimensional image: add a fake last dimension, since # we always assume the last dimension to be the wavelength domain. # TODO SW: Test this and implement for other readers if n is 1: image = np.expand_dims(image, -1) msi = Msi(image) # we pass an explicic image as segmentation if segmentation is not None: segmentation = self.to_image(segmentation, resize_factor) else: # otherwise: search for numpy segmentations try: segmentation_array = [ to_segmentation(f) for f in files_in_folder[position:position + n] ] if do_resize(resize_factor): segmentation = reduce(lambda x, y: x & y, segmentation_array) segmentation = scipy.misc.imresize(segmentation, resize_factor, interp="bilinear") except: logging.info("didn't find segmentation for all images") return msi, segmentation
def run(self): tiff_ring_reader = TiffRingReader() nr_filters = len(sc.other["RECORDED_WAVELENGTHS"]) # analyze all the first image files image_files = get_image_files_from_folder(self.dark_folder, suffix="F0.tiff") # returns the mean dark image vector of all inputted dark image # overly complicated TODO SW: make this simple code readable. dark_means = map( lambda image_name: msimani.calculate_mean_spectrum( tiff_ring_reader.read( os.path.join(self.dark_folder, image_name), nr_filters)[0] ), image_files) dark_means_sum = reduce(lambda x, y: x + y.get_image(), dark_means, 0) final_dark_mean = dark_means_sum / len(dark_means) msi = Msi(image=final_dark_mean) msi.set_wavelengths(sc.other["RECORDED_WAVELENGTHS"]) # write flatfield as nrrd writer = NrrdWriter(msi) writer.write(self.output().path)
def run(self): tiff_ring_reader = TiffRingReader() nr_filters = len(sc.other["RECORDED_WAVELENGTHS"]) # analyze all the first image files image_files = get_image_files_from_folder(self.dark_folder, suffix="F0.tiff") # returns the mean dark image vector of all inputted dark image # overly complicated TODO SW: make this simple code readable. dark_means = map(lambda image_name: msimani.calculate_mean_spectrum( tiff_ring_reader.read(os.path.join(self.dark_folder, image_name), nr_filters)[0]), image_files) dark_means_sum = reduce(lambda x, y: x+y.get_image(), dark_means, 0) final_dark_mean = dark_means_sum / len(dark_means) msi = Msi(image=final_dark_mean) msi.set_wavelengths(sc.other["RECORDED_WAVELENGTHS"]) # write flatfield as nrrd writer = NrrdWriter(msi) writer.write(self.output().path)
def run(self): tiff_ring_reader = TiffRingReader() nr_filters = len(sc.other["RECORDED_WAVELENGTHS"]) # analyze all the first image files image_files = get_image_files_from_folder(self.flatfield_folder) image_files = filter(lambda image_name: "F0" in image_name, image_files) # helper function to take maximum of two images def maximum_of_two_images(image_1, image_name_2): image_2 = tiff_ring_reader.read(os.path.join(self.flatfield_folder, image_name_2), nr_filters)[0].get_image() return np.maximum(image_1, image_2) # now reduce to maximum of all the single images flat_maximum = reduce(lambda x, y: maximum_of_two_images(x, y), image_files, 0) msi = Msi(image=flat_maximum) msi.set_wavelengths(sc.other["RECORDED_WAVELENGTHS"]) # write flatfield as nrrd writer = NrrdWriter(msi) writer.write(self.output().path)
def read(self, fileToRead): """ read the msi from pngs. The fileToRead is a string prefix, all files starting with this prefix will be summarized to one msi""" path, file_prefix = os.path.split(fileToRead) files = os.listdir(path) files_to_read = [ os.path.join(path, f) for f in files if f.startswith(file_prefix) ] files_to_read.sort() image_array = [toImage(f) for f in files_to_read] image = reduce(lambda x, y: np.dstack((x, y)), image_array) msi = Msi(image) return msi
def test_properties_not_shared(self): msi1 = Msi() msi2 = Msi() msi1.add_property({"integration time": np.array([1, 2, 3])}) self.assertTrue('integration time' not in msi2.get_properties())