def save_numpy(array, fname): """ Saves a numpy array to a file in MRC, EM or VTI format. Args: array (numpy.ndarray): input array fname (str): full path to the tomogram, has to end with '.mrc', '.em' or '.vti' Returns: None """ _, ext = os.path.splitext(fname) # Parse input array for fulfilling format requirements if (ext == '.mrc') or (ext == '.em'): if ((array.dtype != 'ubyte') and (array.dtype != 'int16') and (array.dtype != 'float32')): array = array.astype('float32') # if (len(array.shape) == 3) and (array.shape[2] == 1): # array = np.reshape(array, (array.shape[0], array.shape[1])) if ext == '.vti': pname, fnameh = os.path.split(fname) save_vti(numpy_to_vti(array), fnameh, pname) # elif ext == '.fits': # warnings.resetwarnings() # warnings.filterwarnings('ignore', category=UserWarning, append=True) # pyfits.writeto(fname, array, clobber=True, output_verify='silentfix') # warnings.resetwarnings() # warnings.filterwarnings('always', category=UserWarning, append=True) elif ext == '.mrc': img = ImageIO() # img.setData(np.transpose(array, (1, 0, 2))) img.setData(array) img.writeMRC(fname) elif ext == '.em': img = ImageIO() # img.setData(np.transpose(array, (1, 0, 2))) img.setData(array) img.writeEM(fname) else: error_msg = 'Format not valid %s.' % ext raise pexceptions.PySegInputError(expr='save_numpy', msg=error_msg)
def save_numpy(array, fname): """ Saves a numpy array to a file in MRC, EM or VTI format. Args: array (numpy.ndarray): input array fname (str): full path to the tomogram, has to end with '.mrc', '.em' or '.vti' Returns: None """ _, ext = os.path.splitext(fname) # Parse input array for fulfilling format requirements if (ext == '.mrc') or (ext == '.em'): if ((array.dtype != 'ubyte') and (array.dtype != 'int16') and (array.dtype != 'float32')): array = array.astype('float32') # if (len(array.shape) == 3) and (array.shape[2] == 1): # array = np.reshape(array, (array.shape[0], array.shape[1])) if ext == '.vti': pname, fnameh = os.path.split(fname) save_vti(numpy_to_vti(array), fnameh, pname) elif ext == '.mrc': img = ImageIO() # img.setData(np.transpose(array, (1, 0, 2))) img.setData(array) img.writeMRC(fname) elif ext == '.em': img = ImageIO() # img.setData(np.transpose(array, (1, 0, 2))) img.setData(array) img.writeEM(fname) else: raise pexceptions.PySegInputError( expr='save_numpy', msg='Format not valid {}.'.format(ext))