def reduce_image(image): mzs = [] intensities = [] for i, (x, y, z) in enumerate(image.coordinates): mz, ints = image.getspectrum(i) mz = mz[:1] ints = ints[:1] mzs.append(mz) intensities.append(ints) imzmlio.write_imzml( mzs, intensities, image.coordinates, "/mnt/d/MALDI/imzML/MSI_20190419_01/00/peaksel_small.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)) coordinates = imzml.coordinates print("Parsing all spectra") intensities, new_coords = imzmlio.get_spectra_from_images(image) previous_index = 0 array_coords = np.array(new_coords) array_intensities = np.array(intensities) split_intensities, split_coords = [], [] print("Splitting") for i in range(max_z): indices = np.where(array_coords.T[2] == i + 1) current_intensities = list(map(list, array_intensities[indices])) current_coordinates = array_coords[indices] current_coordinates[..., 2] = 1 current_coordinates = list(map(tuple, current_coordinates)) mzs = np.tile(mz, (len(indices[0]), 1)).tolist() outroot, outext = os.path.splitext(outname) current_name = outroot + "_" + str(i) + outext print(current_name) imzmlio.write_imzml(mzs, current_intensities, current_coordinates, current_name)
sitk.sitkNearestNeighbor, 0) outRegister_array[..., i] = sitk.GetArrayFromImage(register_image_3D).T fig, ax = plt.subplots(1, 2) before = sitk.GetArrayFromImage(image).T index = np.unravel_index(np.argmax(np.mean(before, axis=-1), axis=None), before.shape) print(index) after = outRegister_array print(outRegister_array.shape) if len(shape) == 3: before = before[:, :, index[-1]] after = after[:, :, index[-1]] elif len(shape) == 4: before = before[:, :, index[-2], index[-1]] after = after[:, :, index[-2], index[-1]] # ax[0].imshow(before.T) # ax[1].imshow(after.T) # plt.show() if is_imzml: mz, y = imzml.getspectrum(0) intensities, coordinates = imzmlio.get_spectra_from_images( outRegister_array) mzs = [mz] * len(coordinates) imzmlio.write_imzml(mzs, intensities, coordinates, outputname) else: outRegister = sitk.Cast(outRegister, sitk.sitkFloat32) sitk.WriteImage(outRegister, outputname)
outname = args.output factor = int(args.factor) noise_level = float(args.level) nb_peaks = int(args.nbpeaks) step_mz = float(args.step) imzml = io.open_imzml(input_name) spectra = io.get_spectra(imzml) peaks = sp.spectra_peak_mzs_adaptative_noiselevel(spectra, factor=factor, noise_level=noise_level, wlen=100) realigned_spectra = sp.realign_mzs(spectra, peaks, reference="median", nb_occurrence=nb_peaks, step=step_mz) print(realigned_spectra.shape) av = sp.spectra_mean(realigned_spectra) mzs = realigned_spectra[0, 0, :] plt.plot(mzs, av) plt.show() mzs = [] intensities = [] for spectrum in realigned_spectra: x, y = spectrum mzs.append(x) intensities.append(y) io.write_imzml(mzs, intensities, imzml.coordinates, outname)