def show_indexed_timeseries_plotly(timeseries: TimeSeries,
                                   istart=None,
                                   istop=None,
                                   fig: go.FigureWidget = None,
                                   col=None,
                                   row=None,
                                   zero_start=False,
                                   xlabel='time (s)',
                                   ylabel=None,
                                   title=None,
                                   neurodata_vis_spec=None,
                                   **kwargs):
    if ylabel is None and timeseries.unit:
        ylabel = timeseries.unit

    tt = get_timeseries_tt(timeseries, istart=istart, istop=istop)
    if zero_start:
        tt = tt - tt[0]
    data, unit = get_timeseries_in_units(timeseries,
                                         istart=istart,
                                         istop=istop)

    trace_kwargs = dict()
    if col is not None or row is not None:
        trace_kwargs.update(row=row, col=col)
    fig.add_trace(x=tt, y=data, **trace_kwargs, **kwargs)
    layout_kwargs = dict(xaxis_title=xlabel)
    if ylabel is not None:
        layout_kwargs.update(yaxis_title=ylabel)
    if title is not None:
        layout_kwargs.update(title=title)

    fig.update_layout(**layout_kwargs)
Beispiel #2
0
    def update(
        self,
        index: int,
        start_label: str = "start_time",
        before: float = 0.0,
        after: float = 1.0,
        order=None,
        group_inds=None,
        labels=None,
        align_to_zero=False,
        fig: go.FigureWidget = None,
    ):

        data, time_ts_aligned = self.align_data(start_label, before, after,
                                                index)
        if group_inds is None:
            group_inds = np.zeros(len(self.trials), dtype=np.int)
        if align_to_zero:
            for trial_no in order:
                data_zero_id = bisect(time_ts_aligned[trial_no], 0)
                data[trial_no] -= data[trial_no][data_zero_id]
        fig = fig if fig is not None else go.FigureWidget()
        fig.data = []
        fig.layout = {}
        return self.plot_group(group_inds, data, time_ts_aligned, fig, order)
Beispiel #3
0
def addMesh(fig: go.FigureWidget,
            faces,
            props=dict(color='white',
                       opacity=1.0,
                       facecolor=None,
                       lighting=None)):
    (verts, facesIndices) = getVertsAndFaces(faces)
    xVert, yVert, zVert, _ = np.array(verts).T
    xFace, yFace, zFace = np.array(facesIndices).T
    return fig.add_mesh3d(x=xVert,
                          y=yVert,
                          z=zVert,
                          i=xFace,
                          j=yFace,
                          k=zFace,
                          lighting=props["lighting"],
                          facecolor=props["facecolor"],
                          color=props["color"],
                          opacity=props["opacity"])
Beispiel #4
0
    def createFigureWidget(self):
        x_id = self.dimensions[0]
        d_val = self.data[x_id]
        if hasattr(self.data[x_id], 'codes'):
            d_val = self.data[x_id].codes
        hist, bin_edges = np.histogram(d_val.flatten(), bins='auto')
        xedges = []
        for i in range(len(bin_edges) - 1):
            xedges.append((bin_edges[i + 1] + bin_edges[i]) / 2)
        traces = []
        trace = {
            'type': "bar",
            'name': self.data.label,
            'marker': {
                'color': 'rgba(0, 0, 0, 0.1)'
            },
            'x': xedges,
            'y': hist,
        }
        if self.only_subsets == False:
            traces.append(trace)
        for sset in self.data.subsets:
            if hasattr(sset, "disabled") == False or sset.disabled == False:

                s_val = sset[x_id]
                if hasattr(sset[x_id], 'codes'):
                    s_val = sset[x_id].codes
                hist, bin_edges2 = np.histogram(
                    s_val.flatten(),
                    range=(bin_edges[0], bin_edges[len(bin_edges) - 1]),
                    bins=(len(bin_edges) - 1))
                color = sset.style.color
                trace = {
                    'type': "bar",
                    'name': sset.label,
                    'marker': {
                        'color': color
                    },
                    'x': xedges,
                    'y': hist,
                }
                traces.append(trace)

        layout = {
            'title': self.options['title'].value,
            'margin': {
                'l': 50,
                'r': 0,
                'b': 50,
                't': 30
            },
            'xaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['xaxis'].value,
                'linecolor': self.data.get_component(x_id).color,
                'tickcolor': self.data.get_component(x_id).color,
                'ticklen': 4,
                'linewidth': 4,
            },
            'yaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['yaxis'].value,
            },
            'legend': {
                'orientation': self.margins['legend_orientation'].value,
                'x': self.margins['legend_xpos'].value,
                'y': self.margins['legend_ypos'].value
            },
            'showlegend': self.margins['showlegend'].value,
            'barmode': 'overlay',
        }
        return FigureWidget({'data': traces, 'layout': layout})
Beispiel #5
0
    def createFigureWidget(self, x_id, y_id_list):
        traces = []
        alpha_min, alpha_max, alpha_delta = self.getDeltaFunction(
            len(y_id_list))
        alpha_val = alpha_max
        self.polyfun = {}
        for step, y_id in enumerate(y_id_list):
            z = np.polyfit(self.data[x_id].flatten().astype('float'),
                           self.data[y_id].flatten().astype('float'),
                           self.options['fit_degree'].value)
            f = np.poly1d(z)
            color = "#444444"
            color = 'rgba' + str(self.getDeltaColor(color, alpha_val, step))
            trace = {
                'type':
                "scattergl",
                'mode':
                "markers",
                'name':
                self.data.label + "_" + y_id,
                'marker':
                dict({
                    'symbol': 'circle',
                    'size': self.options['marker_size'].value,
                    'color': color,
                    'line': {
                        'width': self.options['line_width'].value,
                        'color': color
                    }
                }),
                'x':
                self.data[x_id].flatten(),
                'y':
                self.data[y_id].flatten(),
            }
            if self.only_subsets == False:
                traces.append(trace)
            x_new = self.data[x_id].flatten().astype('float').tolist()
            x_new.sort()
            y_new = f(x_new)
            trace = {
                'type': "scattergl",
                'mode': "lines",
                'name': self.data.label + "_fit_" + y_id,
                'line': {
                    'width': self.options['line_width'].value,
                    'color': color
                },
                'x': x_new,
                'y': y_new,
                'showlegend': False
            }
            if self.only_subsets == False:
                traces.append(trace)

            alpha_val = alpha_val - alpha_delta
            self.polyfun[y_id] = f

        for sset in self.data.subsets:
            if hasattr(sset, "disabled") == False or sset.disabled == False:
                alpha_val = alpha_max
                for step, y_id in enumerate(y_id_list):
                    z = np.polyfit(sset[x_id].flatten().astype('float'),
                                   sset[y_id].flatten().astype('float'),
                                   self.options['fit_degree'].value)
                    f = np.poly1d(z)
                    color = sset.style.color
                    color = 'rgba' + str(
                        self.getDeltaColor(color, alpha_val, step))
                    trace = {
                        'type':
                        "scattergl",
                        'mode':
                        "markers",
                        'name':
                        sset.label + "_" + y_id,
                        'marker':
                        dict({
                            'symbol': 'circle',
                            'size': self.options['marker_size'].value,
                            'color': color,
                            'line': {
                                'width': self.options['line_width'].value,
                                'color': color
                            }
                        }),
                        'x':
                        sset[x_id].flatten(),
                        'y':
                        sset[y_id].flatten(),
                    }
                    traces.append(trace)

                    x_new = sset[x_id].flatten().astype('float').tolist()
                    x_new.sort()
                    y_new = f(x_new)
                    trace = {
                        'type': "scattergl",
                        'mode': "lines",
                        'name': sset.label + "_fit_" + y_id,
                        'line': {
                            'width': self.options['line_width'].value,
                            'color': color
                        },
                        'x': x_new,
                        'y': y_new,
                        'showlegend': False
                    }
                    traces.append(trace)
                    alpha_val = alpha_val - alpha_delta
                    self.polyfun[sset.label + "_" + y_id] = f

        y_color = 'rgb(0,0,0)'
        if len(y_id_list) == 1:
            y_color = self.data.get_component(y_id_list[0]).color

        layout = {
            'title': self.options['title'].value,
            'margin': {
                'l': 50,
                'r': 0,
                'b': 50,
                't': 30
            },
            'xaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['xaxis'].value,
                'linecolor': self.data.get_component(x_id).color,
                'tickcolor': self.data.get_component(x_id).color,
                'ticklen': 4,
                'linewidth': 4,
            },
            'yaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['yaxis'].value,
                'linecolor': y_color,
                'tickcolor': y_color,
                'ticklen': 4,
                'linewidth': 4,
            },
            'showlegend': self.margins['showlegend'].value,
            'legend': {
                'orientation': self.margins['legend_orientation'].value,
                'x': self.margins['legend_xpos'].value,
                'y': self.margins['legend_ypos'].value
            },
        }
        return FigureWidget({'data': traces, 'layout': layout})
Beispiel #6
0
    def _init_widgets(self):
        """Initializes all configuration widgets. Possible image config parameters are:"""
        layout = Layout(width="200px", max_width="200px")

        self._alpha = FloatSlider(
            value=1,
            min=0,
            max=1.0,
            step=0.1,
            description="alpha:",
            description_tooltip="Overlay image alpha level (0 to 1).",
            disabled=False,
            continuous_update=True,
            orientation="horizontal",
            readout=True,
            readout_format=".1f",
            layout=layout,
        )

        self._lut = Dropdown(
            options=PapayaConfigWidget.lut_options,
            value="Red Overlay",
            description="lut:",
            description_tooltip="The color table name.",
            layout=layout,
        )

        self._nlut = Dropdown(
            options=PapayaConfigWidget.lut_options,
            value="Red Overlay",
            description="negative-lut:",
            description_tooltip=
            "The color table name used by the negative side of the parametric pair.",
            layout=layout,
        )

        self._min = FloatText(
            value=None,
            description="min:",
            description_tooltip="The display range minimum.",
            step=0.01,
            continuous_update=True,
            disabled=False,
            layout=layout,
        )

        self._minp = BoundedFloatText(
            value=None,
            min=0,
            max=100,
            step=1,
            continuous_update=True,
            description="min %:",
            description_tooltip=
            "The display range minimum as a percentage of image max.",
            disabled=False,
            layout=layout,
        )

        self._max = FloatText(
            value=None,
            description="max:",
            description_tooltip="The display range maximum.",
            step=0.01,
            continuous_update=True,
            disabled=False,
            layout=layout,
        )

        self._maxp = BoundedFloatText(
            value=None,
            min=0,
            max=100,
            step=1,
            continuous_update=True,
            description="max %:",
            description_tooltip=
            "The display range minimum as a percentage of image max.",
            disabled=False,
            layout=layout,
        )

        self._sym = Checkbox(
            value=False,
            description="symmetric",
            description_tooltip=
            "When selected, sets the negative range of a parametric pair to the same size as the positive range.",
            disabled=False,
            layout=layout,
        )

        # figure to display histogram of image data
        fig = Figure()
        fig.update_layout(
            height=300,
            margin=dict(l=15, t=15, b=15, r=15, pad=4),
            showlegend=True,
            legend_orientation="h",
        )

        self._hist = FigureWidget(fig)
        self._hist.add_trace(
            Histogram(x=[], name="All image data", visible="legendonly"))
        self._hist.add_trace(Histogram(x=[], name="Image data without 0s"))

        self._handlers = defaultdict()
