Пример #1
0
def pdp_plot(df,
             rows,
             columns,
             values,
             variables=None,
             vars_filter=None,
             clusters=False,
             cluster_centers=5,
             columns_type='N',
             x_title=None,
             y_title=None,
             width=700,
             height=300):
    '''
    Plots a pdp plot for one variable.

    Parameters
    ----------
    df : pandas.DataFrame
        Expects the l

    Returns
    -------
    altair.Chart
    '''
    df = df.copy()
    if vars_filter and variables:
        df = df[df[variables] == vars_filter].drop(variables, axis=1)

    base = alt.Chart(df).properties(width=width, height=height)

    if clusters:
        df_clusters = utils.pdp_clusters(cluster_centers, df, rows, columns,
                                         values)
        background = alt.Chart(df_clusters).mark_line(strokeWidth=2).encode(
            alt.X(f'{columns}:{columns_type}', title=x_title),
            alt.Y(values, title=y_title), alt.Opacity(rows, legend=None),
            alt.ColorValue('#468499')).properties(width=width, height=height)
    else:
        background = base.mark_line(strokeWidth=1).encode(
            alt.X(f'{columns}:{columns_type}', title=x_title),
            alt.Y(values, title=y_title), alt.Opacity(rows, legend=None),
            alt.ColorValue('#bbbbbb'))

    df_avg = df.groupby(columns)[values].mean().reset_index()
    avg_base = alt.Chart(df_avg).encode(
        alt.X(f'{columns}:{columns_type}', title=x_title),
        alt.Y(values, title=y_title),
    )
    avg = avg_base.mark_line(strokeWidth=5, color='gold')
    avg += avg_base.mark_line(strokeWidth=2)
    avg += avg_base.mark_point(filled=True, size=55)

    return background + avg
Пример #2
0
def plot_top_users_time(user, types=['RT', 'Mention', 'Text', 'Reply', 'Quoted', 'Like'] ):
    final = get_user_final_timeline(user, types)
    all_users_x_month = alt.Chart(final,width=400).mark_bar(
        cornerRadiusTopLeft=3,
        cornerRadiusTopRight=3
    ).encode(
        x='yearmonth(created_at):O',
        y=alt.Y('count():Q'),#, sort=alt.SortField(field="count():Q", op="distinct", order='ascending')),
        color=alt.Color('screen_name:N',sort='-y'),
        order=alt.Order('count():Q')
    )
    outlier_transparency = alt.Chart(final, width=400).mark_bar(
        cornerRadiusTopLeft=3,
        cornerRadiusTopRight=3,
        opacity=0.7,
        color='black'
    ).encode(
        x='yearmonth(created_at):O',
        y=alt.Y('count():Q'),#, sort=alt.SortField(field="count():Q", op="distinct", order='ascending')),
        opacity=alt.Opacity('outlier:O',sort='-y', scale=alt.Scale(range=[0.35,0])),
        order='order:N'
    ).transform_calculate(
        order="if (datum.outlier, 1, 0)"
    )
    return alt.layer(all_users_x_month,outlier_transparency).resolve_scale(color='independent')
Пример #3
0
	def initialize_chart(self):
		# return NotImplemented
		x_attr = self.vis.get_attr_by_channel("x")[0]
		y_attr = self.vis.get_attr_by_channel("y")[0]

		chart = alt.Chart(self.data).mark_rect().encode(
			x=alt.X('xBinStart', type='quantitative', axis=alt.Axis(title=x_attr.attribute), bin = alt.BinParams(binned=True)),
			x2=alt.X2('xBinEnd'),
			y=alt.Y('yBinStart', type='quantitative', axis=alt.Axis(title=y_attr.attribute), bin = alt.BinParams(binned=True)),
			y2=alt.Y2('yBinEnd'),
			opacity = alt.Opacity('count',type='quantitative',scale=alt.Scale(type="log"),legend=None)
		)
		chart = chart.configure_scale(minOpacity=0.1,maxOpacity=1) 
		chart = chart.configure_mark(tooltip=alt.TooltipContent('encoding')) # Setting tooltip as non-null
		chart = chart.interactive() # Enable Zooming and Panning

		####################################
		# Constructing Altair Code String ##
		####################################
		
		self.code += "import altair as alt\n"
		# self.code += f"visData = pd.DataFrame({str(self.data.to_dict(orient='records'))})\n"
		self.code += f"visData = pd.DataFrame({str(self.data.to_dict())})\n"
		self.code += f'''
		chart = alt.Chart(visData).mark_rect().encode(
			x=alt.X('xBinStart', type='quantitative', axis=alt.Axis(title='{x_attr.attribute}'), bin = alt.BinParams(binned=True)),
			x2=alt.X2('xBinEnd'),
			y=alt.Y('yBinStart', type='quantitative', axis=alt.Axis(title='{y_attr.attribute}'), bin = alt.BinParams(binned=True)),
			y2=alt.Y2('yBinEnd'),
			opacity = alt.Opacity('count',type='quantitative',scale=alt.Scale(type="log"),legend=None)
		)
		chart = chart.configure_mark(tooltip=alt.TooltipContent('encoding')) # Setting tooltip as non-null
		'''
		return chart 
Пример #4
0
def TrainTestVis(train, test):
    
    df = pd.concat([train, test])
    maptt = {0: "train", 1: "test"}
    df["SPLIT"] = df.split_index.apply(lambda x: maptt[x])
    df.head()
    
    vis = alt.Chart(df).mark_rect().encode(
    alt.X(field="ITEM", type="nominal",
         axis=alt.Axis(orient="top", labelAngle=0)),
    alt.Y(field="USER", type="nominal",
         axis=alt.Axis(orient="left")),
    alt.Color(field="SPLIT", type="ordinal", 
              scale=alt.Scale(type="ordinal", scheme="darkred", nice=True),
              legend=alt.Legend(titleOrient='top', orient="bottom", 
                                direction= "horizontal", tickCount=5)),
    alt.Opacity(value=1)
    ).properties(
        width= 180,
        height=300
    ).configure_axis(
      grid=False
    )

    return vis
