Пример #1
0
    def __init__(self, colors='red', gl=False, **kwargs):
        if isinstance(colors, str):
            colors = [colors]

        color_scale = ColorScale(scheme='plasma')

        scales = {
            'x': LinearScale(allow_padding=False),
            'y': LinearScale(allow_padding=False, orientation='vertical'),
            'color': color_scale,
            'size': LinearScale(min=0, max=1),
        }

        if gl:
            mark = ScatterGL(x=np.zeros((1, )),
                             y=np.zeros((1, )),
                             scales=scales,
                             colors=['red'])
        else:
            mark = Scatter(x=np.zeros((1, )),
                           y=np.zeros((1, )),
                           scales=scales,
                           colors=['red'])

        # link((self, 'color'), (mark, 'color'))

        super().__init__(mark=mark, **kwargs)

        link((self._mark, 'visible'), (self, 'visible'))
Пример #2
0
    def _fig_setup(self):

        import numpy as np
        from bqplot import LinearScale, Scatter, Figure, Axis
        from ipywidgets import HBox, VBox, Button, widgets

        self.xs, self.ys = LinearScale(), LinearScale()
        self.button_sel0 = Button(description='Reset axes', icon='fa-refresh')
        self.button_sel1 = Button(description='Zoom', icon='fa-arrows')
        self.button_sel2a = Button(description='Select', icon='fa-crosshairs')
        self.button_sel2a.style.button_color = 'blue'

        self.button_sel2b = Button(description='Select', icon='fa-crosshairs')
        self.button_sel2b.style.button_color = 'red'

        self.button_sel3 = Button(description='Resubset', icon='fa-refresh')
        self.button_calc = Button(
            description='Calculate!',
            icon='fa-calculator',
        )

        self.button_layout = VBox([
            HBox([self.button_sel0, self.button_sel1, self.button_sel2a]),
            HBox([self.button_sel3, self.button_calc, self.button_sel2b])
        ])
        self.diffex_output = widgets.Output()
        self.scatter = Scatter(x=self.loom.ca[self.subset_mask][self.coord1],
                               y=self.loom.ca[self.subset_mask][self.coord2],
                               scales={
                                   "x": self.xs,
                                   "y": self.ys
                               },
                               default_size=1,
                               colors=['gray'])
        xax, yax = Axis(scale=self.xs,
                        label="UMAP 1"), Axis(scale=self.ys,
                                              label="UMAP 2",
                                              orientation="vertical")
        self.fig = Figure(
            marks=[
                self.scatter,
            ],
            axes=[xax, yax],
            title="Panopticopter",
            #interaction=lasso_sel,
        )
    def __init__(self, canInteract=True):

        # Setup & Axis stuff...
        x_sc = LinearScale(min=0, max=8)
        y_sc = LinearScale(min=0, max=8)
        y_sc.reverse = True

        x_ax = Axis(label='X', scale=x_sc)
        y_ax = Axis(label='Y', scale=y_sc, orientation='vertical')

        # Display starting position for checkers game.

        # Colour checkerboard... Extra stuff for alignment to grid.
        vals = np.zeros((8, 8))
        vals[::2, ::2] = -1
        vals[1::2, 1::2] = -1

        col_sc = ColorScale(colors=['white', 'lightgray'])
        bg = plt.gridheatmap(vals,
                             scales={
                                 'column': x_sc,
                                 'row': y_sc,
                                 'color': col_sc
                             })
        bg.row = np.arange(8)
        bg.column = np.arange(8)

        self.bg = bg

        # Foreground...
        # colors of pieces
        col_sc = ColorScale(colors=['firebrick', 'black'])

        # Create empty scatter grid.
        fg = Scatter(x=[], y=[])
        fg.scales = {'x': x_sc, 'y': y_sc, 'color': col_sc}
        fg.color = []
        fg.default_size = 550
        fg.enable_move = canInteract
        print(fg.drag_size)
        fg.drag_size = 0.1
        print(fg.drag_size)

        self.fg = fg

        fig = Figure(marks=[bg, fg], axes=[x_ax, y_ax])

        # Force square.
        fig.min_aspect_ratio = 1
        fig.max_aspect_ratio = 1

        # display(fig)
        self.fig = fig
Пример #4
0
    def __init__(self):
        # Initialize the chart with a default ticker
        self.data_chart = pd.DataFrame()
        self.chart_dropdown_x = Dropdown(description='X-Axis',
                                         layout=Layout(width='380px'))
        self.chart_dropdown_y = Dropdown(description='Y-Axis',
                                         layout=Layout(width='380px'))

        self.x_sc = LinearScale()
        self.y_sc = LinearScale()
        self.tt = Tooltip(fields=['name', 'x', 'y'],
                          formats=['', '.2f', '.2f'])
        self.scatter = Scatter(scales={
            'x': self.x_sc,
            'y': self.y_sc
        },
                               colors=['dodgerblue'],
                               tooltip=self.tt,
                               unhovered_style={'opacity': 0.5})

        self.ax_x = Axis(scale=self.x_sc)
        self.ax_y = Axis(scale=self.y_sc,
                         orientation='vertical',
                         tick_format='0.2f')
        self.fig = Figure(marks=[],
                          axes=[self.ax_x, self.ax_y],
                          animation_duration=1000,
                          padding_x=0,
                          layout={
                              'width': "100%",
                              'height': "500px"
                          })

        self.data_grid = DataGrid(layout={'width': "720px", 'height': "200px"})

        self.box = VBox([
            HBox([self.fig]),
            HBox([self.chart_dropdown_x, self.chart_dropdown_y])
        ])

        display(self.box)
Пример #5
0
# To generate the appropriate graph, we need to pass the population of the country to the `size` attribute and its region to the `color` attribute.

# %% {"collapsed": true}
# Start with the first year's data
cap_income, life_exp, pop = get_data(initial_year)

