Example #1
0
def show_measurement_2d(measurement_or_func):
    try:
        measurement = measurement_or_func()
        return_callback = True

    except TypeError:
        measurement = measurement_or_func
        return_callback = False

    figure, image = create_image_figure()

    toolbar = Toolbar(figure=figure)

    figure.axes[0].label = format_label(measurement.calibrations[-2])
    figure.axes[1].label = format_label(measurement.calibrations[-1])

    figure.marks = (image, )

    if return_callback:
        update_bqplot_image(image, measurement)

        def callback(*args, **kwargs):
            update_bqplot_image(image, measurement_or_func())

        return widgets.VBox([figure, toolbar]), callback
    else:
        return widgets.VBox([figure, toolbar])
Example #2
0
def show_measurement_1d(measurement,
                        ax=None,
                        figsize=None,
                        legend=False,
                        title=None,
                        label=None,
                        **kwargs):
    """
    Show line function

    Function to display a line scan.

    Parameters
    ----------
    array : ndarray
        Array of measurement values along a line.
    calibration : calibration object
        Spatial calibration for the line.
    ax : axes object, optional
        pyplot axes object.
    title : str, optional
        Title for the plot. Default is None.
    legend : bool, optional
        Option to display a plot legend. Default is False.
    kwargs :
       Remaining keyword arguments are passed to pyplot.
    """

    calibration = measurement.calibrations[0]
    array = measurement.array
    if calibration is None:
        x = np.arange(len(array))
    else:
        x = np.linspace(calibration.offset,
                        calibration.offset + len(array) * calibration.sampling,
                        len(array))

    if ax is None:
        fig, ax = plt.subplots(figsize=figsize)

    if not label:
        label = measurement.name

    lines = ax.plot(x, array, label=label, **kwargs)
    ax.set_xlabel(format_label(calibration))
    ax.set_ylabel(format_label(measurement))

    if legend:
        ax.legend()

    if title is not None:
        ax.set_title(title)

    return ax, lines[0]
Example #3
0
def show_measurement_1d(measurements_or_func,
                        figure=None,
                        throttling=False,
                        **kwargs):
    if figure is None:
        figure = plt.figure(fig_margin={
            'top': 0,
            'bottom': 50,
            'left': 50,
            'right': 0
        })

        figure.layout.height = '250px'
        figure.layout.width = '300px'

    try:
        measurements = measurements_or_func()
        return_callback = True

    except TypeError:
        measurements = measurements_or_func
        return_callback = False

    lines = []
    for measurement, color in zip(measurements, TABLEAU_COLORS.values()):
        calibration = measurement.calibrations[0]
        array = measurement.array
        x = np.linspace(calibration.offset,
                        calibration.offset + len(array) * calibration.sampling,
                        len(array))

        line = plt.plot(x, array, colors=[color], **kwargs)
        figure.axes[0].label = format_label(measurement.calibrations[0])
        lines.append(line)

    # figure.axes[1].label = format_label(measurement)

    if return_callback:

        @throttle(throttling)
        def callback(*args, **kwargs):
            for line, measurement in zip(lines, measurements_or_func()):
                x = np.linspace(
                    calibration.offset,
                    calibration.offset + len(array) * calibration.sampling,
                    len(array))
                line.x = x
                line.y = measurement.array

        return figure, callback
    else:
        return figure