Пример #5
0
def graph_PL_transit_dir_jo_cam(df_pct_pl_transit, *cam):
    """
    graph de synthese du nombre de pl en trasit par heure. Base nb pl dir et pct_pl_transit lapi
    en entree : 
        df_pct_pl_transit : df du pct de pl en transit, issu de resultat.pourcentage_pl_camera
    en sortie : 
        graph : chart altair avec le nb pl, nb pl transit, %PL transit
    """
    #import donnees
    concat_dir_trafic, df_pct_pl_transit_multi_cam=PL_transit_dir_jo_cam(df_pct_pl_transit,cam)
    
    #creation du titre
    if [voie for voie, cams in dico_corrsp_camera_site.items() if cams==list(cam)] :
        titre=f'Nombre de PL et % de PL en transit sur {[voie for voie, cams in dico_corrsp_camera_site.items() if cams==list(cam)][0]}'
    else :
        if len(cam)>1 :
            titre=f'Nombre de PL et % de PL en transit au droit des caméras {cam}'
        else : 
            titre=f'Nombre de PL et % de PL en transit au droit de la caméra {cam[0]}'
    #ajout d'un attribut pour legende
    df_pct_pl_transit_multi_cam['legend']='Pourcentage PL en transit'
    concat_dir_trafic=concat_dir_trafic.loc[concat_dir_trafic['type'].isin(['Tous PL','PL en transit'])].copy()
    
    bar_nb_pl_dir=alt.Chart(concat_dir_trafic, title=titre).mark_bar(opacity=0.7).encode(
        x=alt.X('heure:O',axis=alt.Axis(title='Heure',titleFontSize=14,labelFontSize=14)),
        y=alt.Y('nb_pl:Q',stack=None, axis=alt.Axis(title='Nombre de PL',titleFontSize=14,labelFontSize=14)),
        color=alt.Color('type',legend=alt.Legend(title='Type de PL',titleFontSize=14,labelFontSize=14),sort="descending"))
    line_pct_pl_lapi=alt.Chart(df_pct_pl_transit_multi_cam).mark_line(color='green').encode(
        x=alt.X('heure:O',axis=alt.Axis(title='Heure',titleFontSize=14,labelFontSize=14)),
        y=alt.Y('pct_pl_transit', axis=alt.Axis(title='% PL en transit',labelFontSize=14,labelColor='green',titleFontSize=14,titleColor='green',grid=False),
                scale=alt.Scale(domain=(0,100))),
        opacity=alt.Opacity('legend', legend=alt.Legend(title='Donnees LAPI',titleFontSize=14,labelFontSize=14,labelLimit=300)))
    return (bar_nb_pl_dir+line_pct_pl_lapi).resolve_scale(y='independent').properties(width=800, height=400).configure_title(fontSize=18)
Пример #6
0
def plot_max_sun_hours(base, city):
    max_sun = base.mark_line(color='Gray').encode(
        x='monthdate(date):T',
        y='max_sun_hours:Q',
        opacity=alt.Opacity(
            'year:O',
            title='',
            scale=alt.Scale(domain=[f'Max sun hours in {city}'] * 2,
                            range=[1, 1]))).transform_filter(
                                filter=f'datum.year == {YEAR_MAX_SUN_HOURS}')

    line_old, line_2020 = plot_weather_variable(base, "rolling_sun_h",
                                                "Sun hours")

    return max_sun, line_old, line_2020
Пример #7
0
 def get_lines(data, stroke_w, color, selection=None, **kwargs):
     lines = alt.Chart(data).mark_line(
         strokeWidth=stroke_w, **kwargs).encode(
             alt.X(f'{columns}:{columns_type}',
                   title=x_title,
                   axis=alt.Axis(minExtent=30)), alt.Y(values,
                                                       title=y_title),
             alt.Opacity(rows, legend=None),
             alt.ColorValue(color)).transform_filter(filter_in).properties(
                 width=width, height=height)
     if selection:
         lines = lines.encode(size=alt.condition(
             selection, alt.value(stroke_w *
                                  2), alt.value(stroke_w))).properties(
                                      selection=selection)
     return lines
Пример #8
0
 def make_violation_chart(dimension, width_step):
     return alt.Chart(os.path.basename(violations_data_file)).mark_circle(
         color='red', ).transform_calculate(
             rank="format(datum.rank,'03')", ).transform_filter(
                 select_models).transform_filter(select_brush).encode(
                     x=alt.X(f'{dimension}:N',
                             axis=alt.Axis(labelAngle=0,
                                           title=None,
                                           orient='top',
                                           labelPadding=5)),
                     y=alt.Y('violation:N', axis=alt.Axis(title=None)),
                     size=alt.Size('num:Q', legend=None),
                     opacity=alt.Opacity('weight:Q',
                                         scale=alt.Scale(range=[0, 1]),
                                         legend=None),
                     tooltip=[
                         'set:N', 'rank:Q', 'violation:N', 'num:Q',
                         'weight:Q', 'cost_contrib:Q'
                     ]).properties(
                         width=alt.Step(width_step),
                         title=f'soft rule violations (x-{dimension})'
                     ).interactive()
Пример #9
0
def section_plot(data, stats, Season):
    """
    creates a visualization of  section/elevation of one square yard of seeded planting
    """
    source = format_plotdata(data, stats, Season)
    source2 = source.loc[source['bloom_color'] != 'None']

    opacity = alt.Opacity('y:Q', legend=None)
    if Season == 'Winter':
        scolor = '#D6B08C'
        fcolor = wcolor
    else:
        scolor = '#7BB661'
        fcolor = color
    #define the section elevation
    sectionstems = alt.Chart(
        data=source, title='Section Elevation of Proposed Planting').mark_bar(
            size=3, color=f'{scolor}').encode(
                x=alt.X('x:Q', axis=alt.Axis(title='1 yard')),
                y=alt.Y('ht:Q',
                        axis=alt.Axis(title='height (feet)'),
                        scale=alt.Scale(domain=(0, 8))),
                tooltip=['species:N', 'common_name:N'],
                opacity=opacity)

    sectionflowers = alt.Chart(data=source2).mark_point(
        filled=True, size=100).encode(x=alt.X('x:Q'),
                                      y=alt.Y('ht:Q'),
                                      color=fcolor,
                                      shape=shape,
                                      tooltip=['species:N', 'common_name:N'],
                                      opacity=opacity)
    sectionelevation = (sectionstems + sectionflowers).properties(
        width=650, height=400).configure(background='#D3D3D3')

    return sectionelevation