Beispiel #7
0
    def update(
        self,
        index: int,
        start_label: str = "start_time",
        before: float = 0.0,
        after: float = 1.0,
        order=None,
        group_inds=None,
        labels=None,
        align_to_zero=False,
        sem=False,
        fig: go.FigureWidget = None,
    ):
        data, time_ts_aligned = self.align_data(start_label, before, after,
                                                index)
        if group_inds is None:
            group_inds = np.zeros(len(self.trials), dtype=np.int)
        if align_to_zero:
            for trial_no in order:
                data_zero_id = bisect(time_ts_aligned[trial_no], 0)
                data[trial_no] -= data[trial_no][data_zero_id]
        fig = go.FigureWidget() if fig is None else fig
        fig.data = []
        fig.layout = {}
        if sem:
            group_stats = []
            for group in np.unique(group_inds):
                this_mean = np.nanmean(data[group_inds == group, :], axis=0)
                err = scipy.stats.sem(data[group_inds == group, :],
                                      axis=0,
                                      nan_policy="omit")
                group_stats.append(
                    dict(
                        mean=this_mean,
                        lower=this_mean - 2 * err,
                        upper=this_mean + 2 * err,
                        group=group,
                    ))

            for stats in group_stats:
                plot_kwargs = dict()
                color = color_wheel[stats["group"]]
                if labels is not None:
                    plot_kwargs.update(text=labels[stats["group"]])
                fig.add_scattergl(x=time_ts_aligned[0],
                                  y=stats["lower"],
                                  line_color=color)
                fig.add_scattergl(x=time_ts_aligned[0],
                                  y=stats["upper"],
                                  line_color=color,
                                  fill='tonexty',
                                  opacity=0.2)
                fig.add_scattergl(x=time_ts_aligned[0],
                                  y=stats["mean"],
                                  line_color=color,
                                  **plot_kwargs)

        else:
            fig = self.plot_group(group_inds, data, time_ts_aligned, fig,
                                  order)
        return fig
Beispiel #8
0
def player_strength_by_horizon(player_eps: DF, players_gw_eps: DF, horizon: str, position: str, team: str, player: str, ctx: Context):
    """
    Returns a plotly chart with expected points as the y-axis and cost on the x-axis for a specific time horizon. This chart can be displayed in the Jupyter notebook.

    Args:
        player_eps: The data frame with data to chart.
        horizon: The time horizon of the chart, e.g. Next GW, Next 8 GWs, etc.

    Returns:
        The plotly chart.
    """

    def if_in_cols(df: DF, col: str, other):
        return df[col].fillna(other) if col in df.columns else other

    def in_team_trace(player_eps: DF) -> Scatter:
        return Scatter(
            x=player_eps['Current Cost'],
            y=player_eps['Expected Points ' + horizon],
            mode='markers',
            marker={'size': MAX_POINT_SIZE, 'color': 'white', 'line': {'width': 1}},
            name='In Team',
            text=player_eps['Label'])

    def position_traces(player_eps: DF, ctx: Context) -> list:
        def trace(player_eps: DF, position: str) -> Scatter:
            return Scatter(
                x=player_eps['Current Cost'],
                y=player_eps['Expected Points ' + horizon],
                name=position,
                mode='markers',
                marker={'color': POSITION_COLORS[position],
                        'opacity': player_eps['Opacity'],
                        'size': player_eps['Size']},
                text=player_eps['Label'])

        return [trace(player_eps[player_eps['Field Position'] == position], position) for position in ctx.position_by_type.values()]

    def event_capture_trace(player_eps: DF) -> Scatter:
        return Scatter(
            x=player_eps['Current Cost'],
            y=player_eps['Expected Points ' + horizon],
            mode='markers',
            opacity=0,
            marker={'size': player_eps['Size'], 'color': 'white', 'opacity': 1.0},
            name='Player',
            text=player_eps['Label'])

    def player_clicked(trace, points, selector):
        message.value = ''

        try:
            player = None
            player_code = None
            for ind in points.point_inds:
                player = player_eps_chart.iloc[ind]
                player_code = player_eps_chart.index[ind]

            if player is not None:
                player_gw_eps = players_gw_eps.loc[player_code]
                detail.children = tuple([display_player(player, player_gw_eps, horizon, ctx)])
            else:
                detail.children = tuple([])

        # Make sure exceptions are displayed in footer because they are swallowed otherwise.
        except Exception as e:
            message.value = traceback.format_exc()

    def break_text(text: str, max_num_per_line: int, sep=','):
        lines = []

        items = text.split(sep)
        while len(items) > 0:
            lines += [sep.join(items[:max_num_per_line])]
            items = items[max_num_per_line:]

        return (sep + '<br>').join(lines)

    def get_player_eps_chart(player_eps: DF) -> DF:
        return (player_eps[
                    ['Name', 'Name and Short Team', 'Long Name', 'Team Name', 'Field Position', 'Current Cost', 'Total Points', 'Minutes Percent',
                     'News And Date', 'Minutes Played', 'Total Points Recent Fixtures', 'Avg Total Points Recent Fixtures', 'Avg ICT Index Recent Fixtures',
                     'Avg Influence Recent Fixtures', 'Avg Creativity Recent Fixtures', 'Avg Threat Recent Fixtures', 'ICT Index', 'Influence', 'Creativity', 'Threat',
                     'Chance Avail This GW', 'Stats Completeness', 'Stats Completeness Percent', 'Profile Picture', 'Team Last Updated', 'Player Last Updated']
                    + [col for col in player_eps.columns if col.startswith('Expected Points ') or col.startswith('Fixtures ')]
                    + (['In Team?'] if 'In Team?' in player_eps.columns else [])]
                # Add visualisation columns that need to be calculated on the unfiltered set.
                .assign(**{'Opacity': lambda df: if_in_cols(df, 'Stats Completeness Percent', 100) * (1 - MIN_OPACITY) / 100 + MIN_OPACITY})
                .assign(**{'Size': lambda df: np.maximum(np.where(df['Field Position'] != 'GK',
                                                                  df.groupby('Field Position')['Avg Threat Recent Fixtures'].transform(lambda x: x / x.max() * MAX_POINT_SIZE),
                                                                  df['Avg Influence Recent Fixtures'] / df['Avg Influence Recent Fixtures'].max() * MAX_POINT_SIZE), MIN_POINT_SIZE)}))

    def get_player_eps_formatted(player_eps_chart: DF) -> DF:
        return (player_eps_chart
                .pipe(ctx.dd.format)
                [player_eps_chart.columns]
                .astype(str))

    def get_labels(player_eps_chart: DF) -> S:
        player_eps_formatted = player_eps_chart.pipe(get_player_eps_formatted)

        return (player_eps_formatted['Name and Short Team']
                + ', ' + player_eps_formatted['Field Position']
                + ', Cost: ' + player_eps_formatted['Current Cost']
                + '<br>Exp. Points: ' + player_eps_formatted[f'Expected Points {horizon}']
                + ', Total Points: ' + player_eps_formatted['Total Points']
                + '<br>Minutes Percent: ' + player_eps_formatted['Minutes Percent']
                + f', Stats Completeness (Recent {ctx.player_fixtures_look_back} Fixtures): ' + player_eps_formatted['Stats Completeness']
                + np.where(player_eps_formatted['Field Position'] != 'GK', f'<br>Average Threat (Recent {ctx.player_fixtures_look_back} Fixtures): ' + player_eps_formatted['Avg Threat Recent Fixtures'],
                           f'<br>Avg Influence (Recent {ctx.player_fixtures_look_back} Fixtures): ' + player_eps_formatted['Avg Influence Recent Fixtures'])
                + ', ICT: ' + player_eps_formatted['ICT Index']
                + '<br>Next: ' + player_eps_formatted[f'Fixtures Next 8 GWs'].map(lambda v: break_text(v, 4))
                + '<br>News: ' + player_eps_formatted['News And Date']
                )

    player_eps_chart = player_eps.pipe(get_player_eps_chart)

    pad = 0.5
    min_cost, max_cost = (player_eps_chart['Current Cost'].min() - pad, player_eps_chart['Current Cost'].max() + pad)
    min_eps, max_eps = (player_eps_chart[f'Expected Points {horizon}'].min() - pad, player_eps_chart[f'Expected Points {horizon}'].max() + pad)
    last_updated = player_eps_chart['Player Last Updated'].min()

    if position != SEL_ALL:
        player_eps_chart = player_eps_chart[lambda df: df['Field Position'] == position]

    if team != SEL_ALL:
        player_eps_chart = player_eps_chart[lambda df: df['Team Name'] == team]

    if player is not None and player != '':
        player_eps_chart = player_eps_chart[lambda df: df['Name'].str.lower().str.contains(player.lower())]

    # Add labels
    player_eps_chart = player_eps_chart.assign(**{'Label': lambda df: df.pipe(get_labels)})

    traces = []
    if 'In Team?' in player_eps_chart.columns:
        traces += [in_team_trace(player_eps_chart[lambda df: df['In Team?'] == True])]
    traces += position_traces(player_eps_chart, ctx)
    traces += [event_capture_trace(player_eps_chart)]

    chart = FigureWidget(
        traces,
        layout={
            'title': f'Expected Points from Game Week {ctx.next_gw} for {horizon}' if horizon != 'Next GW' else f'Expected Points for Game Week {ctx.next_gw}',
            'xaxis': dict(title='Current Cost (lower is better)', showspikes=True, range=[min_cost, max_cost]),
            'yaxis': dict(title=f'Expected Points {horizon} (higher is better)', showspikes=True, range=[min_eps, max_eps]),
            'hovermode': 'closest',
            'legend': dict(itemsizing='constant')
        },
    )

    # Register the click event handler. Unfortunately, this cannot be done on the scatter trace itself at creation time for some reason.
    for trace in chart.data:
        trace.on_click(player_clicked, True)

    data_quality = HTML(f'''<p><center>Color: field position - 
            Size: threat (recent fixtures) for non-goalies, influence (recent fixtures) for goalies - 
            Opacity: stats completeness (recent fixtures)<br> 
            Data last updated: {last_updated.strftime("%d %b %Y %H:%M:%S")}</center></p>''')

    message = HTML('<p><center>Click on player for more detail!</center></p>')

    out = Output()
    detail = VBox([])
    chart_and_detail = VBox([message, out, chart, data_quality, detail])

    return chart_and_detail
    def createFigureWidget(self):
        dimensions = self.dimensions
        data_lines = [] 
        i=0
        if self.only_subsets == False:
            colors = [i for r in range(self.data.size)]
            colorscale = [[0,'#EEEEEE']]
        else:
            colors = []
            colorscale = []
            i=-1
            
        for sset in self.data.subsets:
            if hasattr(sset,"disabled") == False or sset.disabled == False:            
                i = i+1
                tmplist = [i for r in range(len(sset.to_index_list()))]
                colors.extend(tmplist)
                colorscale.append([i,sset.style.color])
        t_color = len(colorscale)        
        if (t_color > 1):
            for c in colorscale:
                c[0] = c[0]/(t_color-1)
        else:
            colorscale = [[0,'#EEEEEE'], [1,'#EEEEEE']] 
        traces = []

        for dimension in dimensions:
            line={}
            if hasattr(self.data[dimension].flatten(), 'codes'):
                line['values'] = self.data[dimension].flatten().codes.tolist()
                tickvals, tickmask = np.unique(self.data[dimension].flatten().codes, return_index=True)
                ticktext = self.data[dimension][tickmask]
                line['tickvals'] = tickvals.tolist()
                line['ticktext'] = ticktext.tolist()
            else:
                line['values'] = self.data[dimension].flatten().tolist()
            
            line['label'] = dimension
            for sset in self.data.subsets:
                if hasattr(sset,"disabled") == False or sset.disabled == False:            
                    if hasattr(sset[dimension].flatten(), 'codes'):
                        tmplist = sset[dimension].codes.tolist()
                    else:
                        tmplist = sset[dimension].tolist()
                    line['values'].extend(tmplist)
            data_lines.append(line);
        trace = {
            'type' : 'parcoords',
            'line' : {
                'color' : colors, 
                'colorscale' : colorscale
            },
            'dimensions' : data_lines,
        }
        traces.append(trace)
        for sset in self.data.subsets:
            if hasattr(sset,"disabled") == False or sset.disabled == False:                    
                color = sset.style.color
                trace = {
                    'type': "scatter",
                    'name' : sset.label, 
                    'textposition' : 'middle right',
                    'x' : [-1000],
                    'y' : [i],
                    'mode' : 'markers',
                    'marker': {
                        'color' : color,
                        'size' : 20,
                        'line': {
                            'width': 1,
                            'color' : 'light grey'
                        },
                        'symbol' : 'square'
                    }
                }
                traces.append(trace)
        
        layout = {
            'title' : self.options['title'].value,
            'xaxis': {
                'title' : self.options['xaxis'].value,
                'range' : [0,1],
                'showgrid':False,
                'showline':False,
                'showticklabels':False,
                'zeroline':False
            },
            'yaxis': {
                'title' : self.options['yaxis'].value,
                'range' : [0,1],
                'showgrid':False,
                'showline':False,
                'showticklabels':False,
                'zeroline':False
            },
            'showlegend': self.margins['showlegend'].value,
            'legend' : {
                'orientation' : self.margins['legend_orientation'].value,
                'x' : self.margins['legend_xpos'].value,
                'y' : self.margins['legend_ypos'].value
            },  
        }
        return FigureWidget(data = traces, layout = layout)
    def createFigureWidget(self, x_id, y_id, z_id):
        traces = []
        color = "#444444"
        color = 'rgba' + str(self.getDeltaColor(color, .8))
        trace = {
            'type':
            "scatter3d",
            'mode':
            "markers",
            'name':
            self.data.label,
            'marker':
            dict({
                'symbol': 'circle',
                'size': self.options['marker_size'].value,
                'color': color,
            }),
            'x':
            self.data[x_id].flatten(),
            'y':
            self.data[y_id].flatten(),
            'z':
            self.data[z_id].flatten(),
        }
        if self.only_subsets == False:
            traces.append(trace)
        for sset in self.data.subsets:
            if hasattr(sset, "disabled") == False or sset.disabled == False:
                color = sset.style.color
                color = 'rgba' + str(self.getDeltaColor(color, .8))
                trace = {
                    'type':
                    "scatter3d",
                    'mode':
                    "markers",
                    'name':
                    sset.label,
                    'marker':
                    dict({
                        'symbol': 'circle',
                        'size': self.options['marker_size'].value,
                        'color': color,
                    }),
                    'x':
                    sset[x_id].flatten(),
                    'y':
                    sset[y_id].flatten(),
                    'z':
                    sset[z_id].flatten(),
                }
                traces.append(trace)

        layout = {
            'margin': {
                'l': 0,
                'r': 0,
                'b': 0,
                't': 30
            },
            'scene': {
                'xaxis': {
                    'title': self.options['xaxis'].value,
                    'linecolor': self.data.get_component(x_id).color,
                    'tickcolor': self.data.get_component(x_id).color,
                    'ticklen': 4,
                    'linewidth': 4,
                },
                'yaxis': {
                    'title': self.options['yaxis'].value,
                    'linecolor': self.data.get_component(y_id).color,
                    'tickcolor': self.data.get_component(y_id).color,
                    'ticklen': 4,
                    'linewidth': 4,
                },
                'zaxis': {
                    'title': self.options['zaxis'].value,
                    'linecolor': self.data.get_component(z_id).color,
                    'tickcolor': self.data.get_component(z_id).color,
                    'ticklen': 4,
                    'linewidth': 4,
                }
            },
            'showlegend': self.margins['showlegend'].value,
            'legend': {
                'orientation': self.margins['legend_orientation'].value,
                'x': self.margins['legend_xpos'].value,
                'y': self.margins['legend_ypos'].value
            }
        }
        return FigureWidget({'data': traces, 'layout': layout})
