Beispiel #1
0
def create_stem_histogram(num_bins,
                          num_hist,
                          reader,
                          inner_radii,
                          outer_radii,
                          width=0,
                          height=0,
                          center_x=-1,
                          center_y=-1):
    # create stem images
    imgs = _image.create_stem_images(reader.begin(), reader.end(), inner_radii,
                                     outer_radii, width, height, center_x,
                                     center_y)

    all_bins = []
    all_freqs = []

    for in_image in imgs:
        bins = _image.get_container(in_image, num_bins)
        # each image can have numHist histograms, but all with the same bins
        freq = _image.create_stem_histogram(in_image, num_hist, num_bins, bins)
        bins = np.array(bins, copy=False)
        freq = np.array(freq, copy=False)
        all_bins.append(bins)
        all_freqs.append(freq)

    return all_bins, all_freqs
Beispiel #2
0
def create_stem_images(reader,
                       inner_radii,
                       outer_radii,
                       width=0,
                       height=0,
                       center_x=-1,
                       center_y=-1):
    imgs = _image.create_stem_images(reader.begin(), reader.end(), inner_radii,
                                     outer_radii, width, height, center_x,
                                     center_y)

    images = [np.array(img, copy=False) for img in imgs]
    return np.array(images, copy=False)
Beispiel #3
0
def create_stem_histogram(numBins,
                          reader,
                          inner_radii,
                          outer_radii,
                          scan_dimensions=(0, 0),
                          center=(-1, -1)):
    """Create a histogram of the stem images generated from the input.

    :param numBins: the number of bins the histogram should have.
    :param reader: the file reader that has already opened the data.
    :type reader: stempy.io.reader
    :param inner_radii: a list of inner radii. Must match
                        the length of `outer_radii`.
    :type inner_radii: list of ints
    :param outer_radii: a list of outer radii. Must match
                        the length of `inner_radii`.
    :type outer_radii: list of ints
    :param scan_dimensions: the dimensions of the scan, where the order is
                            (width, height). If set to (0, 0), an attempt
                            will be made to read the scan dimensions from
                            the data file.
    :type scan_dimensions: tuple of ints of length 2
    :param center: the center of the images, where the order is (x, y). If set
                   to (-1, -1), the center will be set to
                   (scan_dimensions[0] / 2, scan_dimensions[1] / 2).
    :type center: tuple of ints of length 2

    :return: The bins and the frequencies of the histogram.
    :rtype: a tuple of length 2 of lists
    """
    # create stem images
    imgs = _image.create_stem_images(reader.begin(), reader.end(), inner_radii,
                                     outer_radii, scan_dimensions, center)

    allBins = []
    allFreqs = []
    for inImage in imgs:
        bins = _image.get_container(inImage, numBins)
        freq = _image.create_stem_histogram(inImage, numBins, bins)
        bins = np.array(bins, copy=False)
        freq = np.array(freq, copy=False)
        allBins.append(bins)
        allFreqs.append(freq)

    return allBins, allFreqs
Beispiel #4
0
def create_stem_histogram(numBins,
                          reader,
                          inner_radii,
                          outer_radii,
                          width=0,
                          height=0,
                          center_x=-1,
                          center_y=-1):
    # create stem images
    imgs = _image.create_stem_images(reader.begin(), reader.end(), inner_radii,
                                     outer_radii, width, height, center_x,
                                     center_y)

    allBins = []
    allFreqs = []
    for inImage in imgs:
        bins = _image.get_container(inImage, numBins)
        freq = _image.create_stem_histogram(inImage, numBins, bins)
        bins = np.array(bins, copy=False)
        freq = np.array(freq, copy=False)
        allBins.append(bins)
        allFreqs.append(freq)

    return allBins, allFreqs
