def visualise_tsne(tsne_df, save=True, fig_num=15): """Visualise tsne plot""" tsne_base = alt.Chart(tsne_df).encode( x=alt.X("x:Q", title="", axis=alt.Axis(ticks=False, labels=False)), y=alt.Y("y:Q", title="", axis=alt.Axis(ticks=False, labels=False)), ) tsne_points = (( tsne_base.mark_point( filled=True, opacity=0.5, stroke="black", strokeOpacity=0.5).encode( color=alt.Color("org_type", title="Organisation type"), strokeWidth=alt.Stroke("top", scale=alt.Scale(range=[0, 1]), legend=None), # stroke = alt.value('blue'), size=alt.Size("activity:Q", title="Number of papers"), facet=alt.Facet("size", columns=2, title="Number of organisations in plot"), tooltip=["index"], )).interactive().resolve_scale( y="independent", x="independent").properties(width=250, height=250)) if save is True: save_altair(tsne_points, "fig_15_tsne", driv) return tsne_points
def main(): st.title('Covid-19 SIR Model') days=st.slider('Select no. of days :',0,100,1,1) Istart=st.slider('Intially infected pop % :',.01,1.0,.01,.01) trans_rate=st.slider('Transfer rate :',0,10,1,1) reco_rate=st.slider('Recovery rate :',0.0,1.0,0.1,0.01) df = corona(Istart,trans_rate,reco_rate,100,days) df_melt=pd.melt(df,id_vars='Days',value_vars=['Suspect','Infected','Recovered'],var_name='Legend',value_name='% of Population') st.altair_chart(alt.layer( Charts(df_melt,'Infected','darkred'), Charts(df_melt,'Suspect','darkblue'), Charts(df_melt,'Recovered','darkgreen')).encode( alt.Stroke( 'clm:N',scale=alt.Scale(domainMid=1,domain=['Infected','Suspect','Recoverd'], range=['darkred','darkblue','darkgreen'] ))))
def test_convert_stroke_fail_temporal(column): chart = alt.Chart(df).mark_point().encode(alt.Stroke(column)) convert(chart)
def app(): # Load 6,000 rows of data into the dataframe. bData = load_data(bFile) rRawData = load_data(rFile, rDateCol, 6000) # add col to bData for int version of ratings bData['starsInt'] = [int(i) for i in bData['stars']] # parse rData to add year column rRawData['year'] = pd.DatetimeIndex(rRawData['date'], tz='UTC').year rData = copy.copy(rRawData) rData = rData.drop(columns=['date']) # st.subheader('Dropped date col') # st.write(rData.head()) yearSet = set(rData['year']) bSmallData = bData.head() rSmallData = rData.head() #uSmallData = uData.head() ################################################################################ # Titles and Information ################################################################################ st.title('Explore City-Wide Restaurant Reviews') st.subheader( 'Check out the ratings of restaurants in Pittsburgh and other major cities, their reviews, and their sentiment over time.' ) ################################################################################ # Maps ################################################################################ # MAP VISUALIZATION # break apart data into city specific data pittsburghData = bData.loc[bData['city'] == 'Pittsburgh'] montrealData = bData.loc[bData['city'] == 'Montréal'] clevelandData = bData.loc[bData['city'] == 'Cleveland'] torontoData = bData.loc[bData['city'] == 'Toronto'] st.header('City Restaurant Data') # slider and data from user input #st.map(bData) selectedStars = st.sidebar.slider('Filter maps by selecting ratings:', 0, 5, (0, 5)) filteredDataP = pittsburghData[ (pittsburghData['starsInt'] >= selectedStars[0]) & (pittsburghData['starsInt'] <= selectedStars[1])] filteredDataM = montrealData[ (montrealData['starsInt'] >= selectedStars[0]) & (montrealData['starsInt'] <= selectedStars[1])] filteredDataC = clevelandData[ (clevelandData['starsInt'] >= selectedStars[0]) & (clevelandData['starsInt'] <= selectedStars[1])] filteredDataT = torontoData[(torontoData['starsInt'] >= selectedStars[0]) & (torontoData['starsInt'] <= selectedStars[1])] # PREFERENCE OF FILTERED DATA # initiate bar chart below maps starsBarP = barChart = alt.Chart( pittsburghData, title='Pittsburgh Restaurant Rating').mark_bar().encode( alt.X('count():Q', title='Number of reviews'), alt.Y('starsInt:N', title='Stars Rated'), alt.Color('starsInt:Q', legend=None)) starsBarM = barChart = alt.Chart( montrealData, title='Montreal Restaurant Rating').mark_bar().encode( alt.X('count():Q', title='Number of reviews'), alt.Y('starsInt:N', title='Stars Rated'), alt.Color('starsInt:Q', legend=None)) starsBarC = barChart = alt.Chart( clevelandData, title='Cleveland Restaurant Rating').mark_bar().encode( alt.X('count():Q', title='Number of reviews'), alt.Y('starsInt:N', title='Stars Rated'), alt.Color('starsInt:Q', legend=None)) starsBarT = barChart = alt.Chart( torontoData, title='Toronto Restaurant Rating').mark_bar().encode( alt.X('count():Q', title='Number of reviews'), alt.Y('starsInt:N', title='Stars Rated'), alt.Color('starsInt:Q', legend=None)) #CONFIGURE LAYOUT # first 2 maps col1, col2 = st.beta_columns(2) col1.subheader('Pittsburgh') col1.map(filteredDataP) col1.write(starsBarP) col2.subheader('Montreal') col2.map(filteredDataM) col2.write(starsBarM) # second 2 maps col3, col4 = st.beta_columns(2) col3.subheader('Cleveland') col3.map(filteredDataC) col3.write(starsBarC) col4.subheader('Toronto') col4.map(filteredDataT) col4.write(starsBarT) ################################################################################ # Exploratory Visuals ################################################################################ # PREFERENCE OVER TIME rDataFiltered = copy.copy(rData) cola, colb, colc = st.beta_columns(3) pref = 'neu' prefTitle = 'Neutral ' st.subheader('View colorized sentiment overtime.') st.write('Select review type to filter preference overtime') if cola.button('Negative Reviews'): #rDataFiltered = rDataFiltered[rDataFiltered['neg']] pref = 'neg' prefTitle = 'Negative ' if colb.button('Positive Reviews'): #rDataFiltered = rDataFiltered[rDataFiltered['pos']] pref = 'pos' prefTitle = 'Positive ' if colc.button('Neutral Reviews'): #rDataFiltered = rDataFiltered[rDataFiltered['neu']] pref = 'neu' prefTitle = 'Neutral ' st.write('Preference Over Time:') preference = alt.Chart( rDataFiltered, title='Preference Over Time').mark_circle(strokeWidth=2).encode( alt.X('year:N', title='Date', axis=alt.Axis(labelAngle=0)), alt.Y('bcity:N', title='City'), alt.Size('count():Q', scale=alt.Scale(range=[0, 4000]), legend=alt.Legend(title='Number of Reviews')), #alt.Opacity(pref, type = 'quantitative', aggregate = 'mean'), alt.Stroke('bcity:N', legend=None), alt.Color(pref, type='quantitative', aggregate='mean'), alt.Tooltip('neg:Q', aggregate='mean')).properties(height=440, width=800) # map total average pref reviews over time totalPreference = alt.Chart( rDataFiltered, title=prefTitle + 'Over Time').mark_area( opacity=0.8, color='black').encode( alt.X('year:O', axis=alt.Axis(labelAngle=0), title='Year'), alt.Y(pref, type='quantitative', title=prefTitle + 'Sentiment Average')).properties( height=150, width=800) st.write(preference & totalPreference)
def test_quantitative_stroke(): chart = alt.Chart(df_quant).mark_point().encode(alt.Stroke('fill')) convert(chart)
def test_line_stroke(self, x, y, s): chart = alt.Chart(df_line).mark_line().encode(alt.X(x), alt.Y(y), alt.Stroke(s)) fig, _ = convert(chart) return fig
def noise_plot(source=data['students']['kd']['noise'], title="Diferencia con ResNet101 contra ruido", y='delta_teacher', x='noise', color='feat_dist', shape='layer', student='ResNet18', xscale='linear', yscale='linear'): d = locals() ks = [ i for i in d.keys() if i not in ['source', 'title', 'xscale', 'yscale', 'scale', 'bs'] ] vals = [d[i] for i in ks] source = source.drop(columns=[ i for i in source.columns if i not in vals + ['student', 'noise'] ]) sou = source[source['student'] == student] #reduce data selection = alt.selection_multi(fields=[color], bind='legend') ytitle = global_titles[ y] if yscale == 'linear' else global_titles[y] + " [%s]" % yscale xtitle = global_titles[ x] if xscale == 'linear' else global_titles[x] + " [%s]" % xscale base = alt.Chart(sou, title=title).mark_line().encode( alt.Y(y, type='quantitative', title=ytitle, scale=alt.Scale(zero=False, base=10, type=yscale)), alt.X('noise:Q', type='quantitative', scale=alt.Scale(zero=False, base=10, type=xscale), title=xtitle), color=alt.Color('%s:N' % color, legend=alt.Legend(title=global_titles[color]), scale=alt.Scale(scheme='spectral')), strokeDash=alt.Stroke('%s:N' % shape, legend=alt.Legend(title=global_titles[shape])), opacity=alt.condition( selection, alt.value(1), alt.value(0.2))).add_selection(selection).properties(width=600, height=600) line = alt.Chart(sou).mark_line().encode( x=x, #y='mean(%s)'%y, y=alt.Y('mean(%s)' % y, type='quantitative', title=ytitle, scale=alt.Scale(zero=False, base=10, type=yscale)), color=alt.Color('%s:N' % color, legend=alt.Legend(title=global_titles[color]), scale=alt.Scale(scheme='spectral')), opacity=alt.condition(selection, alt.value(1), alt.value(0.2)), shape=alt.Shape('%s:N' % color, legend=alt.Legend(title='media')), #opacity=alt.value(0.5) ) point = alt.Chart(sou).mark_point(filled=True).encode( x=x, #y='mean(%s)'%y, y=alt.Y('mean(%s)' % y, type='quantitative', title=ytitle, scale=alt.Scale(zero=False, base=10, type=yscale)), color=alt.Color('%s:N' % color, legend=alt.Legend(title=global_titles[color]), scale=alt.Scale(scheme='spectral')), opacity=alt.condition(selection, alt.value(1), alt.value(0.2)), shape=alt.Shape('%s:N' % color, legend=alt.Legend(title='media')), #opacity=alt.value(0.5) ) band = alt.Chart(sou).mark_errorband(extent='ci').encode( x=alt.X('noise', title=xtitle), y=alt.Y('%s' % y, title=global_titles[y]), color=alt.Color('%s:N' % color, legend=alt.Legend(title=global_titles[color]), scale=alt.Scale(scheme='spectral')), opacity=alt.condition(selection, alt.value(0.4), alt.value(0.05))) w = base + band + line + point w.properties(width=600, height=600) return w
def grafico_con_barra_y_CE( source=data['students']['kd']['KD'], title="Diferencia de accuracy en entrenamiento con respecto a Resnet101.", y="delta_teacher", x="feat_dist", fill='layer', color='student', shape='layer', bs=True, scale='linear'): if x == 'dists': source = source.copy() source['dists'] = source['feat_dist'] + ", " + source['log_dist'] elif x == 'feat,block': source = source.copy() source['feat,block'] = [ i + ", " + str(j) for i, j in list(zip(source['feat_dist'], source['layer'])) ] #reduce data d = locals() ks = [ i for i in d.keys() if i not in ['source', 'title', 'xscale', 'yscale', 'scale', 'bs'] ] vals = [d[i] for i in ks] source = source.drop(columns=[i for i in source.columns if i not in vals]) chart = alt.Chart(source, title=title).mark_point(size=100).encode( y=alt.Y( y, type='quantitative', scale=alt.Scale(zero=True, base=2, constant=1, type=scale), title=global_titles[y] if scale == 'linear' else global_titles[y] + " [%s]" % scale), x=alt.X(x, title=global_titles[x]), color=alt.Color(color, legend=alt.Legend(title=global_titles[color])), fill=alt.Fill('%s:O' % fill, legend=alt.Legend(title=global_titles[fill]), scale=alt.Scale(scheme='pastel1')), shape=alt.Shape("%s:O" % shape, legend=alt.Legend(title=global_titles[shape])), opacity=alt.value(0.5)) bar = alt.Chart(bar_source).mark_rule(opacity=0.5).encode( y=y, color=alt.Color('model', legend=alt.Legend(title="Modelo")), stroke=alt.Stroke('model', legend=alt.Legend(title="Modelo en Cross Entropy")), size=alt.value(2)) if bs: bar = alt.Chart(bar_source).mark_rule(opacity=0.5).encode( y=y, color=alt.Color('model', legend=alt.Legend(title="Modelo")), stroke=alt.Stroke( 'model', legend=alt.Legend(title="Modelo en Cross Entropy")), size=alt.value(2)) d = bar + chart d.properties(width=600, height=600).configure_axis( titleFontSize=12).configure_title(fontSize=15) return d chart.properties(width=600, height=600).configure_axis( titleFontSize=12).configure_title(fontSize=15) return chart
def grafico_con_barra_xy_CE( source=data['students']['kd']['KD'], title="Ratio entre exactitud en validacion y accuracy en entrenamiento.", y="test/train", x="temp", color='student', shape='log_dist', bs=True, fill=None, xscale='log', yscale='linear'): #reduce data d = locals() ks = [ i for i in d.keys() if i not in ['source', 'title', 'xscale', 'yscale', 'scale', 'bs'] ] vals = [d[i] for i in ks] source = source.drop(columns=[i for i in source.columns if i not in vals]) ytitle = global_titles[ y] if yscale == 'linear' else global_titles[y] + " [%s]" % yscale xtitle = global_titles[ x] if xscale == 'linear' else global_titles[x] + " [%s]" % xscale encodings = { 'y': alt.Y(y, type='quantitative', title=ytitle, scale=alt.Scale(zero=False, base=10, type=yscale)), 'x': alt.X(x, type='quantitative', title=xtitle, scale=alt.Scale(zero=False, base=10, type=xscale)), 'color': alt.Color(color, legend=alt.Legend(title=global_titles[color])), 'shape': alt.Shape(shape, legend=alt.Legend(title=global_titles[shape])), #size=50 } if fill is not None: encodings["fill"] = alt.Fill(fill) chart = alt.Chart( source, title=title).mark_point(size=100).encode(**encodings).interactive() if bs: bar = alt.Chart(bar_source).mark_rule(opacity=0.5).encode( y=y, color=alt.Color('model', legend=alt.Legend(title="Modelo")), stroke=alt.Stroke( 'model', legend=alt.Legend(title="Modelo en Cross Entropy")), size=alt.value(2)) d = bar + chart d.properties(width=600, height=600).configure_axis( titleFontSize=12).configure_title(fontSize=15) return d chart.properties(width=600, height=600).configure_axis( titleFontSize=12).configure_title(fontSize=15) return chart
if colc.button('Neutral Reviews'): #rDataFiltered = rDataFiltered[rDataFiltered['neu']] pref = 'neu' prefTitle = 'Neutral ' st.write('Preference Over Time:') methods2 = alt.Chart( rDataFiltered, title='Preference Over Time').mark_circle(strokeWidth=2).encode( alt.X('year:N', title='Date', axis=alt.Axis(labelAngle=0)), alt.Y('bcity:N', title='City'), alt.Size('count():Q', scale=alt.Scale(range=[0, 4000]), legend=alt.Legend(title='Number of Reviews')), #alt.Opacity(pref, type = 'quantitative', aggregate = 'mean'), alt.Stroke('bcity:N', legend=None), alt.Color(pref, type='quantitative', aggregate='mean'), alt.Tooltip('neg:Q', aggregate='mean')).properties(height=440, width=800) #st.write(methods2) # map total average pref reviews over time total = alt.Chart(rDataFiltered, title=prefTitle + 'Over Time').mark_area( opacity=0.8, color='black').encode( alt.X('year:O', axis=alt.Axis(labelAngle=0), title='Year'), alt.Y(pref, type='quantitative', title=prefTitle + 'Average Preference')).properties(height=150, width=800) st.write(methods2 & total)