Пример #10
0
def test_convert_opacity_fail_temporal(column):
    chart = alt.Chart(df).mark_point().encode(alt.Opacity(column))
    convert(chart)
Пример #11
0
def test_quantitative_opacity_array():
    chart = alt.Chart(df_quant).mark_point().encode(alt.Opacity('alpha'))
    convert(chart)
def compare_data(datasets, without_weak=True, dataset_name="dwtc", COLUMNS=3):
    charts = []

    alt.renderers.enable("html")
    all_data = pd.DataFrame()
    #  point_datas = pd.DataFrame()

    for dataset in datasets:
        for result in dataset:
            if dataset_name is not False:
                if result.dataset_name != dataset_name:
                    continue
            metrics = loads(result.metrics_per_al_cycle)
            test_data_metrics = [
                metrics["test_data_metrics"][0][f][0]["weighted avg"]
                for f in range(0, len(metrics["test_data_metrics"][0]))
            ]
            test_acc = [
                metrics["test_data_metrics"][0][f][0]["accuracy"]
                for f in range(0, len(metrics["test_data_metrics"][0]))
            ]

            data = pd.DataFrame({
                "iteration":
                range(0, len(metrics["all_unlabeled_roc_auc_scores"])),
                "all_unlabeled_roc_auc_scores":
                metrics["all_unlabeled_roc_auc_scores"],
                "query_length":
                metrics["query_length"],
                "recommendation":
                metrics["recommendation"],
                "query_strong_accuracy_list":
                metrics["query_strong_accuracy_list"],
                "f1": [i["f1-score"] for i in test_data_metrics],
                "test_acc":
                test_acc,
                "top_n":
                result.legend.replace("_", "\_"),
                "color":
                4,
                "opacity":
                0.7,
                "size":
                metrics["recommendation"]
                #'asked_queries': [sum(metrics['query_length'][:i]) for i in range(0, len(metrics['query_length']))],
            })

            if without_weak:
                data = pd.concat([
                    data[data.recommendation == "G"],
                    data[data.recommendation == "A"]
                ])

            # bar width
            data["asked_queries"] = data["query_length"].cumsum()
            data["asked_queries_end"] = data["asked_queries"].shift(
                fill_value=0)

            data["recommendation"].replace(
                {
                    "A": "Oracle",
                    "C": "\\textproc{WeakClust}",
                    "U": "\\textproc{WeakCert}",
                    "G": "Ground Truth",
                },
                inplace=True,
            )

            data["size"].replace(
                {
                    "A": "Oracle",
                    "C": "Weak",
                    "U": "Weak",
                    "G": "Oracle",
                },
                inplace=True,
            )

            #  if not without_weak:
            #  point_data = data[data.recommendation != "Oracle"]
            #  print(data)
            all_data = pd.concat([all_data, data])
            #  if not without_weak:
            #  point_datas = pd.concat([point_datas, point_data])
    #  points = (
    #  alt.Chart(point_datas)
    #  .mark_point()
    #  .encode(
    #  x="asked_queries:Q",
    #  y="test_acc:Q",
    #  shape="recommendation:N",
    #  #  color="color:N",
    #  #  color="recommendation:N",
    #  )
    #  )
    if without_weak:
        show_top_legend = alt.Legend()
        show_thickness_legend = None
        if dataset_name != False:
            x_scale = alt.Scale()
        else:
            x_scale = alt.Scale(type="log")
    else:
        if dataset_name != False:
            x_scale = alt.Scale(domain=[0, 3000])
        else:
            x_scale = alt.Scale(type="log")
        show_top_legend = None
        show_thickness_legend = alt.Legend()

    lines = (
        alt.Chart(all_data, ).mark_trail(interpolate="step-before").encode(
            x=alt.X("asked_queries:Q", title="\#Asked Queries", scale=x_scale),
            y=alt.Y(
                "test_acc:Q",
                title="test accuracy",
                scale=alt.Scale(domain=[0, 1], type="linear"),
            ),
            color=alt.Color(
                "top_n:N",
                legend=show_top_legend,
            ),
            opacity=alt.Opacity("opacity", legend=None),
            size=alt.Size("size:N", legend=show_thickness_legend)
            # shape="top_n",
            # strokeDash="top_n",
            # shape="recommendation",
            # color="recommendation:N",
        ))
    plot = lines
    return (alt.layer(plot).resolve_scale(
        opacity="independent", color="independent",
        shape="independent").configure_legend(
            orient="bottom-right",
            padding=10,
            offset=5,
            #  labelSeparation=20,
            fillColor="#ffffff",
            gradientOpacity=0,
            #  labelOpacity=0,
            #  labelOverlap=True,
            title=None,
            columns=COLUMNS,
            #  strokeColor= "#878787"
        ).configure_axisBottom(labelSeparation=10).properties(
            width=200, height=125).configure_axisLeft(titlePadding=10)
            #  .properties(title="Comparison of ")
            )
RACES_SAMPLED_DATA_COMBINED["action"] = (
    RACES_SAMPLED_DATA_COMBINED.decile_score > THRESHOLD)

RACES_SAMPLED_DATA_COMBINED["correct"] = (
    RACES_SAMPLED_DATA_COMBINED.is_recid == RACES_SAMPLED_DATA_COMBINED.action)

SHOW_CORRECT = streamlit.checkbox(
    translation("compas_threshold.show_correct_checkbox"))

