def test_parser_get_physical_coordinates(data_path, parse_lib): parser = ImzMLParser(data_path, parse_lib=parse_lib) x, y = parser.get_physical_coordinates(0) assert x == 100.0 assert y == 100.0
def write_msi_to_hdf(self, h5f_fname_w_path, imzml_fname_w_path, norm=''): """ Converts imzML data to HDF5 format. Iterates through imzML and, one spectrum at a time, reads and writes spectral data to h5 as new raw dataset. Spectra are stored to HDF5 dataset in the order in which they are read from imzML file. """ imzml = ImzMLParser(imzml_fname_w_path) #Infer dataset dimensions from first spectrum in imzML samp_mz, samp_int = imzml.getspectrum(0) length = len(samp_mz) self.mzsaxis = np.asarray(samp_mz) height = len(imzml.mzOffsets) del samp_int, samp_mz #Create and open hdf5 file hf = h5py.File(h5f_fname_w_path, 'w') print('\n%i spectra x %i mz data points'%(height, length)) print() print('Importing...') print('') grp = hf.create_group('MALDI_001') dset1 = grp.create_dataset('Intensities', shape=(height, length), chunks=(1, 1024), dtype='float64') grp.create_dataset('m_over_z', data=self.mzsaxis) dset3 = grp.create_dataset('Coordinates', shape=(height,2)) grp.create_group('Normalization Factors') # Iterates through spectra contained in .ibd binary using getspectrum() # For each spectrum, writes mz axis and intensity list as line in # appropriate datasets in group "MALDI_001" summed_ints = np.zeros(length) max_int = 0 for i in range(height): mz, intensity = imzml.getspectrum(i) try: coordinates = imzml.get_physical_coordinates(i) except KeyError: imzml.imzmldict['pixel size y'] = imzml.imzmldict['pixel size x'] coordinates = imzml.get_physical_coordinates(i) point = np.asarray([coordinates[0], coordinates[1]]) dset1[i] = intensity dset3[i] = point if max(intensity) > max_int: max_int = max(intensity) summed_ints = summed_ints + intensity if self.log and i > 0: if i % 1000 == 0: print('%i / %i'%((i, height))) average_spectrum = summed_ints / height grp.create_dataset('Average spectrum', data=(average_spectrum), dtype='float64') del summed_ints, mz, intensity, coordinates, point, max_int #Revert Numpy error handling to default setting (print) np.seterr(all = 'print') # Clean up, flush buffer, close file print('Finished importing!') if norm=='' or norm.upper()=='NONE': pass else: self._calculate_new_normalization_(norm, hf) hf.flush() hf.close() return