def metrics(M, metrics_labels, types, filename='metrics.html'): rows = [(metrics_labels[i], types[j], types[k], m) for (i, j, k), m in np.ndenumerate(M)] columns = ['m', 't1', 't2', 'v'] df = pd.DataFrame.from_records(rows, columns=columns) rating_radio = alt.binding_radio(options=metrics_labels) rating_select = alt.selection_single(fields=['m'], bind=rating_radio, name="Metric", empty='none', init={'m': metrics_labels[0]}) heatmap = alt.Chart(df).encode( alt.X('t2:N'), alt.Y('t1:N'), ).mark_rect().encode(color='v:Q') text = alt.Chart(df).encode( alt.X('t2:N'), alt.Y('t1:N'), ).mark_text(baseline='middle').encode( text=alt.Text('v:Q', format='.3f'), color=alt.value('black'), # color=alt.condition( # alt.datum.v < 0.8, # alt.value('black'), # alt.value('white') # ) ) p = (heatmap + text).add_selection(rating_select).transform_filter( rating_select).properties(width=400, height=400) p.save(filename) return p
def plot_tsne(source, x_col, y_col, category, perplexities, img_name): cats = list(source[category].unique()) # Base base = alt.Chart(source).mark_point(filled=True).encode(x=x_col + ':Q', y=y_col + ':Q', tooltip=img_name + ':N') # A slider filter perp_slider = alt.binding_range(min=10, max=40, step=10) slider_selection = alt.selection_single(bind=perp_slider, fields=[perplexities], name="Change") # Color changing marks rating_radio = alt.binding_radio(options=cats) rating_select = alt.selection_single(fields=[category], bind=rating_radio, name="Filter") rating_color_condition = alt.condition( rating_select, alt.Color(category + ':N', legend=None), alt.value('lightgray')) highlight_ratings = base.add_selection( rating_select, slider_selection).encode( color=rating_color_condition).transform_filter( slider_selection).properties(title="tSNE Scatter Plot") return highlight_ratings.properties(width=800, height=300)
def plot_normalization_comparison(): normalized = pd.read_csv( 'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/id_normalized.csv', names=['NodeID', 'id']) not_normal = pd.read_csv( 'https://raw.githubusercontent.com/jamescoller/multilayer_design_network_tool/master/results/id_not_normalized.csv', names=['NodeID', 'id']) # normalization = pd.DataFrame() # normalization['NodeID'] = normalized['NodeID'] # normalization['normal'] = normalized['id'] # normalization['not_normal'] = not_normal['id'] # normalization normalized['normal'] = 1 not_normal['normal'] = 0 norm2 = pd.concat([normalized, not_normal]) radial_input = alt.binding_radio(options=[1, 0]) norm_choice = alt.selection_single(fields=['normal'], bind=radial_input, name='Normalized?') norm_comparison = alt.Chart(norm2).mark_bar().encode( x=alt.X('id:Q', bin=alt.Bin(maxbins=20), title='Interdependency Rating'), y=alt.Y('count()', title='Number of Nodes')).add_selection( norm_choice).transform_filter(norm_choice).transform_filter( alt.datum.NodeID != 64) norm_comparison.serve()
def other_viz3(result): #result = load5() types = [ 'Action Fires/Supressed Fires', 'Natural Out', 'Support Action/Assist Fire', 'Fire Management/Perscribed', 'False Alarm', 'Severe' ] firetype_df2 = result.groupby(['YEAR_', 'FIRETYPE' ]).size().reset_index(name="Firetype Count") firetype_df2['FIRETYPE'] = firetype_df2['FIRETYPE'].replace( 0, 'Action Fires/Supressed Fires') firetype_df2['FIRETYPE'] = firetype_df2['FIRETYPE'].replace( 1, 'Natural Out') firetype_df2['FIRETYPE'] = firetype_df2['FIRETYPE'].replace( 2, 'Support Action/Assist Fire') firetype_df2['FIRETYPE'] = firetype_df2['FIRETYPE'].replace( 3, 'Fire Management/Perscribed') firetype_df2['FIRETYPE'] = firetype_df2['FIRETYPE'].replace( 4, 'False Alarm') firetype_df2['FIRETYPE'] = firetype_df2['FIRETYPE'].replace(5, 'Severe') base = alt.Chart(firetype_df2, width=600, height=400).mark_point(filled=True).encode( x=alt.X('YEAR_:N', axis=alt.Axis(labelAngle=360, values=[ 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2016 ]), title='Year'), y='Firetype Count:Q', tooltip=["FIRETYPE:N", 'YEAR_:N']) type_radio = alt.binding_radio(options=types) type_select = alt.selection_single(fields=['FIRETYPE'], bind=type_radio, name='Pick a') type_color_condition = alt.condition(type_select, alt.Color('FIRETYPE:N'), alt.value('lightgray')) highlight_types = base.add_selection(type_select).encode( color=type_color_condition).properties( title="U.S. Number of Fires (1980-2016)") highlight_types
name="Release Year_") filter_year = base.add_selection(slider_selection).transform_filter( slider_selection).properties(title="Slider Filtering") # A dropdown filter genre_dropdown = alt.binding_select(options=genres) genre_select = alt.selection_single(fields=['Major_Genre'], bind=genre_dropdown, name="Genre") filter_genres = base.add_selection(genre_select).transform_filter( genre_select).properties(title="Dropdown Filtering") #color changing marks rating_radio = alt.binding_radio(options=ratings) rating_select = alt.selection_single(fields=['MPAA_Rating'], bind=rating_radio, name="Rating") rating_color_condition = alt.condition(rating_select, alt.Color('MPAA_Rating:N', legend=None), alt.value('lightgray')) highlight_ratings = base.add_selection(rating_select).encode( color=rating_color_condition).properties(title="Radio Button Highlighting") # Boolean selection for format changes input_checkbox = alt.binding_checkbox() checkbox_selection = alt.selection_single(bind=input_checkbox, name="Big Budget Films")
def chart_altair(df, system_ini='peninsular'): ''' Create an altair chart with the average of last 7 days of electric generation of total enery, renewable energy, solar photovoltaic energy and wind powered energy by date, the chart can be filtered by year and electric system. also add a vertical line to show the values where put the mouse in the chart. :param df: Pandas Dataframe with de average of last 7 days of electric generation by electric system, date, year and technology :param system_ini: Initial system to show in the chart, by default 'peninsular :return: altair layered chart ''' # labels of X axis to show in the chart, every first day of month from 2016-01 to 2021-12. x_labels = [ '2016-01-01', '2016-02-01', '2016-03-01', '2016-04-01', '2016-05-01', '2016-06-01', '2016-07-01', '2016-08-01', '2016-09-01', '2016-10-01', '2016-11-01', '2016-12-01', '2016-12-31', '2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01', '2017-05-01', '2017-06-01', '2017-07-01', '2017-08-01', '2017-09-01', '2017-10-01', '2017-11-01', '2017-12-01', '2017-12-31', '2018-01-01', '2018-02-01', '2018-03-01', '2018-04-01', '2018-05-01', '2018-06-01', '2018-07-01', '2018-08-01', '2018-09-01', '2018-10-01', '2018-11-01', '2018-12-01', '2018-12-31', '2019-01-01', '2019-02-01', '2019-03-01', '2019-04-01', '2019-05-01', '2019-06-01', '2019-07-01', '2019-08-01', '2019-09-01', '2019-10-01', '2019-11-01', '2019-12-01', '2019-12-31', '2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01', '2020-07-01', '2020-08-01', '2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01', '2020-12-31', '2021-01-01', '2021-02-01', '2021-03-01', '2021-04-01', '2021-05-01', '2021-06-01', '2021-07-01', '2021-08-01', '2021-09-01', '2021-10-01', '2021-11-01', '2021-12-01', '2021-12-31' ] # list of elements in Color to be plotted domain = ['Generación total', 'Renovable', 'Solar fotovoltaica', 'Eólica'] # colors in hexadecimal, for each element in domain list range_ = ['#85C1E9', '#239B56', '#D35400', '#F7DC6F'] # set a select box to select the system to show in the chart select_box_sys = alt.binding_select(options=list(df['system'].unique())) selection_sys = alt.selection_single(name='REE', fields=['system'], bind=select_box_sys, init={'system': system_ini}) # set a radio selector to select the year to show in the chart select_radio_year = alt.binding_radio(options=list(df['year'].unique())) selection_year = alt.selection_single(name='Choose', fields=['year'], bind=select_radio_year, init={'year': max(df['year'])}) # create a markpoint with variable fecha as X axis # with a selection that works over the variable fecha showing the nearest value where the mouse is over. nearest = alt.selection(type='single', nearest=True, on='mouseover', fields=['fecha'], empty='none') selectors = alt.Chart(df).mark_point().encode( alt.X('fecha'), opacity=alt.value(0)).add_selection(nearest).transform_filter( selection_sys).transform_filter(selection_year) # Create the main chart, with the electric generation by date, and add the selectors of year and systems bar = alt.Chart(df[df['Renov_norenov'] == 'Generación total']).mark_area( color='#85C1E9').encode( alt.X('fecha', axis=alt.Axis(values=x_labels, labelAngle=0)), alt.Y('Generacion_Mwh:Q')).add_selection( selection_sys, selection_year).transform_filter( selection_sys).transform_filter(selection_year).properties( width=1400, height=450) # Create the chart of renewable energy by fecha, also add the color with the list domain and his colors in list range_ # also add the transformers of the main chart bar_renov = alt.Chart( df[df['Tecnologia'] == 'Renovable']).mark_area().encode( alt.X('fecha'), alt.Y('Generacion_Mwh:Q'), color=alt.Color('Tecnologia', scale=alt.Scale(domain=domain, range=range_)) ).transform_filter(selection_sys).transform_filter(selection_year) # add a text chart to show the value of the bar_renov chart text_renov = bar_renov.mark_text( align='left', dx=3, dy=-20, color='#212F3C').encode( text=alt.condition(nearest, 'Generacion_Mwh', alt.value(' '))) rules = alt.Chart(df).mark_rule(color='gray').encode( x='fecha', ).transform_filter(nearest) # Create the chart of Solar photovoltaic by fecha, also add the transformers of the main chart bar_solar = alt.Chart( df[df['Tecnologia'] == 'Solar fotovoltaica']).mark_area( opacity=.8, color='#D35400').encode( alt.X('fecha'), alt.Y('Generacion_Mwh:Q')).transform_filter( selection_sys).transform_filter(selection_year) # add a text chart to show the value of the bar_renov chart text_solar = bar_solar.mark_text( align='left', dx=5, dy=-5, color='#212F3C').encode( text=alt.condition(nearest, 'Generacion_Mwh', alt.value(' '))) # Create the chart of wind power by fecha, also add the transformers of the main chart bar_eolica = alt.Chart( df[df['Tecnologia'] == 'Eólica']).mark_area(color='#F7DC6F').encode( alt.X('fecha'), alt.Y('Generacion_Mwh:Q')).transform_filter( selection_sys).transform_filter(selection_year) # add a text chart to show the value of the bar_renov chart text_eolica = bar_eolica.mark_text( align='left', dx=5, dy=-5, color='#212F3C').encode( text=alt.condition(nearest, 'Generacion_Mwh', alt.value(' '))) # retrun a altair layered chart with all the elements created in the function return alt.layer( bar, bar_renov, bar_eolica, bar_solar, selectors, rules, text_renov, text_eolica, text_solar).configure_axis( labelFontSize=13, titleFontSize=14).configure_text( fill='#212F3C', fontSize=13).configure_legend(labelFontSize=14).interactive()
st.write(base_peak) if 1 == 1: base = alt.Chart(data).mark_circle(size=60).encode( x=alt.X('phi', type='ordinal'), y=alt.Y(option_FOM, type='quantitative', scale=alt.Scale(type='log')), facet=alt.Facet('bf_id_s:N', columns=len(unq_bf), spacing=-.5), color='np_id_s:N', size='a_33:O', ).properties(width=180).interactive() p_radio = alt.binding_radio(options=unq_p.tolist()) p_select = alt.selection_single(fields=['p'], bind=p_radio, name="Aspect ratio, p") p_color_condition = alt.condition( p_select, alt.Color('p:N', legend=None), alt.value('lightgray')) np_dropdown = alt.binding_select(options=unq_np.tolist()) np_select = alt.selection_single(fields=['np_id_s'], bind=np_dropdown, name="Nanoparticle") radio_p = base.add_selection(p_select).encode( color=p_color_condition, ).add_selection( np_select).transform_filter(np_select).properties(
def make_figure(ranks_pd: pd.DataFrame, o_ranks_explored: str, p_pair_number: int, p_color_palette: str, omic1_column: str, omic2_column: str, omic1: str, omic2: str, p_omic1_filt: str, p_omic1_value: str, p_omic2_filt: str, p_omic2_value: str, p_omic1_max: int, p_omic2_max: int, p_color_top: str) -> None: conditionals_1 = 'conditionals_per_%s' % omic1 conditionals_2 = 'conditionals_per_%s' % omic2 if p_omic1_max: ranks_pd = ranks_pd.loc[ranks_pd[conditionals_1] <= p_omic1_max, :] if p_omic2_max: ranks_pd = ranks_pd.loc[ranks_pd[conditionals_2] <= p_omic2_max, :] ranks_pd2merge = ranks_pd[[omic1, omic2, conditionals_1, conditionals_2]] ranks_st = get_stacked(ranks_pd, omic1_column, omic2_column, omic1, omic2) ranks_st = ranks_st.merge(ranks_pd2merge, on=[omic1, omic2], how='left') text = [ "\n\nCo-occurrence matrix exploration tool (mmvec)", "%s (%s features) vs. %s (%s features)" % (omic1, ranks_st[omic1].unique().size, omic2, ranks_st[omic2].unique().size) ] subtext = [] if p_omic1_filt and p_omic1_value: subtext.append("Subset %s for '%s' in '%s'" % (omic1, ', '.join(list(p_omic1_value)), p_omic1_filt)) if p_omic2_filt and p_omic2_value: subtext.append("Subset %s for '%s' in '%s'" % (omic2, ', '.join(list(p_omic1_value)), p_omic2_filt)) conditionals = [ 'conditionals', 'ranked_conditionals', conditionals_1, conditionals_2 ] conditionals_radio = alt.binding_radio(options=conditionals, name="Co-occurrence measure") conditionals_select = alt.selection_single( fields=['conditional'], bind=conditionals_radio, init={'conditional': 'conditionals'}) max_rank1 = int(ranks_st[conditionals_1].max()) init1 = p_pair_number if p_pair_number > max_rank1: init1 = max_rank1 slider1 = alt.binding_range(min=1, max=max_rank1, step=1, name='Maximum number of %s for each %s:' % (omic2, omic1)) selector1 = alt.selection_single(name="cutoff1", fields=['cutoff1'], bind=slider1, init={'cutoff1': init1}) max_rank2 = int(ranks_st[conditionals_2].max()) init2 = p_pair_number if p_pair_number > max_rank2: init2 = max_rank2 slider2 = alt.binding_range(min=1, max=max_rank2, step=1, name='Maximum number of %s for each %s:' % (omic1, omic2)) selector2 = alt.selection_single(name="cutoff2", fields=['cutoff2'], bind=slider2, init={'cutoff2': init2}) mlt1 = alt.selection_multi(fields=[omic1], toggle=True) mlt2 = alt.selection_multi(fields=[omic2], toggle=True) slider_label1 = alt.binding_range(min=0, max=max_rank1, step=1, name='Label this much top %s per %s:' % (omic2, omic1)) label1 = alt.selection_single(name="cutoff_label1", fields=['cutoff_label1'], bind=slider_label1, init={'cutoff_label1': 0}) slider_label2 = alt.binding_range(min=0, max=max_rank2, step=1, name='Label this much top %s per %s:' % (omic1, omic2)) label2 = alt.selection_single(name="cutoff_label2", fields=['cutoff_label2'], bind=slider_label2, init={'cutoff_label2': 0}) sorted_omic1 = get_sorted(ranks_st, omic1_column, omic1) sorted_omic2 = get_sorted(ranks_st, omic2_column, omic2) tooltips = [ omic1, omic2, 'conditional', 'rank', conditionals_1, conditionals_2 ] if omic1_column: tooltips.append(omic1_column) if omic2_column: tooltips.append(omic2_column) x_size = len(sorted_omic1) * 6 y_size = len(sorted_omic2) * 6 rect = alt.Chart(ranks_st).mark_rect().encode( x=alt.X('%s:O' % omic1, sort=sorted_omic1, axis=alt.Axis(labelOverlap=False, labelFontSize=6, labelLimit=500, orient='top', labelAngle=45, titleFontSize=0)), y=alt.Y('%s:O' % omic2, sort=sorted_omic2, axis=alt.Axis(labelOverlap=False, labelFontSize=6, titleFontSize=0, labelLimit=500)), color=alt.Color('rank:Q', legend=alt.Legend(orient='left'), sort="descending", scale=alt.Scale(scheme=p_color_palette)), tooltip=tooltips).add_selection( conditionals_select, mlt1, mlt2, selector1, selector2).transform_filter(conditionals_select).transform_filter( mlt1).transform_filter(mlt2).transform_filter( alt.datum[conditionals_1] <= selector1.cutoff1 ).transform_filter( alt.datum[conditionals_2] <= selector2.cutoff2).properties( width=x_size, height=y_size, ) circ = alt.Chart(ranks_st).mark_point(size=5, shape='diamond').encode( x=alt.X('%s:O' % omic1, sort=sorted_omic1, axis=alt.Axis(labelOverlap=False, labelFontSize=6, orient='top', labelAngle=45, titleFontSize=0)), y=alt.Y('%s:O' % omic2, sort=sorted_omic2, axis=alt.Axis(labelOverlap=False, labelFontSize=6, titleFontSize=0)), color=alt.ColorValue(p_color_top), tooltip=tooltips).add_selection(label1, label2).transform_filter( conditionals_select ).transform_filter(mlt1).transform_filter(mlt2).transform_filter( alt.datum[conditionals_1] <= selector1.cutoff1).transform_filter( alt.datum[conditionals_2] <= selector2.cutoff2 ).transform_filter( alt.datum[conditionals_1] <= label1.cutoff_label1 ).transform_filter( alt.datum[conditionals_2] <= label2.cutoff_label2).properties( width=x_size, height=y_size) bar_omic1 = get_bar_chart(ranks_st, sorted_omic1, conditionals_1, conditionals_2, omic1_column, omic1, omic1, omic2, x_size, y_size, mlt1, selector1, selector2) bar_omic2 = get_bar_chart(ranks_st, sorted_omic2, conditionals_1, conditionals_2, omic2_column, omic2, omic1, omic2, x_size, y_size, mlt2, selector1, selector2) chart = alt.vconcat(alt.hconcat((rect + circ), bar_omic2), bar_omic1) chart.resolve_legend( color="independent", size="independent").configure_axis( labelLimit=300, labelFontSize=8, ).configure_legend(labelLimit=1000, labelFontSize=8, titleFontSize=8, symbolSize=12, columns=3).properties( title={ "text": text, "subtitle": (subtext + ["(based on altair)"]), "color": "black", "subtitleColor": "grey" }) if not isdir(dirname(o_ranks_explored)): os.makedirs(dirname(o_ranks_explored)) chart.save(o_ranks_explored) print('-> Written:', o_ranks_explored)