Exemplo n.º 1
0
def histc_vega(tensor, min, max, bins):
    bins = torch.linspace(min, max, bins)
    hist = tensor.histc(min=bins.min(), max=bins.max(), bins=len(bins)).int()
    return altair.Chart(
        altair.Data(values=[
            dict(x=b, y=v) for b, v in zip(bins.tolist(), hist.tolist())
        ])).mark_bar().encode(x=altair.X('x:Q'), y=altair.Y('y:Q')).to_dict()
def filter_geodata_and_merge(
    gdf_out: gpd.GeoDataFrame,
    df_mapping_choro: DataFrame,
    da_choice: str,
    right_merge: str,
) -> alt.Data:
    """
    Filter geodata and merge with non-geodata
    """
    # 5. Reset (multi) index from slice of boundary data (if necessary)
    gdf_out_cols = (["geometry", "community_x", "beat_num_x", "area"]
                    if da_choice == "community_area" else
                    ["geometry", "sector", "beat_num", "area"])
    df_right_merge = gdf_out[gdf_out_cols + [right_merge]]
    df_right_merge = (df_right_merge if da_choice == "neighbourhood" else
                      df_right_merge.reset_index(drop=True))

    # 6. INNER JOIN crime and boundary data
    df_grouped_merged = df_mapping_choro.merge(df_right_merge,
                                               left_on=da_choice,
                                               right_on=right_merge)

    # 7. Convert merged data into GeoDataFrame
    df_map = gpd.GeoDataFrame(df_grouped_merged, geometry="geometry")
    # display(df_map.head())

    # 8. Create data structure for Altair choropleth mapping
    choro_json = loads(df_map.to_json())
    choro_data = alt.Data(values=choro_json["features"])
    return choro_data
Exemplo n.º 3
0
 def geoencode(self) -> alt.Chart:  # coverage: ignore
     """Returns an `altair <http://altair-viz.github.io/>`_ encoding of the
     shape to be composed in an interactive visualization.
     """
     return alt.Chart(alt.Data(values=self.geojson())).mark_geoshape(
         stroke="#aaaaaa", strokeWidth=1
     )
Exemplo n.º 4
0
 def on_view(self, file) -> JsonDict:
     reader = csv.DictReader(file)
     data = alt.Data(values=list(reader))
     return json.loads(alt.Chart(data).mark_bar().encode(
         x='x:O',  # specify ordinal data
         y='y:Q',  # specify quantitative data
     ).to_json())
Exemplo n.º 5
0
def data_to_table(data, step):
    table = []

    try:
        import torch
    except ImportError:
        torch = None

    if torch is not None and isinstance(data, torch.Tensor):
        data = data.detach().cpu().numpy()

    for i in range(data.shape[0]):
        if len(data.shape) == 2:
            m = data.shape[1] // 2
            row = {'step': data[i, 0], 'v5': data[i, m]}
            for j in range(1, min(m, 5)):
                row[f"v{5 - j}"] = data[i, m - j]
                row[f"v{5 + j}"] = data[i, m + j]
        elif step is not None:
            row = {'step': step[i], 'v5': data[i]}
        else:
            row = {'step': i, 'v5': data[i]}
        table.append(row)

    return alt.Data(values=table)
Exemplo n.º 6
0
def read_minsal_table():
    '''
    reads a table with latest data on confirmed cases, by region
    '''

    URL = 'https://www.minsal.cl/nuevo-coronavirus-2019-ncov/casos-confirmados-en-chile-covid-19/'

    minsal = pd.read_html(URL)[0].iloc[2:]
    minsal.columns = minsal.iloc[0]
    minsal = minsal.iloc[1:-1]
    minsal['codregion'] = [15, 1, 2, 3, 4, 5, 13, 6, 7, 16, 8, 9, 14, 10, 11, 12]

    ### reads shapefile with chilean regions
    # URL = 'https://www.bcn.cl/obtienearchivo?id=repositorio/10221/10398/2/Regiones.zip'
    # regions = geopandas.read_file(URL)
    
    shape = 'data/Regional'
    regions = geopandas.read_file(shape)

    regions = regions.merge(minsal, on='codregion')

    choro_json = json.loads(regions.to_json())
    choro_data = alt.Data(values=choro_json['features'])

    return choro_data
