def load_LEEM(self): """ Load raw binary LEEM-IV data to a 3d numpy array emit the numpy array as a custom SIGNAL to be retrieved in gui.py :return: """ # requires params: path, imht, imwd if ( 'path' not in self.params.keys() or 'imht' not in self.params.keys() or 'imwd' not in self.params.keys()): print('Terminating - ERROR: incorrect parameters for LOAD task') print('Required Parameters: path, imht, imwd') self.quit() self.exit() if 'bits' not in self.params.keys(): # if bit size is not specified, use default values in process_LEEM_Data() self.params['bits'] = None if 'byte' not in self.params.keys(): self.params['byte'] = 'L' # default to Little Endian # load raw data dat_3d = LF.process_LEEM_Data(dirname=self.params['path'], ht=self.params['imht'], wd=self.params['imwd'], bits=self.params['bits'], byte=self.params['byte']) # emit output signal with np array as generic pyobject type self.emit(QtCore.SIGNAL('output(PyQt_PyObject)'), dat_3d)
def load_LEED_Images(self): """ Load LEED data from image files Supported formats are TIFF, PNG, JPG emit the 3d data array as a custom SIGNAL to be retrieved in gui.py """ if ('path' not in self.params.keys() or 'ext' not in self.params.keys()): print('Terminating - ERROR: incorrect parameters for LOAD task') print('Required Parameters: path, ext') print('Loading LEED Data from Images via QThread ...') """ if 'byte' in self.params.keys(): if self.params['byte'] == 'L': swap = False elif self.params['byte'] == 'B': print("Byte order set as Big-Endian - swapping order when plotting data ...") swap = True else: swap = False print("Error reading byte order from experimental config ...") """ data = LF.get_img_array(self.params['path'], ext=self.params['ext'], swap=False) if data is None: self.quit() self.exit() else: self.emit(QtCore.SIGNAL('output(PyQt_PyObject)'), data)
def test_16bitTiff(self): """Test LF.read_img() with 16bit TIFF.""" dtype = self.dtypes[1] self.createImage(dtype, self.imtypes[0]) files = glob.glob(os.path.join(self.test_data_path, "*.tif")) for fl in files: im = LF.read_img(os.path.join(self.test_data_path, fl)) # NOTE: This should fail since read_img() uses .convert('L') which downscales to 8-bit self.assertTrue(im.dtype == dtype)
def test_8bit_images(self): """Test LF.read_img with 8bit images.""" dtype = self.dtypes[0] for imtype in self.imtypes: self.createImage(dtype, imtype) files = glob.glob(os.path.join(self.test_data_path, "*.*")) for fl in files: im = LF.read_img(os.path.join(self.test_data_path, fl)) self.assertTrue(im.dtype == dtype)
def load_LEEM(self): """Load raw binary LEEM-IV data to a 3d numpy array. Emit the numpy array as a custom SIGNAL to be retrieved in gui.py :return: None """ # requires params: path, imht, imwd if ('path' not in self.params.keys() or ('imht' not in self.params.keys()) or ('imwd' not in self.params.keys())): print('Terminating - ERROR: incorrect parameters for LOAD task') print('Required Parameters: path, imht, imwd') self.quit() self.exit() if 'bits' not in self.params.keys(): # if bit size is not specified, use default values in process_LEEM_Data() self.params['bits'] = None if 'byte' not in self.params.keys(): self.params['byte'] = 'L' # default to Little Endian # load raw data dat_3d = None try: dat_3d = LF.process_LEEM_Data(dirname=self.params['path'], ht=self.params['imht'], wd=self.params['imwd'], bits=self.params['bits'], byte=self.params['byte']) except IOError as e: print("Error Loading LEEM Data:") print(e) print( "LEEM data files not found in directory specified in YAML experiment config file." ) print( "Please re-check the settings in your YAML experiment config file." ) print( "Ensure that the path setting points to the correct directory." ) return except LF.InvalidParameterError as e: print(e.message) return if dat_3d is None: self.quit() self.exit() else: self.outputSIGNAL.emit(dat_3d) # type: np.ndarray
def processInputFiles(self): if self.image_type == "TIFF": exts = [".tiff", ".TIFF", ".tif", ".TIF"] elif self.image_type == "PNG": exts = [".png", ".PNG"] else: print("Error: Unknown Image Type {}".format(self.image_type)) return False self.files = [] for ext in exts: for name in os.listdir(self.indir): if name.endswith(ext): self.files.append(name) if not self.files: print("Error: No Files found in directory {} with extensions {}". format(self.indir, exts)) return False if self.image_type in ['.tif', '.tiff', '.TIF', '.TIFF']: try: print('Parsing file {0}'.format( os.path.join(self.indir, self.files[0]))) self.byte_order = LF.parse_tiff_header( os.path.join(self.indir, self.files[0]), self.widthw, self.heighth, self.bit_depth) except LF.ParseError as e: print( "Failed to parse tiff header; defaulting to big endian bye order" ) print(e.message) print(e.errors) byte_order = 'B' # default to big endian else: # PNG and JPEG always use Big Endian self.byte_order = 'B' # default to big endian # swap to numpy syntax if self.byte_order == 'L': self.byte_order = '<' elif self.byte_order == 'B': self.byte_order = '>' if self.bit_depth == '16-bit': self.bytes_per_pixel = 2 elif self.bit_depth == '8-bit': self.bytes_per_pixel = 1 else: print( "Error: Unknown image bit_depth. Only 8bit and 16-bit images can be processed." ) return False # All settings are ready to output files return True
def load_LEED_TIFF(self, dirname): """ Load LEED data from TIFF files into 3d numpy array This is the slowest data loading method There is much overhead in processing tiff image files This function should only be called from load_LEED_Data() :param dirname: path to directory containing tiff files :return: 3d numpy array where 3rd axis corresponds to Energy via self.elist """ # maybe not needed prev_dir = self.data_dir return np.array(LF.get_img_array(dirname, ext='.tif'))
def load_LEED_PNG(self, dirname): """ Load LEED-I(V) image files into numpy array This function uses PIL to parse the png files This is slower than loading raw binary data due to the overhead in parsing image files, however, it is faster than loading TIFF files :param dirname: path to directory containing png files :return: 3d numpy array """ # maybe not needed prev_dir = self.data_dir return np.array(LF.get_img_array(dirname, ext='.png'))
def processInputFiles(self): if self.image_type == "TIFF": exts = [".tiff", ".TIFF", ".tif", ".TIF"] elif self.image_type == "PNG": exts = [".png", ".PNG"] else: print("Error: Unknown Image Type {}".format(self.image_type)) return False self.files = [] for ext in exts: for name in os.listdir(self.indir): if name.endswith(ext): self.files.append(name) if not self.files: print("Error: No Files found in directory {} with extensions {}".format(self.indir, exts)) return False if self.image_type in ['.tif', '.tiff', '.TIF', '.TIFF']: try: print('Parsing file {0}'.format(os.path.join(self.indir, self.files[0]))) self.byte_order = LF.parse_tiff_header(os.path.join(self.indir, self.files[0]), self.widthw, self.heighth, self.bit_depth) except LF.ParseError as e: print("Failed to parse tiff header; defaulting to big endian bye order") print(e.message) print(e.errors) byte_order = 'B' # default to big endian else: # PNG and JPEG always use Big Endian self.byte_order = 'B' # default to big endian # swap to numpy syntax if self.byte_order == 'L': self.byte_order = '<' elif self.byte_order == 'B': self.byte_order = '>' if self.bit_depth == '16-bit': self.bytes_per_pixel = 2 elif self.bit_depth == '8-bit': self.bytes_per_pixel = 1 else: print("Error: Unknown image bit_depth. Only 8bit and 16-bit images can be processed.") return False # All settings are ready to output files return True
def load_LEED_Images(self): """Load LEED data from image files. Supported formats are TIFF, PNG, JPG Emit the 3d data array as a custom SIGNAL to be retrieved in please.py """ if ('path' not in self.params.keys() or ('ext' not in self.params.keys())): print('Terminating - ERROR: incorrect parameters for LOAD task') print('Required Parameters: path, ext') print('Loading LEED Data from Images via QThread ...') """ if 'byte' in self.params.keys(): if self.params['byte'] == 'L': swap = False elif self.params['byte'] == 'B': print("Byte order set as Big-Endian - swapping order when plotting data ...") swap = True else: swap = False print("Error reading byte order from experimental config ...") """ data = None try: data = LF.get_img_array(self.params['path'], ext=self.params['ext'], swap=False) except IOError as e: print("Error Loading LEED Images:") print(e) print( "LEED image files not found in directory specified in YAML experiment config file." ) print( "Please re-check the settings in your YAML experiment config file." ) print( "Ensure that the path setting points to the correct directory." ) return if data is None: self.quit() self.exit() else: self.outputSIGNAL.emit(data) # type: np.ndarray
def load_LEED_RAW(self, dirname): """ Load RAW binary files into 3d numpy array This is the fastest method for loading data This should only be called from load_LEED_Data() :return: 3d numpy array """ # maybe not needed prev_dir = self.data_dir files = [name for name in os.listdir(dirname) if name.endswith('.dat')] print('Number of Files found: {}'.format(len(files))) if self.ht >= 2 and self.wd >= 2: # image parameters were correctly set through GUI return LF.process_LEEM_Data(dirname, self.ht, self.wd) else: # image parameters were not set through GUI print('!Warning: Invalid Image Parameters while loading RAW data!') print('Returning Empty array ...') return np.zeros((500, 500, 100)) # placeholder array
def test_read_images(self): """Use LF.read_img to read the images created by createImage().""" files = glob.glob(os.path.join(self.test_data_path, "*.*")) for fl in files: im = LF.read_img(os.path.join(self.test_data_path, fl)) tmp = fl.split("_") dtype_from_image_name = None for typ in self.dtype_strings: if typ in tmp: dtype_from_image_name = typ if not dtype_from_image_name: print("Error reading image dtype from file name") break idx = self.dtype_strings.index(dtype_from_image_name) dtype = self.dtypes[idx] print("Testing dtype from image {}".format(fl)) print("\t" + str(im.dtype)) print("\t" + str(dtype_from_image_name)) self.assertTrue(im.dtype == dtype)
def load_LEEM_Images(self): """ """ if ('path' not in self.params.keys() and 'ext' not in self.params.keys()): print('Terminating - ERROR: incorrect parameters for LOAD task') print('Required Parameters: path, ext') print('Loading LEEM Data from Images via QThread ...') try: data = LF.get_img_array(self.params['path'], ext=self.params['ext']) except IOError as e: print(e) print('Error occurred while loading LEEM data from images using a QThread') return except ValueError as e: print(e) print('Error occurred while loading LEEM data from images using a QThread') return self.emit(QtCore.SIGNAL('output(PyQt_PyObject)'), data)
def load_LEEM_Images(self): """Load LEEM data from image files. Supported formats are TIFF, PNG, JPG Emit the 3d data array as a custom SIGNAL to be retrieved in please.py """ if ('path' not in self.params.keys() and ('ext' not in self.params.keys())): print('Terminating - ERROR: incorrect parameters for LOAD task') print('Required Parameters: path, ext') print('Loading LEEM Data from Images via QThread ...') data = None try: data = LF.get_img_array(self.params['path'], ext=self.params['ext']) except IOError as e: print("Error Loading LEEM Experiment:") print(e) print( "LEEM data files not found in directory specified in YAML experiment config file." ) print( "Please re-check the settings in your YAML experiment config file." ) print( "Ensure that the path setting points to the correct directory." ) return except ValueError as e: print(e) print( 'Error occurred while loading LEEM data from images using a QThread' ) return if data is None: self.quit() self.exit() else: self.outputSIGNAL.emit(data) # type: np.ndarray