Esempio n. 1
0
    def create_charts(self):
        self.epoch_slider = IntSlider(description='Epoch:',
                                      min=1,
                                      max=self.num_epochs,
                                      value=1)
        self.mode_dd = Dropdown(
            description='View',
            options=['Weights', 'Gradients', 'Activations'],
            value='Weights')
        self.update_btn = Button(description='Update')

        self.bar_figure = plt.figure()
        self.bar_plot = plt.bar([], [], scales={'x': OrdinalScale()})

        self.hist_figure = plt.figure(title='Histogram of Activations')
        self.hist_plot = plt.hist([], bins=20)

        self.controls = HBox(
            [self.epoch_slider, self.mode_dd, self.update_btn])
        self.graph.tooltip = self.bar_figure
Esempio n. 2
0
def feature_histogram(features,
                      property,
                      maxBuckets=None,
                      minBucketWidth=None,
                      **kwargs):
    """
    Generates a Chart from a set of features.
    Computes and plots a histogram of the given property.
    - X-axis = Histogram buckets (of property value).
    - Y-axis = Frequency

    Reference:
    https://developers.google.com/earth-engine/guides/charts_feature#uichartfeaturehistogram

    Args:
        features  (ee.FeatureCollection): The features to include in the chart.
        property                   (str): The name of the property to generate the histogram for.
        maxBuckets       (int, optional): The maximum number of buckets (bins) to use when building a histogram;
                                          will be rounded up to a power of 2.
        minBucketWidth (float, optional): The minimum histogram bucket width, or null to allow any power of 2.

    Raises:
        Exception: If the provided xProperties is not a list or dict.
        Exception: If the chart fails to create.
    """
    import math

    if not isinstance(features, ee.FeatureCollection):
        raise Exception("features must be an ee.FeatureCollection")

    first = features.first()
    props = first.propertyNames().getInfo()
    if property not in props:
        raise Exception(
            f"property {property} not found. Available properties: {', '.join(props)}"
        )

    def nextPowerOf2(n):
        return pow(2, math.ceil(math.log2(n)))

    def grow_bin(bin_size, ref):
        while bin_size < ref:
            bin_size *= 2
        return bin_size

    try:

        raw_data = pd.to_numeric(
            pd.Series(features.aggregate_array(property).getInfo()))
        y_data = raw_data.tolist()

        if "ylim" in kwargs:
            min_value = kwargs["ylim"][0]
            max_value = kwargs["ylim"][1]
        else:
            min_value = raw_data.min()
            max_value = raw_data.max()

        data_range = max_value - min_value

        if not maxBuckets:
            initial_bin_size = nextPowerOf2(data_range / pow(2, 8))
            if minBucketWidth:
                if minBucketWidth < initial_bin_size:
                    bin_size = grow_bin(minBucketWidth, initial_bin_size)
                else:
                    bin_size = minBucketWidth
            else:
                bin_size = initial_bin_size
        else:
            initial_bin_size = math.ceil(data_range / nextPowerOf2(maxBuckets))
            if minBucketWidth:
                if minBucketWidth < initial_bin_size:
                    bin_size = grow_bin(minBucketWidth, initial_bin_size)
                else:
                    bin_size = minBucketWidth
            else:
                bin_size = initial_bin_size

        start_bins = (math.floor(min_value / bin_size) *
                      bin_size) - (bin_size / 2)
        end_bins = (math.ceil(max_value / bin_size) * bin_size) + (bin_size /
                                                                   2)

        if start_bins < min_value:
            y_data.append(start_bins)
        else:
            y_data[y_data.index(min_value)] = start_bins
        if end_bins > max_value:
            y_data.append(end_bins)
        else:
            y_data[y_data.index(max_value)] = end_bins

        num_bins = math.floor((end_bins - start_bins) / bin_size)

        if "title" not in kwargs:
            title = ""
        else:
            title = kwargs["title"]

        fig = plt.figure(title=title)

        if "width" in kwargs:
            fig.layout.width = kwargs["width"]
        if "height" in kwargs:
            fig.layout.height = kwargs["height"]

        if "xlabel" not in kwargs:
            xlabel = ""
        else:
            xlabel = kwargs["xlabel"]

        if "ylabel" not in kwargs:
            ylabel = ""
        else:
            ylabel = kwargs["ylabel"]

        histogram = plt.hist(
            sample=y_data,
            bins=num_bins,
            axes_options={
                "count": {
                    "label": ylabel
                },
                "sample": {
                    "label": xlabel
                }
            },
        )

        if "colors" in kwargs:
            histogram.colors = kwargs["colors"]
        if "stroke" in kwargs:
            histogram.stroke = kwargs["stroke"]
        else:
            histogram.stroke = "#ffffff00"
        if "stroke_width" in kwargs:
            histogram.stroke_width = kwargs["stroke_width"]
        else:
            histogram.stroke_width = 0

        if ("xlabel" in kwargs) and ("ylabel" in kwargs):
            histogram.tooltip = Tooltip(
                fields=["midpoint", "count"],
                labels=[kwargs["xlabel"], kwargs["ylabel"]],
            )
        else:
            histogram.tooltip = Tooltip(fields=["midpoint", "count"])
        plt.show()

    except Exception as e:
        raise Exception(e)
