def test_normalize(self): self.imzml = io.open_imzml( "/mnt/d/MALDI/imzML/MSI_20190419_01/00/peaksel_small.imzML") image = io.to_image_array(self.imzml) norm = io.normalize(image) print(image) print(norm.dtype) print(norm.shape)
def closest_reconstruction(image, image1, image2, image_eigenvectors, image_eigenvectors_2=None): """ Find proximities based on the similarity between projection images. Parameters ---------- image: np.ndarray None image1: np.ndarray reference image image2: np.ndarray target image image_eigenvectors: np.ndarray component images associated to image 1 image_eigenvectors_2: np.ndarray component images associated to image 2, if different Returns ---------- np.ndarray average differences between reconstructions for each image """ if image_eigenvectors_2 is None: image_eigenvectors_2 = image_eigenvectors w_2 = image2 / np.sum(image2) reconstructed_image2 = reconstruct_image_from_components( image_eigenvectors_2, w_2.T) reconstructed_image2 = imzmlio.normalize(reconstructed_image2) diff = np.zeros((image1.shape[0], )) for index in range(image1.shape[0]): w = image1[index, ...] / np.sum(image1[index, ...]) reconstructed_image = reconstruct_image_from_components( image_eigenvectors, w.T) reconstructed_image = imzmlio.normalize(reconstructed_image) diff[index] = np.mean( np.abs(reconstructed_image2 - reconstructed_image)) return diff
utils.export_figure_matplotlib("stats_target_all.png", img_ref[..., 0]) return img mean_noise = 10 stddev_noise = 2 reference = create_data() target = create_data(deformation=True) reference_itk = sitk.GetImageFromArray(reference) target_itk = sitk.GetImageFromArray(target) reference_norm = imzmlio.normalize(reference) target_norm = imzmlio.normalize(target) reference_norm = fusion.flatten(reference_norm) target_norm = fusion.flatten(target_norm, is_spectral=True) nmf = NMF(n_components=4, init='nndsvda', solver='cd', random_state=0, max_iter=1000) fit_red = nmf.fit(target_norm) W = fit_red.transform(target_norm) H = fit_red.components_
nb_peaks = image.shape[-1] print("Number of peaks=", nb_peaks) shape = image.shape if len(shape) == 4: for i in range(shape[-2]): current_image = image[..., i, :] image_labels = spatially_aware_clustering(current_image, k, n, radius) plt.imshow(image_labels) plt.show() else: image_labels = spatially_aware_clustering(image, k, n, radius) image = imzmlio.normalize(image) outname_csv = os.path.splitext(outname)[0] + ".csv" out_array = np.zeros(shape=(nb_peaks, k)) for i in range(k): indices = np.where(image_labels == i) not_indices = np.where(image_labels != i) median_spectrum = np.median(image[indices], axis=0) print(median_spectrum.shape) other_median_spectrum = np.median(image[not_indices], axis=0) median_spectrum -= other_median_spectrum top_indices = np.argsort(median_spectrum)[::-1] top_molecules = mzs[top_indices] out_array[:, i] = top_molecules np.savetxt(outname_csv, out_array, delimiter=";", fmt="%s") image_labels_itk = sitk.GetImageFromArray(image_labels.astype(np.uint8))
referencename = args.reference targetname = args.target spectra_ref, reference_image = read_imzml(referencename) spectra_target, target_image = read_imzml(targetname) mzs_ref = spectra_ref[0, 0, ...] mzs_target = spectra_target[0, 0, ...] intensities_ref = spectra_ref[:, 1, ...] intensities_target = spectra_target[:, 1, ...] print(mzs_ref.shape, intensities_ref.shape) reference_image_norm = imzmlio.normalize(reference_image) target_image_norm = imzmlio.normalize(target_image) print(reference_image_norm.shape, target_image_norm.shape) max_diff_ref, max_diff_target = None, None max_mz = 0 diff = 0 max_diff_index = 0 for i, mz in enumerate(mzs_ref): index_target = np.argwhere(mzs_target == mz).flatten() if not index_target: continue current_ref = reference_image_norm[..., i].astype(np.float64) current_target = target_image_norm[..., index_target[0]].astype(np.float64)
## Affine followed by variational on the full MSI image msi_registered_itk = apply_registration(msi_images_itk, mri_itk_reg, resampler) msi_registered_itk = apply_transform_itk(transformname, msi_registered_itk) ## Joint analysis print("Joint statistical analysis") print("--------------------------") mzs, intensities = imzml.getspectrum(0) threshold_mz = 569 image = np.transpose(sitk.GetArrayFromImage(msi_registered_itk), (1, 2, 0)) image = image[..., mzs >= threshold_mz] mzs = mzs[mzs >= threshold_mz] mzs = np.around(mzs, decimals=2) mzs = mzs.astype(str) image = io.normalize(image) image_norm = fusion.flatten(image) mri_norm = io.normalize(mri_image) mri_norm = fusion.flatten(mri_norm) n = 7 nmf = NMF(n_components=n, init='nndsvda', solver='cd', random_state=0) fit_red = nmf.fit(image_norm) point = fit_red.transform(mri_norm) X_r = fit_red.transform(image_norm) image_eigenvectors = fit_red.components_ centers = X_r point_mri = point image_eigenvectors = image_eigenvectors.T new_shape = image.shape[:-1] + (image_eigenvectors.shape[-1], ) image_eigenvectors = image_eigenvectors.reshape(new_shape)
def statistical_analysis(outname, image, image_mri, mzs, n, is_ratio, top, post_process, is_norm, is_denoise, is_memmap, translate_components): if is_denoise: size = (5, 5, 1) if len(image.shape) == 4: size += (1, ) image = median_filter(image, size=size) # image[image_mri == 0] = 0 if is_ratio: ratio_images, ratio_mzs = fusion.extract_ratio_images(image, mzs) image = np.concatenate((image, ratio_images), axis=-1) mzs = np.concatenate((mzs, ratio_mzs)) indices_delete = fusion.remove_indices(image) image = np.delete(image, indices_delete, axis=-1) mzs = np.delete(mzs, indices_delete) memmap_dir = os.path.dirname(outname) + os.path.sep + "mmap" + os.path.sep memmap_basename = os.path.splitext(os.path.basename(outname))[0] memmap_image_filename = memmap_dir + memmap_basename + ".npy" memmap_files_exist = (os.path.exists(memmap_dir) and os.path.exists(memmap_image_filename)) if is_memmap and not memmap_files_exist: print("Writing mem-map") os.makedirs(memmap_dir, exist_ok=True) np.save(memmap_image_filename, image) if is_memmap: print("Reading mem-map") image = np.load(memmap_image_filename, mmap_mode="r") image_norm = imzmlio.normalize(image) mri_norm = imzmlio.normalize(image_mri) print(image_norm.shape) if is_norm: image_norm = image_norm.astype(np.float64) mri_norm = mri_norm.astype(np.float64) for index in range(image_norm.shape[-1]): current_image = image_norm[..., index] norm = np.linalg.norm(current_image) if norm > 0: current_image /= norm image_norm[..., index] = current_image mri_norm /= np.linalg.norm(mri_norm) image_norm = fusion.flatten(image_norm, is_spectral=True) mri_norm = fusion.flatten(mri_norm) print("Computing Dimension reduction") nmf = NMF(n_components=n, init='nndsvda', solver='mu', random_state=0, max_iter=1000) # fit_red = nmf.fit(image_norm.T) # eigenvectors = fit_red.components_ #H # image_eigenvectors = nmf.transform(image_norm.T); #W # mri_eigenvectors = nmf.transform(mri_norm.T) # shape_mri = image_mri.shape + (mri_eigenvectors.shape[-1],) # mri_eigenvectors = mri_eigenvectors.reshape(shape_mri) fit_red = nmf.fit(image_norm) W = fit_red.transform(image_norm) H = fit_red.components_ image_eigenvectors = H.T new_shape = image.shape[:-1] + (image_eigenvectors.shape[-1], ) image_eigenvectors = image_eigenvectors.reshape(new_shape) image_eigenvectors_translated = image_eigenvectors H_translated = H if translate_components: image_eigenvectors_translated, translated_image = reg.register_component_images( image_mri, image, image_eigenvectors, 3) H_translated = image_eigenvectors_translated.reshape(H.T.shape).T # We use translated components ONLY for MRI reconstruction fit_red.components_ = H_translated point = fit_red.transform(mri_norm) #Normal components for MS images fit_red.components_ = H X_r = fit_red.transform(image_norm) centers = X_r point_mri = point print("Explained variance ratio=", fusion.get_score(fit_red, image_norm)) if post_process: X_train, X_test = fusion.post_processing(X_r, point) centers = X_train point_mri = X_test clustering = fusion.clustering_kmeans(X_train) plt.plot(X_train[:, 0], X_train[:, 1], "b.") plt.plot(X_test[:, 0], X_test[:, 1], "ro") plt.show() plt.close() if not is_ratio: X_all = np.concatenate((centers, point_mri), axis=0) tsne_all = StandardScaler().fit_transform(X_all) X_r_all = np.concatenate((X_r, point), axis=0) pca_all = StandardScaler().fit_transform(X_r_all) mri = StandardScaler().fit_transform(point) pca_all = pca_all[..., :2] size = (100, 100) if len(image.shape) == 3: images_maldi = [cv2.resize(i, size) for i in image.T] image_mri = cv2.resize(image_mri.T, size) # visualize_scatter_with_images(pca_all, # images_maldi, # image_mri, # figsize=size, # image_zoom=0.7) elif len(image.shape) == 4: images_maldi = [ cv2.resize(i[..., i.shape[-1] // 2], size) for i in np.transpose(image, (3, 0, 1, 2)) ] thumbnail_mri = image_mri.copy() thumbnail_mri = cv2.resize( thumbnail_mri[..., thumbnail_mri.shape[-1] // 2], size) #visualize_scatter_with_images(pca_all, # images_maldi, # thumbnail_mri, # figsize=size, # image_zoom=0.7) # plt.plot(X_r[:, 0], X_r[:, 1], "b.") # plt.plot(point[:, 0], point[:, 1], "ro") # plt.show() # plt.close() labels = None weights = [1 for i in range(centers.shape[1])] if top is not None: af = fusion.clustering_kmeans(image_norm, X_r) labels = af.labels_ centers = af.cluster_centers_ similar_images, similar_mzs, distances = fusion.select_images( image, point_mri, centers, weights, mzs, labels, None) print("Selecting images end") diff = fusion.closest_reconstruction(image, X_r, point, image_eigenvectors, image_eigenvectors_translated) print(mzs.shape) print(similar_mzs[:10], mzs[diff.argsort()][:10]) similar_images = similar_images[..., 0:100] similar_images = imzmlio.normalize(similar_images) index_closest = np.where(mzs == similar_mzs[0]) index = index_closest[0] w = X_r[index, ...] / np.sum(X_r[index, ...]) image_closest = fusion.reconstruct_image_from_components( image_eigenvectors, w.T) image_closest = image_closest.T image_closest = imzmlio.normalize(image_closest) w_mri = point / np.sum(point) mri_reconstructed = fusion.reconstruct_image_from_components( image_eigenvectors_translated, w_mri.T) mri_reconstructed = mri_reconstructed.T i = np.where((mri_reconstructed > 0)) img_tmp = np.reshape(mri_norm, image_mri.shape) mri_reconstructed = imzmlio.normalize(mri_reconstructed) mri_reconstructed_diff = mri_reconstructed.copy().astype(float) if is_norm: mri_reconstructed_diff /= np.linalg.norm(mri_reconstructed_diff) diff_reconstruction = np.mean( np.abs(mri_reconstructed_diff[i] - img_tmp[i])) / np.max(img_tmp) print("Average diff NMF reconstruction (percentage)=", "{:.5f}".format(diff_reconstruction)) if len(similar_images.shape) == 3: fig, ax = plt.subplots(1, 4) ax[0].imshow(similar_images[..., 0], cmap="gray") ax[1].imshow(np.reshape(mri_norm, image_mri.shape), cmap="gray") ax[2].imshow(image_closest, cmap="gray") ax[3].imshow(mri_reconstructed, cmap="gray") # plt.show() elif len(similar_images.shape) == 4: fig, ax = plt.subplots(1, 4) tracker = SliceViewer(ax, np.transpose(similar_images[..., 0], (2, 1, 0)), np.transpose( np.reshape(mri_norm, image_mri.shape), (2, 1, 0)), np.transpose(image_closest, (2, 1, 0)), np.transpose(mri_reconstructed, (2, 1, 0)), vmin=0, vmax=255) fig.canvas.mpl_connect('scroll_event', tracker.onscroll) # plt.show() if len(similar_images.shape) == 4: s = similar_images.shape similar_images = similar_images.reshape(s[0], s[1], s[2], s[3], order="F") tifffile.imwrite(outname, similar_images.T, imagej=True) else: itk_similar_images = sitk.GetImageFromArray(similar_images.T) sitk.WriteImage(itk_similar_images, outname) sitk.WriteImage(sitk.GetImageFromArray(mri_reconstructed.T), os.path.splitext(outname)[0] + "_reconstruction.tif") outname_csv = os.path.splitext(outname)[0] + ".csv" np.savetxt(outname_csv, np.transpose((similar_mzs, distances)), delimiter=";", fmt="%s")
imzml = imzmlio.open_imzml(inputname) spectra = imzmlio.get_full_spectra(imzml) max_x = max(imzml.coordinates, key=lambda item: item[0])[0] max_y = max(imzml.coordinates, key=lambda item: item[1])[1] max_z = max(imzml.coordinates, key=lambda item: item[2])[2] image = imzmlio.get_images_from_spectra(spectra, (max_x, max_y, max_z)) mzs, intensities = imzml.getspectrum(0) else: image = sitk.GetArrayFromImage(sitk.ReadImage(inputname)).T mzs = [i for i in range(image.shape[2])] mzs = np.asarray(mzs) image = image[..., mzs >= threshold] print(image.shape) mzs = mzs[mzs >= threshold] mzs = np.around(mzs, decimals=2) mzs = mzs.astype(str) shape = image.shape[:-1] image_norm = imzmlio.normalize(image) image_norm = fusion.flatten(image_norm, is_spectral=True).T n = 3 X_tsne = TSNE(n_components=n, random_state=0).fit_transform(image_norm) X_tsne = imzmlio.normalize(X_tsne) X_tsne = X_tsne.reshape(shape + (3, )) tsne_itk = sitk.GetImageFromArray(X_tsne, isVector=True) sitk.WriteImage(tsne_itk, outputname)
default=-1) args = parser.parse_args() inputname = args.input reconstructedname = args.reconstruction number_slice = int(args.number_slice) input_mri = sitk.ReadImage(inputname) reconstructed_mri = sitk.ReadImage(reconstructedname) if number_slice >= 0: if input_mri.GetDimension() >= 3 and number_slice < input_mri.GetSize( )[-1]: input_mri = input_mri[:, :, number_slice] if reconstructed_mri.GetDimension( ) >= 3 and number_slice < reconstructed_mri.GetSize()[-1]: reconstructed_mri = reconstructed_mri[:, :, number_slice] input_mri_array = sitk.GetArrayFromImage(input_mri) input_mri_array = imzmlio.normalize(input_mri_array) input_mri = sitk.GetImageFromArray(input_mri_array) reconstructed_mri_array = sitk.GetArrayFromImage(reconstructed_mri) reconstructed_mri_array = imzmlio.normalize(reconstructed_mri_array) reconstructed_mri = sitk.GetImageFromArray(reconstructed_mri_array) print(reconstructed_mri_array.shape, input_mri_array.shape) mse_stddev = utils.mse_stddev(input_mri, reconstructed_mri) print("MSE from std images=", np.sqrt(mse_stddev))
# plt.show() image[image_mri == 0, :] = 0 if is_ratio: ratio_images, ratio_mzs = fusion.extract_ratio_images(image, mzs) image = np.concatenate((image, ratio_images), axis=-1) mzs = np.concatenate((mzs, ratio_mzs)) image = ratio_images mzs = ratio_mzs indices_delete = fusion.remove_indices(image) image = np.delete(image, indices_delete, axis=-1) mzs = np.delete(mzs, indices_delete) print(image.shape) image = imzmlio.normalize(image) # image = np.uint8(cv.normalize(image, None, 0, 255, cv.NORM_MINMAX)) image_mri = imzmlio.normalize(image_mri) image = image.astype(np.float64) image_mri = image_mri.astype(np.float64) for index in range(image.shape[-1]): current_image = image[..., index] norm = np.linalg.norm(current_image) if norm > 0: current_image /= norm image[..., index] = current_image mask = np.ones_like(image_mri) mask[image_mri == 0] = 0 weights = sobel(image_mri, mask=mask)