Beispiel #5
0
def create_stem_images(input,
                       inner_radii,
                       outer_radii,
                       scan_dimensions=(0, 0),
                       center=(-1, -1),
                       frame_dimensions=None,
                       frame_offset=0):
    """Create a series of stem images from the input.

    :param input: the file reader that has already opened the data, or an
                  open h5py file, or an ElectronCountedData namedtuple
                  containing the sparse data, or a SparseArray, or a
                  numpy.ndarray of either the sparse or the raw data (if the
                  frame_dimensions argument is supplied, numpy.ndarray is
                  inferred to be sparse data).
    :type input: stempy.io.reader, an h5py file, ElectronCountedData,
                 SparseArray, or numpy.ndarray
    :param inner_radii: a list of inner radii. Must match
                        the length of `outer_radii`.
    :type inner_radii: list of ints
    :param outer_radii: a list of outer radii. Must match
                        the length of `inner_radii`.
    :type outer_radii: list of ints
    :param scan_dimensions: the dimensions of the scan, where the order is
                            (width, height). If set to (0, 0), an attempt
                            will be made to read the scan dimensions from
                            the data file.
    :type scan_dimensions: tuple of ints of length 2
    :param center: the center of the images, where the order is (x, y). If set
                   to (-1, -1), the center will be set to
                   (scan_dimensions[0] / 2, scan_dimensions[1] / 2).
    :type center: tuple of ints of length 2
    :param frame_dimensions: the dimensions of each frame, where the order is
                             (width, height). Only used for input of type
                             numpy.ndarray, in which case its presence implies
                             that the input is sparse data rather than raw
                             data.
    :type frame_dimensions: tuple of ints of length 2
    :param frame_offset: the amount by which to offset the frame. Only used
                         for sparse data input of type numpy.ndarray.
    :type frame_offset: int

    :return: A numpy array of the STEM images.
    :rtype: numpy.ndarray
    """
    # Ensure the inner and outer radii are tuples or lists
    if not isinstance(inner_radii, (tuple, list)):
        inner_radii = [inner_radii]
    if not isinstance(outer_radii, (tuple, list)):
        outer_radii = [outer_radii]

    # Electron counted data attributes
    ecd_attrs = ['data', 'scan_dimensions', 'frame_dimensions']

    if isinstance(input, h5py._hl.files.File):
        # Handle h5py file
        input = get_hdf5_reader(input)
        imgs = _image.create_stem_images(input.begin(), input.end(),
                                         inner_radii, outer_radii,
                                         scan_dimensions, center)
    elif issubclass(type(input), ReaderMixin):
        # Handle standard reader
        imgs = _image.create_stem_images(input.begin(), input.end(),
                                         inner_radii, outer_radii,
                                         scan_dimensions, center)
    elif hasattr(input, '_electron_counted_data'):
        # Handle electron counted data with C++ object
        # This could also be a SparseArray created via electron_count()
        imgs = _image.create_stem_images(input._electron_counted_data,
                                         inner_radii, outer_radii, center)
    elif isinstance(input, SparseArray):
        imgs = _image.create_stem_images(input.data, inner_radii, outer_radii,
                                         input.scan_shape[::-1],
                                         input.frame_shape, center)
    elif all([hasattr(input, x) for x in ecd_attrs]):
        # Handle electron counted data without C++ object
        # Assume this is v1 data and that each frame is one scan position
        data = input.data
        if data.ndim == 1:
            data = data[:, np.newaxis]
        imgs = _image.create_stem_images(input.data, inner_radii, outer_radii,
                                         input.scan_dimensions,
                                         input.frame_dimensions, center)
    elif isinstance(input, np.ndarray):
        # The presence of frame dimensions implies it is sparse data
        if frame_dimensions is not None:
            # Handle sparse data
            if input.ndim == 1:
                input = input[:, np.newaxis]
            imgs = _image.create_stem_images(input, inner_radii, outer_radii,
                                             scan_dimensions, frame_dimensions,
                                             center)
        else:
            # Handle raw data
            # Make sure the scan dimensions were passed
            if not scan_dimensions or scan_dimensions == (0, 0):
                msg = ('scan_dimensions must be provided for np.ndarray '
                       'raw data input')
                raise Exception(msg)

            # Should have shape (num_images, frame_height, frame_width)
            num_images = input.shape[0]
            image_numbers = np.arange(num_images)
            block_size = 32
            reader = PyReader(input, image_numbers, scan_dimensions,
                              block_size, num_images)

            imgs = _image.create_stem_images(reader.begin(), reader.end(),
                                             inner_radii, outer_radii,
                                             scan_dimensions, center)
    else:
        raise Exception('Type of input, ' + str(type(input)) +
                        ', is not known to stempy.image.create_stem_images()')

    images = [np.array(img, copy=False) for img in imgs]
    return np.array(images, copy=False)