Beispiel #11
0
    def createFigureWidget(self):
        traces = []
        Xpca = []
        for x_id in self.dimensions:            
            if hasattr(self.data[x_id].flatten(), 'codes'):
                Xpca.append(self.data[x_id].flatten().codes.tolist())
            else:
                Xpca.append(self.data[x_id].flatten())

        Xpca = np.array(Xpca).transpose()
        self.pca.fit(Xpca)
        Xt = self.pca.transform(Xpca)

        data_l = [self.data[col_id].astype(str) for col_id in self.dimensions]
        data_l = zip(*data_l)
        t_val = [' - '.join( dat ) for dat in data_l]
        x_val = Xt[:,0]
        y_val = Xt[:,1]
        color = "#444444"
    
        trace = {
            'type': "scattergl", 'mode': "markers", 'name': self.data.label,
            'marker': dict({
                'symbol':'circle', 'size': self.options['marker_size'].value, 'color': color,
                'line' : { 'width' : self.options['line_width'].value, 'color' : color }
            }),
            'x': x_val.tolist(),
            'y': y_val.tolist(),
            'text': t_val,
        }
        
        #print(trace)

        if self.only_subsets == False:
            traces.append(trace)
            
        for sset in self.data.subsets:
            if hasattr(sset,"disabled") == False or sset.disabled == False:            
                sset_mask = sset.to_mask()
                color = sset.style.color
                trace = {
                    'type': "scattergl", 'mode': "markers", 'name': sset.label,
                    'marker': dict({
                        'symbol':'circle', 'size': self.focused_size_marker, 'color': color,
                        'line' : { 'width' : self.options['line_width'].value, 'color' : color}      
                    }),
                    'x': (x_val[sset_mask]).tolist(),
                    'y': (y_val[sset_mask]).tolist(),
                }
                traces.append(trace)  
          

        layout = {
            'title' : self.options['title'].value,
            'margin' : {
                'l':self.margins['left'].value,
                'r':self.margins['right'].value,
                'b':self.margins['bottom'].value,
                't':self.margins['top'].value 
            },            
            'xaxis': { 'autorange' : True, 'zeroline': True, 
                'title' : self.options['xaxis'].value, 
                'type' : self.options['xscale'].value,
            },
            'yaxis': { 'autorange':True, 'zeroline': True, 
                'title' : self.options['yaxis'].value, 
                'type' : self.options['yscale'].value
            },
            'showlegend': self.margins['showlegend'].value,
            'legend' : {
                'orientation' : self.margins['legend_orientation'].value,
                'x' : self.margins['legend_xpos'].value,
                'y' : self.margins['legend_ypos'].value
            }               
        }
        
        return FigureWidget({
                'data': traces,
                'layout': layout
        })
Beispiel #12
0
    def createFigureWidget(self):
        traces = []
        params = []
        for param in self.dimensions:
            if hasattr(self.data[param].flatten(), 'codes'):
                params.append(self.data[param].flatten().codes.tolist())
            else:
                params.append(self.data[param].flatten())

        self.mat = np.corrcoef(params)
        self.mat = np.nan_to_num(self.mat)
        self.mat = np.flip(self.mat, axis=1)

        trace = {
            'type': 'heatmap',
            'x': self.dimensions[::-1],
            'y': self.dimensions,
            'z': self.mat,
            'colorscale': 'RdBu',
            'autocolorscale': False,
            'reversescale': True,
            'zauto': False,
            'zmin': -1,
            'zmax': 1,
        }

        layout = {
            'title': self.options['title'].value,
            'margin': {
                'l': self.margins['left'].value,
                'r': self.margins['right'].value,
                'b': self.margins['bottom'].value,
                't': self.margins['top'].value
            },
            'showlegend': True,
            'xaxis': {
                'side': 'top'
            },
            'annotations': []
        }

        if self.only_subsets == False:
            traces.append(trace)
            for i in range(len(self.dimensions)):
                for j in range(len(self.dimensions)):
                    currentValue = self.mat[i][j]
                    if abs(currentValue) > 0.6:
                        textColor = 'white'
                    else:
                        textColor = 'black'

                    result = {
                        'xref': 'x1',
                        'yref': 'y1',
                        'x': j,
                        'y': i,
                        'text': round(currentValue, 2),
                        'showarrow': False,
                        'font': {
                            'color': textColor
                        }
                    }
                    layout['annotations'].append(result)

        for sset in self.data.subsets:
            if hasattr(sset, "disabled") == False or sset.disabled == False:
                params = []
                for param in self.dimensions:
                    if hasattr(sset[param].flatten(), 'codes'):
                        params.append(sset[param].flatten().codes.tolist())
                    else:
                        params.append(sset[param].flatten())
                mat = np.corrcoef(params)
                mat = np.nan_to_num(mat, 1)
                mat = np.flip(mat, axis=1)
                trace = {
                    'type': 'heatmap',
                    'x': self.dimensions[::-1],
                    'y': self.dimensions,
                    'z': mat,
                    'colorscale': 'RdBu',
                    'autocolorscale': False,
                    'reversescale': True,
                    'zauto': False,
                    'zmin': -1,
                    'zmax': 1,
                }
                traces.append(trace)

                for i in range(len(self.dimensions)):
                    for j in range(len(self.dimensions)):
                        currentValue = mat[i][j]
                        if abs(currentValue) > 0.6:
                            textColor = 'white'
                        else:
                            textColor = 'black'

                            result = {
                                'xref': 'x1',
                                'yref': 'y1',
                                'x': j,
                                'y': i,
                                'text': round(currentValue, 2),
                                'showarrow': False,
                                'font': {
                                    'color': textColor
                                }
                            }
                        layout['annotations'].append(result)

        return FigureWidget({'data': traces, 'layout': layout})
Beispiel #13
0
    def createFigureWidget(self):
        x_id = self.dimensions[0]
        y_id_list = [
            self.dimensions[i] for i in range(1, len(self.dimensions))
        ]
        d_val = self.data[x_id]
        if hasattr(self.data[x_id], 'codes'):
            d_val = self.data[x_id].codes.flatten()
        hist, bin_edges = np.histogram(d_val.flatten(), bins='auto')
        bin_list = bin_edges.searchsorted(d_val.flatten(), 'right')

        xedges = []
        for i in range(len(bin_edges) - 1):
            xedges.append((bin_edges[i + 1] + bin_edges[i]) / 2)
        traces = []
        alpha_min, alpha_max, alpha_delta = self.getDeltaFunction(
            len(y_id_list), 0.1, 0.5)
        alpha_val = alpha_max
        for y_id in y_id_list:

            d_val = self.data[y_id]
            if hasattr(self.data[y_id], 'codes'):
                d_val = self.data[y_id].codes

            y_mean = []
            y_std_u = []
            y_std_l = []
            y_min = []
            y_max = []

            for i in range(1, len(bin_edges)):
                d_col = d_val[(bin_list == i)].flatten()
                if len(d_col) > 0:
                    i_mean = d_col.mean()
                    i_min = d_col.min()
                    i_max = d_col.max()
                    i_std = d_col.std()
                else:
                    i_mean = 0
                    i_std = 0
                    i_min = 0
                    i_max = 0
                y_mean.append(i_mean)
                y_std_u.append(i_mean + i_std)
                y_std_l.append(i_mean - i_std)
                y_min.append(i_min)
                y_max.append(i_max)

            color = "#444444"
            color = 'rgba' + str(self.getDeltaColor(color, alpha_val))

            trace = {
                'type': "scatter",
                'name': '-',
                'marker': {
                    'color': color
                },
                'line': {
                    'width': 0
                },
                'x': xedges,
                'y': y_min,
                'mode': 'lines',
                'showlegend': False
            }
            if self.only_subsets == False:
                traces.append(trace)

            trace = {
                'type': "scatter",
                'name': self.data.label + "_" + y_id,
                'marker': {
                    'color': color
                },
                'fillcolor': color,
                'fill': 'tonexty',
                'line': {
                    'color': color
                },
                'x': xedges,
                'y': y_mean,
                'mode': 'lines',
            }
            if self.only_subsets == False:
                traces.append(trace)

            trace = {
                'type': "scatter",
                'name': '+',
                'marker': {
                    'color': color
                },
                'fillcolor': color,
                'fill': 'tonexty',
                'line': {
                    'width': 0
                },
                'x': xedges,
                'y': y_max,
                'mode': 'lines',
                'showlegend': False
            }
            if self.only_subsets == False:
                traces.append(trace)
            alpha_val = alpha_val - alpha_delta

        for sset in self.data.subsets:
            if hasattr(sset, "disabled") == False or sset.disabled == False:
                s_val = sset[x_id]
                if hasattr(sset[x_id], 'codes'):
                    s_val = sset[x_id].codes
                bin_list = bin_edges.searchsorted(s_val, 'right')
                alpha_val = alpha_max
                for y_id in y_id_list:

                    s_val = sset[y_id]
                    if hasattr(sset[y_id], 'codes'):
                        s_val = sset[y_id].codes

                    y_s_mean = []
                    y_s_std_u = []
                    y_s_std_l = []

                    for i in range(1, len(bin_edges)):
                        s_col = s_val[(bin_list == i)]
                        if len(s_col) > 0:
                            i_mean = s_col.mean()
                            i_std = s_col.std()
                            i_min = s_col.min()
                            i_max = s_col.max()
                        else:
                            i_mean = 0
                            i_std = 0
                            i_min = 0
                            i_max = 0
                        y_s_mean.append(i_mean)
                        y_s_std_u.append(i_mean + i_std)
                        y_s_std_l.append(i_mean - i_std)

                    color = sset.style.color
                    color = 'rgba' + str(self.getDeltaColor(color, alpha_val))
                    trace = {
                        'type': "scatter",
                        'name': '-',
                        'marker': {
                            'color': color
                        },
                        'line': {
                            'width': 0
                        },
                        'x': xedges,
                        'y': y_s_std_l,
                        'mode': 'lines',
                        'showlegend': False
                    }
                    traces.append(trace)

                    trace = {
                        'type': "scatter",
                        'name': sset.label + "_" + y_id,
                        'marker': {
                            'color': color
                        },
                        'fillcolor': color,
                        'fill': 'tonexty',
                        'line': {
                            'color': color
                        },
                        'x': xedges,
                        'y': y_s_mean,
                        'mode': 'lines',
                    }
                    traces.append(trace)

                    trace = {
                        'type': "scatter",
                        'name': '+',
                        'marker': {
                            'color': color
                        },
                        'fillcolor': color,
                        'fill': 'tonexty',
                        'line': {
                            'width': 0
                        },
                        'x': xedges,
                        'y': y_s_std_u,
                        'mode': 'lines',
                        'showlegend': False
                    }
                    traces.append(trace)
                    alpha_val = alpha_val - alpha_delta
        y_color = 'rgb(0,0,0)'
        if len(y_id_list) == 1:
            y_color = self.data.get_component(y_id_list[0]).color
        layout = {
            'title': self.options['title'].value,
            'margin': {
                'l': 50,
                'r': 0,
                'b': 50,
                't': 30
            },
            'xaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['xaxis'].value,
                'linecolor': self.data.get_component(x_id).color,
                'tickcolor': self.data.get_component(x_id).color,
                'ticklen': 4,
                'linewidth': 4,
            },
            'yaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['yaxis'].value,
                'linecolor': y_color,
                'tickcolor': y_color,
                'ticklen': 4,
                'linewidth': 4,
            },
            'showlegend': self.margins['showlegend'].value,
            'barmode': 'overlay',
            'legend': {
                'orientation': self.margins['legend_orientation'].value,
                'x': self.margins['legend_xpos'].value,
                'y': self.margins['legend_ypos'].value
            },
        }

        return FigureWidget({'data': traces, 'layout': layout})