DECILE_SCORE_CHART_BASE = altair.Chart(RACES_SAMPLED_DATA_COMBINED).mark_bar(
    tooltip=altair.TooltipContent("encoding")).encode(
        x="decile_score:O",
        y="count(is_recid):Q",
        opacity=altair.Opacity("action:N",
                               scale=altair.Scale(
                                   domain=["Released", "Jailed"],
                                   range=[1.00, 0.50])
                               # sort="descending"
                               ),
        color=altair.Color(
            "is_recid:N" if not SHOW_CORRECT else "correct:N",
            scale=altair.Scale(domain=["Not Recid", "Recid"])
            if not SHOW_CORRECT else altair.Scale(
                domain=["Incorrect", "Correct"],
                range=["Tomato", "LimeGreen"]))).transform_calculate(
                    is_recid="{0: 'Not Recid', 1: 'Recid'}[datum.is_recid]",
                    action="{0: 'Released', 1: 'Jailed'}[datum.action]",
                    correct="{0: 'Incorrect', 1: 'Correct'}[datum.correct]"
                ).transform_window(x="rank()", groupby=["decile_score"])

DECILE_SCORE_CHART_ALL = DECILE_SCORE_CHART_BASE
Пример #14
0
                x=alt.X('carbon_conc_mM:Q', title='concentration [mM]'),
                y=alt.Y('rel_area_phosphate:Q', title='relative peak area of phosphate'),
                color=alt.Color('compound:N', title='compound')
)


# calib_points

#%%
_percs = calib_percs[(calib_percs['carbon_source']=='acetate') & 
                     (calib_percs['compound_turnover']=='acetate')]