# %% {"collapsed": true}
wealth_scat = Scatter(x=cap_income,
                      y=life_exp,
                      color=data['region'],
                      size=pop,
                      names=data['name'],
                      display_names=False,
                      scales={
                          'x': x_sc,
                          'y': y_sc,
                          'color': c_sc,
                          'size': size_sc
                      },
                      default_size=4112,
                      tooltip=tt,
                      animate=True,
                      stroke='Black',
                      unhovered_style={'opacity': 0.5})

# %% {"collapsed": true}
nation_line = Lines(x=data['income'][0],
                    y=data['lifeExpectancy'][0],
                    colors=['Gray'],
                    scales={
                        'x': x_sc,
Пример #6
0
    def process(self, inputs):
        """
        Plot the Scatter plot

        Arguments
        -------
         inputs: list
            list of input dataframes.
        Returns
        -------
        Figure

        """
        input_df = inputs[self.INPUT_PORT_NAME]
        if isinstance(input_df, dask_cudf.DataFrame):
            input_df = input_df.compute()  # get the computed value
        num_points = self.conf['points']
        stride = max(len(input_df) // num_points, 1)

        sc_x = scaleMap[self.conf.get('col_x_scale', 'LinearScale')]()
        sc_y = scaleMap[self.conf.get('col_y_scale', 'LinearScale')]()

        x_col = self.conf['col_x']
        y_col = self.conf['col_y']
        ax_y = Axis(label=y_col,
                    scale=sc_y,
                    orientation='vertical',
                    side='left')

        ax_x = Axis(label=x_col,
                    scale=sc_x,
                    num_ticks=10,
                    label_location='end')
        m_chart = dict(top=50, bottom=70, left=50, right=100)
        if 'col_color' in self.conf:
            color_col = self.conf['col_color']
            sc_c1 = ColorScale()
            ax_c = ColorAxis(scale=sc_c1,
                             tick_format='0.2%',
                             label=color_col,
                             orientation='vertical',
                             side='right')
            if isinstance(input_df, (cudf.DataFrame, dask_cudf.DataFrame)):
                scatter = Scatter(
                    x=input_df[x_col][::stride].to_array(),
                    y=input_df[y_col][::stride].to_array(),
                    color=input_df[color_col][::stride].to_array(),
                    scales={
                        'x': sc_x,
                        'y': sc_y,
                        'color': sc_c1
                    },
                    stroke='black')
            else:
                scatter = Scatter(x=input_df[x_col][::stride],
                                  y=input_df[y_col][::stride],
                                  color=input_df[color_col][::stride],
                                  scales={
                                      'x': sc_x,
                                      'y': sc_y,
                                      'color': sc_c1
                                  },
                                  stroke='black')
            fig = Figure(axes=[ax_x, ax_c, ax_y],
                         marks=[scatter],
                         fig_margin=m_chart,
                         title=self.conf['title'])

        else:
            if isinstance(input_df, (cudf.DataFrame, dask_cudf.DataFrame)):
                scatter = Scatter(x=input_df[x_col][::stride].to_array(),
                                  y=input_df[y_col][::stride].to_array(),
                                  scales={
                                      'x': sc_x,
                                      'y': sc_y
                                  },
                                  stroke='black')
            else:
                scatter = Scatter(x=input_df[x_col][::stride],
                                  y=input_df[y_col][::stride],
                                  scales={
                                      'x': sc_x,
                                      'y': sc_y
                                  },
                                  stroke='black')
            fig = Figure(axes=[ax_x, ax_y],
                         marks=[scatter],
                         fig_margin=m_chart,
                         title=self.conf['title'])
        return {self.OUTPUT_PORT_NAME: fig}
Пример #7
0
    def create_fig(self, ts):

        if self.ptype != 'PCA' and self.dims == None:
            ts.sort_index(inplace=True)
            df = ts.reset_index()  # time = ts.Time

        else:
            df = ts
        self.xd = df[self.xlabel]
        self.yd = df[self.cols].T

        if self.ptype == 'PCA' or self.dims is not None:
            pplt = Scatter(x=self.xd.values.ravel(), y=self.yd.values.ravel(), scales={'x': self.xScale, \
            'y': self.yScale, 'color': ColorScale(scheme=self.scheme)}, selected_style={'opacity': '1'}, \
            unselected_style={'opacity': '0.2'},color = self.colors, default_size=32)

        elif not self.ptype:
            pplt = Lines(x=self.xd, y=self.yd, scales={'x': self.xScale, 'y': self.yScale}, labels=self.legends,
                         display_legend=True, line_style=self.linestyle, stroke_width = 1, marker = 'circle', \
                         interpolation = self.interp)
            # {‘linear’, ‘basis’, ‘cardinal’, ‘monotone’}
        else:
            pplt = Lines(x=self.xd, y=self.yd, scales={'x': self.xScale, 'y': self.yScale}, labels=self.legends, \
                         display_legend=True, line_style=self.linestyle, selected_style={'opacity': '1'}, \
                         unselected_style={'opacity': '0.2'},interpolation=self.interp)
            # enable_hover=True)  # axes_options=axes_options)

        x_axis = Axis(scale=self.xScale, label=self.xlabel, grid_lines='none')
        y_axis = Axis(scale=self.yScale,
                      label=self.ylabel,
                      orientation='vertical',
                      grid_lines='none')
        c_axis = ColorAxis(scale=ColorScale(scheme=self.scheme),
                           orientation='vertical',
                           side='right')

        axis = [x_axis, y_axis, c_axis] if isinstance(
            pplt, Scatter) else [x_axis, y_axis]

        if self.debug:
            margin = dict(top=0, bottom=40, left=50, right=50)
        else:
            margin = dict(top=0, bottom=50, left=50, right=50)

        self.fig = Figure(marks=[pplt],
                          axes=axis,
                          legend_location='top-right',
                          fig_margin=margin)  # {'top':50,'left':60})

        if self.debug:
            self.deb = HTML()

        y = getattr(self, "vbox", None)
        if y is not None:
            box_layout = Layout(display='flex',
                                flex_flow='column',
                                align_items='stretch')
            if self.debug:
                self.vbox = VBox(
                    [self.selection_interacts, self.fig, self.deb],
                    layout=box_layout)
            else:
                self.vbox = VBox([self.selection_interacts, self.fig],
                                 layout=box_layout)
Пример #8
0
    def create_fig(self, ts):

        ts.sort_index(inplace=True)

        df = ts.reset_index()  # time = ts.Time

        self.xd = df[self.xlabel]
        self.yd = df[self.cols].T

        if self.ptype == 'PCA':

            pplt = Scatter(x=self.xd,
                           y=self.yd,
                           scales={
                               'x': self.xScale,
                               'y': self.yScale
                           },
                           color=self.colors)  #labels=self.legends,
            #display_legend=True, line_style='solid', stroke_width = 0, marker = 'circle')

        elif not self.ptype:
            pplt = Lines(x=self.xd,
                         y=self.yd,
                         scales={
                             'x': self.xScale,
                             'y': self.yScale
                         },
                         labels=self.legends,
                         display_legend=True,
                         line_style='solid',
                         stroke_width=0,
                         marker='circle')

        else:
            pplt = Lines(x=self.xd,
                         y=self.yd,
                         scales={
                             'x': self.xScale,
                             'y': self.yScale
                         },
                         labels=self.legends,
                         display_legend=True,
                         line_style='solid',
                         selected_style={'opacity': '1'},
                         unselected_style={
                             'opacity': '0.2'
                         })  # enable_hover=True)  # axes_options=axes_options)

        x_axis = Axis(scale=self.xScale, label=self.xlabel, grid_lines='none')
        y_axis = Axis(scale=self.yScale,
                      label=self.ylabel,
                      orientation='vertical',
                      grid_lines='none')

        if self.debug:
            margin = dict(top=0, bottom=40, left=50, right=50)
        else:
            margin = dict(top=0, bottom=50, left=50, right=50)

        self.fig = Figure(marks=[pplt],
                          axes=[x_axis, y_axis],
                          legend_location='top-right',
                          fig_margin=margin)  # {'top':50,'left':60})

        if self.debug:
            self.deb = HTML()
        # self.deb2 = HTML()
        y = getattr(self, "vbox", None)
        if y is not None:
            box_layout = Layout(display='flex',
                                flex_flow='column',
                                align_items='stretch')
            if self.debug:
                self.vbox = VBox(
                    [self.selection_interacts, self.fig, self.deb],
                    layout=box_layout)
            else:
                self.vbox = VBox([self.selection_interacts, self.fig],
                                 layout=box_layout)
def simple_optimazation_app():
    population_cnt = 20
    itter_time = 50
    crossover_rate = 0.1
    drop_rate = 0.5
    mutation_rate = 0.1

    i = 0
    best_score = 0
    best_ind = []
    best_ind_ready = []
    population = []
    '''
    dynamic figure
    '''
    X = np.linspace(0, 1, 1000)
    y = np.array([target_function(x) for x in X])

    x_sc = LinearScale()
    y_sc = LinearScale()

    ref = Lines(x=X, y=y, scales={'x': x_sc, 'y': y_sc})
    # scatter = Scatter(x=[population], y=np.array([target_function(ind) for ind in population]),
    #                     scales={'x': x_sc, 'y': y_sc},
    #                     colors=['DarkOrange'], stroke='red',
    #                     stroke_width=0.4, default_size=20)
    scatter = Scatter(x=[],
                      y=[],
                      scales={
                          'x': x_sc,
                          'y': y_sc
                      },
                      colors=['DarkOrange'],
                      stroke='red',
                      stroke_width=0.4,
                      default_size=20)

    x_ax = Axis(label='X', scale=x_sc)
    y_ax = Axis(label='Y', scale=y_sc, orientation='vertical')

    x_ax.min = 0
    x_ax.max = 1
    x_ax.num_ticks = 7
    x_ax.grid_color = 'orangered'

    fig = Figure(marks=[ref, scatter],
                 title='A Figure',
                 axes=[x_ax, y_ax],
                 animation_duration=1000)
    # display(fig)
    # %%
    run_itter_slider = population_slider = widgets.IntSlider(
        value=50, description='#Iteration', min=1, max=100, step=1)

    run_btn = widgets.Button(description='Run', icon='play', disabled=True)

    population_cnt_slider = widgets.IntSlider(value=30,
                                              description='#Population',
                                              min=0,
                                              max=100,
                                              step=10)

    init_population_btn = widgets.Button(description='Initialize Population')

    descriptor1 = widgets.Label('crossover_rate')
    crossover_rate_slider = widgets.FloatSlider(value=0.1,
                                                description='',
                                                min=0,
                                                max=1.0,
                                                step=0.1)
    descriptor2 = widgets.Label('drop_rate')
    drop_rate_slider = widgets.FloatSlider(value=0.5,
                                           description='',
                                           min=0,
                                           max=1.0,
                                           step=0.1)
    descriptor3 = widgets.Label('mutation_rate')
    mutation_rate_slider = widgets.FloatSlider(value=0.3,
                                               description='',
                                               min=0,
                                               max=1.0,
                                               step=0.1)
    patch1 = widgets.HBox([descriptor1, crossover_rate_slider])
    patch2 = widgets.HBox([descriptor2, drop_rate_slider])
    patch3 = widgets.HBox([descriptor3, mutation_rate_slider])

    blank = widgets.Label('')

    run_out = widgets.Output(layout={
        'border': '1px solid black',
        'height': '50px'
    })
    row1 = widgets.VBox([population_cnt_slider, init_population_btn])
    row2 = widgets.HBox([patch1, patch2, patch3])
    row_n = widgets.VBox([run_itter_slider, run_btn])

    app = widgets.VBox([row1, blank, row2, blank, row_n, run_out, fig])

    # %%
    def initialize():
        nonlocal population, i
        population = np.random.rand(population_cnt_slider.value)
        scatter.x = population
        scatter.y = get_scores(scatter.x)
        i = 0
        fig.title = f'迭代{i}次\n'

    @run_out.capture()
    def update(itter_time=itter_time,
               crossover_rate=crossover_rate,
               drop_rate=drop_rate,
               mutation_rate=mutation_rate):
        nonlocal scatter, fig, best_score, best_ind_ready, best_ind, i
        for j in range(itter_time):
            new_population = select_and_crossover(
                population, crossover_rate=crossover_rate, drop_rate=drop_rate)
            new_population_ready = encode_all(new_population)

            new_population_ready = mutatie_all(new_population_ready,
                                               mutation_rate=mutation_rate)

            new_population = decode_all(new_population_ready)

            ind, score = get_best(new_population)
            if score > best_score:
                best_ind = ind
                best_score = score
                best_ind_ready = encode(best_ind)
            i += 1
        scatter.x = new_population
        scatter.y = get_scores(new_population)
        fig.title = f'迭代{i}次'  # + f'最优个体为: {best_ind_ready}; 函数值为:{best_score}'
        clear_output(wait=True)
        display(f'最优个体为: {best_ind_ready}; 函数值为:{best_score}')

    # %%
    # update()

    # %%
    def on_click_init(change):
        initialize()
        run_btn.disabled = False

    def on_click_run(change):
        update(itter_time=run_itter_slider.value,
               crossover_rate=crossover_rate_slider.value,
               drop_rate=drop_rate_slider.value,
               mutation_rate=mutation_rate_slider.value)

    init_population_btn.on_click(on_click_init)
    run_btn.on_click(on_click_run)
    return app
def gradient_descent():
    line_params = {"b": 0, "m": 0, "iter": 1}

    sc_x = LinearScale(min=-100, max=100)
    sc_y = LinearScale(min=-100, max=100)
    scat = Scatter(x=[],
                   y=[],
                   scales={
                       "x": sc_x,
                       "y": sc_y
                   },
                   colors=["orange"],
                   enable_move=True)
    lin = Lines(x=[], y=[], scales={"x": sc_x, "y": sc_y}, colors=["blue"])

    ax_x = Axis(scale=sc_x, tick_format="0.0f", label="x")
    ax_y = Axis(scale=sc_y,
                tick_format="0.0f",
                orientation="vertical",
                label="y")

    fig_function = Figure(marks=[scat, lin], axes=[ax_x, ax_y])

    sc_x_cost = LinearScale(min=0, max=100)
    sc_y_cost = LinearScale(min=0, max=100)
    lin_cost = Lines(x=[], y=[], scales={"x": sc_x_cost, "y": sc_y_cost})
    ax_x_cost = Axis(scale=sc_x_cost, tick_format="0.0f", label="iteration")
    ax_y_cost = Axis(
        scale=sc_y_cost,
        tick_format="0.0f",
        orientation="vertical",
        label="Mean Squared Error",
    )

    fig_cost = Figure(marks=[lin_cost], axes=[ax_x_cost, ax_y_cost])

    def draw_line():
        x = np.linspace(-100, 100)
        y = line_params["b"] + line_params["m"] * x
        with lin.hold_sync():
            lin.x = x
            lin.y = y

    play_button = widgets.Play(
        interval=100,
        value=0,
        min=0,
        max=100,
        step=1,
        repeat=True,
        description="Run gradient descent",
        disabled=False,
    )

    year_slider = widgets.IntSlider(min=0,
                                    max=100,
                                    step=1,
                                    description="Step",
                                    value=0,
                                    disabled=True)

    def mse():
        b = line_params["b"]
        m = line_params["m"]
        return (((scat.x * m + b) - scat.y)**2).mean()

    def play_change(change):
        b = line_params["b"]
        m = line_params["m"]
        b_gradient = 0
        m_gradient = 0
        n = len(scat.x)
        learning_rate = 0.0001
        for i in range(0, len(scat.x)):
            b_gradient += -(2 / n) * (scat.y[i] - ((m * scat.x[i]) + b))
            m_gradient += -(2 / n) * scat.x[i] * (scat.y[i] -
                                                  ((m * scat.x[i]) + m))
        b = b - (learning_rate * 500 * b_gradient)
        m = m - (learning_rate * m_gradient)

        line_params["b"] = b
        line_params["m"] = m
        lin_cost.x = np.append(np.array(lin_cost.x),
                               np.array([line_params["iter"]]))
        lin_cost.y = np.append(np.array(lin_cost.y), mse())
        sc_x_cost.min = np.min(lin_cost.x)
        sc_x_cost.max = np.max(lin_cost.x)
        sc_y_cost.min = 0
        sc_y_cost.max = np.max(lin_cost.y)

        line_params["iter"] = line_params["iter"] + 1

        draw_line()

    play_button.observe(play_change, "value")
    widgets.jslink((play_button, "value"), (year_slider, "value"))

    # reset reset_button
    reset_button = widgets.Button(description="Reset")

    def on_button_clicked(change=None):
        x, y = fake_datasets("Linear")
        with scat.hold_sync():
            scat.x = x.flatten()
            scat.y = y.flatten()
        with lin_cost.hold_sync():
            lin_cost.x = []
            lin_cost.y = []

        line_params["b"] = (np.random.random() - 0.5) * 100
        line_params["m"] = np.random.random() - 0.5
        line_params["iter"] = 1
        draw_line()

    on_button_clicked()

    reset_button.on_click(on_button_clicked)

    return VBox((
        widgets.HTML("<h1>Gradient Descent</h1>"),
        reset_button,
        HBox((Label("Run gradient descent"), play_button, year_slider)),
        HBox((fig_function, fig_cost)),
    ))
Пример #11
0
import matplotlib.pyplot as plt
from bqplot import pyplot as bqplt
from bqplot import LinearScale, Scatter, Axis, Figure, Lines
def target_function(x):
    return np.exp(-(x-0.1) ** 2) * np.sin(6 * np.pi * x ** (3 / 4)) ** 2
X = np.linspace(0, 1, 1000)
y = np.array([target_function(x) for x in X])
population = np.random.rand(30)


x_sc = LinearScale()
y_sc = LinearScale()

ref = Lines(x=X, y=y, scales={'x': x_sc, 'y': y_sc})
scatter = Scatter(x=population, y=np.array([target_function(ind) for ind in population]), 
                    scales={'x': x_sc, 'y': y_sc},
                    colors=['DarkOrange'], stroke='red', 
                    stroke_width=0.4, default_size=20)

x_ax = Axis(label='X', scale=x_sc)
y_ax = Axis(label='Y', scale=y_sc, orientation='vertical')

x_ax.min = 0
x_ax.max = 1
x_ax.num_ticks = 7
x_ax.grid_color = 'orangered'

fig = Figure(marks=[ref, scatter], title='A Figure', axes=[x_ax, y_ax],
                animation_duration=1000)
fig
# %%
scatter.x=np.random.rand(30)
Пример #12
0
def polynomial_regression():
    """Polynomial regression example"""
    regression_config = {"degree": 1}
    sc_x = LinearScale(min=-100, max=100)
    sc_y = LinearScale(min=-100, max=100)
    scat = Scatter(
        x=[], y=[], scales={"x": sc_x, "y": sc_y}, colors=["orange"], enable_move=True
    )
    lin = Lines(
        x=[], y=[], scales={"x": sc_x, "y": sc_y}, line_style="dotted", colors=["blue"]
    )

    def update_line(change=None):
        if len(scat.x) == 0 or len(scat.y) == 0 or scat.x.shape != scat.y.shape:
            lin.x = []
            lin.y = []
            return
        pipe = make_pipeline(
            PolynomialFeatures(degree=regression_config["degree"]), LinearRegression()
        )

        pipe.fit(scat.x.reshape(-1, 1), scat.y)
        with lin.hold_sync():
            if len(lin.x) == 0:
                lin.x = np.linspace(sc_x.min, sc_x.max)
            lin.y = pipe.predict(np.linspace(sc_x.min, sc_x.max).reshape(-1, 1))

    update_line()
    # update line on change of x or y of scatter
    scat.observe(update_line, names=["x", "y"])
    with scat.hold_sync():
        scat.enable_move = False
        scat.interactions = {"click": "add"}
    ax_x = Axis(scale=sc_x, tick_format="0.0f", label="x")
    ax_y = Axis(scale=sc_y, tick_format="0.0f", orientation="vertical", label="y")

    fig = Figure(marks=[scat, lin], axes=[ax_x, ax_y])

    # reset reset_button
    reset_button = widgets.Button(description="Reset")

    def on_button_clicked(change=None):
        with scat.hold_sync():
            scat.x = []
            scat.y = []
        dropdown_w.value = None

    reset_button.on_click(on_button_clicked)

    # polynomial degree slider
    degree = widgets.IntSlider(
        value=regression_config["degree"], min=1, max=5, step=1, description="Degree"
    )

    def degree_change(change):
        regression_config["degree"] = change["new"]
        update_line()

    degree.observe(degree_change, names="value")

    # dropdown for dataset selection
    dropdown_w = widgets.Dropdown(
        options=fake_datasets(ndim=2), value=None, description="Dataset:"
    )

    def dropdown_on_change(change):
        if change["type"] == "change" and change["name"] == "value":
            if change["new"] is not None:
                x, y = fake_datasets(name=change["new"])
                with scat.hold_sync():
                    scat.x = x.flatten()
                    scat.y = y.flatten()

    dropdown_w.observe(dropdown_on_change)

    return VBox(
        (
            widgets.HTML("<h1>Polynomial regression</h1>"),
            reset_button,
            dropdown_w,
            degree,
            fig,
        )
    )
Пример #13
0
class Panopticopter:
    def __init__(self,
                 loom,
                 layername,
                 coord1='log2(TP10k+1) PCA UMAP embedding 1',
                 coord2='log2(TP10k+1) PCA UMAP embedding 2',
                 n_points_to_display=1000):
        self.loom = loom
        self.layername = layername

        self.coord1 = coord1
        self.coord2 = coord2
        for coordi in [coord1, coord2]:
            if coordi not in loom.ca.keys():
                raise Exception("{} not in loom.ca.keys()".format(coordi))
        self.selection1 = []
        self.selection2 = []
        self.subset_mask = None
        self.fig = None
        self.n_point_to_display = n_points_to_display
        self.xs = None
        self.ys = None
        self.button_sel1 = None
        self.button_sel2 = None
        self.button_calc = None
        self.button_layout = None
        self.diffex_output = None
        self._which_to_select = "blue"
        self._ignore_observe = False
        import numpy as np
        subset_p = np.min([1, self.n_point_to_display / self.loom.shape[1]])
        self.subset_mask = np.random.choice([True, False],
                                            p=[subset_p, 1 - subset_p],
                                            size=loom.shape[1])

    def _fig_setup(self):

        import numpy as np
        from bqplot import LinearScale, Scatter, Figure, Axis
        from ipywidgets import HBox, VBox, Button, widgets

        self.xs, self.ys = LinearScale(), LinearScale()
        self.button_sel0 = Button(description='Reset axes', icon='fa-refresh')
        self.button_sel1 = Button(description='Zoom', icon='fa-arrows')
        self.button_sel2a = Button(description='Select', icon='fa-crosshairs')
        self.button_sel2a.style.button_color = 'blue'

        self.button_sel2b = Button(description='Select', icon='fa-crosshairs')
        self.button_sel2b.style.button_color = 'red'

        self.button_sel3 = Button(description='Resubset', icon='fa-refresh')
        self.button_calc = Button(
            description='Calculate!',
            icon='fa-calculator',
        )

        self.button_layout = VBox([
            HBox([self.button_sel0, self.button_sel1, self.button_sel2a]),
            HBox([self.button_sel3, self.button_calc, self.button_sel2b])
        ])
        self.diffex_output = widgets.Output()
        self.scatter = Scatter(x=self.loom.ca[self.subset_mask][self.coord1],
                               y=self.loom.ca[self.subset_mask][self.coord2],
                               scales={
                                   "x": self.xs,
                                   "y": self.ys
                               },
                               default_size=1,
                               colors=['gray'])
        xax, yax = Axis(scale=self.xs,
                        label="UMAP 1"), Axis(scale=self.ys,
                                              label="UMAP 2",
                                              orientation="vertical")
        self.fig = Figure(
            marks=[
                self.scatter,
            ],
            axes=[xax, yax],
            title="Panopticopter",
            #interaction=lasso_sel,
        )

    def _zoom_setup(self):
        from bqplot.interacts import (PanZoom)
        panzoom = PanZoom(scales={"x": [self.xs], "y": [self.ys]})
        self.fig.interaction = panzoom

    def _reset(self):
        import numpy as np
        xmin = float(np.min(self.loom.ca[self.coord1]))
        xmax = float(np.max(self.loom.ca[self.coord2]))
        ymin = float(np.min(self.loom.ca[self.coord1]))
        ymax = float(np.max(self.loom.ca[self.coord2]))
        self.scatter.scales['x'].min = xmin
        self.scatter.scales['x'].max = xmax
        self.scatter.scales['y'].min = ymin
        self.scatter.scales['y'].max = ymax

    def resubset(self):
        import numpy as np
        mask = np.array([True] * self.loom.shape[1])
        if self.xs.min is not None:
            mask *= self.loom.ca[self.coord1] >= self.xs.min
        if self.xs.max is not None:
            mask *= self.loom.ca[self.coord1] <= self.xs.max
        if self.ys.min is not None:
            mask *= self.loom.ca[self.coord2] >= self.ys.min
        if self.ys.max is not None:
            mask *= self.loom.ca[self.coord2] <= self.ys.max
        subset_p = np.min([1, self.n_point_to_display / mask.sum()])
        self.subset_mask = mask * np.random.choice(
            [True, False], p=[subset_p, 1 - subset_p], size=self.loom.shape[1])
        self.scatter.x = self.loom.ca[self.subset_mask][self.coord1]
        self.scatter.y = self.loom.ca[self.subset_mask][self.coord2]

    def _lasso_setup(self, which_to_select):
        if self.fig.interaction is not None:
            self.fig.interaction.close()
            if which_to_select == 'blue':
                self.scatter.colors = [
                    x.replace('blue', 'gray') for x in self.scatter.colors
                ]  #*self.subset_mask.sum()
                self.selection1 = []
            elif which_to_select == 'red':
                self.scatter.colors = [
                    x.replace('red', 'gray') for x in self.scatter.colors
                ]  #*self.subset_mask.sum()
                self.selection2 = []

        from bqplot.interacts import (
            LassoSelector,
            #PanZoom,
        )
        lasso_sel = LassoSelector()
        lasso_sel.marks = [
            self.scatter,
        ]
        self.fig.interaction = lasso_sel
        self.fig.interaction.color = which_to_select

        # _which_to_select = self._which_to_select
        def make_selection():
            self.scatter.unobserve_all()
            #if 'blue' not in self.scatter.colors:
            #    which_to_select = 'blue'
            #elif 'red' not in self.scatter.colors:
            #    which_to_select = 'red'
            #else:
            #    which_to_select = None
            if which_to_select == "blue":
                self.selection1 = self.scatter.selected
                #lasso_sel.reset()

                #recolor()
            elif which_to_select == "red":
                self.selection2 = self.scatter.selected
                #lasso_sel.reset()

                #recolor()
            self.scatter.observe(select_and_recolor, names=['selected'])

        def select_and_recolor(self):
            make_selection()
            recolor()

        def recolor():
            self.scatter.unobserve_all()
            if self.selection1 is None:
                self.selection1 = []
            if self.selection2 is None:
                self.selection2 = []
            colors = []
            for i in range(self.subset_mask.sum()):
                if i in self.selection1:
                    colors.append('blue')
                elif i in self.selection2:
                    colors.append('red')
                else:
                    colors.append('gray')
            self.scatter.colors = colors
            self.scatter.observe(select_and_recolor, names=['selected'])

        self.scatter.unobserve_all()
        self.scatter.observe(select_and_recolor, names=['selected'])

    def __call__(self):
        import numpy as np
        from bqplot import pyplot as plt
        #from bqplot import DateScale, LinearScale, Axis, Lines, Scatter, Bars, Hist, Figure
        from bqplot import LinearScale, Scatter, Figure, Axis
        from ipywidgets import HBox, VBox, Button, widgets

        self._fig_setup()
        self.selection1 = []
        self.selection2 = []

        #  lasso_sel.o
        def button0_click(widget):
            self._reset()

        def button1_click(widget):
            self._zoom_setup()

    # def button2_click(widget):
    #    self._lasso_setup()

        def button2a_click(widget):
            self._lasso_setup('blue')

        def button2b_click(widget):
            self._lasso_setup('red')

        def button3_click(widget):
            self.resubset()

        def button_calc_click(widget):
            self.diffex_output.clear_output()
            from panopticon.analysis import get_cluster_differential_expression
            import pandas as pd
            from IPython.display import HTML

            mask1 = np.isin(
                self.loom.ca['cellname'],
                self.loom.ca['cellname'][self.subset_mask][self.selection1])
            mask2 = np.isin(
                self.loom.ca['cellname'],
                self.loom.ca['cellname'][self.subset_mask][self.selection2])
            with self.diffex_output:

                if mask1 is not None and mask2 is not None:
                    diffex = get_cluster_differential_expression(
                        self.loom,
                        self.layername,
                        mask1=mask1,
                        mask2=mask2,
                        verbose=True)
                self.diffex = diffex
                if 'GeneAlternateName' in diffex.columns:
                    interesting_columns = [
                        'pvalue', 'CommonLanguageEffectSize',
                        'GeneAlternateName'
                    ]
                else:
                    interesting_columns = [
                        'pvalue', 'CommonLanguageEffectSize', 'gene'
                    ]
                diffex = diffex[interesting_columns]
                diffex.columns = ['p', 'CLES', 'gene']
                d1 = diffex.sort_values(
                    'CLES', ascending=False).head(10).reset_index(drop=True)
                d2 = diffex.sort_values(
                    'CLES', ascending=True).head(10).reset_index(drop=True)
                d1.columns = [x + '_blue' for x in d1.columns]
                d2.columns = [x + '_red' for x in d2.columns]

                d1d2 = pd.concat([d1, d2], axis=1)
                self.d1d2 = d1d2

                display(
                    d1d2.style.set_properties(**{
                        'background-color': 'blue'
                    },
                                              subset=d1.columns).
                    set_properties(**{
                        'background-color': 'red'
                    },
                                   subset=d2.columns).set_properties(
                                       **{'color': 'white'}))
            # print(HTML(d1d2.to_html(index=False)))

        #  diffex_output1.append_display_data(HTML(diffex.sort_values('CommonLanguageEffectSize').head(10).to_html()))
        #with diffex_output2:
        #  diffex_output2.append_display_data(HTML(diffex.sort_values('CommonLanguageEffectSize', ascending=False).head(10).to_html()))
        self.button_sel0.on_click(button0_click)
        self.button_sel1.on_click(button1_click, )
        self.button_sel2a.on_click(button2a_click)
        self.button_sel2b.on_click(button2b_click)

        self.button_sel3.on_click(button3_click)
        self.button_calc.on_click(button_calc_click)

        from IPython.display import display
        #scatter_lasso.enable_move = True
        #fig_lasso
        final_layout = HBox(
            [VBox([self.fig, self.button_layout]), self.diffex_output])
        return final_layout
Пример #14
0
    def __init__(
        self,
        measures,
        x=None,
        y=None,
        c=None,
        mouseover=False,
        host="localhost",
        port=4090,
    ):
        """Interactive scatter plot visualisation - this is a base class,
        use either `ROIScatterViz` for one image with multiple ROIs
        or `ImageScatterViz` for a scatterplot with multiple images
        """
        self.port = port
        self.measures = measures
        self.columns = list(measures.columns)
        x_col = x if x else self.columns[0]
        y_col = y if y else self.columns[1]
        c_col = c if c else self.columns[2]

        selector_layout = widgets.Layout(height="40px", width="100px")
        self.x_selecta = widgets.Dropdown(
            options=self.columns,
            value=x_col,
            description="",
            disabled=False,
            layout=selector_layout,
        )
        self.y_selecta = widgets.Dropdown(
            options=self.columns,
            value=y_col,
            description="",
            disabled=False,
            layout=selector_layout,
        )

        self.c_selecta = widgets.Dropdown(
            options=self.columns,
            value=c_col,
            description="",
            disabled=False,
            layout=selector_layout,
        )

        self.sheet = widgets.Output()
        self.thumbs = {}
        self.goto = widgets.HTML("")
        if is_datetime(self.measures, x_col):
            x_sc = DateScale()
        else:
            x_sc = LinearScale()

        if is_datetime(self.measures, y_col):
            y_sc = DateScale()
        else:
            y_sc = LinearScale()

        if is_datetime(self.measures, c_col):
            c_sc = DateColorScale(scheme="viridis")
        else:
            c_sc = ColorScale()

        self.scat = Scatter(
            x=self.measures[self.x_selecta.value],
            y=self.measures[self.y_selecta.value],
            color=self.measures[self.c_selecta.value],
            scales={
                "x": x_sc,
                "y": y_sc,
                "color": c_sc,
            },
            names=self.measures.index,
            display_names=False,
            fill=True,
            default_opacities=[
                0.8,
            ],
        )
        self.ax_x = Axis(scale=x_sc, label=self.x_selecta.value)
        self.ax_y = Axis(scale=y_sc,
                         label=self.y_selecta.value,
                         orientation="vertical")
        self.ax_c = ColorAxis(
            scale=c_sc,
            label=self.c_selecta.value,
            orientation="vertical",
            offset={
                "scale": y_sc,
                "value": 100
            },
        )
        self.fig = Figure(
            marks=[
                self.scat,
            ],
            axes=[self.ax_x, self.ax_y, self.ax_c],
        )
        self.scat.on_element_click(self.goto_db)
        self.scat.on_element_click(self.show_data)
        if mouseover:
            self.scat.on_hover(self.show_thumb)
            self.scat.tooltip = widgets.HTML("")
        self.x_selecta.observe(self.update_scatter)
        self.y_selecta.observe(self.update_scatter)
        self.c_selecta.observe(self.update_scatter)
        self.connector = OMEConnect(host=host, port=4064)
        self.connector.gobtn.on_click(self.setup_graph)
        super().__init__([self.connector])
Пример #15
0
class ThumbScatterViz(widgets.VBox):
    def __init__(
        self,
        measures,
        x=None,
        y=None,
        c=None,
        mouseover=False,
        host="localhost",
        port=4090,
    ):
        """Interactive scatter plot visualisation - this is a base class,
        use either `ROIScatterViz` for one image with multiple ROIs
        or `ImageScatterViz` for a scatterplot with multiple images
        """
        self.port = port
        self.measures = measures
        self.columns = list(measures.columns)
        x_col = x if x else self.columns[0]
        y_col = y if y else self.columns[1]
        c_col = c if c else self.columns[2]

        selector_layout = widgets.Layout(height="40px", width="100px")
        self.x_selecta = widgets.Dropdown(
            options=self.columns,
            value=x_col,
            description="",
            disabled=False,
            layout=selector_layout,
        )
        self.y_selecta = widgets.Dropdown(
            options=self.columns,
            value=y_col,
            description="",
            disabled=False,
            layout=selector_layout,
        )

        self.c_selecta = widgets.Dropdown(
            options=self.columns,
            value=c_col,
            description="",
            disabled=False,
            layout=selector_layout,
        )

        self.sheet = widgets.Output()
        self.thumbs = {}
        self.goto = widgets.HTML("")
        if is_datetime(self.measures, x_col):
            x_sc = DateScale()
        else:
            x_sc = LinearScale()

        if is_datetime(self.measures, y_col):
            y_sc = DateScale()
        else:
            y_sc = LinearScale()

        if is_datetime(self.measures, c_col):
            c_sc = DateColorScale(scheme="viridis")
        else:
            c_sc = ColorScale()

        self.scat = Scatter(
            x=self.measures[self.x_selecta.value],
            y=self.measures[self.y_selecta.value],
            color=self.measures[self.c_selecta.value],
            scales={
                "x": x_sc,
                "y": y_sc,
                "color": c_sc,
            },
            names=self.measures.index,
            display_names=False,
            fill=True,
            default_opacities=[
                0.8,
            ],
        )
        self.ax_x = Axis(scale=x_sc, label=self.x_selecta.value)
        self.ax_y = Axis(scale=y_sc,
                         label=self.y_selecta.value,
                         orientation="vertical")
        self.ax_c = ColorAxis(
            scale=c_sc,
            label=self.c_selecta.value,
            orientation="vertical",
            offset={
                "scale": y_sc,
                "value": 100
            },
        )
        self.fig = Figure(
            marks=[
                self.scat,
            ],
            axes=[self.ax_x, self.ax_y, self.ax_c],
        )
        self.scat.on_element_click(self.goto_db)
        self.scat.on_element_click(self.show_data)
        if mouseover:
            self.scat.on_hover(self.show_thumb)
            self.scat.tooltip = widgets.HTML("")
        self.x_selecta.observe(self.update_scatter)
        self.y_selecta.observe(self.update_scatter)
        self.c_selecta.observe(self.update_scatter)
        self.connector = OMEConnect(host=host, port=4064)
        self.connector.gobtn.on_click(self.setup_graph)
        super().__init__([self.connector])

    def setup_graph(self, btn):

        if self.connector.conn is None:
            return

        self.conn = self.connector.conn
        wait = 0
        while not self.connector.conn.isConnected():
            sleep(1)
            wait += 1
            if wait > 30:
                self.children = [
                    widgets.HTML("<a><h4>Connection time out</h4></a>")
                ]
                return

        sbox_layout = widgets.Layout(min_width="120px")
        fig_layout = widgets.Layout(max_width="800px")
        self.children = [
            widgets.HBox([
                widgets.VBox(
                    [
                        widgets.HTML("<a><h4>x axis</h4></a>"),
                        self.x_selecta,
                        widgets.HTML("<a><h4>y axis</h4></a>"),
                        self.y_selecta,
                        widgets.HTML("<a><h4>color</h4></a>"),
                        self.c_selecta,
                    ],
                    layout=sbox_layout,
                ),
                widgets.VBox([self.fig, Toolbar(figure=self.fig)],
                             layout=fig_layout),
                widgets.VBox([self.goto, self.sheet], layout=sbox_layout),
            ]),
        ]

    def update_scatter(self, elem):
        col = self.x_selecta.value
        if is_datetime(self.measures, col):
            x_sc = DateScale()
        else:
            x_sc = LinearScale()
        self.ax_x.scale = x_sc
        self.scat.x = self.measures[col]
        self.ax_x.label = col

        col = self.y_selecta.value
        if is_datetime(self.measures, col):
            y_sc = DateScale()
        else:
            y_sc = LinearScale()
        self.ax_y.scale = y_sc
        self.scat.y = self.measures[col]
        self.ax_y.label = col

        col = self.c_selecta.value
        if is_datetime(self.measures, col):
            c_sc = DateColorScale()
        else:
            c_sc = ColorScale()
        self.ax_c.scale = c_sc
        self.scat.color = self.measures[col]
        self.ax_c.label = col
        self.scat.scales = {
            "x": x_sc,
            "y": y_sc,
            "color": c_sc,
        }

    def get_thumb(self, idx):
        raise NotImplementedError

    def show_thumb(self, cbk, target):
        name = target["data"]["name"]
        self.scat.tooltip = widgets.HTML("")
        self.scat.tooltip = widgets.HTML(self.get_thumb(name))
        self.scat.tooltip.style = {"opacity": 1.0}

    def goto_db(self, cbk, target):
        raise NotImplementedError

    def show_data(self, cbk, target):
        self.sheet.clear_output()
        name = target["data"]["name"]
        active_cols = [
            self.x_selecta.value,
            self.y_selecta.value,
            self.c_selecta.value,
        ]
        with self.sheet:
            display(pd.DataFrame(self.measures.loc[name, active_cols]))