# coding: utf-8

from bqplot import pyplot as plt
import ipyvuetify as v
import ipywidgets as widgets
import numpy as np

# generate some fake data
np.random.seed(0)
n = 2000
x = np.linspace(0.0, 10.0, n)
y = np.cumsum(np.random.randn(n)*10).astype(int)

# create a bqplot figure
fig_hist = plt.figure(title='Histogram')
hist = plt.hist(y, bins=25)

# slider
slider = v.Slider(thumb_label='always', class_="px-4", v_model=30)
widgets.link((slider, 'v_model'), (hist, 'bins'))

fig_lines = plt.figure( title='Line Chart')
lines = plt.plot(x, y)

# even handling
selector = plt.brush_int_selector()
def update_range(*ignore):
    if selector.selected is not None and len(selector.selected) == 2:
        xmin, xmax = selector.selected
        mask = (x > xmin) & (x < xmax)
        hist.sample = y[mask]
Esempio n. 4
0
    def plot(self,
             x,
             y,
             plot_type=None,
             overlay=False,
             position='bottomright',
             min_width=None,
             max_width=None,
             min_height=None,
             max_height=None,
             **kwargs):
        """Creates a plot based on x-array and y-array data.

        Args:
            x (numpy.ndarray or list): The x-coordinates of the plotted line.
            y (numpy.ndarray or list): The y-coordinates of the plotted line.
            plot_type (str, optional): The plot type can be one of "None", "bar", "scatter" or "hist". Defaults to None.
            overlay (bool, optional): Whether to overlay plotted lines on the figure. Defaults to False.
            position (str, optional): Position of the control, can be ‘bottomleft’, ‘bottomright’, ‘topleft’, or ‘topright’. Defaults to 'bottomright'.
            min_width (int, optional): Min width of the widget (in pixels), if None it will respect the content size. Defaults to None.
            max_width (int, optional): Max width of the widget (in pixels), if None it will respect the content size. Defaults to None.
            min_height (int, optional): Min height of the widget (in pixels), if None it will respect the content size. Defaults to None.
            max_height (int, optional): Max height of the widget (in pixels), if None it will respect the content size. Defaults to None.            

        """
        if self.plot_widget is not None:
            plot_widget = self.plot_widget
        else:
            plot_widget = widgets.Output(layout={'border': '1px solid black'})
            plot_control = WidgetControl(widget=plot_widget,
                                         position=position,
                                         min_width=min_width,
                                         max_width=max_width,
                                         min_height=min_height,
                                         max_height=max_height)
            self.plot_widget = plot_widget
            self.plot_control = plot_control
            self.add_control(plot_control)

        if max_width is None:
            max_width = 500
        if max_height is None:
            max_height = 300

        if (plot_type is None) and ('markers' not in kwargs.keys()):
            kwargs['markers'] = 'circle'

        with plot_widget:
            try:
                fig = plt.figure(1, **kwargs)
                if max_width is not None:
                    fig.layout.width = str(max_width) + 'px'
                if max_height is not None:
                    fig.layout.height = str(max_height) + 'px'

                plot_widget.clear_output(wait=True)
                if not overlay:
                    plt.clear()

                if plot_type is None:
                    if 'marker' not in kwargs.keys():
                        kwargs['marker'] = 'circle'
                    plt.plot(x, y, **kwargs)
                elif plot_type == 'bar':
                    plt.bar(x, y, **kwargs)
                elif plot_type == 'scatter':
                    plt.scatter(x, y, **kwargs)
                elif plot_type == 'hist':
                    plt.hist(y, **kwargs)
                plt.show()

            except Exception as e:
                print(e)
                print("Failed to create plot.")