Beispiel #14
0
class PapayaConfigWidget(VBox):
    """A widget that displays widgets to adjust NLPapayaViewer image parameters."""

    lut_options = [
        "Grayscale",
        "Red Overlay",
        "Green Overlay",
        "Blue Overlay",
        "Gold",
        "Spectrum",
        "Overlay (Positives)",
        "Overlay (Negatives)",
    ]

    def __init__(self, viewer, *args, **kwargs):
        """
        Parameters
        ----------
        viewer: NlPapayaViewer
            associated viewer.
        """
        super().__init__(*args, **kwargs)

        self._viewer = viewer
        self._init_widgets()

        self.children = [
            VBox([
                VBox(
                    [self._hist],
                    layout=Layout(
                        height="auto",
                        margin="0px 0px 0px 0px",
                        padding="5px 5px 5px 5px",
                    ),
                ),
                VBox(
                    [
                        self._alpha,
                        self._lut,
                        self._nlut,
                        self._min,
                        self._minp,
                        self._max,
                        self._maxp,
                        self._sym,
                    ],
                    layout=Layout(width="230px"),
                ),
            ])
        ]

    def _init_widgets(self):
        """Initializes all configuration widgets. Possible image config parameters are:"""
        layout = Layout(width="200px", max_width="200px")

        self._alpha = FloatSlider(
            value=1,
            min=0,
            max=1.0,
            step=0.1,
            description="alpha:",
            description_tooltip="Overlay image alpha level (0 to 1).",
            disabled=False,
            continuous_update=True,
            orientation="horizontal",
            readout=True,
            readout_format=".1f",
            layout=layout,
        )

        self._lut = Dropdown(
            options=PapayaConfigWidget.lut_options,
            value="Red Overlay",
            description="lut:",
            description_tooltip="The color table name.",
            layout=layout,
        )

        self._nlut = Dropdown(
            options=PapayaConfigWidget.lut_options,
            value="Red Overlay",
            description="negative-lut:",
            description_tooltip=
            "The color table name used by the negative side of the parametric pair.",
            layout=layout,
        )

        self._min = FloatText(
            value=None,
            description="min:",
            description_tooltip="The display range minimum.",
            step=0.01,
            continuous_update=True,
            disabled=False,
            layout=layout,
        )

        self._minp = BoundedFloatText(
            value=None,
            min=0,
            max=100,
            step=1,
            continuous_update=True,
            description="min %:",
            description_tooltip=
            "The display range minimum as a percentage of image max.",
            disabled=False,
            layout=layout,
        )

        self._max = FloatText(
            value=None,
            description="max:",
            description_tooltip="The display range maximum.",
            step=0.01,
            continuous_update=True,
            disabled=False,
            layout=layout,
        )

        self._maxp = BoundedFloatText(
            value=None,
            min=0,
            max=100,
            step=1,
            continuous_update=True,
            description="max %:",
            description_tooltip=
            "The display range minimum as a percentage of image max.",
            disabled=False,
            layout=layout,
        )

        self._sym = Checkbox(
            value=False,
            description="symmetric",
            description_tooltip=
            "When selected, sets the negative range of a parametric pair to the same size as the positive range.",
            disabled=False,
            layout=layout,
        )

        # figure to display histogram of image data
        fig = Figure()
        fig.update_layout(
            height=300,
            margin=dict(l=15, t=15, b=15, r=15, pad=4),
            showlegend=True,
            legend_orientation="h",
        )

        self._hist = FigureWidget(fig)
        self._hist.add_trace(
            Histogram(x=[], name="All image data", visible="legendonly"))
        self._hist.add_trace(Histogram(x=[], name="Image data without 0s"))

        self._handlers = defaultdict()

    def _set_values(self, config, range, data):
        """Sets config values from the specified `config` and creates histogram for `data`.

        Parameters
        ----------
        config : dict
            configuration parameters for the image. Possible keywords are:
            alpha : int
                the overlay image alpha level (0 to 1).
            lut : str
                the color table name.
            negative_lut : str
                the color table name used by the negative side of the parametric pair.
            max : int
                the display range maximum.
            maxPercent : int
                the display range maximum as a percentage of image max.
            min : int
                the display range minimum.
            minPercent : int
                the display range minimum as a percentage of image min.
           symmetric : bool
                if true, sets the negative range of a parametric pair to the same size as the positive range.
        range: float
            range of image values.
        data: []
           flattened image data.
        """
        self._alpha.value = config.get("alpha", 1)
        self._lut.value = config.get("lut", PapayaConfigWidget.lut_options[1])
        self._nlut.value = config.get("negative_lut",
                                      PapayaConfigWidget.lut_options[1])
        self._min.value = config.get("min", 0)
        self._minp.value = self._get_per_from_value(range,
                                                    config.get("min", 0))
        self._max.value = config.get("max", 0.1)
        self._maxp.value = self._get_per_from_value(range,
                                                    config.get("max", 0.1))
        self._sym.value = config.get("symmetric", "false") == "true"

        # set histogram data
        self._hist.data[0].x = data
        # leave out 0 values
        self._hist.data[1].x = [] if (data == []
                                      or data is None) else data[data != 0]

    def _add_handlers(self, image):
        """Add config widget event handlers to change the config values for the specified `image`.

        Parameters
        ----------
        image: neurolang_ipywidgets.PapayaImage
            image whose config values will be viewed/modified using this config widget.
        """

        # Dropdown does not support resetting event handlers after Dropdown.unobserve_all is called
        # So handlers are stored to be removed individually
        # github issue https://github.com/jupyter-widgets/ipywidgets/issues/1868

        self._handlers["alpha"] = partial(self._config_changed,
                                          image=image,
                                          name="alpha")
        self._handlers["lut"] = partial(self._config_changed,
                                        image=image,
                                        name="lut")
        self._handlers["nlut"] = partial(self._config_changed,
                                         image=image,
                                         name="negative_lut")
        self._handlers["min"] = partial(self._config_changed,
                                        image=image,
                                        name="min")
        self._handlers["minp"] = partial(self._set_min_max,
                                         image=image,
                                         name="minPercent")
        self._handlers["max"] = partial(self._config_changed,
                                        image=image,
                                        name="max")
        self._handlers["maxp"] = partial(self._set_min_max,
                                         image=image,
                                         name="maxPercent")
        self._handlers["sym"] = partial(self._config_bool_changed,
                                        image=image,
                                        name="symmetric")

        self._alpha.observe(self._handlers["alpha"], names="value")

        self._lut.observe(self._handlers["lut"], names="value")

        self._nlut.observe(self._handlers["nlut"], names="value")

        self._min.observe(self._handlers["min"], names="value")

        self._minp.observe(self._handlers["minp"], names="value")

        self._max.observe(self._handlers["max"], names="value")

        self._maxp.observe(self._handlers["maxp"], names="value")

        self._sym.observe(self._handlers["sym"], names="value")

    def _remove_handlers(self):
        """Removes all event handlers set for the config widgets."""
        if len(self._handlers):
            self._alpha.unobserve(self._handlers["alpha"], names="value")
            self._lut.unobserve(self._handlers["lut"], names="value")
            self._nlut.unobserve(self._handlers["nlut"], names="value")
            self._min.unobserve(self._handlers["min"], names="value")
            self._minp.unobserve(self._handlers["minp"], names="value")
            self._max.unobserve(self._handlers["max"], names="value")
            self._maxp.unobserve(self._handlers["maxp"], names="value")
            self._sym.unobserve(self._handlers["sym"], names="value")

            self._handlers = defaultdict()

    @debounce(0.5)
    def _config_changed(self, change, image, name):
        if name == "min":
            self._minp.unobserve(self._handlers["minp"], names="value")
            self._minp.value = self._get_per_from_value(
                image.range, change.new)
            self._minp.observe(self._handlers["minp"], names="value")
        elif name == "max":
            self._maxp.unobserve(self._handlers["maxp"], names="value")
            self._maxp.value = self._get_per_from_value(
                image.range, change.new)
            self._maxp.observe(self._handlers["maxp"], names="value")

        self._set_config(image, name, change.new)

    @debounce(0.5)
    def _set_min_max(self, change, image, name):
        if name == "minPercent":
            self._min.unobserve(self._handlers["min"], names="value")
            self._min.value = self._get_value_from_per(image.range, change.new)
            self._set_config(image, "min", self._min.value)
            self._min.observe(self._handlers["min"], names="value")
        elif name == "maxPercent":
            self._max.unobserve(self._handlers["max"], names="value")
            self._max.value = self._get_value_from_per(image.range, change.new)
            self._set_config(image, "max", self._max.value)
            self._max.observe(self._handlers["max"], names="value")

    def _config_bool_changed(self, change, image, name):
        value = "false"
        if change.new:
            value = "true"
        self._set_config(image, name, value)

    def _set_config(self, image, key, value):
        image.config[key] = value
        self._viewer.set_images()

    def _get_per_from_value(self, range, value):
        return round(value * 100 / range, 0)

    def _get_value_from_per(self, range, per):
        return round(per * range / 100, 2)

    def set_image(self, image):
        """Sets the image whose config values will be viewed/modified using this config widget.
        If image is `None`, all config values are reset.

        Parameters
        ----------
        image: neurolang_ipywidgets.PapayaImage
            image whose config values will be viewed/modified using this config widget.
        """
        if image:
            self._remove_handlers()
            self._set_values(image.config, image.range,
                             image.image.get_fdata().flatten())
            self._add_handlers(image)
        else:
            self.reset()

    def reset(self):
        """Resets values for all config widgets."""
        self._remove_handlers()
        self._set_values({}, 100, [])
        self.layout.visibility = "hidden"
