def dm32h5stack(mrcdir, h5dir): """Takes in directory where dm3s containing individual images are and directory where h5 should go. Stacks all mrcs into one h5 file. Also returns stacked images and filenames""" mrcd = mrcdir + '/*.dm3' filenames = sorted(glob(mrcd)) lenfilename = len(filenames[0]) for f in filenames: if len(f) != lenfilename: print( 'changing length of file names indicates that files do not have ordered, decimal numbering' ) break d = datetime.datetime.today() d = d.strftime('%Y%m%d') h5name = h5dir + '/' + mrcdir.split('/')[-1] + '_' + d + '.h5' array = [] for idx in np.arange(0, len(filenames), 1): struct = dm.dmReader(filenames[idx]) data = struct['data'] data = np.expand_dims(data, axis=3) array.append(data) array = np.asanyarray(array) if len(array.shape) == 5: print('Had to reshape array because its shape is: ', array.shape) array = array.reshape(array.shape[0], array.shape[2], array.shape[3], array.shape[4]) i = h5py.File(h5name, 'w') h5data = i.create_dataset( 'images', (array.shape[0], array.shape[1], array.shape[2], array.shape[3]), data=array) h5data.attrs['files'] = np.array(filenames, dtype='S') print('Done!') return (array, filenames)
def save_for_labeller(directory, dm3file): stack = dm.dmReader(dm3file) warnings.filterwarnings("ignore") if os.path.isdir(directory) == False: os.mkdir(directory) fname_base = directory + '/' + stack['filename'].split('.')[0] if len(stack['data'].shape) == 2: fname = fname_base + '.png' io.imsave(fname, img) else: for idx, img in enumerate(stack['data']): fname = fname_base + '_' + str(idx) + '.png' imsave(fname, median_filter(img)) print('done!')
dm4_filepath = 'Capture25.dm4' # Parameters for dark reference determination drwidth = 100 # Parameters for electron counting Nsamples = 40 thresh_bkgrnd_Nsigma = 4 thresh_xray_Nsigma = 30 binfactor = 1 subpixel = False output = 'pointlist' # Get memory mapped 4D datacube from dm file datacube = dm.dmReader(dm4_filepath, dSetNum=0, verbose=False)['data'] datacube = np.moveaxis(datacube, (0, 1), (2, 3)) # Get dark reference darkreference = 1 # TODO: get_darkreference(datacube = ...! electron_counted_data = electron_count( datacube, darkreference, Nsamples=Nsamples, thresh_bkgrnd_Nsigma=thresh_bkgrnd_Nsigma, thresh_xray_Nsigma=thresh_xray_Nsigma, binfactor=binfactor, sub_pixel=True, output='pointlist')
dm4_filepath = 'Capture25.dm4' # Parameters for dark reference determination drwidth = 100 # Parameters for electron counting Nsamples = 40 thresh_bkgrnd_Nsigma = 4 thresh_xray_Nsigma = 30 binfactor = 1 subpixel = False output = 'pointlist' # Get memory mapped 4D datacube from dm file datacube = dm.dmReader(dm4filename,dSetNum=0,verbose=False)['data'] datacube = np.moveaxis(datacube,(0,1),(2,3)) # Get dark reference darkreference = 1 # TODO: get_darkreference(datacube = ...! electron_counted_data = electron_count(datacube, darkreference, Nsamples=Nsamples, thresh_bkgrnd_Nsigma=thresh_bkgrnd_Nsigma, thresh_xray_Nsigma=thresh_xray_Nsigma, binfactor=binfactor, sub_pixel=True, output='pointlist') # For outputting datacubes, wrap counted into a py4DSTEM DataCube if output=='datacube': electron_counted_data = DataCube(data=counted)