Exemplo n.º 7
0
def marginals_altair(fanova):
    """Outputs the marginal effect of each hyper-parameter according to FANOVA

    Parameters
    ----------
    fanova: FANOVA
        instance of FANOVA class

    Examples
    --------

    .. image:: ../../../docs/_static/plots/marginals.png

    """
    import altair as alt
    alt.themes.enable('dark')

    data = fanova.compute_marginals()
    marginals = alt.Data(values=data)

    base = alt.Chart(marginals).encode(alt.X('value', type='quantitative'),
                                       alt.Y('objective', type='quantitative'),
                                       yError='std:Q',
                                       color='name:N').properties(width=200,
                                                                  height=200)

    chart = (base.mark_errorband() + base.mark_line())\
        .facet(column='name:N').interactive()

    return chart
Exemplo n.º 8
0
def prepare_for_altair(attribute_column, geography, chloropleth, pickle_dir='data/pickles'):
    df = merge_data(attribute_column, geography, chloropleth, pickle_dir)
    gdf = gpd.GeoDataFrame(df, crs={'init' :'epsg:4326'}, geometry='geometry')
    gdf = gdf[['key', 'category', attribute_column, 'geometry']]
    json_gdf = gdf.to_json()
    json_features = json.loads(json_gdf)
    return alt.Data(values=json_features['features'])
Exemplo n.º 9
0
def _rankingmap_altair(countries,
                       ranking,
                       x,
                       scenario=None,
                       method='number',
                       title='',
                       label=''):
    # Adapted from https://altair-viz.github.io/gallery/index.html

    import pandas as pd
    import altair as alt

    if method not in ['number', 'value']:
        raise ValueError('method must be "number" or "value"')

    source = alt.Data(values=countries)

    if ranking.plot_type == 'indicator_vs_temperature':
        details = 'warming level: {} {}'.format(x, ranking.plot_unit_x)
    else:
        details = 'period: {}, scenario: {}'.format(
            x, {
                'rcp26': 'RCP 2.6',
                'rcp45': 'RCP 4.5',
                'rcp60': 'RCP 6',
                'rcp85': 'RCP 8.5'
            }.get(scenario, scenario))
    default_title = getattr(ranking, 'plot_label_y', '') + '\n' + details
    # default_label = 'ranking number' if method == 'number' else ('ranking value ({})'.format(getattr(ranking, 'plot_unit_y')))

    ranking_data = get_ranking_data(countries, ranking, x, scenario, method)

    chart = alt.Chart(source).mark_geoshape().encode(
        # color="Rank:Q",
        color=alt.Color("Rank:Q", sort='ascending')
        if method == 'number' else alt.Color("Value:Q", sort='descending'),
        # tooltip=["Country:N", "Code:N", "Value:Q", "Rank:Q"]
        tooltip=[
            "label:N", "unit:N", "Country:N", "Code:N", "Value:Q", "Rank:Q"
        ]).transform_lookup(lookup='properties.ISIPEDIA',
                            from_=alt.LookupData(ranking_data, 'Code',
                                                 ranking_data.columns.tolist())
                            ).project('naturalEarth1').properties(
                                width=800,
                                autosize=alt.AutoSizeParams(contains="padding",
                                                            type="fit-x"),
                                title=ranking.plot_title
                                # ).configure_view(stroke=None
                            ).configure(background='#F1F4F4').configure_title(
                                fontSize=16, ).configure_axis(
                                    labelFontSize=14,
                                    titleFontSize=16,
                                ).configure_legend(
                                    titleFontSize=14,
                                    labelFontSize=14,
                                ).configure_mark(fontSize=14)
    # ).interactive()

    return chart