Beispiel #15
0
    def createFigureWidget(self, x_id, y_id_list):
        traces = []
        alpha_min, alpha_max, alpha_delta = self.getDeltaFunction(len(y_id_list))
        alpha_val = alpha_max    
        x_values = self.data[x_id].flatten()        
        x_sort = x_values.argsort()
        for y_id in y_id_list:
            y_values = self.data[y_id].flatten()
            color = "#444444"
            color = 'rgba'+str(self.getDeltaColor(color, alpha_val))
            trace = {
                'type': "scattergl", 'mode': self.options['marker_type'].value, 'name': self.data.label + "_" + y_id,
                'line' : { 'width' : self.options['line_width'].value, 'color' : color },
                'x': x_values[x_sort],
                'y': y_values[x_sort],
            }
            if self.only_subsets == False:
                traces.append(trace)
            alpha_val = alpha_val - alpha_delta
            
        for sset in self.data.subsets:
            if hasattr(sset,"disabled") == False or sset.disabled == False:            
                alpha_val = alpha_max   
                x_values = sset[x_id].flatten()    
                x_sort = x_values.argsort()
                for step, y_id in enumerate(y_id_list):
                    y_values = sset[y_id].flatten()            
                    color = sset.style.color
                    color = 'rgba'+str(self.getDeltaColor(color, alpha_val, step))
                    trace = {
                        'type': "scattergl", 'mode': self.options['marker_type'].value, 'name': sset.label + "_" + y_id,
                        'line' : { 'width' : self.options['line_width'].value, 'color' : color},
                        'x': x_values[x_sort],
                        'y': y_values[x_sort],
                    }
                    traces.append(trace)  
                    alpha_val = alpha_val - alpha_delta                
        y_color = 'rgb(0,0,0)'
        if len(y_id_list) == 1:
            y_color = self.data.get_component(y_id_list[0]).color

        layout = {
            'margin' : {'l':50,'r':0,'b':50,'t':30 },
            'xaxis': { 'autorange' : True, 'zeroline': True, 
                'title' : self.options['xaxis'].value, 
                'type' : self.options['xscale'].value ,
                'linecolor' : self.data.get_component(x_id).color,
                'tickcolor' : self.data.get_component(x_id).color,
                'ticklen' : 4,
                'linewidth' : 4,                
            },
            'yaxis': { 'autorange':True, 'zeroline': True, 
                'title' : self.options['yaxis'].value, 
                'type' : self.options['yscale'].value,
                'linecolor' : y_color,
                'tickcolor' : y_color,
                'ticklen' : 4,
                'linewidth' : 4,                
            },            
            'showlegend': self.margins['showlegend'].value,
            'legend' : {
                'orientation' : self.margins['legend_orientation'].value,
                'x' : self.margins['legend_xpos'].value,
                'y' : self.margins['legend_ypos'].value
            },               
        }
        return FigureWidget({
                'data': traces,
                'layout': layout
        })
Beispiel #16
0
    def createFigureWidget(self, x_id, y_id):

        traces = []

        color = "#444444"
        df = self.data.to_dataframe()
        df_merge = df.merge(df, on=y_id)
        dfo = pd.crosstab(df_merge[x_id + '_x'], df_merge[x_id + '_y'])
        idx = dfo.columns.union(dfo.index)
        dfo = dfo.reindex(index=idx, columns=idx, fill_value=0)
        self.G = nx.convert_matrix.from_pandas_adjacency(dfo)
        if self.options['layout_type'].value == 'bipartite_layout':
            pos = nx.bipartite_layout(self.G)
        elif self.options['layout_type'].value == 'circular_layout':
            pos = nx.circular_layout(self.G)
        elif self.options['layout_type'].value == 'kamada_kawai_layout':
            pos = nx.kamada_kawai_layout(self.G)
        elif self.options['layout_type'].value == 'planar_layout':
            pos = nx.planar_layout(self.G)
        elif self.options['layout_type'].value == 'random_layout':
            pos = nx.random_layout(self.G)
        elif self.options['layout_type'].value == 'shell_layout':
            pos = nx.shell_layout(self.G)
        elif self.options['layout_type'].value == 'spectral_layout':
            pos = nx.spectral_layout(self.G)
        elif self.options['layout_type'].value == 'shell_layout':
            pos = nx.shell_layout(self.G)
        else:
            pos = nx.spring_layout(
                self.G,
                k=self.options['layout_k'].value,
                iterations=self.options['layout_iter'].value)

        degree = dict(nx.degree(self.G))

        n_x = [0 for i in range(len(self.G.nodes()))]
        n_y = [0 for i in range(len(self.G.nodes()))]
        n_t = ['' for i in range(len(self.G.nodes()))]
        n_s = [0 for i in range(len(self.G.nodes()))]
        for i, node in enumerate(self.G.nodes()):
            n_x[i] = pos[node][0]
            n_y[i] = pos[node][1]
            n_t[i] = node
            n_s[i] = degree[node]

        max_degree = max(n_s)
        min_degree = min(n_s)
        n_s = [
            math.ceil(
                (n - min_degree) / (max_degree - min_degree) *
                self.focused_size_marker * self.options['marker_size'].value) +
            self.options['marker_size'].value for n in n_s
        ]

        node_trace = {
            'type': "scatter",
            'name': self.data.label + "_" + x_id,
            'x': n_x,
            'y': n_y,
            'text': n_t,
            'mode': 'markers',
            'hoverinfo': 'text',
            'marker': {
                'size': n_s,
                'color': self.data.get_component(x_id).color,
                'line': {
                    'width': self.options['line_width'].value,
                    'color': self.data.get_component(x_id).color,
                }
            },
            'showlegend': True,
        }

        e_x = [0 for i in range(2 * len(self.G.edges()))]
        e_y = [0 for i in range(2 * len(self.G.edges()))]

        for i, edge in enumerate(self.G.edges()):
            e_x[2 * i] = pos[edge[0]][0]
            e_y[2 * i] = pos[edge[0]][1]
            e_x[2 * i + 1] = pos[edge[1]][0]
            e_y[2 * i + 1] = pos[edge[1]][1]

        color = self.data.get_component(y_id).color
        color = re.match(r"rgb\((\d+),\s*(\d+),\s*(\d+)\)", color)
        color = (int(color[1]), int(color[2]), int(color[3]))
        color = '#%02x%02x%02x' % color
        color = 'rgba' + str(self.getDeltaColor(color, 0.2))

        edge_trace = {
            'type': "scatter",
            'name': self.data.label + "_" + y_id,
            'x': e_x,
            'y': e_y,
            'line': {
                'width': self.options['line_width'].value,
                'color': color,
            },
            'hoverinfo': 'none',
            'mode': 'lines',
            'showlegend': True,
        }

        if self.only_subsets == False:
            traces.append(edge_trace)

        for sset in self.data.subsets:
            if hasattr(sset, "disabled") == False or sset.disabled == False:
                color = sset.style.color

                dfs = df[sset.to_mask()]
                df_merge = dfs.merge(dfs, on=y_id)
                dfs = pd.crosstab(df_merge[x_id + '_x'], df_merge[x_id + '_y'])
                idx = dfs.columns.union(dfs.index)
                dfs = dfs.reindex(index=idx, columns=idx, fill_value=0)
                G = nx.convert_matrix.from_pandas_adjacency(dfs)

                e_x = [0 for i in range(2 * len(G.edges()))]
                e_y = [0 for i in range(2 * len(G.edges()))]

                for i, edge in enumerate(G.edges()):
                    e_x[2 * i] = pos[edge[0]][0]
                    e_y[2 * i] = pos[edge[0]][1]
                    e_x[2 * i + 1] = pos[edge[1]][0]
                    e_y[2 * i + 1] = pos[edge[1]][1]

                trace = {
                    'type': "scatter",
                    'name': str(sset.label) + "_" + str(y_id),
                    'x': e_x,
                    'y': e_y,
                    'line': {
                        'width': self.options['line_width'].value,
                        'color': color
                    },
                    'hoverinfo': 'none',
                    'mode': 'lines',
                    'showlegend': True,
                }

                traces.append(trace)

        traces.append(node_trace)

        layout = {
            'title': self.options['title'].value,
            'margin': {
                'l': self.margins['left'].value,
                'r': self.margins['right'].value,
                'b': self.margins['bottom'].value,
                't': self.margins['top'].value
            },
            'xaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['xaxis'].value,
                'autorange': True,
                'zeroline': False,
                'showticklabels': False,
                'showline': True,
                'showgrid': False,
            },
            'yaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['yaxis'].value,
                'autorange': True,
                'zeroline': False,
                'showticklabels': False,
                'showline': True,
                'showgrid': False,
            },
            'showlegend': self.margins['showlegend'].value,
            'legend': {
                'orientation': self.margins['legend_orientation'].value,
                'x': self.margins['legend_xpos'].value,
                'y': self.margins['legend_ypos'].value
            }
        }
        return FigureWidget({'data': traces, 'layout': layout})