Esempio n. 5
0
    def build_widgets(self):
        # loss curve
        self.loss_fig = plt.figure(title='Loss Curve')
        axes_options = {
            'y': {
                'label': 'Loss',
                'tick_format': '.1f',
                'label_offset': '-1em',
                'label_location': 'end'
            },
            'x': {
                'label': 'Epochs'
            }
        }
        self.loss_plot = plt.plot([], [],
                                  colors=['orangered', 'limegreen'],
                                  axes_options=axes_options,
                                  display_legend=True,
                                  labels=['Train', 'Test'])

        # accuracy curve
        self.accuracy_fig = plt.figure(title='Accuracy Curve')
        plt.scales(scales={'y': LinearScale(min=0, max=1)})
        axes_options = {
            'y': {
                'label': 'R Square',
                'tick_format': '.1%',
                'label_offset': '-1em',
                'label_location': 'end'
            },
            'x': {
                'label': 'Epochs'
            }
        }
        self.accuracy_plot = plt.plot([], [],
                                      colors=['orangered', 'limegreen'],
                                      axes_options=axes_options,
                                      display_legend=True,
                                      labels=['Train', 'Test'])

        self.progress_bar = IntProgress(description='Training Progress',
                                        min=0,
                                        max=(self.epochs - 1))

        # first tab components: loss/accuracy curves
        self.plots_layout = VBox(
            [self.progress_bar,
             HBox([self.loss_fig, self.accuracy_fig])],
            layout=self.tab_layout)

        axes_options = {
            'sample': {
                'grid_lines': 'none',
                'tick_format': '.1f',
                'num_ticks': 5
            },
            'count': {
                'grid_lines': 'none',
                'num_ticks': 6
            }
        }

        # weights hist
        self.weights_fig = plt.figure(title='Weights')
        self.weights_hist = plt.hist([],
                                     colors=['salmon'],
                                     axes_options=axes_options,
                                     bins=25)

        # biases hist
        self.biases_fig = plt.figure(title='Biases')
        self.biases_hist = plt.hist([],
                                    colors=['salmon'],
                                    axes_options=axes_options,
                                    bins=25)

        # activations hist
        self.activations_fig = plt.figure(title='Activations')
        self.activations_hist = plt.hist([],
                                         colors=['salmon'],
                                         axes_options=axes_options,
                                         bins=25)

        for fig in [self.weights_fig, self.biases_fig, self.activations_fig]:
            fig.layout.width = '400px'
            fig.layout.height = '350px'

        self.layers_dd = Dropdown(description='Layers')
        self.epoch_slider = IntSlider(description='Epoch', min=1, step=1)

        # second tab components: distributions of weights/biases/activations
        self.distributions_layout = VBox([
            self.layers_dd, self.epoch_slider,
            HBox([self.weights_fig, self.biases_fig, self.activations_fig])
        ],
                                         layout=self.tab_layout)

        self.tab = Tab([self.plots_layout, self.distributions_layout],
                       _titles={
                           0: 'Loss/Accuracy Plots',
                           1: 'Distributions'
                       })

        self.widgets_layout = self.tab