def read_and_convert(filename, start_wav=None, stop_wav=None, data_type="absorbance", sample_info=None): if sample_info is None: sample_info = ir_map.sample_info() omnic_object = OmnicMap.OmnicMap(filename) wavenumbers = None if wavenumbers is None: n_wav = omnic_object.data.shape[2] if (start_wav is None) or (stop_wav is None): if omnic_object.info['OmnicInfo'] is not None: start_wav = omnic_object.info['OmnicInfo']['First X value'] stop_wav = omnic_object.info['OmnicInfo']['Last X value'] n_wav = omnic_object.info['OmnicInfo']['Number of points'] else: raise ValueError( 'There is an issue with the Ominc file or its parser. Make sure that the meta info from the header is present and parsed correctly.' ) wavenumbers = np.linspace(start_wav, stop_wav, n_wav) image_grid_param = omnic_object.info['OmnicInfo'][ 'Mapping stage parameters'] image_mask = np.ones(omnic_object.data.shape[0:2]) > 0.5 # build the basis object this_ir_map = ir_map.ir_map(wavenumbers=wavenumbers, sample_info=sample_info, data_type=data_type) this_ir_map.add_image_cube(omnic_object.data, image_mask, image_grid_param) return this_ir_map
def save(self, sample_id='simulated_dataset'): """Save the simulated dataset as an hdf5 file. Arguments: ---------- sample_id : A string that identifies the sample, spaces will be substituted for underscores. """ si = sample_info( sample_id=sample_id, sample_meta_data= f"This dataset has {self.NbaseSpectra} components and map size is {self.Ny} * {self.Nx}" ) si.show() self.spectra_map_gen() # generate simulated dataset y, x = np.where(self.mask) self.xy = np.c_[x, y] ir_data = ir_map(self.wavenumber, si, with_factorization=True) ir_data.add_data(spectrum=self.data, xy=self.xy) ir_data.to_image_cube() ir_data.add_factorization(component=self.spectraMat, component_coef=self.densityVecsCondense, prefix='MCR') ir_data.write_as_hdf5(f'{sample_id}.h5')
def read_npy(filename, wavenumbers=None, data_type="absorbance", sample_info=None): if sample_info is None: sample_info = ir_map.sample_info() data = np.load(filename) if wavenumbers is None: wavenumbers = np.arange(data.shape[2]) image_grid_param = [0, 0, 1, 1] image_mask = np.ones(data.shape[0:2]) > 0.5 this_ir_map = ir_map.ir_map(wavenumbers=wavenumbers, sample_info=sample_info, data_type=data_type) this_ir_map.add_image_cube(data, image_mask, image_grid_param) return this_ir_map
def read_npz(filename, data_type="absorbance", sample_info=None): if sample_info is None: sample_info = ir_map.sample_info() npzFile = np.load(filename) for k in npzFile.files: if 'energy' in k: wavenumbers = npzFile[k] else: data = npzFile[k] image_grid_param = [0, 0, 1, 1] image_mask = np.ones(data.shape[0:2]) > 0.5 this_ir_map = ir_map.ir_map(wavenumbers=wavenumbers, sample_info=sample_info, data_type=data_type) this_ir_map.add_image_cube(data, image_mask, image_grid_param) return this_ir_map
def openBtnClicked(self): # open omnic map file if self.fileFormat == 'map': self.filePath, self.fileName, canceled = uiGetFile( 'Open map file', self.path, "Omnic Map Files (*.map)") elif self.fileFormat == 'npy': self.filePath, self.fileName, canceled = uiGetFile( 'Open npy file', self.path, "Numpy array Files (*.npy)") if canceled: self.infoBox.setText('Open file canceled.') return # set sample_id if self.sampleName.text() == 'None': sample_info = ir_map.sample_info(sample_id=self.fileName[:-4]) else: sample_info = ir_map.sample_info(sample_id=self.sampleName.text()) #try to open omnic map file try: if self.fileFormat == 'map': self.irMap = read_and_convert(self.filePath + self.fileName, sample_info=sample_info) elif self.fileFormat == 'npy': self.irMap = read_npy(self.filePath + self.fileName, sample_info=sample_info) except Exception as error: self.infoBox.setText(error.args[0] + f'\nFailed to open file: {self.fileName}.') else: if self.fileFormat == 'map': # check whether to perform T->A conversion spec0 = self.irMap.imageCube[0, 0, :] maxSpecY = np.max(spec0) if (not self.T2AConvert.isChecked()) and (maxSpecY >= self.minYLimit): userMsg = YesNoDialog( f'max(Y) of the first spectrum is greater than {self.minYLimit}, \ while the "Auto T->A" box is not checked. \nPlease make sure data format is in absorbance.\ \nDo you want to perform "Auto T->A" conversion?') userChoice = userMsg.choice() if userChoice == QMessageBox.Yes: self.T2AConvert.setChecked(True) self.irMap.imageCube = -np.log10(self.irMap.imageCube / 100 + self.epsilon) self.irMap.data = -np.log10(self.irMap.data / 100 + self.epsilon) self.infoBox.setText( f'User chooses to perform T->A conversion in {self.fileName}.' ) else: self.infoBox.setText( f'User chooses not to perform T->A conversion in {self.fileName}.' ) elif maxSpecY >= self.minYLimit: self.irMap.imageCube = -np.log10(self.irMap.imageCube / 100 + self.epsilon) self.irMap.data = -np.log10(self.irMap.data / 100 + self.epsilon) self.infoBox.setText( f'T->A conversion is performed in {self.fileName}.') else: self.infoBox.setText( f"{self.fileName}'s datatype is absorbance. \nT->A conversion is not performed." ) self.dataCube = np.moveaxis(np.flipud(self.irMap.imageCube), -1, 0) # set up required data/properties in self.imageview row, col = self.irMap.imageCube.shape[ 0], self.irMap.imageCube.shape[1] wavenumbers = self.irMap.wavenumbers rc2ind = {tuple(x[1:]): x[0] for x in self.irMap.ind_rc_map} self.updateImage(row, col, wavenumbers, rc2ind, self.dataCube) # set up required data/properties in self.spectra self.spectra.wavenumbers = self.imageview.wavenumbers self.spectra.rc2ind = self.imageview.rc2ind self.spectra._data = self.irMap.data
def run(self): n_files = len(self.filePaths) for i, filePath in enumerate(self.filePaths): folderPath = os.path.dirname(filePath) + '/' fileName = os.path.basename(filePath) # set sample_id if self.sampleName == 'None': sample_info = ir_map.sample_info(sample_id=fileName[:-4]) else: sample_info = ir_map.sample_info(sample_id=self.sampleName) # try open omnic map and show image try: irMap = read_and_convert(filePath, sample_info=sample_info) except Exception as error: self.sigText.emit(error.args[0] + f'\nFailed to open file: {fileName}.') MsgBox(error.args[0] + f'\nFailed to open file: {fileName}.') break else: # check whether to perform T->A conversion spec0 = irMap.imageCube[0, 0, :] maxSpecY = np.max(spec0) if (not self.T2AConvertStatus) and (maxSpecY >= self.minYLimit): userMsg = YesNoDialog( f'max(Y) of the first spectrum is greater than {self.minYLimit}, \ while the "Auto T->A" box is not checked. \nPlease make sure data format is in absorbance.\ \nDo you want to perform "Auto T->A" conversion?') # get user choice userMsg.addButton(QMessageBox.YesToAll) userChoice = userMsg.choice() if userChoice == QMessageBox.YesToAll: # set 'auto T->A' on self.sigT2A.emit(True) if (userChoice == QMessageBox.YesToAll) or (userChoice == QMessageBox.Yes): irMap.imageCube = -np.log10(irMap.imageCube / 100 + self.epsilon) irMap.data = -np.log10(irMap.data / 100 + self.epsilon) self.sigText.emit( f'User chooses to perform T->A conversion in {fileName}.' ) else: self.sigText.emit( f'User chooses not to perform T->A conversion in {fileName}.' ) elif maxSpecY >= self.minYLimit: irMap.imageCube = -np.log10(irMap.imageCube / 100 + self.epsilon) irMap.data = -np.log10(irMap.data / 100 + self.epsilon) self.sigText.emit( f'T->A conversion is performed in {fileName}.') else: self.sigText.emit( f"{fileName}'s datatype is absorbance. \nT->A conversion is not performed." ) dataCube = np.moveaxis(np.flipud(irMap.imageCube), -1, 0) # set up required data/properties in self.imageview and show image row, col = irMap.imageCube.shape[0], irMap.imageCube.shape[1] wavenumbers = irMap.wavenumbers rc2ind = {tuple(x[1:]): x[0] for x in irMap.ind_rc_map} self.sigImage.emit((row, col, wavenumbers, rc2ind, dataCube)) # save hdf5 h5Name = fileName[:-4] + '.h5' try: irMap.write_as_hdf5(folderPath + h5Name) self.sigText.emit( f'#{i + 1} out of {n_files} maps HDF5-conversion complete! \ \nHDF5 File Location: {folderPath + h5Name}') except Exception as error: MsgBox(error.args[0], 'error') break # QTimer.singleShot(0, lambda: self.infoBox.setText(f'Start processing #{i + 1} out of {n_files} files.')) # MsgBox('All file conversion complete!') self.quit()
def batchBtnClicked(self): folderPath, canceled = uiGetDir('Select a folder', self.path) if canceled: self.infoBox.setText('Open folder canceled.') return filePaths = glob(folderPath + '*.map') # if no map file was found if not filePaths: MsgBox('No .map file was found.\nPlease select another folder') self.infoBox.setText( 'No .map file was found in the selected folder.') return #try to use thread # mapConverter = BatchMapConverter(self.T2AConvert.isChecked(), self.sampleName.text(),\ # self.epsilon, self.minYLimit, filePaths) # mapConverter.sigText.connect(lambda x:self.infoBox.setText(x)) # mapConverter.sigImage.connect(lambda x:self.updateImage(*x)) # mapConverter.sigT2A.connect(lambda x:self.T2AConvert.setChecked(x)) # mapConverter.start() # ToDo : change this Long loop to thread n_files = len(filePaths) for i, filePath in enumerate(filePaths): fileName = os.path.basename(filePath) # set sample_id if self.sampleName.text() == 'None': sample_info = ir_map.sample_info(sample_id=fileName[:-4]) else: sample_info = ir_map.sample_info( sample_id=self.sampleName.text()) # try open omnic map and show image try: irMap = read_and_convert(filePath, sample_info=sample_info) except Exception as error: self.infoBox.setText(error.args[0] + f'\nFailed to open file: {fileName}.') MsgBox(error.args[0] + f'\nFailed to open file: {fileName}.') break else: # check whether to perform T->A conversion spec0 = irMap.imageCube[0, 0, :] maxSpecY = np.max(spec0) if (not self.T2AConvert.isChecked()) and (maxSpecY >= self.minYLimit): userMsg = YesNoDialog( f'max(Y) of the first spectrum is greater than {self.minYLimit}, \ while the "Auto T->A" box is not checked. \nPlease make sure data format is in absorbance.\ \nDo you want to perform "Auto T->A" conversion?') # get user choice userMsg.addButton(QMessageBox.YesToAll) userChoice = userMsg.choice() if userChoice == QMessageBox.YesToAll: # set 'auto T->A' on self.T2AConvert.setChecked(True) if (userChoice == QMessageBox.YesToAll) or (userChoice == QMessageBox.Yes): irMap.imageCube = -np.log10(irMap.imageCube / 100 + self.epsilon) irMap.data = -np.log10(irMap.data / 100 + self.epsilon) self.infoBox.setText( f'User chooses to perform T->A conversion in {fileName}.' ) else: self.infoBox.setText( f'User chooses not to perform T->A conversion in {fileName}.' ) elif maxSpecY >= self.minYLimit: irMap.imageCube = -np.log10(irMap.imageCube / 100 + self.epsilon) irMap.data = -np.log10(irMap.data / 100 + self.epsilon) self.infoBox.setText( f'T->A conversion is performed in {fileName}.') else: self.infoBox.setText( f"{fileName}'s datatype is absorbance. \nT->A conversion is not performed." ) dataCube = np.moveaxis(np.flipud(irMap.imageCube), -1, 0) # set up required data/properties in self.imageview and show image row, col = irMap.imageCube.shape[0], irMap.imageCube.shape[1] wavenumbers = irMap.wavenumbers rc2ind = {tuple(x[1:]): x[0] for x in irMap.ind_rc_map} self.updateImage(row, col, wavenumbers, rc2ind, dataCube) # save hdf5 h5Name = fileName[:-4] + '.h5' try: irMap.write_as_hdf5(folderPath + h5Name) self.infoBox.setText( f'#{i+1} out of {n_files} maps HDF5-conversion complete! \ \nHDF5 File Location: {folderPath + h5Name}') except Exception as error: MsgBox(error.args[0], 'error') break QApplication.processEvents() MsgBox('All file conversion complete!')
) wavenumbers = np.linspace(start_wav, stop_wav, n_wav) image_grid_param = omnic_object.info['OmnicInfo'][ 'Mapping stage parameters'] image_mask = np.ones(omnic_object.data.shape[0:2]) > 0.5 # build the basis object this_ir_map = ir_map.ir_map(wavenumbers=wavenumbers, sample_info=sample_info, data_type=data_type) this_ir_map.add_image_cube(omnic_object.data, image_mask, image_grid_param) return this_ir_map if __name__ == "__main__": map_name = '190519_N2_L2w1_mp2' + '.map' file_name = '../../ir_data/' + map_name sample_id = file_name sample_id = sample_id.replace("../", "") sample_id = sample_id.replace(".map", "") sample_id = sample_id.replace("/", "_") sample_id = sample_id.replace(" ", "_") print(sample_id) sample_info = ir_map.sample_info(sample_id=sample_id, sample_meta_data='Hello World') data = read_and_convert(file_name, sample_info=sample_info) data.write_as_hdf5('../../ir_data/' + map_name + '.h5')