perc_plot = alt.Chart(_percs).mark_area(color=colors['primary_black']).encode(
                x=alt.X('conc_range_mM:Q', title='concentration [mM]'),
                y=alt.Y('rel_area_lower:Q', title = 'relative peak area of phosphate'),
                y2='rel_area_upper:Q',
                opacity=alt.Opacity('percentile:O', sort='descending')
)
(perc_plot + calib_points).interactive()
# %%
for g, _ in data.groupby(['carbon_source', 'compound']):

        # Get the parameter summary
        _summary = summary[(summary['carbon_source']==g[0]) & 
                           (summary['compound_turnover'] == g[1]) &
                           (summary['level']=='hyperparameter') & 
                           (summary['parameter']=='yield_slope')]

        # Define the plot title
        mean_val = np.abs(_summary['mean_val'].values[0])
        ci_95_low, ci_95_up = np.sort(np.abs(np.array(
                                        [_summary['ci_95th_upper'].values[0], 
Пример #15
0
                                                     x=alt.X('start:T',
                                                             scale=x_scale),
                                                     text=alt.Text('mark'),
                                                     tooltip="dead_desc")

y_scale = alt.Scale(padding=0.3)

alt_gantt_1 = alt.\
    Chart(gd).\
    mark_bar().\
    encode(
        x=alt.X('start', scale=x_scale, axis=no_axis_title),
        x2='end',
        y=alt.Y('task', scale=y_scale, axis=no_axis_title),
        color=alt.Color('person', legend=alt.Legend(orient="right")),
        opacity=alt.Opacity('num_fte', legend=None),
        tooltip=tt
    )\
    .properties(width=CHART_WIDTH)

alt_gantt_2 = alt_gantt_1.mark_text(dx=4, dy=0, align='left', baseline='middle')\
    .encode(
    text='desc'
)
alt_gantt_2.encoding.color = alt.Undefined
alt_gantt_2.encoding.opacity = alt.Undefined
alt_gantt_2.encoding.tooltip = tt

alt_gantt_layered = alt_gantt_1 + alt_gantt_2 + alt_dead

alt_util = alt.Chart(final).mark_area(interpolate="monotone").encode(
Пример #16
0
    def initialize_chart(self):
        # return NotImplemented
        x_attr = self.vis.get_attr_by_channel("x")[0]
        y_attr = self.vis.get_attr_by_channel("y")[0]

        x_attr_abv = str(x_attr.attribute)
        y_attr_abv = str(y_attr.attribute)

        if len(x_attr_abv) > 25:
            x_attr_abv = x_attr.attribute[:15] + "..." + x_attr.attribute[-10:]
        if len(y_attr_abv) > 25:
            y_attr_abv = y_attr.attribute[:15] + "..." + y_attr.attribute[-10:]

        if isinstance(x_attr.attribute, str):
            x_attr.attribute = x_attr.attribute.replace(".", "")
        if isinstance(y_attr.attribute, str):
            y_attr.attribute = y_attr.attribute.replace(".", "")

        chart = (alt.Chart(self.data).mark_rect().encode(
            x=alt.X(
                "xBinStart",
                type="quantitative",
                axis=alt.Axis(title=x_attr_abv),
                bin=alt.BinParams(binned=True),
            ),
            x2=alt.X2("xBinEnd"),
            y=alt.Y(
                "yBinStart",
                type="quantitative",
                axis=alt.Axis(title=y_attr_abv),
                bin=alt.BinParams(binned=True),
            ),
            y2=alt.Y2("yBinEnd"),
            opacity=alt.Opacity(
                "count",
                type="quantitative",
                scale=alt.Scale(type="log"),
                legend=None,
            ),
        ))
        chart = chart.configure_scale(minOpacity=0.1, maxOpacity=1)
        # Setting tooltip as non-null
        chart = chart.configure_mark(tooltip=alt.TooltipContent("encoding"))
        chart = chart.interactive()  # Enable Zooming and Panning

        ####################################
        # Constructing Altair Code String ##
        ####################################

        self.code += "import altair as alt\n"
        # self.code += f"visData = pd.DataFrame({str(self.data.to_dict(orient='records'))})\n"
        self.code += f"visData = pd.DataFrame({str(self.data.to_dict())})\n"
        self.code += f"""
		chart = alt.Chart(visData).mark_rect().encode(
			x=alt.X('xBinStart', type='quantitative', axis=alt.Axis(title='{x_attr_abv}'), bin = alt.BinParams(binned=True)),
			x2=alt.X2('xBinEnd'),
			y=alt.Y('yBinStart', type='quantitative', axis=alt.Axis(title='{y_attr_abv}'), bin = alt.BinParams(binned=True)),
			y2=alt.Y2('yBinEnd'),
			opacity = alt.Opacity('count',type='quantitative',scale=alt.Scale(type="log"),legend=None)
		)
		chart = chart.configure_mark(tooltip=alt.TooltipContent('encoding')) # Setting tooltip as non-null
		"""
        return chart
def _plot_isotype_array(baseline_ef,
                        exposed_ef,
                        population_size=100,
                        title="",
                        configure_chart=True,
                        icon_shape=PERSON_SHAPE,
                        icon_size=75,
                        stroke_color="black",
                        stroke_width=1.3,
                        cross_shape=CROSS_SHAPE,
                        cross_width=None,
                        chart_width=350,
                        chart_height=400):
    if isinstance(baseline_ef, float) or isinstance(exposed_ef, float):
        warnings.warn(
            "Can't currently plot (color) fractional icons. Rounding to nearest integer."
        )
    baseline_ef = round(baseline_ef)
    exposed_ef = round(exposed_ef)

    data = __generate_chart_source_data(baseline_ef, exposed_ef,
                                        population_size)

    root = round(math.sqrt(
        population_size))  # Create a square grid of total `population_size`

    # https://altair-viz.github.io/gallery/isotype_grid.html
    base_chart = alt.Chart(data).transform_calculate(
        row=f"ceil(datum.id/{root})",
        col=f"datum.id - datum.row*{root}",
    ).encode(
        x=alt.X("col:O", axis=None),
        y=alt.Y("row:O", axis=None),
    ).properties(width=chart_width,
                 height=chart_height,
                 title=title if title else "")
    icons = base_chart.mark_point(
        filled=True,
        stroke=stroke_color,
        strokeWidth=stroke_width,  # 2,
        size=icon_size,
    ).encode(
        color=alt.Color(
            'hue:N',
            scale=alt.Scale(
                domain=[
                    0, 1, 2
                ],  # Explicitly specify `hue` values or coloring will fail if <3 levels exist in data
                range=[
                    "#FFFFFF",  # Population (0)
                    "#4A5568",  # Baseline (1)
                    "#FA5765",  # Exposed (2)  "#4078EF"
                ]),
            # TODO: add uncertainty using shade: lighter color fill of icons in the 95% CI.
            legend=None),
        shape=alt.ShapeValue(icon_shape),
    )
    chart = icons
    if exposed_ef < baseline_ef:
        stroke_out = base_chart.mark_point(
            # shape="cross",
            filled=True,
            stroke="#4078EF",  # "black"
            # strokeWidth=cross_width,
            strokeWidth=math.sqrt(icon_size) /
            1.7 if cross_width is None else cross_width,
            strokeCap="round",
            size=icon_size,
        ).encode(shape=alt.ShapeValue(cross_shape),
                 opacity=alt.Opacity(
                     'reduced:N',
                     legend=None,
                     scale=alt.Scale(domain=[False, True], range=[0, 1]),
                 ))
        chart += stroke_out
    if configure_chart:  # Configured charts cannot be later concatenated.
        chart = chart.configure_title(
            align="left",
            anchor="start",
            offset=-10,
        ).configure_view(strokeWidth=0, )

    return chart
Пример #18
0
def accuracy_vs_horizon_chart(
    base: alt.Chart,
    horizon_selection_brush: alt.Selection,
    belief_horizon_unit: str,
    intuitive_forecast_horizon: bool,
    unique_belief_horizons,
) -> alt.LayerChart:
    ha_chart = (
        base.mark_circle()
        .transform_joinaggregate(
            on_the_fly_mae="mean(mae)",
            on_the_fly_reference="mean(reference_value)",
            groupby=["belief_horizon", "source"],
        )
        .transform_calculate(
            on_the_fly_wape=alt.datum.on_the_fly_mae / alt.datum.on_the_fly_reference,
            accuracy=1 - alt.datum.on_the_fly_wape,
        )
        .encode(
            opacity=alt.condition(
                selectors.time_selection_brush,
                alt.Opacity(
                    "event_start:T", scale=alt.Scale(domain=(0.9999, 1)), legend=None
                ),
                alt.value(0),
            ),
            # Trick to be able to apply the selection filter for event_start (event_start must be a field in one of the encoding channels)
            x=alt.X(
                "belief_horizon:Q",
                axis=alt.Axis(labelFlush=False),
                scale=alt.Scale(
                    zero=False,
                    domain=(unique_belief_horizons[0], unique_belief_horizons[-1]),
                ),
                title="%s (%s)"
                % (
                    "Forecast horizon"
                    if intuitive_forecast_horizon
                    else "Belief horizon",
                    belief_horizon_unit,
                ),
            ),
            y=alt.Y(
                "accuracy:Q",
                title="Accuracy (1-WAPE)",
                axis=alt.Axis(format="%", minExtent=30),
                scale={"domain": (0, 1)},
            ),
            color=selectors.source_color_or(
                selectors.idle_color,
                selectors.horizon_hover_brush | horizon_selection_brush,
            ),
            size=alt.value(100),
            tooltip=[
                alt.Tooltip("accuracy:Q", title="Accuracy (1-WAPE)", format=".1%"),
                alt.Tooltip(
                    "belief_horizon:Q",
                    title="%s (%s)"
                    % (
                        "Forecast horizon"
                        if intuitive_forecast_horizon
                        else "Belief horizon",
                        belief_horizon_unit,
                    ),
                ),
                alt.Tooltip("source:O", title="Source"),
            ],
        )
        .properties(title="Model accuracy in the %s before" % belief_horizon_unit)
        # .configure_title(offset=-270, orient="bottom")
    )
    ha_interpolation_chart = ha_chart.mark_line(interpolate="monotone").encode(
        size=alt.value(1), color=alt.Color("source:N", legend=None)
    )
    return ha_interpolation_chart + ha_chart
Пример #19
0
def create_exploratory_visualisation(trial_id,
                                     directory,
                                     vis_data_file,
                                     match_data_file,
                                     violations_data_file,
                                     props_data_file,
                                     baseline_label='baseline',
                                     verde_label='verde'):
    """
    Uses altair to generate the exploratory visualisation.
    :param trial_id:
    :param directory:
    :param vis_data_file:
    :param match_data_file:
    :param violations_data_file:
    :param props_data_file:
    :param baseline_label:
    :param verde_label:
    :return:
    """

    vl_viewer = f'{trial_id}_view_one_vl.html?vl_json='

    # common data and transforms for first layer with marks for each vis model
    base = alt.Chart(os.path.basename(vis_data_file)).transform_calculate(
        rank="format(datum.rank,'03')",
        link=f"'{vl_viewer}' + datum.vl_spec_file").properties(
            width=250, height=alt.Step(30), title='visualisation rankings')

    # add a selectable square for each vis model
    select_models = alt.selection_multi(fields=['set', 'rank'])
    select_brush = alt.selection_interval()
    squares = base.mark_square(size=150).encode(
        alt.X('set:O',
              axis=alt.Axis(labelAngle=0,
                            title=None,
                            orient='top',
                            labelPadding=5)),
        alt.Y('rank:O', axis=alt.Axis(title=None)),
        tooltip=['set:N', 'rank:N', 'cost:Q'],
        opacity=alt.Opacity('has_match:O', legend=None),
        color=alt.condition(
            select_models | select_brush, alt.value('steelblue'),
            alt.value('lightgray'))).add_selection(select_models,
                                                   select_brush).interactive()

    # add a small circle with the hyperlink to the actual vis.
    # Shame that xoffset is not an encoding channel, so we have to do in two steps...
    def make_circles(vis_set, offset):
        return base.transform_filter(datum.set == vis_set).mark_circle(
            size=25,
            xOffset=offset,
        ).encode(alt.X('set:O',
                       axis=alt.Axis(labelAngle=0,
                                     title=None,
                                     orient='top',
                                     labelPadding=5)),
                 alt.Y('rank:O'),
                 tooltip=['link:N'],
                 href='link:N',
                 color=alt.condition(select_models | select_brush,
                                     alt.value('steelblue'),
                                     alt.value('lightgray'))).interactive()

    baseline_circles = make_circles(baseline_label, -15)
    verde_circles = make_circles(verde_label, 15)

    # next layer is match lines, handle case of no matches
    if match_data_file:
        col_domain = ['not', 'with_equal_cost', 'with_different_cost']
        col_range_ = ['steelblue', 'green', 'red']
        match_lines = alt.Chart(
            os.path.basename(match_data_file)).mark_line().transform_calculate(
                rank="format(datum.rank,'03')").encode(
                    alt.X('set:O',
                          axis=alt.Axis(labelAngle=0,
                                        title=None,
                                        orient='top',
                                        labelPadding=5)),
                    alt.Y('rank:O'),
                    detail=['match:N', 'match_type:N'],
                    strokeDash=alt.StrokeDash(
                        'match_type:N',
                        scale=alt.Scale(domain=['verde_addition', 'exact'],
                                        range=[[5, 4], [1, 0]]),
                        legend=alt.Legend(orient='bottom')),
                    color=alt.condition(
                        select_models | select_brush,
                        alt.Color('crossed:N',
                                  scale=alt.Scale(domain=col_domain,
                                                  range=col_range_),
                                  legend=alt.Legend(orient='bottom')),
                        alt.value('lightgray')))
    else:
        match_lines = None

    # rules to connect models with the same cost
    cost_rules = base.mark_rule(strokeWidth=2).transform_aggregate(
        min_rank='min(rank)', max_rank='max(rank)',
        groupby=['set', 'cost'
                 ]).encode(alt.X('set:O',
                                 axis=alt.Axis(labelAngle=0,
                                               title=None,
                                               orient='top',
                                               labelPadding=5)),
                           alt.Y('min_rank:O'),
                           alt.Y2('max_rank:O'),
                           color=alt.condition(select_models | select_brush,
                                               alt.value('steelblue'),
                                               alt.value('lightgray')),
                           tooltip=['cost:Q', 'min_rank:O',
                                    'max_rank:O']).interactive()

    rank_chart = baseline_circles + verde_circles

    if match_lines:
        rank_chart = rank_chart + match_lines

    rank_chart = rank_chart + cost_rules + squares

    # chart to show violation occurrences and weights for selected vis models across sets
    def make_violation_chart(dimension, width_step):
        return alt.Chart(os.path.basename(violations_data_file)).mark_circle(
            color='red', ).transform_calculate(
                rank="format(datum.rank,'03')", ).transform_filter(
                    select_models).transform_filter(select_brush).encode(
                        x=alt.X(f'{dimension}:N',
                                axis=alt.Axis(labelAngle=0,
                                              title=None,
                                              orient='top',
                                              labelPadding=5)),
                        y=alt.Y('violation:N', axis=alt.Axis(title=None)),
                        size=alt.Size('num:Q', legend=None),
                        opacity=alt.Opacity('weight:Q',
                                            scale=alt.Scale(range=[0, 1]),
                                            legend=None),
                        tooltip=[
                            'set:N', 'rank:Q', 'violation:N', 'num:Q',
                            'weight:Q', 'cost_contrib:Q'
                        ]).properties(
                            width=alt.Step(width_step),
                            title=f'soft rule violations (x-{dimension})'
                        ).interactive()

    violation_set_chart = make_violation_chart('set', 40)
    violation_rank_chart = make_violation_chart('rank', 30)

    # chart to show prop occurrences for selected vis models across sets
    def make_prop_chart(dimension, width_step):
        return alt.Chart(os.path.basename(props_data_file)).mark_circle(
            size=50, color='green').transform_calculate(
                rank="format(datum.rank,'03')").transform_filter(
                    select_models).transform_filter(select_brush).encode(
                        x=alt.X(f'{dimension}:N',
                                axis=alt.Axis(labelAngle=0,
                                              title=None,
                                              orient='top',
                                              labelPadding=5)),
                        y=alt.Y('prop:N', axis=alt.Axis(title=None)),
                        tooltip=['prop:N']).properties(
                            width=alt.Step(width_step),
                            title=f'specification terms (x-{dimension})'
                        ).interactive()

    prop_set_chart = make_prop_chart('set', 40)
    prop_rank_chart = make_prop_chart('rank', 30)

    # glue them all together
    top_chart = rank_chart | violation_set_chart | prop_set_chart
    bottom_chart = violation_rank_chart | prop_rank_chart
    chart = top_chart & bottom_chart
    # put a timestamp
    ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    chart = chart.properties(title=f'{trial_id} {ts}')

    file_name = os.path.join(directory, 'vegalite',
                             f'{trial_id}_view_compare.html')
    logging.info(f'writing comparison visualisation to {file_name}')
    chart.save(file_name)
Пример #20
0
 def test_line_opacity_color(self, c, o):
     chart = alt.Chart(df_line).mark_line().encode(alt.X('a'), alt.Y('b'),
                                                   alt.Color(c),
                                                   alt.Opacity(o))
     fig, ax = convert(chart)
     return fig
Пример #21
0
def intervalle_confiance_cam(df_pct_pl_transit,df_concat_pl_jo,intervall_conf, *cam): 
    """
    fgraph pour intervalle de confiance et affichage données de comptagesde PL.
    en entrée : 
        intervall_conf : booleen : Terue si on veut le zonage de l'intervalle de confiance, False si on ne le veut pas
    """
    pour_graph_synth,lien_traf_gest_traf_lapi=indice_confiance_cam(df_pct_pl_transit,df_concat_pl_jo,cam)
    lien_traf_gest_traf_lapi['heure']=lien_traf_gest_traf_lapi.apply(lambda x : pd.to_datetime(0)+pd.Timedelta(str(x['heure'])+'H'), axis=1)
    pour_graph_synth['heure']=pour_graph_synth.apply(lambda x : pd.to_datetime(0)+pd.Timedelta(str(x['heure'])+'H'), axis=1)
    #print(pour_graph_synth,lien_traf_gest_traf_lapi)
    #print(dico_corrsp_camera_site.items(),[voie for voie, cams in dico_corrsp_camera_site.items() if cams==list(cam)])
    if [voie for voie, cams in dico_corrsp_camera_site.items() if cams==list(cam)] :
        titre_interv=f'Nombre de PL et % de PL en transit sur {[voie for voie, cams in dico_corrsp_camera_site.items() if cams==list(cam)][0]}'
        titre_nb_pl=f'Nombre de PL selon la source sur {[voie for voie, cams in dico_corrsp_camera_site.items() if cams==list(cam)][0]}'
    else :
        if len(cam)>1 :
            titre_interv=f'Nombre de PL et % de PL en transit au droit des caméras {cam}'
            titre_nb_pl=f'Nombre de PL selon la source au droit des caméras {cam}'
        else : 
            titre_interv=f'Nombre de PL et % de PL en transit au droit de la caméra {cam[0]}'
            titre_nb_pl=f'Nombre de PL selon la source au droit de la caméra {cam[0]}'
    
    #pour n'affcihier que "Comptage gestionnnaire" si Comptage gestionnnaire=Comptage gestionnnaire recale
    if ((pour_graph_synth.loc[pour_graph_synth['type']=='Comptage gestionnnaire recalé'].nb_veh==pour_graph_synth.loc[
        pour_graph_synth['type']=='Comptage gestionnnaire'].nb_veh).all()) :
        pour_graph_synth=pour_graph_synth.loc[pour_graph_synth.type.isin(['LAPI','Comptage gestionnnaire'])].copy()
        df_intervalle=pour_graph_synth.copy()
    else : 
        df_intervalle=pour_graph_synth.loc[pour_graph_synth['type'].isin(['LAPI', 'Comptage gestionnnaire recalé'])].copy()
        
    #pour legende
    lien_traf_gest_traf_lapi['legend_pct_transit']='Pourcentage PL transit'
    lien_traf_gest_traf_lapi['legend_i_conf']='Intervalle de confiance'
    line_trafic=alt.Chart(df_intervalle, title=titre_interv).mark_line().encode(
        x=alt.X('hoursminutes(heure)',axis=alt.Axis(title='Heure', titleFontSize=14,labelFontSize=14)),
        y=alt.Y('nb_veh:Q', axis=alt.Axis(title='Nombre de PL',titleFontSize=14,labelFontSize=14)), 
        color=alt.Color('type',
                        sort=(['LAPI', 'Comptage gestionnnaire recale']),
                        legend=alt.Legend(title='source du nombre de PL',titleFontSize=14,labelFontSize=14,labelLimit=400)))
    area_pct_max=alt.Chart(lien_traf_gest_traf_lapi).mark_area(opacity=0.7, color='green').encode(
        x='hoursminutes(heure)',
        y=alt.Y('pct_pl_transit_max:Q',
                axis=alt.Axis(title='Pourcentage de PL en transit',titleFontSize=14,labelFontSize=14,labelColor='green',titleColor='green'),
                scale=alt.Scale(domain=(0,100))), 
        y2='pct_pl_transit_min:Q',
        opacity=alt.Opacity('legend_i_conf'))
    line_pct=alt.Chart(lien_traf_gest_traf_lapi).mark_line(color='green').encode(
        x='hoursminutes(heure)',
        y=alt.Y('pct_pl_transit',
                axis=alt.Axis(title='Pourcentage de PL en transit',titleFontSize=14,labelFontSize=14,labelColor='green',titleColor='green'),
                scale=alt.Scale(domain=(0,100))),
        opacity=alt.Opacity('legend_pct_transit', legend=alt.Legend(title='Analyse du transit LAPI',titleFontSize=14,labelFontSize=14)))
    pct=(area_pct_max+line_pct) if intervall_conf else line_pct
    graph_interval=(line_trafic+pct).resolve_scale(y='independent').properties(width=800, height=400).configure_title(fontSize=18)
    
    #graph comparaison nb_pl
    graph_nb_pl=alt.Chart(pour_graph_synth, title=titre_nb_pl).mark_line(opacity=0.7).encode(
        x=alt.X('hoursminutes(heure)',axis=alt.Axis(title='Heure', titleFontSize=14,labelFontSize=14)),
        y=alt.Y('nb_veh:Q', axis=alt.Axis(title='Nombre de PL',titleFontSize=14,labelFontSize=14)), 
        color=alt.Color('type',sort=['LAPI','Comptage gestionnnaire','Comptage gestionnnaire recale'],title='source du nombre de PL', 
                        legend=alt.Legend(titleFontSize=14,labelFontSize=14,labelLimit=400))).properties(
            width=800, height=400).configure_title(fontSize=18)
        
    return graph_interval,graph_nb_pl
Пример #22
0
def horizon_selector(
    base: alt.Chart,
    horizon_selection_brush: alt.MultiSelection,
    belief_horizon_unit: str,
    intuitive_forecast_horizon: bool,
    unique_belief_horizons,
) -> alt.LayerChart:
    bar_chart = (
        base.mark_rule(orient="vertical").transform_filter(
            time_selection_brush
        )  # Apply brush before calculating accuracy metrics for the selected events on the fly
        .transform_calculate(
            constant=1 + alt.datum.event_start -
            alt.datum.event_start).transform_calculate(
                belief_horizon_str='datum.belief_horizon + " %s"' %
                belief_horizon_unit).
        encode(
            opacity=alt.condition(
                time_selection_brush,
                alt.Opacity("event_start:T",
                            scale=alt.Scale(domain=(0.9999, 1)),
                            legend=None),
                alt.value(0),
            ),
            # Trick to be able to apply the selection filter for event_start (event_start must be a field in one of the encoding channels)
            x=alt.X(
                "belief_horizon:Q",
                axis=alt.Axis(labelFlush=False),
                scale=alt.Scale(
                    zero=False,
                    domain=(unique_belief_horizons[0],
                            unique_belief_horizons[-1]),
                ),
                title="",
            ),
            y=alt.Y(
                "constant:Q",
                title=" ",
                axis=alt.Axis(values=[], domain=False, ticks=False),
            ),
            color=alt.condition(
                horizon_selection_brush | horizon_hover_brush,
                alt.ColorValue("#c21431"),
                alt.ColorValue(idle_color),
            ),
            size=alt.value(1),
            tooltip=[
                alt.Tooltip(
                    "belief_horizon_str:N",
                    title="Click to select %s" %
                    ("forecast horizon"
                     if intuitive_forecast_horizon else "belief horizon"),
                )
            ],
        ).properties(
            height=30,
            title="Select %s" %
            ("forecast horizon"
             if intuitive_forecast_horizon else "belief horizon"),
        ).transform_filter(time_selection_brush))
    circle_chart = (bar_chart.mark_circle().transform_calculate(
        half_constant=alt.datum.constant / 2).encode(
            y=alt.Y("half_constant:Q", title="", axis=alt.Axis(values=[])),
            size=alt.value(100),
        ))
    return (
        bar_chart.add_selection(horizon_selection_brush, horizon_hover_brush) +
        circle_chart)
Пример #23
0
import random
import altair as alt
import pandas as pd
import streamlit as st

df = pd.DataFrame({
    'date': pd.date_range(start='2020-01-01', end='2020-01-31'),
    'OLS': [random.randint(0, 1) for x in range(31)],
    'OLS2': [random.randint(0, 10) for x in range(31)],
    'OLS3_nopoint': [random.randint(0, 100) for x in range(31)]
}).melt(id_vars='date', var_name="OLS", value_name="value")

df['opacity'] = 0
df.loc[df['OLS'] != "OLS3_nopoint", 'opacity'] = 1

base = alt.Chart(df).encode(x="date:T", y="value:Q", color='OLS:N')
lines = base.mark_line()
points = base.mark_point(filled=True).encode(
    opacity=alt.Opacity("opacity:N", legend=None))
st.altair_chart(lines + points, use_container_width=True)
Пример #24
0
st.markdown('**Country specific growth:**')
st.markdown(selectedCountry)


d0= alt.Chart(df_corona_countryChange).mark_area(opacity =0.5).encode(
                                                                  x = alt.X('Date:T', axis = alt.Axis(title = 'Date')),
                                                                  y = alt.Y('Count:Q', axis = alt.Axis(title = 'Count',format = ("~s"))),
                                                                  color = alt.Color('Situation:N', scale=alt.Scale(domain=['Confirmed', 'Deaths'],
                                                                                                    range=['#5677A4', 'red']),
                                                                                    legend = alt.Legend(title = 'Cases', orient ='top',labelFontSize=12)),
                                                                ).interactive()

d1= alt.Chart(df_corona_countryChange[df_corona_countryChange['Situation']=='Confirmed']).mark_area(color='green', size=100, filled=True, opacity=0.1).encode(
                                                                  x = alt.X('Date:T', axis = alt.Axis(title = 'Date')),
                                                                  y = alt.Y('movingMean_of_Change:Q', axis = alt.Axis(title = 'Change')),
                                                                  opacity=alt.Opacity('Situation', legend=alt.Legend(title="Rate", orient='right'))

                                                                  #color = alt.Color(scale=alt.Scale(range=['red']), legend = alt.Legend(title = 'Rate', orient ='right',labelFontSize=12)),
                                                                ).interactive()


d3=(d0+d1).resolve_scale(y='independent').properties(width=300,height=400).interactive()
st.altair_chart(d3,use_container_width=True)

st.markdown('**Growth rate in last 20 days:**')


d0= alt.Chart(df_corona_country_croppedChange).mark_area(opacity =0.5).encode(
                                                                  x = alt.X('Date:T', axis = alt.Axis(title = 'Date')),
                                                                  y = alt.Y('Count:Q', axis = alt.Axis(title = 'Count',format = ("~s"))),
                                                                  color = alt.Color('Situation:N', scale=alt.Scale(domain=['Confirmed', 'Deaths'],