Beispiel #17
0
    def createFigureWidget(self):
        traces = []
        xtickmode = "auto"
        xtickvals = None
        xticktext = None
        dimensions = []
        color = "#444444"
        for dimension in self.dimensions:
            value = {'label': dimension}
            if hasattr(self.data[dimension].flatten(), 'codes'):
                val = self.data[dimension].flatten().codes.tolist()
            else:
                val = self.data[dimension].flatten()
            value["values"] = val
            dimensions.append(value)

        trace = {
            'type':
            "splom",
            'name':
            self.data.label,
            'dimensions':
            dimensions,
            'marker':
            dict({
                'symbol': 'circle',
                'size': self.options['marker_size'].value,
                'color': color,
                'line': {
                    'width': self.options['line_width'].value,
                    'color': color
                }
            }),
            'showupperhalf':
            False,
            'diagonal': {
                'visible': False
            },
        }
        if self.only_subsets == False:
            traces.append(trace)

        for sset in self.data.subsets:
            if hasattr(sset, "disabled") == False or sset.disabled == False:
                color = sset.style.color
                dimensions = []
                for dimension in self.dimensions:
                    value = {'label': dimension}
                    if hasattr(sset[dimension].flatten(), 'codes'):
                        val = sset[dimension].flatten().codes.tolist()
                    else:
                        val = sset[dimension].flatten()
                    value["values"] = val
                    dimensions.append(value)
                trace = {
                    'type':
                    "splom",
                    'name':
                    sset.label,
                    'dimensions':
                    dimensions,
                    'marker':
                    dict({
                        'symbol': 'circle',
                        'size': self.options['marker_size'].value,
                        'color': color,
                        'line': {
                            'width': self.options['line_width'].value,
                            'color': color
                        }
                    }),
                    'showupperhalf':
                    False,
                    'diagonal': {
                        'visible': False
                    },
                }
                traces.append(trace)

        layout = {
            'title': self.options['title'].value,
            'margin': {
                'l': self.margins['left'].value,
                'r': self.margins['right'].value,
                'b': self.margins['bottom'].value,
                't': self.margins['top'].value
            },
            'showlegend': self.margins['showlegend'].value,
            'legend': {
                'orientation': self.margins['legend_orientation'].value,
                'x': self.margins['legend_xpos'].value,
                'y': self.margins['legend_ypos'].value
            },
            'plot_bgcolor': 'rgba(245,245,245, 0.95)',
        }
        for i, dimension in enumerate(self.dimensions):
            layout['xaxis' + str(i + 1)] = {
                'autorange': True,
                'zeroline': False,
                'showline': True,
                #'mirror' : 'ticks',
                'linecolor': self.data.get_component(dimension).color,
                'tickcolor': self.data.get_component(dimension).color,
                'ticklen': 4,
                'linewidth': 4,
            }
            layout['yaxis' + str(i + 1)] = {
                'autorange': True,
                'zeroline': False,
                'showline': True,
                #'mirror' : 'ticks',
                'linecolor': self.data.get_component(dimension).color,
                'tickcolor': self.data.get_component(dimension).color,
                'ticklen': 4,
                'linewidth': 4,
            }

        return FigureWidget({'data': traces, 'layout': layout})
    def createFigureWidget(self):
        dimensions = self.dimensions
        data_lines = [] 
        nodes = [{} for dim in dimensions]
        values = []
        colors = []
        traces = []
        if self.options['colorscale'].value == GlueParallelCategoriesPlotly.default_color:
            color_set = [self.data.get_component(dim).color for dim in dimensions]
        else:
            color_set = cl.scales['8']['qual'][self.options['colorscale'].value]
            if (len(self.dimensions) > 8):
                color_set = cl.interp( color_set, len(dimensions) )
            color_set = cl.to_rgb(color_set)
        for i, dimension in enumerate(dimensions):
            dvalues = np.unique(self.data[dimension].flatten())            
            if len(dvalues) < self.options['grouping_limit'].value or hasattr(self.data[dimension].flatten(), 'codes'):
                nodes[i]['values'] = np.unique(self.data[dimension].flatten())
                nodes[i]['masks'] = []
                for val in nodes[i]['values']:
                     nodes[i]['masks'].append(self.data[dimension] == val)
            else:
                hist, bin_edges  = np.histogram(self.data[dimension].flatten(), bins='auto')
                if (len(bin_edges) > self.options['grouping_limit'].value):
                    hist, bin_edges  = np.histogram(self.data[dimension].flatten(), bins=self.options['grouping_limit'].value)
                
                nodes[i]['values'] = []
                for edge in range(len(bin_edges)-1):
                    nodes[i]['values'].append( "{:.1f}".format(bin_edges[edge]) + " - " + "{:.1f}".format(bin_edges[edge+1]))
                nodes[i]['masks'] = []
                for edge in range(len(bin_edges)-1):
                    if edge == 0:
                        nodes[i]['masks'].append((self.data[dimension] >= bin_edges[edge]) & (self.data[dimension] <= bin_edges[edge+1]))      
                    else : 
                        nodes[i]['masks'].append((self.data[dimension] > bin_edges[edge]) & (self.data[dimension] <= bin_edges[edge+1]))      
                        
            line = {}
            
            line['values'] = np.array(['' for i in range(self.data.size)], dtype = 'object')
            colorv = np.array(['#EEEEEE' for i in range(self.data.size)])
            for k, value in enumerate(nodes[i]['values']):
                mask = nodes[i]['masks'][k] 
                line['values'][mask] = value
                for sset in self.data.subsets:
                    if hasattr(sset,"disabled") == False or sset.disabled == False:            
                        sset_mask = sset.to_mask()
                        color = sset.style.color
                        colorv[mask & sset_mask] = color
                        mask = mask & ~sset_mask
            colors.extend(colorv)
            line['values'] = line['values'].tolist()
            line['label'] = dimension            
            data_lines.append(line);
            
        
            
        parcats = {
            'type' : 'parcats',
            'dimensions' : data_lines,
            'line' :  {
                'color' : colors,
                'shape': 'hspline'
            }
            
        }

        traces.append(parcats)

        layout = {
            'title' : self.options['title'].value,
            'xaxis': {
                'title' : self.options['xaxis'].value,
                'range' : [0,1],
                'showgrid':False,
                'showline':False,
                'showticklabels':False,
                'zeroline':False
            },
            'yaxis': {
                'title' : self.options['yaxis'].value,
                'range' : [0,1],
                'showgrid':False,
                'showline':False,
                'showticklabels':False,
                'zeroline':False                
            },
            'showlegend': self.margins['showlegend'].value,            
            'legend' : {
                'orientation' : self.margins['legend_orientation'].value,
                'x' : self.margins['legend_xpos'].value,
                'y' : self.margins['legend_ypos'].value
            }
        }
        

        if self.only_subsets == False:
            trace = {
                'type': "scatter",
                'name' : self.data.label, 
                'textposition' : 'middle right',
                'x' : [-1000],
                'y' : [i],
                'mode' : 'markers',
                'marker': {
                    'color' : "#EEEEEE",
                    'size' : 20,
                    'line': {
                        'width': 0,
                    },
                    'symbol' : 'square'
                }
            }
            traces.append(trace)

        for sset in self.data.subsets:
            color = sset.style.color
            trace = {
                'type': "scatter",
                'name' : sset.label, 
                'textposition' : 'middle right',
                'x' : [-1000],
                'y' : [i],
                'mode' : 'markers',
                'marker': {
                    'color' : color,
                    'size' : 20,
                    'line': {
                        'width': 0,
                    },
                    'symbol' : 'square'
                }
            }
            traces.append(trace)

            
        data = traces
        FigureWidget(data = data, layout = layout)
        return FigureWidget(data = data, layout = layout)
Beispiel #19
0
    def createFigureWidget(self, x_id, y_id_list):
        traces = []
        alpha_min, alpha_max, alpha_delta = self.getDeltaFunction(
            len(y_id_list))
        alpha_val = alpha_max
        xtickmode = "auto"
        xtickvals = None
        xticktext = None
        if hasattr(self.data[x_id].flatten(), 'codes'):
            x_val = self.data[x_id].flatten().codes.tolist()
            tickvals, tickmask = np.unique(self.data[x_id].flatten().codes,
                                           return_index=True)
            ticktext = self.data[x_id][tickmask]
            xtickmode = "array"
            xtickvals = tickvals.tolist()
            xticktext = ticktext.tolist()
        else:
            x_val = self.data[x_id].flatten()
        for step, y_id in enumerate(y_id_list):
            color = "#444444"
            color = 'rgba' + str(self.getDeltaColor(color, alpha_val, step))
            if hasattr(self.data[y_id].flatten(), 'codes'):
                y_val = self.data[y_id].flatten().codes.tolist()
            else:
                y_val = self.data[y_id].flatten()

            trace = {
                'type': "scattergl",
                'mode': "markers",
                'name': self.data.label + "_" + y_id,
                'marker': {
                    'symbol': 'circle',
                    'size': self.options['marker_size'].value,
                    'color': color,
                    'line': {
                        'width': self.options['line_width'].value,
                        'color': color
                    }
                },
                'x': x_val,
                'y': y_val,
            }
            if self.only_subsets == False:
                traces.append(trace)
            alpha_val = alpha_val - alpha_delta

        for sset in self.data.subsets:
            if hasattr(sset, "disabled") == False or sset.disabled == False:
                alpha_val = alpha_max
                if hasattr(sset[x_id].flatten(), 'codes'):
                    x_val = sset[x_id].flatten().codes.tolist()
                else:
                    x_val = sset[x_id].flatten()
                for step, y_id in enumerate(y_id_list):
                    if hasattr(sset[y_id].flatten(), 'codes'):
                        y_val = sset[y_id].flatten().codes.tolist()
                    else:
                        y_val = sset[y_id].flatten().astype('float')
                    color = sset.style.color
                    color = 'rgba' + str(
                        self.getDeltaColor(color, alpha_val, step))
                    trace = {
                        'type': "scattergl",
                        'mode': "markers",
                        'name': sset.label + "_" + y_id,
                        'marker': {
                            'symbol': 'circle',
                            'size': self.options['marker_size'].value,
                            'color': color,
                            'line': {
                                'width': self.options['line_width'].value,
                                'color': color
                            }
                        },
                        'x': x_val,
                        'y': y_val,
                    }
                    traces.append(trace)
                    alpha_val = alpha_val - alpha_delta

        y_fid = y_id_list[0]
        layout = {
            'title': self.options['title'].value,
            'margin': {
                'l': self.margins['left'].value,
                'r': self.margins['right'].value,
                'b': self.margins['bottom'].value,
                't': self.margins['top'].value
            },
            'xaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['xaxis'].value,
                'type': self.options['xscale'].value,
                'tickmode': xtickmode,
                'tickvals': xtickvals,
                'ticktext': xticktext,
                'linecolor': self.data.get_component(x_id).color,
                'tickcolor': self.data.get_component(x_id).color,
                'ticklen': 4,
                'linewidth': 4,
            },
            'yaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['yaxis'].value,
                'type': self.options['yscale'].value,
                'linecolor': self.data.get_component(y_fid).color,
                'tickcolor': self.data.get_component(y_fid).color,
                'ticklen': 4,
                'linewidth': 4,
            },
            'showlegend': self.margins['showlegend'].value,
            'legend': {
                'orientation': self.margins['legend_orientation'].value,
                'x': self.margins['legend_xpos'].value,
                'y': self.margins['legend_ypos'].value
            }
        }
        return FigureWidget({'data': traces, 'layout': layout})
Beispiel #20
0
    def display_eps(player_gw_eps: DF) -> Widget:
        data = (player_gw_eps
                .reset_index()
                .sort_values(['Season', 'Game Week']))

        data_formatted = data.pipe(ctx.dd.format)
        data = (data.assign(**{'Label': 'FDR: ' + data_formatted['Fixture Short Name FDR'] + ', '
                                        + 'Rel. Strength: ' + data_formatted['Rel Strength'] + ', '
                                        + 'Cost: ' + data_formatted['Fixture Cost']})
                .assign(**{'Game Week': lambda df: 'GW ' + df['Game Week'].apply('{:.0f}'.format)})
                .assign(**{'Season Game Week': lambda df: df['Season'] + ', GW ' + df['Game Week']}))

        x_axis = [data['Season'], data['Game Week']]

        eps_trace = Scatter(x=x_axis,
                            y=data['Expected Points'],
                            name='Expected Points',
                            line=dict(color='rgb(255, 127, 14)'),
                            mode='lines')

        ftp_trace = Scatter(x=x_axis,
                            y=data['Fixture Total Points'],
                            name='Actual Points',
                            line=dict(color='rgba(44, 160, 44, 0.3)'),
                            mode='lines')

        ftpr_trace = Scatter(x=x_axis,
                             y=data['Rolling Avg Game Points'],
                             name='Rolling Actual Points',
                             line=dict(color='rgb(44, 160, 44)'),
                             line_shape='spline',
                             mode='lines')

        fs_trace = Scatter(x=x_axis,
                           y=data['Expected Points'],
                           name='Rel. Strength',
                           mode='markers',
                           marker=dict(color=(data['Team FDR'].fillna(3)), colorscale=FDR_COLOR_SCALE),
                           text=data['Label'])

        last_gw = [ctx.current_season, f'GW {ctx.next_gw - 1}']
        first_gw = [ctx.current_season, 'GW 1']
        last_season_gws_color = 'rgb(230, 230, 230)'
        past_gws_color = 'rgb(240, 240, 240)'

        last_season_shape = Shape(type='rect', yref='paper', x0=-6, x1=first_gw, y0=0, y1=1, fillcolor=last_season_gws_color, layer='below', line_width=0, opacity=0.5)
        past_shape = Shape(type='rect', yref='paper', x0=first_gw, x1=last_gw, y0=0, y1=1, fillcolor=past_gws_color, layer='below', line_width=0, opacity=0.5)
        start_shape = Shape(type='line', yref='paper', x0=first_gw, x1=first_gw, y0=0, y1=1, line=dict(width=2, color='DarkGrey'), layer='below')
        current_gw_shape = Shape(type='line', yref='paper', x0=last_gw, x1=last_gw, y0=0, y1=1, line=dict(width=2, color='DarkGrey'), layer='below')

        max_points = max(max(data['Expected Points'].max(), data['Fixture Total Points'].max()) + 1, 15)
        min_points = min(data['Expected Points'].min(), data['Fixture Total Points'].min()) - 1

        layout = dict(
            yaxis=dict(title=f'Points', showspikes=True, range=[min_points, max_points]),
            xaxis=dict(tickfont=dict(size=8)),
            shapes=[last_season_shape, start_shape, past_shape, current_gw_shape],
            hovermode='closest',
            legend=dict(yanchor='top', xanchor='left', x=0, y=1, bgcolor='rgba(0,0,0,0)'),
            height=300,
            margin=dict(l=20, r=0, t=5, b=20, pad=0),
            annotations=[
                dict(x=last_gw, y=0.9, yref='paper', text='Last Game Week', ax=80, ay=0),
                dict(x=first_gw, y=0.9, yref='paper', text='Start of Season', ax=-80, ay=0)
            ]
        )
        return VBox([HTML('<h3>Expected Points vs Actual Points</h3>'),
                     FigureWidget([ftp_trace, ftpr_trace, eps_trace, fs_trace], layout=layout)])