def scatter_matrix_altair(configs, columns, color='epoch'):
    """Plots hyper-parameter space exploration

    Parameters
    ----------
    configs: List[dict]
        A list of configuration tried by the hyper-parameter

    columns: List[str]
        A list of the hyper-parameters

    color: str
        Dimension to use to color each points

    Examples
    --------

    >>> columns = ['a', 'b', 'c']
    >>> data = [
    ...     dict(a=1, b=2, c=3, epoch=1),
    ...        dict(a=2, b=1, c=1, epoch=2),
    ...        dict(a=3, b=3, c=2, epoch=3),
    ... ]
    >>> chart = scatter_matrix_altair(data, columns, color='epoch')

    .. image:: ../../../docs/_static/plots/space_exploration.png

    """
    import altair as alt
    alt.themes.enable('dark')

    from olympus.dashboard.plots.utilities import AltairMatrix

    space = alt.Data(values=configs)
    base = alt.Chart().properties(width=120, height=120)

    def scatter_plot(row, col):
        """Standard Scatter plot"""
        return base.mark_circle(size=5).encode(
            alt.X(row, type='quantitative'),
            alt.Y(col, type='quantitative'),
            color=f'{color}:N').interactive()

    def density_plot(row):
        """Estimate the density function using KDE"""
        return base.transform_density(row,
                                      as_=[row, 'density'
                                           ]).mark_line().encode(x=f'{row}:Q',
                                                                 y='density:Q')

    def histogram_plot(row):
        """Show density as an histogram"""
        return base.mark_bar().encode(alt.X(row, type='quantitative',
                                            bin=True),
                                      y='count()')

    return (AltairMatrix(space).fields(*columns)
            # .upper(scatter_plot)
            .diag(histogram_plot).lower(scatter_plot)).render()
Exemplo n.º 11
0
def get_distribution() -> Any:
    scores = models.get_statistics_scores()

    data = alt.Data(values=[dict(score=score) for score in scores])
    return (alt.Chart(data).mark_bar().encode(
        x=alt.X("score:O", bin=True, axis=alt.Axis(title="package score")),
        y=alt.Y("count()", axis=alt.Axis(title="count")),
    ).configure_axis(grid=False).properties(width=600, height=400).to_json())
Exemplo n.º 12
0
 def geoencode(self, **kwargs) -> alt.Chart:  # coverage: ignore
     """Returns an `altair <http://altair-viz.github.io/>`_ encoding of the
     shape to be composed in an interactive visualization.
     Specific plot features, such as line widths, can be passed via **kwargs.
     See `documentation
     <https://altair-viz.github.io/user_guide/marks.html>`_.
     """
     return alt.Chart(alt.Data(values=self.geojson())).mark_geoshape(
         stroke="#aaaaaa", **kwargs)
 def prepare_for_altair(self,
                        df=None,
                        file_name=None,
                        processed_dir=cn.CSV_DIR):
     df = self.merge_data(df, file_name, processed_dir)
     gdf = gpd.GeoDataFrame(df, crs=cn.CRS_EPSG, geometry=cn.GEOMETRY)
     json_gdf = gdf.to_json()
     json_features = json.loads(json_gdf)
     return alt.Data(values=json_features['features'])
Exemplo n.º 14
0
    def __data_to_table(self, data):
        table = []

        for i in range(data.shape[0]):
            row = {'step': data[i, 0]}
            for j in range(1, 10):
                row[f"v{j}"] = data[i, j]
            table.append(row)

        return alt.Data(values=table)
Exemplo n.º 15
0
def data_to_table(data: np.ndarray, x_data: np.ndarray):
    table = []

    for i in range(data.shape[0]):
        if len(data.shape) == 2:  # Distribution
            row = {'x': x_data[i, 5], 'y': data[i, 5]}
        else:
            row = {'x': x_data[i], 'y': data[i]}
        table.append(row)

    return alt.Data(values=table)
