def main(input_path, scan_num): if input_path is None: if scan_num is None: raise click.ClickException('Please provide scan number') input_path = Path(f'/mnt/hdd1/data_scan{scan_num}_th4_electrons.h5') # Load the electron counted data ee = stio.load_electron_counts(str(input_path)) # Create STEM images with inner and outer radii ims = stim.create_stem_images(ee, (0, 0, 110, 220), (110, 220, 240, 288), center=(307, 282)) # Calculate summed diffraction pattern dp = stim.calculate_sum_sparse(ee.data, ee.frame_dimensions)
max = np.max(stem_image_data) stem_image_data = stem_image_data.reshape((160, 160)) stem_image_data = np.interp(stem_image_data, [min, max], [0, 256]) stem_image_data = stem_image_data.astype(np.uint8) img = Image.fromarray(stem_image_data) img.save(name) stem_image_data_day = np.zeros((160 * 160, ), dtype=float) stem_image_data_night = np.zeros((160 * 160, ), dtype=float) mask_size = 20 files = [] for f in glob.glob('/data/4dstem/smallScanningDiffraction/data*.dat'): files.append(f) inner_radii = [0, 40] outer_radii = [288, 288] reader = io.reader(files) imgs = image.create_stem_images(reader, inner_radii, outer_radii, width=160, height=160) for i, img in enumerate(imgs): suffix = str(inner_radii[i]) + '_' + str(outer_radii[i]) + '.png' save_img(img, 'img_' + suffix)
min = np.min(stem_image_data) max = np.max(stem_image_data) stem_image_data = stem_image_data.reshape(scan_dimensions) stem_image_data = np.interp(stem_image_data, [min, max], [0, 256]) stem_image_data = stem_image_data.astype(np.uint8) img = Image.fromarray(stem_image_data) img.save(name) with h5py.File('stem_image.h5', 'r') as rf: frames = rf['/electron_events/frames'][()] attrs = rf['/electron_events/frames'].attrs frame_dimensions = (attrs['Nx'], attrs['Ny']) attrs = rf['/electron_events/scan_positions'].attrs scan_dimensions = (attrs['Nx'], attrs['Ny']) num_pixels = frame_dimensions[0] * frame_dimensions[1] inner_radius = 40 outer_radius = 288 img = image.create_stem_images(frames, inner_radius, outer_radius, scan_dimensions, frame_dimensions=frame_dimensions)[0] save_img(img, 'img.png', scan_dimensions)
def main(files, center, inner_radii, outer_radii): center = center.split(',') if len(center) != 2: raise click.ClickException( 'Center must be of the form: center_x,center_y.') center = tuple(int(x) for x in center) inner_radii = inner_radii.split(',') outer_radii = outer_radii.split(',') if len(inner_radii) != len(outer_radii): raise click.ClickException( 'Number of inner and outer radii must match') inner_radii = [int(x) for x in inner_radii] outer_radii = [int(x) for x in outer_radii] comm = MPI.COMM_WORLD rank = comm.Get_rank() world_size = comm.Get_size() comm.Barrier() start = MPI.Wtime() files = get_files(files) # Create local sum reader = io.reader(files, version=io.FileVersion.VERSION3) # Get the scan image size block = reader.read() scan_dimensions = block.header.scan_dimensions reader.reset() local_stems = image.create_stem_images(reader, inner_radii, outer_radii, center=center) # Now reduce to root global_stems = [ np.zeros(scan_dimensions[0] * scan_dimensions[1], dtype='uint64') for _ in range(len(inner_radii)) ] for i in range(len(inner_radii)): comm.Reduce(local_stems[i], global_stems[i], op=MPI.SUM) # Save out the image if rank == 0: for global_stem, inner, outer in zip(global_stems, inner_radii, outer_radii): thr = global_stem[:] > 0 vmin = global_stem[thr].min() cmap = plt.cm.viridis norm = plt.Normalize(vmin=vmin) stem_image = cmap( norm( global_stem.reshape(scan_dimensions[1], scan_dimensions[0]))) filename = 'stem_%d_%d.png' % (inner, outer) plt.imsave(filename, stem_image) comm.Barrier() end = MPI.Wtime() if rank == 0: print('time: %s' % (end - start))
def make_stem_hdf5(files, dark_sample, width, height, inner_radius, outer_radius, reader_version, dark_reader_version, save_raw, zip_raw, output): """Make an HDF5 file containing a STEM image Example: "python create_hdf5.py -d darksample.dat /path/to/data/data*.dat" """ if len(files) == 0: sys.exit('No files found') if reader_version == 1: reader_version = io.FileVersion.VERSION1 elif reader_version == 2: reader_version = io.FileVersion.VERSION2 else: sys.exit('Unknown reader version:', reader_version) if dark_reader_version == 1: dark_reader_version = io.FileVersion.VERSION1 elif dark_reader_version == 2: dark_reader_version = io.FileVersion.VERSION2 else: sys.exit('Unknown dark reader version:', dark_reader_version) scan_dimensions = (width, height) if dark_sample: reader = io.reader(dark_sample, version=dark_reader_version) dark = image.calculate_average(reader) else: # Get the frame dimensions from a block header, and use zeros # for the dark sample. reader = io.reader(files, version=reader_version) frame_dimensions = reader.read().header.frame_dimensions dark = np.zeros(frame_dimensions) reader = io.reader(files, version=reader_version) data = image.electron_count(reader, dark, scan_dimensions=scan_dimensions) frame_dimensions = data.frame_dimensions inner_radii = [0, inner_radius] outer_radii = [outer_radius, outer_radius] names = ['bright', 'dark'] reader.reset() imgs = image.create_stem_images(reader, inner_radii, outer_radii, scan_dimensions=scan_dimensions) io.save_electron_counts(output, data) io.save_stem_images(output, imgs, names) if save_raw: reader.reset() # In order to avoid two copies of the data, we must allocate # space for the large numpy array first. raw_data = np.zeros((np.prod(scan_dimensions), frame_dimensions[1], frame_dimensions[0]), dtype=np.uint16) # Make sure the frames are sorted in order for block in reader: for i in range(len(block.header.image_numbers)): num = block.header.image_numbers[i] raw_data[num] = block.data[i] io.save_raw_data(output, raw_data, zip_data=zip_raw)
from stempy import io, image import numpy as np from PIL import Image def save_img(stem_image_data, name): min = np.min(stem_image_data) max = np.max(stem_image_data) stem_image_data = stem_image_data.reshape((160, 160)) stem_image_data = np.interp(stem_image_data, [min, max], [0, 256]) stem_image_data = stem_image_data.astype(np.uint8) img = Image.fromarray(stem_image_data) img.save(name) if(len(sys.argv)!=2): print ("python3 reader_hdf5.py <file name>") exit(0) filename=sys.argv[1] h5file= h5py.File(filename, 'r') inner_radii = [0, 40] outer_radii = [288, 288] imgs = image.create_stem_images(h5file, inner_radii, outer_radii, scan_dimensions=(160, 160)) for i, img in enumerate(imgs): suffix = str(inner_radii[i]) + '_' + str(outer_radii[i]) + '.png' save_img(img, 'img_' + suffix)
def make_stem_hdf5(files, dark_sample, width, height, inner_radius, outer_radius, reader_version, dark_reader_version, save_raw, zip_raw, output): """Make an HDF5 file containing a STEM image Example: "python create_hdf5.py -d darksample.dat /path/to/data/data*.dat" """ if len(files) == 0: sys.exit('No files found') if reader_version == 1: reader_version = io.FileVersion.VERSION1 elif reader_version == 2: reader_version = io.FileVersion.VERSION2 else: sys.exit('Unknown reader version:', reader_version) if dark_reader_version == 1: dark_reader_version = io.FileVersion.VERSION1 elif dark_reader_version == 2: dark_reader_version = io.FileVersion.VERSION2 else: sys.exit('Unknown dark reader version:', dark_reader_version) reader = io.reader(dark_sample, version=dark_reader_version) dark = image.calculate_average(reader) reader = io.reader(files, version=reader_version) frame_events = image.electron_count(reader, dark, scan_width=width, scan_height=height) # Read one block in to get the detector frames reader.reset() block = reader.read() detector_nx = block.header.frame_width detector_ny = block.header.frame_height inner_radii = [0, inner_radius] outer_radii = [outer_radius, outer_radius] names = ['bright', 'dark'] reader.reset() imgs = image.create_stem_images(reader, inner_radii, outer_radii, width=width, height=height) io.save_electron_counts(output, frame_events, width, height, detector_nx, detector_ny) io.save_stem_images(output, imgs, names) if save_raw: reader.reset() blocks = [block for block in reader] raw_data = np.concatenate([block.data for block in blocks]) io.save_raw_data(output, raw_data, zip_raw)
def main(files, dark_file, center, inner_radii, outer_radii, output_file, generate_sparse): center = center.split(',') if len(center) != 2: msg = 'Center must be of the form: center_x,center_y.' raise click.ClickException(msg) center = tuple(int(x) for x in center) inner_radii = inner_radii.split(',') outer_radii = outer_radii.split(',') if len(inner_radii) != len(outer_radii): msg = 'Number of inner and outer radii must match' raise click.ClickException(msg) inner_radii = [int(x) for x in inner_radii] outer_radii = [int(x) for x in outer_radii] comm = MPI.COMM_WORLD rank = comm.Get_rank() world_size = comm.Get_size() if (world_size > len(files)): if rank == 0: print('Error: number of MPI processes,', world_size, ', exceeds', 'the number of files:', len(files)) return comm.Barrier() start = MPI.Wtime() if dark_file is not None: # Every process will do the dark field reference average for now reader = io.reader(dark_file, version=io.FileVersion.VERSION3) dark = image.calculate_average(reader) else: dark = np.zeros((576, 576)) # Split up the files among processes files = get_files(files) # Create local electron count reader = io.reader(files, version=io.FileVersion.VERSION3) electron_counted_data = image.electron_count(reader, dark, verbose=True) local_frame_events = electron_counted_data.data # Now reduce to root global_frame_events = reduce_to_root_method1(local_frame_events) # global_frame_events = reduce_to_root_method2(local_frame_events) comm.Barrier() end = MPI.Wtime() if rank == 0: print('time: %s' % (end - start)) if rank == 0: # Create new electron counted data with the global frame events data = namedtuple('ElectronCountedData', ['data', 'scan_dimensions', 'frame_dimensions']) data.data = global_frame_events data.scan_dimensions = electron_counted_data.scan_dimensions data.frame_dimensions = electron_counted_data.frame_dimensions # Write out the HDF5 file io.save_electron_counts(output_file, data) if generate_sparse: # Save out the sparse image stem_imgs = image.create_stem_images(data, inner_radii, outer_radii, center=center) for i, img in enumerate(stem_imgs): fig, ax = plt.subplots(figsize=(12, 12)) ax.matshow(img) name = 'sparse_stem_image_' + str(i) + '.png' plt.savefig(name, dpi=300)