Beispiel #21
0
    def createFigureWidget(self):
        data_lines = []
        traces = []
        i=0
        if self.only_subsets == False:
            colors = [i for r in range(self.data.size)]
            colorscale = [[0,'#EEEEEE']]
        else:
            colors = []
            colorscale = []
            i=-1

        values = []
        labels = []
        nodes_id = []
        parents = []
        self.masks = []
        colors = []
        df = self.data.to_dataframe()
        ids = 1
        queue = deque()
        if self.options['colorscale'].value == GlueSunburstPlotly.default_color:
            color_set = [self.data.get_component(dim).color for dim in self.dimensions]
        else:
            color_set = cl.scales['8']['qual'][self.options['colorscale'].value]
            if (len(self.dimensions) > 8):
                color_set = cl.interp( color_set, len(dimensions) )
            color_set = cl.to_rgb(color_set)
        color_set.insert(0,'rgb(255,255,255)')    
        
        queue.append({'id':ids,'dimension':0, 'label':"Data", 'mask':[True for i in range(self.data.size)], 'parent':''})
        while len(queue) > 0:
            toprocess = queue.popleft()
            mask = toprocess['mask']
            id = toprocess['id']
            label = toprocess['label']
            value = np.count_nonzero(mask)
            if value > 0:
                labels.append(label)
                values.append(value)
                parents.append(toprocess['parent'])
                nodes_id.append(id)
                self.masks.append(mask)
                colors.append(color_set[toprocess['dimension']])
                data = df[mask]
                if toprocess['dimension'] < len(self.dimensions):
                    dimension = self.dimensions[toprocess['dimension']]
                    dvalues = np.unique(data[dimension].ravel())
                    if hasattr(self.data[dimension].flatten(), 'codes'):
                        for val in dvalues:
                            ids += 1
                            process = {'id':ids,'dimension':toprocess['dimension']+1, 'label':str(val), 'parent':toprocess['id']}
                            process['mask'] = (mask & (self.data[dimension] == val))
                            queue.append(process)
                                          
                    elif len(dvalues) < self.options['grouping_limit'].value:
                        for val in dvalues:
                            ids += 1
                            process = {'id':ids,'dimension':toprocess['dimension']+1, 'label':str(val), 'parent':toprocess['id']}
                            process['mask'] = (mask & (self.data[dimension] == val))
                            queue.append(process)
                    else:    
                        hist, bin_edges  = np.histogram(self.data[dimension].flatten(), bins='auto')
                        if (len(bin_edges) > self.options['grouping_limit'].value):
                            hist, bin_edges  = np.histogram(self.data[dimension].flatten(), bins=self.options['grouping_limit'].value)                        
                        for edge in range(len(bin_edges)-1):
                            ids += 1
                            label = "{:.1f}".format(bin_edges[edge]) + " - " + "{:.1f}".format(bin_edges[edge+1])
                            process = {'id':ids,'dimension':toprocess['dimension']+1, 'label':label, 'parent':toprocess['id']}
                            if edge == 0:
                                process['mask'] = (mask & ((self.data[dimension] >= bin_edges[edge]) & (self.data[dimension] <= bin_edges[edge+1])))
                            else :
                                process['mask'] = (mask & ((self.data[dimension] > bin_edges[edge])  & (self.data[dimension] <= bin_edges[edge+1])))
                            queue.append(process)
        trace = {
            'type': "sunburst",
            'ids': nodes_id,
            'labels': labels,
            'parents': parents,
            'values':  values,
            'branchvalues' : 'total',
            'maxdepth' : 4,
            'outsidetextfont': {
                'size': 20, 
                'color': "#377eb8"
            },
            'leaf': {
                'opacity' : 1
            },
            'marker': {
                'line': {
                    'width': 2
                },
                'colors' : colors
            },
            'domain':{
                'x': [0, 0.9],
                'y': [0, 1],
            },              
        }
        traces.append(trace)
        
        layout = {
            'title' : self.options['title'].value,
            'margin' : {
                'l':self.margins['left'].value,
                'r':self.margins['right'].value,
                'b':self.margins['bottom'].value,
                't':self.margins['top'].value
            },
            'showlegend': True,
            'xaxis': {
                'title' : self.options['xaxis'].value,
                'range' : [0,1],
                'showgrid':False,
                'showline':False,
                'showticklabels':False,
                'zeroline':False
            },
            'yaxis': {
                'title' : self.options['yaxis'].value,
                'range' : [0,1],
                'showgrid':False,
                'showline':False,
                'showticklabels':False,
                'zeroline':False                
            },      
            'showlegend': self.margins['showlegend'].value,
            'legend' : {
                'orientation' : self.margins['legend_orientation'].value,
                'x' : self.margins['legend_xpos'].value,
                'y' : self.margins['legend_ypos'].value
            }
        }
        
        for i in range(len(self.dimensions)):
            dimension = self.dimensions[i]
            trace = {
                'type': "scatter",
                'name' : dimension, 
                'textposition' : 'middle right',
                'x' : [-1000],
                'y' : [i],
                'mode' : 'markers',
                'marker': {
                    'color' : color_set[i+1],
                    'size' : 20,
                    'line': {
                        'width': 0,
                    },
                    'symbol' : 'square'
                }
            }
            traces.append(trace)




            
        return FigureWidget({
                'data': traces,
                'layout': layout
        })
Beispiel #22
0
def show_indexed_timeseries_plotly(
    timeseries: TimeSeries,
    istart: int = 0,
    istop: int = None,
    time_window: list = None,
    trace_range: list = None,
    offsets=None,
    fig: go.FigureWidget = None,
    col=None,
    row=None,
    zero_start=False,
    scatter_kwargs: dict = None,
    figure_kwargs: dict = None,
):
    if istart != 0 or istop is not None:
        if time_window is not None:
            raise ValueError(
                "input either time window or istart/stop but not both")
        if not (0 <= istart < timeseries.data.shape[0] and
                (istop is None or 0 < istop <= timeseries.data.shape[0])):
            raise ValueError("enter correct istart/stop values")
        t_istart = istart
        t_istop = istop
    elif time_window is not None:
        t_istart = timeseries_time_to_ind(timeseries, time_window[0])
        t_istop = timeseries_time_to_ind(timeseries, time_window[1])
    else:
        t_istart = istart
        t_istop = istop
    tt = get_timeseries_tt(timeseries, istart=t_istart, istop=t_istop)
    data, unit = get_timeseries_in_units(timeseries,
                                         istart=t_istart,
                                         istop=t_istop)
    if len(data.shape) == 1:
        data = data[:, np.newaxis]
    if trace_range is not None:
        if not (0 <= trace_range[0] < data.shape[1]
                and 0 < trace_range[1] <= data.shape[1]):
            raise ValueError("enter correct trace range")
        trace_istart = trace_range[0]
        trace_istop = trace_range[1]
    else:
        trace_istart = 0
        trace_istop = data.shape[1]
    if offsets is None:
        offsets = np.zeros(trace_istop - trace_istart)
    if zero_start:
        tt = tt - tt[0]
    scatter_kwargs = dict() if scatter_kwargs is None else scatter_kwargs
    if fig is None:
        fig = go.FigureWidget(make_subplots(rows=1, cols=1))
    row = 1 if row is None else row
    col = 1 if col is None else col
    for i, trace_id in enumerate(range(trace_istart, trace_istop)):
        fig.add_trace(
            go.Scattergl(x=tt,
                         y=data[:, trace_id] + offsets[i],
                         mode="lines",
                         **scatter_kwargs),
            row=row,
            col=col,
        )
    input_figure_kwargs = dict(
        xaxis=dict(title_text="time (s)", range=[tt[0], tt[-1]]),
        yaxis=dict(title_text=unit if unit is not None else None),
        title=timeseries.name,
    )
    if figure_kwargs is None:
        figure_kwargs = dict()
    input_figure_kwargs.update(figure_kwargs)
    fig.update_xaxes(input_figure_kwargs.pop("xaxis"), row=row, col=col)
    fig.update_yaxes(input_figure_kwargs.pop("yaxis"), row=row, col=col)
    fig.update_layout(**input_figure_kwargs)
    return fig
Beispiel #23
0
    def createFigureWidget(self):
        data_lines = []
        traces = []
        sources = []
        targets = []
        values = []
        labels = []
        parents = []
        self.masks = []
        colors = []
        df = self.data.to_dataframe()
        ids = 0
        queue = deque()
        if self.options[
                'colorscale'].value == GlueSankeytreePlotly.default_color:
            color_set = [
                self.data.get_component(dim).color for dim in self.dimensions
            ]
        else:
            color_set = cl.scales['8']['qual'][
                self.options['colorscale'].value]
            if (len(self.dimensions) > 8):
                color_set = cl.interp(color_set, len(self.dimensions))
            color_set = cl.to_rgb(color_set)

        queue.append({
            'id': ids,
            'dimension': 0,
            'label': 'Data',
            'mask': [True for i in range(self.data.size)],
            'parent': ''
        })
        while len(queue) > 0:
            toprocess = queue.popleft()
            mask = toprocess['mask']
            id = toprocess['id']
            label = toprocess['label']
            value = np.count_nonzero(mask)
            if True:  #value > 0:
                labels.append(label)
                sources.append(toprocess['parent'])
                targets.append(id)
                values.append(value)
                parents.append(toprocess['parent'])
                colors.append(color_set[toprocess['dimension'] - 1])
                self.masks.append(mask)
                data = df[mask]
                if toprocess['dimension'] < len(self.dimensions):
                    dimension = self.dimensions[toprocess['dimension']]
                    dvalues = np.unique(data[dimension].ravel())
                    if hasattr(self.data[dimension].flatten(), 'codes'):
                        for val in dvalues:
                            ids += 1
                            process = {
                                'id': ids,
                                'dimension': toprocess['dimension'] + 1,
                                'label': str(val),
                                'parent': toprocess['id']
                            }
                            process['mask'] = (mask &
                                               (self.data[dimension] == val))
                            queue.append(process)

                    elif len(dvalues) < self.options['grouping_limit'].value:
                        for val in dvalues:
                            ids += 1
                            process = {
                                'id': ids,
                                'dimension': toprocess['dimension'] + 1,
                                'label': str(val),
                                'parent': toprocess['id']
                            }
                            process['mask'] = (mask &
                                               (self.data[dimension] == val))
                            queue.append(process)
                    else:
                        hist, bin_edges = np.histogram(
                            self.data[dimension].flatten(), bins='auto')
                        if (len(bin_edges) >
                                self.options['grouping_limit'].value):
                            hist, bin_edges = np.histogram(
                                self.data[dimension].flatten(),
                                bins=self.options['grouping_limit'].value)
                        for edge in range(len(bin_edges) - 1):
                            ids += 1
                            label = '{:.1f}'.format(
                                bin_edges[edge]) + ' - ' + '{:.1f}'.format(
                                    bin_edges[edge + 1])
                            process = {
                                'id': ids,
                                'dimension': toprocess['dimension'] + 1,
                                'label': label,
                                'parent': toprocess['id']
                            }
                            if edge == 0:
                                process['mask'] = (mask & (
                                    (self.data[dimension] >= bin_edges[edge]) &
                                    (self.data[dimension] <=
                                     bin_edges[edge + 1])))
                            else:
                                process['mask'] = (mask & (
                                    (self.data[dimension] > bin_edges[edge]) &
                                    (self.data[dimension] <=
                                     bin_edges[edge + 1])))
                            queue.append(process)

        trace = {
            'type': 'sankey',
            'node': {
                'pad': 15,
                'thickness': 20,
                'line': {
                    'color': 'black',
                    'width': 0.5
                },
                'label': labels[1:],
                'color': colors[1:],
            },
            'link': {
                'source': [s - 1 for s in sources[1:]],
                'target': [t - 1 for t in targets[1:]],
                'value':
                values[1:],
                'hovertemplate':
                '%{label}<br><b>Source: %{source.label}<br>Target: %{target.label}<br> %{flow.value}</b>'
            },
        }
        traces.append(trace)

        layout = {
            'title': self.options['title'].value,
            'margin': {
                'l': self.margins['left'].value,
                'r': self.margins['right'].value,
                'b': self.margins['bottom'].value,
                't': self.margins['top'].value
            },
            'showlegend': True,
            'xaxis': {
                'title': self.options['xaxis'].value,
                'range': [0, 1],
                'showgrid': False,
                'showline': False,
                'showticklabels': False,
                'zeroline': False
            },
            'yaxis': {
                'title': self.options['yaxis'].value,
                'range': [0, 1],
                'showgrid': False,
                'showline': False,
                'showticklabels': False,
                'zeroline': False
            },
            'legend': {
                'orientation': self.margins['legend_orientation'].value,
                'x': self.margins['legend_xpos'].value,
                'y': self.margins['legend_ypos'].value
            }
        }

        for i in range(len(self.dimensions)):
            dimension = self.dimensions[i]
            trace = {
                'type': 'scatter',
                'name': dimension,
                'textposition': 'middle right',
                'x': [-1000],
                'y': [i],
                'mode': 'markers',
                'marker': {
                    'color': color_set[i],
                    'size': 20,
                    'line': {
                        'width': 0,
                    },
                    'symbol': 'square'
                }
            }
            traces.append(trace)

        return FigureWidget({'data': traces, 'layout': layout})