def package_score_reports_to_scores_histogram(
    reports: Iterable[models.PackageReport], ) -> alt.Chart:
    """
    Returns a vega spec and data to render a histogram of the
    distribution of scores for the provided package score reports
    """
    data = alt.Data(values=[dict(score=report.score) for report in reports])
    return (alt.Chart(data).mark_bar().encode(
        x=alt.X("score:O", bin=True, axis=alt.Axis(title="package score")),
        y=alt.Y("count()", axis=alt.Axis(title="count")),
    ))
Exemplo n.º 17
0
def chart_obj(request):
    h_list = Homicide.objects.annotate(c=Count('count')).values(
        'age', 'c', 'gender')
    data = alt.Data(values=list(h_list))
    chart = alt.Chart(data, height=300, width=300,
                      title='Age / Gender').mark_circle().encode(
                          alt.X('age:Q', bin=True),
                          alt.Y('gender:N'),
                          alt.Size('sum(c):Q', title='Counts'),
                          color='sum(c):Q')
    return JsonResponse(chart.to_dict(), safe=False)
Exemplo n.º 18
0
def altair_example():
    import altair as alt
    data = alt.Data(values=[{'x': 'A', 'y': 5},
                            {'x': 'B', 'y': 3},
                            {'x': 'C', 'y': 6},
                            {'x': 'D', 'y': 7},
                            {'x': 'E', 'y': 2}])
    chart = alt.Chart(data).mark_bar().encode(
        x='x:O',  # specify ordinal data
        y='y:Q',  # specify quantitative data
    )
    return chart
Exemplo n.º 19
0
def unit_count_through_time():
    with open('/home/setepenre/work/LuaFun/botscpp/unit_size.txt', 'r') as f:
        data = [dict(x=i, y=int(r)) for i, r in enumerate(f.readlines())]

    data = alt.Data(values=data)

    chart = alt.Chart(data).mark_line().encode(
        x='x:Q',
        y='y:Q',
    )

    chart.save('unitcount.png', webdriver='firefox')
Exemplo n.º 20
0
def chart_stack(request):
    h_list = Homicide.objects.annotate(
        month=TruncMonth('date')).values('month').annotate(
            ct=Count('count')).values('month', 'ct', 'gender')
    data = alt.Data(values=list(h_list))

    chart = alt.Chart(data,
                      height=300,
                      width=300,
                      title='Month Count by Gender').mark_bar(size=30).encode(
                          x='month:T', y='ct:Q',
                          color='gender:N').interactive()
    return JsonResponse(chart.to_dict(), safe=False)
Exemplo n.º 21
0
def chart_suspect(request):
    h_list = Homicide.objects.annotate(ct=Count('count')).values(
        'ethnicity', 'killerethnicity', 'ct').annotate(s=Sum('ethnicity'))
    data = alt.Data(values=list(h_list))

    chart = alt.Chart(data, height=300, width=300,
                      title='Culprit vs Victim').mark_rect().encode(
                          alt.X('ethnicity:N', title='victim ethnicity'),
                          alt.Y('killerethnicity:N',
                                title='culprit ethnicity'),
                          color='ct:Q',
                          tooltip=['ct:Q']).interactive()
    return JsonResponse(chart.to_dict(), safe=False)
Exemplo n.º 22
0
def chart_cod(request):
    h_list = Homicide.objects.values('means', 'gender')
    data = alt.Data(values=list(h_list))
    chart = alt.Chart(data,
                      height=300,
                      width=300,
                      title='CoD - Cause of Death').mark_bar().encode(
                          alt.X('means:N',
                                title='Cause (G)un, (O)ther, (S)tabbing'),
                          alt.Y('count(means):Q', title='Count'),
                          color='gender:N',
                          tooltip=['count(means):Q'])
    return JsonResponse(chart.to_dict(), safe=False)