Example #4
0
def show_measurement_2d(measurement,
                        ax=None,
                        figsize=None,
                        cbar=False,
                        cbar_label=None,
                        cmap='gray',
                        discrete_cmap=False,
                        vmin=None,
                        vmax=None,
                        power=1.,
                        log_scale=False,
                        title=None,
                        equal_ticks=False,
                        is_rgb=False,
                        x_label=None,
                        y_label=None,
                        **kwargs):
    """
    Show image function

    Function to display an image.

    Parameters
    ----------
    array : ndarray
        Image array.
    calibrations : tuple of calibration objects.
        Spatial calibrations.
    ax : axes object
        pyplot axes object.
    title : str, optional
        Image title. Default is None.
    colorbar : bool, optional
        Option to show a colorbar. Default is False.
    cmap : str, optional
        Colormap name. Default is 'gray'.
    figsize : float, pair of float, optional
        Size of the figure in inches, either as a square for one number or a rectangle for two. Default is None.
    scans : ndarray, optional
        Array of scans. Default is None.
    discrete : bool, optional
        Option to discretize intensity values to integers. Default is False.
    cbar_label : str, optional
        Text label for the color bar. Default is None.
    vmin : float, optional
        Minimum of the intensity scale. Default is None.
    vmax : float, optional
        Maximum of the intensity scale. Default is None.
    kwargs :
        Remaining keyword arguments are passed to pyplot.
    """

    if ax is None:
        fig, ax = plt.subplots(figsize=figsize)

    if is_rgb:
        calibrations = measurement.calibrations[-3:-1]
    else:
        calibrations = measurement.calibrations[-2:]

    if not is_rgb:
        array = measurement.array[(0, ) * (measurement.dimensions - 2) +
                                  (slice(None), ) * 2]
    else:
        array = measurement.array[:, :, :]

    if np.iscomplexobj(array):
        array = domain_coloring(array)

    if power != 1:
        array = array**power

    if log_scale:
        array = np.log(array)

    extent = []
    for calibration, num_elem in zip(calibrations, array.shape):
        extent.append(calibration.offset)
        extent.append(calibration.offset + num_elem * calibration.sampling -
                      calibration.sampling)

    if vmin is None:
        vmin = np.min(array)
        if discrete_cmap:
            vmin -= .5

    if vmax is None:
        vmax = np.max(array)
        if discrete_cmap:
            vmax += .5

    if discrete_cmap:
        cmap = plt.get_cmap(cmap, np.max(array) - np.min(array) + 1)

    im = ax.imshow(np.swapaxes(array, 0, 1),
                   extent=extent,
                   cmap=cmap,
                   origin='lower',
                   vmin=vmin,
                   vmax=vmax,
                   interpolation='nearest',
                   **kwargs)

    if cbar:
        if cbar_label is None:
            cbar_label = format_label(measurement)

        cax = plt.colorbar(im, ax=ax, label=cbar_label)
        if discrete_cmap:
            cax.set_ticks(ticks=np.arange(np.min(array), np.max(array) + 1))

    if x_label is None:
        x_label = format_label(calibrations[-2])

    if y_label is None:
        y_label = format_label(calibrations[-1])

    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)

    if title is not None:
        ax.set_title(title)
    elif len(measurement.array.shape) > 2:
        if any([n > 1 for n in measurement.array.shape[:-2]]):
            ax.set_title(
                f'Slice {(0,) * (len(measurement.array.shape) - 2)} of {measurement.array.shape} measurement'
            )

    if equal_ticks:
        d = max(np.diff(ax.get_xticks())[0], np.diff(ax.get_yticks())[0])
        xticks = np.arange(*ax.get_xlim(), d)
        yticks = np.arange(*ax.get_ylim(), d)
        ax.set_xticks(xticks)
        ax.set_yticks(yticks)

    return ax, im
Example #5
0
def show_measurement_2d(measurement,
                        ax=None,
                        figsize=None,
                        colorbar=False,
                        cmap='gray',
                        discrete_cmap=False,
                        vmin=None,
                        vmax=None,
                        power=1.,
                        log_scale=False,
                        title=None,
                        **kwargs):
    """
    Show image function

    Function to display an image.

    Parameters
    ----------
    array : ndarray
        Image array.
    calibrations : tuple of calibration objects.
        Spatial calibrations.
    ax : axes object
        pyplot axes object.
    title : str, optional
        Image title. Default is None.
    colorbar : bool, optional
        Option to show a colorbar. Default is False.
    cmap : str, optional
        Colormap name. Default is 'gray'.
    figsize : float, pair of float, optional
        Size of the figure in inches, either as a square for one number or a rectangle for two. Default is None.
    scans : ndarray, optional
        Array of scans. Default is None.
    discrete : bool, optional
        Option to discretize intensity values to integers. Default is False.
    cbar_label : str, optional
        Text label for the color bar. Default is None.
    vmin : float, optional
        Minimum of the intensity scale. Default is None.
    vmax : float, optional
        Maximum of the intensity scale. Default is None.
    kwargs :
        Remaining keyword arguments are passed to pyplot.
    """

    if ax is None:
        fig, ax = plt.subplots(figsize=figsize)

    calibrations = measurement.calibrations[-2:]
    array = measurement.array[(0, ) * (measurement.dimensions - 2) +
                              (slice(None), ) * 2]

    if power != 1:
        array = array**power

    if log_scale:
        array = np.log(array)

    extent = []
    for calibration, num_elem in zip(calibrations, array.shape):
        extent.append(calibration.offset)
        extent.append(calibration.offset + num_elem * calibration.sampling -
                      calibration.sampling)

    if vmin is None:
        vmin = np.min(array)
        if discrete_cmap:
            vmin -= .5

    if vmax is None:
        vmax = np.max(array)
        if discrete_cmap:
            vmax += .5

    if discrete_cmap:
        cmap = plt.get_cmap(cmap, np.max(array) - np.min(array) + 1)

    im = ax.imshow(array.T,
                   extent=extent,
                   cmap=cmap,
                   origin='lower',
                   vmin=vmin,
                   vmax=vmax,
                   interpolation='nearest',
                   **kwargs)

    if colorbar:
        cax = plt.colorbar(im, ax=ax, label=format_label(measurement))
        if discrete_cmap:
            cax.set_ticks(ticks=np.arange(np.min(array), np.max(array) + 1))

    ax.set_xlabel(format_label(calibrations[-2]))
    ax.set_ylabel(format_label(calibrations[-1]))

    if title is not None:
        ax.set_title(title)

    return ax, im