Beispiel #24
0
    def createFigureWidget(self):
        dimensions = self.dimensions
        data_lines = []
        sources = []
        targets = []
        nodes = [{} for dim in dimensions]
        nodest = [{} for dim in dimensions]
        values = []
        labels = []
        colors = []
        self.masks = []
        nodes_id = []
        if self.options['colorscale'].value == GlueSankeyPlotly.default_color:
            color_set = [
                self.data.get_component(dim).color for dim in dimensions
            ]
        else:
            color_set = cl.scales['8']['qual'][
                self.options['colorscale'].value]
            if (len(self.dimensions) > 8):
                color_set = cl.interp(color_set, len(dimensions))
            color_set = cl.to_rgb(color_set)

        for i, dimension in enumerate(dimensions):
            dvalues = np.unique(self.data[dimension].flatten())

            if len(dvalues) < self.options['grouping_limit'].value or hasattr(
                    self.data[dimension].flatten(), 'codes'):
                nodes[i]['values'] = np.unique(self.data[dimension].flatten())
                nodes[i]['local_nodes'] = [
                    len(nodes_id) + cnt
                    for cnt in range(len(nodes[i]['values']))
                ]
                nodes[i]['masks'] = []
                for val in nodes[i]['values']:
                    nodes[i]['masks'].append(self.data[dimension] == val)
            else:
                hist, bin_edges = np.histogram(self.data[dimension].flatten(),
                                               bins='auto')
                if (len(bin_edges) > self.options['grouping_limit'].value):
                    hist, bin_edges = np.histogram(
                        self.data[dimension].flatten(),
                        bins=self.options['grouping_limit'].value)

                nodes[i]['values'] = []
                for edge in range(len(bin_edges) - 1):
                    nodes[i]['values'].append(
                        '{:.1f}'.format(bin_edges[edge]) + ' - ' +
                        '{:.1f}'.format(bin_edges[edge + 1]))
                nodes[i]['local_nodes'] = [
                    len(nodes_id) + cnt
                    for cnt in range(len(nodes[i]['values']))
                ]
                nodes[i]['masks'] = []
                for edge in range(len(bin_edges) - 1):
                    if edge == 0:
                        nodes[i]['masks'].append(
                            (self.data[dimension] >= bin_edges[edge])
                            & (self.data[dimension] <= bin_edges[edge + 1]))
                    else:
                        nodes[i]['masks'].append(
                            (self.data[dimension] > bin_edges[edge])
                            & (self.data[dimension] <= bin_edges[edge + 1]))

            nodes_id.extend(nodes[i]['local_nodes'])
            labels.extend(nodes[i]['values'])
            colors.extend([color_set[i] for val in nodes[i]['values']])
            self.masks.extend(nodes[i]['masks'])
        link_colors = []
        for i in range(len(dimensions) - 1):
            for j in range(len(nodes[i]['local_nodes'])):
                for k in range(len(nodes[i + 1]['local_nodes'])):
                    total = np.count_nonzero(
                        (nodes[i]['masks'][j] & nodes[i + 1]['masks'][k]))
                    if total > 0:
                        mask = nodes[i]['masks'][j] & nodes[i + 1]['masks'][k]
                        for sset in self.data.subsets:
                            if hasattr(sset, "disabled"
                                       ) == False or sset.disabled == False:
                                sset_mask = sset.to_mask()
                                color = sset.style.color
                                settotal = np.count_nonzero(mask & sset_mask)
                                mask = mask & ~sset_mask
                                sources.append(nodes[i]['local_nodes'][j])
                                targets.append(nodes[i + 1]['local_nodes'][k])
                                values.append(settotal)
                                link_colors.append(color)
                                total = total - settotal
                        if self.only_subsets == False:
                            sources.append(nodes[i]['local_nodes'][j])
                            targets.append(nodes[i + 1]['local_nodes'][k])
                            values.append(total)
                            link_colors.append('rgba(238, 238, 238, 0.6)')

        traces = []

        sankey = {
            'type': 'sankey',
            #'arrangement' : 'perpendicular',
            'node': {
                'pad': 8,
                'thickness': 15,
                'line': {
                    'color': 'black',
                    'width': 0.5
                },
                'label': labels,
                'color': colors
            },
            'link': {
                'source': sources,
                'target': targets,
                'value': values,
                'hovertemplate':
                '%{label}<br><b>Source: %{source.label}<br>Target: %{target.label}<br> %{flow.value}</b>',
                'color': link_colors,
                'line': {
                    'color': 'lightgrey',
                    'width': 0.5
                }
            },
            #'domain':{
            #    'x': [0, 0.9],
            #    'y': [0, 1],
            #},
        }

        traces.append(sankey)

        layout = {
            'title': self.options['title'].value,
            'xaxis': {
                'title': self.options['xaxis'].value,
                'range': [0, 1],
                'showgrid': False,
                'showline': False,
                'showticklabels': False,
                'zeroline': False
            },
            'yaxis': {
                'title': self.options['yaxis'].value,
                'range': [0, 1],
                'showgrid': False,
                'showline': False,
                'showticklabels': False,
                'zeroline': False
            },
            'showlegend': self.margins['showlegend'].value,
            'legend': {
                'orientation': self.margins['legend_orientation'].value,
                'x': self.margins['legend_xpos'].value,
                'y': self.margins['legend_ypos'].value
            },
            'font': {
                'size': 10,
            },
        }

        for i in range(len(self.dimensions)):
            dimension = self.dimensions[i]
            trace = {
                'type': 'scatter',
                'name': dimension,
                'textposition': 'middle right',
                'x': [-1000],
                'y': [i],
                'mode': 'markers',
                'marker': {
                    'color': color_set[i],
                    'size': 20,
                    'line': {
                        'width': 0,
                    },
                    'symbol': 'square'
                }
            }
            traces.append(trace)

        if self.only_subsets == False:
            trace = {
                'type': "scatter",
                'name': self.data.label,
                'textposition': 'middle right',
                'x': [-1000],
                'y': [i],
                'mode': 'markers',
                'marker': {
                    'color': "#EEEEEE",
                    'size': 20,
                    'line': {
                        'width': 1,
                        'color': 'light grey'
                    },
                    'symbol': 'square'
                }
            }
            traces.append(trace)

        for sset in self.data.subsets:
            if hasattr(sset, "disabled") == False or sset.disabled == False:
                color = sset.style.color
                trace = {
                    'type': "scatter",
                    'name': sset.label,
                    'textposition': 'middle right',
                    'x': [-1000],
                    'y': [i],
                    'mode': 'markers',
                    'marker': {
                        'color': color,
                        'size': 20,
                        'line': {
                            'width': 1,
                            'color': 'light grey'
                        },
                        'symbol': 'square'
                    }
                }
                traces.append(trace)
        data = traces
        FigureWidget(data=data, layout=layout)
        return FigureWidget(data=data, layout=layout)
Beispiel #25
0
    def createFigureWidget(self):
        x_id = self.dimensions[0]
        y_id = self.dimensions[1]
        z_id = self.dimensions[2]
        x_value = self.data[x_id].flatten().astype('float')
        y_value = self.data[y_id].flatten().astype('float')
        x_value, x_inv = np.unique(x_value, return_inverse=True)
        y_value, y_inv = np.unique(y_value, return_inverse=True)
        try:
            z_value = np.reshape(self.data[z_id].flatten(),
                                 (len(x_value), len(y_value)))
        except:
            t_value = self.data[z_id].flatten()
            x_value = np.sort(x_value)
            y_value = np.sort(y_value)
            z_value = np.zeros((len(x_value), len(y_value)))
            for i, value in enumerate(t_value):
                z_value[x_inv[i]][y_inv[i]] = value
        #with self.debug:
        #    print (x_value,y_value,z_value)

        traces = []
        trace = {
            'type': "contour",
            'colorscale': self.options['color_scale'].value,
            'showlegend': False,
            'autocontour': False,
            'ncontours': 1,
            'contours': {
                'coloring': 'heatmap'
            },
            'x': x_value.tolist(),
            'y': y_value.tolist(),
            'z': z_value.tolist(),
            'zauto': False,
            'zmin': self.options['color_range_min'].value,
            'zmax': self.options['color_range_max'].value,
        }
        if self.only_subsets == False:
            traces.append(trace)

        for sset in self.data.subsets:
            if hasattr(sset, "disabled") == False or sset.disabled == False:
                color = sset.style.color
                color = 'rgba' + str(self.getDeltaColor(color, 0.6))
                trace = {
                    'type':
                    "scattergl",
                    'mode':
                    "markers",
                    'name':
                    sset.label + "_" + y_id,
                    'marker':
                    dict({
                        'symbol': 'circle',
                        'size': self.options['marker_size'].value,
                        'color': color,
                        'line': {
                            'width': self.options['line_width'].value,
                            'color': color
                        }
                    }),
                    'selected': {
                        'marker': {
                            'color': color,
                            'size': self.options['marker_size'].value
                        }
                    },
                    'unselected': {
                        'marker': {
                            'color': color,
                            'size': self.options['marker_size'].value
                        }
                    },
                    'x':
                    sset[x_id].flatten(),
                    'y':
                    sset[y_id].flatten(),
                }
                traces.append(trace)

        layout = {
            'title': self.options['title'].value,
            'margin': {
                'l': self.margins['left'].value,
                'r': self.margins['right'].value,
                'b': self.margins['bottom'].value,
                't': self.margins['top'].value
            },
            'xaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['xaxis'].value,
                'linecolor': self.data.get_component(x_id).color,
                'tickcolor': self.data.get_component(x_id).color,
                'ticklen': 4,
                'linewidth': 4,
            },
            'yaxis': {
                'autorange': True,
                'zeroline': True,
                'title': self.options['yaxis'].value,
                'linecolor': self.data.get_component(y_id).color,
                'tickcolor': self.data.get_component(y_id).color,
                'ticklen': 4,
                'linewidth': 4,
            },
            'showlegend': self.margins['showlegend'].value,
            'legend': {
                'orientation': self.margins['legend_orientation'].value,
                'x': self.margins['legend_xpos'].value,
                'y': self.margins['legend_ypos'].value
            },
        }
        return FigureWidget({'data': traces, 'layout': layout})