Exemplo n.º 23
0
def my_cool_chart_view(req):
    # import ipdb; ipdb.set_trace()
    query = Solution.objects.all().values()
    data = alt.Data(values=list(query))
    chart_obj = alt.Chart(data).mark_bar().encode(
        alt.Y("sector:N"),
        alt.X('co2_reduction:Q'),
        alt.Color("solution:N",
                  legend=None,
                  scale=alt.Scale(scheme='tableau20')),
        tooltip=["solution:N", 'co2_reduction:Q'])
    # import ipdb; ipdb.set_trace()
    return JsonResponse(chart_obj.to_dict(), safe=False)
Exemplo n.º 24
0
def main():
    from luafun.train.metrics import MetricWriter
    from luafun.utils.options import datapath
    from luafun.model.components import HeroEmbedding
    import luafun.game.constants as const

    uid = 'ea844d250dfa4137a8c386972424939c'
    writer = MetricWriter(datapath('metrics'), uid)

    hero_encoder = HeroEmbedding(128)

    henc = writer.load_weights('henc')
    hero_encoder.load_state_dict(henc)
    # ==== Done loading the model

    weight = list(hero_encoder.baseline.parameters())[0]
    print(weight.shape)

    viz = VizEmbeddingSpace()
    proj, var = viz.pca_torch(weight)
    # proj, var = viz.pca_manual(weight)
    print(var)
    print(proj.shape)

    points = []
    for i in range(122):
        x, y, z = proj[i, :] * 100
        hero = const.HERO_LOOKUP.from_offset(i)
        name = hero.get('pretty_name', hero.get('alias', hero.get('name')))
        points.append(dict(x=x.item(), y=y.item(), z=z.item(), name=name))

    import altair as alt
    alt.themes.enable('dark')

    # .mark_point().encode(
    #     x='x:Q',
    #     y='y:Q',
    #     color='z:Q',
    #     text='name:N'
    # )
    chart = alt.Chart(alt.Data(values=points)).mark_text().encode(
        x='x:Q',
        y='y:Q',
        color='z:Q',
        text='name:N'
    ).properties(
        width=1980 * 0.75,
        height=1080 * 0.75
    ).interactive()
    chart.save('chart_2.html')
Exemplo n.º 25
0
def merge_geodata(data):
    """
    Create the geospacial data
    :param data:
    :return:
    """
    gdf = get_geodata()
    gdf = gdf.merge(data, left_on='iso_a3', right_on='CountryCode', how='inner')

    # Convert the geodata to json
    choro_json = json.loads(gdf.to_json())
    final_geo_data = alt.Data(values=choro_json['features'])

    return final_geo_data
Exemplo n.º 26
0
    def test_chart(self):
        qs = Sale.objects.all().values('date', 'amount', 'payment_method')

        data = alt.Data(values=list(qs))

        chart = alt.Chart(data).mark_bar().encode(x='date:T',
                                                  y='amount:Q',
                                                  color='payment_method:N',
                                                  column='payment_method:N')

        output = render(chart.to_dict())

        pathlib.Path('test.svg').write_text(output['svg'])
        pathlib.Path('test.png').write_bytes(base64.b64decode(output['png']))
Exemplo n.º 27
0
def importance_heatmap_altair(fanova):
    """Outputs the importance of each hyper-parameter according to FANOVA

    Parameters
    ----------
    fanova: FANOVA
        instance of FANOVA class

    Examples
    --------

    >>> from olympus.dashboard.analysis.hpfanova import FANOVA
    >>> import pandas as pd
    >>> data = [
    ...     dict(objective=0.12 / 0.08, uid=0, epoch=32, hp1=0.12, hp2=0.08),
    ...     dict(objective=0.14 / 0.09, uid=0, epoch=32, hp1=0.14, hp2=0.09),
    ...     dict(objective=0.15 / 0.10, uid=0, epoch=32, hp1=0.15, hp2=0.10),
    ...     dict(objective=0.16 / 0.11, uid=0, epoch=32, hp1=0.16, hp2=0.11),
    ...     dict(objective=0.17 / 0.12, uid=0, epoch=32, hp1=0.17, hp2=0.12)
    ... ]
    >>> space = {
    ...     'hp1': 'uniform(0, 1)',
    ...     'hp2': 'uniform(0, 1)'
    ... }
    >>> data = pd.DataFrame(data)
    >>> fanova = FANOVA(
    ...    data,
    ...    hp_names=list(space.keys()),
    ...    objective='objective',
    ...    hp_space=space)
    >>> chart = importance_heatmap_altair(fanova)

    .. image:: ../../../docs/_static/plots/importance.png

    """
    import altair as alt
    alt.themes.enable('dark')

    data = alt.Data(values=fanova.importance_long)

    base = alt.Chart(data).mark_rect().encode(x='row:O',
                                              y='col:O').properties(width=200,
                                                                    height=200)

    chart = alt.concat(
        base.encode(color='importance:Q'),
        base.encode(color='std:Q')).resolve_scale(color='independent')

    return chart
Exemplo n.º 28
0
def chart_heat(request):
    h_list = Homicide.objects.annotate(ct=Count('count')).values(
        'ethnicity', 'gender', 'age', 'ct')
    data = alt.Data(values=list(h_list))

    chart = alt.Chart(
        data, height=300, width=300,
        title='Age vs Ethnicity').mark_rect().encode(
            alt.X('ethnicity:N', title='ethnicity'),
            alt.Y('age:N', title='age', bin=True),
            color=alt.Color('sum(ct):Q',
                            scale=alt.Scale(domain=['1', '25', '50'],
                                            range=['yellow', 'red', 'black'])),
            tooltip=['sum(ct):Q']).interactive()
    return JsonResponse(chart.to_dict(), safe=False)
Exemplo n.º 29
0
def map(business_to_choose="Beauty Salon"):
    """
    Given the business type and year, filter the dataset and plot a map for the counts of the buisness per neighbourhood

    parameters 
    --------------------
    business_to_choose: (str)
            type of business to filter the dataset
    year_to_choose: (int)
            year to filter the dataset
    returns
    ---------------------
    an altair map.
    """
    dataset = pd.read_csv("Data/dataset.csv").dropna()[[
        "FOLDERYEAR", "LicenceRSN", "LocalArea", "Business_type_pp", "X", "Y"
    ]]
    dataset_subset = dataset.query("Business_type_pp == @business_to_choose")
    url = "https://maps.vancouver.ca/server/rest/services/Hosted/NeighbourhoodBoundaries/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson"
    # Vancouver Map Json file
    r = requests.get(url)
    file = r.json()
    background_data = alt.Data(values=file['features'])

    # Vancouver Background
    background = alt.Chart(background_data).mark_geoshape(
        fill='lightgray', stroke='white').encode().properties(width=500,
                                                              height=300)

    # Obtaining the mean coordinates per LocalArea
    dataset_subset_grouped = dataset_subset.groupby(by="LocalArea").aggregate({
        "LicenceRSN":
        'count',
        "X":
        "mean",
        "Y":
        "mean"
    }).reset_index()
    points = alt.Chart(dataset_subset_grouped).mark_circle().encode(
        longitude='X:Q',
        latitude='Y:Q',
        size=alt.Size('LicenceRSN:Q', title='Number of ' + business_to_choose),
        color=alt.value('steelblue'),
        tooltip=[
            "LocalArea", "LicenceRSN"
        ]).properties(title='Number of ' + business_to_choose +
                      ' Registered Businesses in Vancouver per Neighbourhood')
    return background + points
Exemplo n.º 30
0
def test_chart_data():
    data = pd.read_csv(CSV_URL)

    # sanity check
    assert data.equals(pd.read_json(JSON_URL))

    chart1 = altair.Chart(data)
    chart2 = altair.Chart(CSV_URL)
    chart3 = altair.Chart(JSON_URL)

    # TODO: test layered charts

    data_by_value = altair.Data(values=data.to_dict(orient='records'))
    for datatype in [data, CSV_URL, JSON_URL, data_by_value]:
        chart = altair.Chart(datatype)
        assert data.equals(chart_data(chart))