def plotly_animate(sim, do_show=False): ''' Plot an animation of each person in the sim ''' z, states = get_individual_states(sim) min_color = min(states, key=lambda x: x['value'])['value'] max_color = max(states, key=lambda x: x['value'])['value'] colorscale = [[x['value'] / max_color, x['color']] for x in states] aspect = 5 y_size = int(np.ceil((z.shape[0] / aspect)**0.5)) x_size = int(np.ceil(aspect * y_size)) z = np.pad(z, ((0, x_size * y_size - z.shape[0]), (0, 0)), mode='constant', constant_values=np.nan) days = sim.tvec fig_dict = {"data": [], "layout": {}, "frames": []} fig_dict["layout"]["updatemenus"] = [{ "buttons": [{ "args": [ None, { "frame": { "duration": 200, "redraw": True }, "fromcurrent": True } ], "label": "Play", "method": "animate" }, { "args": [[None], { "frame": { "duration": 0, "redraw": True }, "mode": "immediate", "transition": { "duration": 0 } }], "label": "Pause", "method": "animate" }], "direction": "left", "pad": { "r": 10, "t": 87 }, "showactive": False, "type": "buttons", "x": 0.1, "xanchor": "right", "y": 0, "yanchor": "top" }] sliders_dict = { "active": 0, "yanchor": "top", "xanchor": "left", "currentvalue": { "font": { "size": 16 }, "prefix": "Day: ", "visible": True, "xanchor": "right" }, "transition": { "duration": 200 }, "pad": { "b": 10, "t": 50 }, "len": 0.9, "x": 0.1, "y": 0, "steps": [] } # make data fig_dict["data"] = [ go.Heatmap( z=np.reshape(z[:, 0], (y_size, x_size)), zmin=min_color, zmax=max_color, colorscale=colorscale, showscale=False, ) ] for state in states: fig_dict["data"].append( go.Scatter(x=[None], y=[None], mode='markers', marker=dict(size=10, color=state['color']), showlegend=True, name=state['name'])) # make frames for i, day in enumerate(days): frame = { "data": [go.Heatmap(z=np.reshape(z[:, i], (y_size, x_size)))], "name": i } fig_dict["frames"].append(frame) slider_step = { "args": [[i], { "frame": { "duration": 5, "redraw": True }, "mode": "immediate", }], "label": i, "method": "animate" } sliders_dict["steps"].append(slider_step) fig_dict["layout"]["sliders"] = [sliders_dict] fig = go.Figure(fig_dict) fig.update_layout( autosize=True, xaxis=dict( showgrid=False, showline=False, showticklabels=False, ), yaxis=dict( automargin=True, showgrid=False, showline=False, showticklabels=False, ), ) fig.update_layout(title={'text': 'Epidemic over time'}, **plotly_legend) if do_show: fig.show() return fig
def visualization_correlation_matrix(df): fg = go.Figure(data=go.Heatmap(z=df.values, x=df.keys(), y=df.keys())) fg.show()
plt.show() # In[ ]: plt.imshow(X_train.corr()) plt.colorbar() plt.show() colorscale = [[0.0, "rgb(240, 0, 0)"], [0.3, "rgb(240, 240, 239)"], [1.0, 'rgb(240, 240, 240)']] fig = go.Figure() fig.add_trace( go.Heatmap(z=X_train.corr(), x=X_train.columns, y=X_train.columns, colorscale=colorscale)) fig.update_layout(width=700, height=700) fig.show() # In[ ]: def remove_high_correlation(df, threshold): dataset = df.copy() remove_columns = [] col_corr = set() # Set of all the names of deleted columns corr_matrix = dataset.corr()
go.Heatmap( z=normalized_lang_lists, x=day_index, y=[ "Arab", "Turkish", "English", "Greek", "Armenian", #"Serbian", "Georgian", "Persian", "Spanish", "Slovenian", "German", "Romanian", "Hungarian", "Czechoslovak", "Dutch", "Italian", "Russian", "Chinese", "Polish", #"Punjabi", "Hebrew", "Sorani Kurdish", #"Pashto", "Vietnamese", "French", #"Urdu", "Portuguese", "Finnish", "Norwegian", "Danish", #"Sindhi", "Japanese", "Catalan", "EU", "Hindi", "Swedish", "Ukrainian", #"Korean", "Latvian", "Icelandic", "Lithuanian", "Indonesian", "Welsh", "Estonian", "Haitian", "Filipinine", "Bulgarian", #"Thai", "Malayalam","Uyghur","Bengali", "Marathi", "Undefined" ], dy=0.1, hoverongaps=False, colorscale='YlOrRd')
def plotly_heatmap( df, normed_by_cols=False, transposed=False, clustered=False, add_dendrogram=False, name="", x_tick_colors=None, height=None, width=None, correlation=False, call_show=False, verbose=False, ): max_is_not_zero = df.max(axis=1) != 0 non_zero_labels = max_is_not_zero[max_is_not_zero].index df = df.loc[non_zero_labels] plot_type = "Heatmap" colorscale = "Bluered" plot_attributes = [] if normed_by_cols: df = df.divide(df.max()).fillna(0) plot_attributes.append("normalized") if transposed: df = df.T if correlation: plot_type = "Correlation" df = df.corr() colorscale = [ [0.0, "rgb(165,0,38)"], [0.1111111111111111, "rgb(215,48,39)"], [0.2222222222222222, "rgb(244,109,67)"], [0.3333333333333333, "rgb(253,174,97)"], [0.4444444444444444, "rgb(254,224,144)"], [0.5555555555555556, "rgb(224,243,248)"], [0.6666666666666666, "rgb(171,217,233)"], [0.7777777777777778, "rgb(116,173,209)"], [0.8888888888888888, "rgb(69,117,180)"], [1.0, "rgb(49,54,149)"], ] else: plot_type = "Heatmap" if clustered: dendro_side = ff.create_dendrogram( df, orientation="right", labels=df.index.to_list(), color_threshold=0, colorscale=["black"] * 8, ) dendro_leaves = dendro_side["layout"]["yaxis"]["ticktext"] df = df.loc[dendro_leaves, :] if correlation: df = df[df.index] x = df.columns if clustered: y = dendro_leaves else: y = df.index.to_list() z = df.values heatmap = go.Heatmap(x=x, y=y, z=z, colorscale=colorscale) if name == "": title = "" else: title = f'{plot_type} of {",".join(plot_attributes)} {name}' # Figure without side-dendrogram if (not add_dendrogram) or (not clustered): fig = go.Figure(heatmap) fig.update_layout( {"title_x": 0.5}, title={"text": title}, yaxis={ "title": "", "tickmode": "array", "automargin": True }, ) fig.update_layout({ "height": height, "width": width, "hovermode": "closest" }) else: # Figure with side-dendrogram fig = go.Figure() for i in range(len(dendro_side["data"])): dendro_side["data"][i]["xaxis"] = "x2" for data in dendro_side["data"]: fig.add_trace(data) y_labels = heatmap["y"] heatmap["y"] = dendro_side["layout"]["yaxis"]["tickvals"] fig.add_trace(heatmap) fig.update_layout( { "height": height, "width": width, "showlegend": False, "hovermode": "closest", "paper_bgcolor": "white", "plot_bgcolor": "white", "title_x": 0.5, }, title={"text": title}, # X-axis of main figure xaxis={ "domain": [0.11, 1], "mirror": False, "showgrid": False, "showline": False, "zeroline": False, "showticklabels": True, "ticks": "", }, # X-axis of side-dendrogram xaxis2={ "domain": [0, 0.1], "mirror": False, "showgrid": True, "showline": False, "zeroline": False, "showticklabels": False, "ticks": "", }, # Y-axis of main figure yaxis={ "domain": [0, 1], "mirror": False, "showgrid": False, "showline": False, "zeroline": False, "showticklabels": False, }, ) fig["layout"]["yaxis"]["ticktext"] = np.asarray(y_labels) fig["layout"]["yaxis"]["tickvals"] = np.asarray( dendro_side["layout"]["yaxis"]["tickvals"]) fig.update_layout( # margin=dict( l=50, r=10, b=200, t=50, pad=0 ), autosize=True, hovermode="closest", ) fig.update_yaxes(automargin=True) fig.update_xaxes(automargin=True) if call_show: fig.show(config={"displaylogo": False}) else: return fig
}) st.markdown("### Dataframe representing overall recommendations") st.dataframe(count) #heatmap to show the correlation between age,bmi,height and weight st.sidebar.markdown("### correlation and heatmaps") if not st.sidebar.checkbox("Hide", True, key=8): st.markdown( "### Heatmap to show the correlation between age,bmi,height and weight" ) bmi_correl = data[['Height', 'Weight', 'BMI', 'Age in yrs']].corr(method='pearson') xdf = ['Height', 'Weight', 'BMI', 'Age in yrs'] ydf = ['Age in yrs', 'BMI', 'Weight', 'Height'] zdf = np.array(bmi_correl) trace = go.Heatmap(x=xdf, y=ydf, z=zdf, type='heatmap', colorscale='GnBu') data1 = [trace] fig = go.Figure(data=data1) st.plotly_chart(fig) #Pairplots to show the correlation between age,bmi,height,temperature,Pulse and weight based on Sex st.sidebar.markdown("### Correlation and Pairplots ") if not st.sidebar.checkbox("Hide", True, key=9): st.markdown( "### Pairplots to show the correlation between age,bmi,Pulse,temperature,height and weight based on Sex" ) fig = px.scatter_matrix(data, dimensions=[ 'LEP0', 'LEP1', 'REP0', 'REP1', 'LEPG0', 'LEPG1', 'REPG0', 'REPG1' ],
'COGS 180', 'COGS 163', 'COGS 155', 'COGS 172', 'COGS 154', 'COGS 176', 'COGS 107A', 'COGS 170', 'COGS 189', 'COGS 157' ] } big_dict['Digital Circuits'] = { 'ECE': ['ECE 111', 'ECE 108', 'ECE 165', 'ECE 25'], 'MATH': [], 'CSE': [], 'COGS': [] } for subj in big_dict.keys(): for key in big_dict[subj].keys(): big_dict[subj][key] = len(big_dict[subj][key]) x = list(big_dict.keys()) z = [list(big_dict[keys].items()) for keys in big_dict.keys()] for i in range(len(z)): z[i] = [el[1] for el in z[i]] z[i] = [el / sum(z[i]) for el in z[i]] z = np.asarray(z) z = np.transpose(z) fig = go.Figure( data=go.Heatmap(z=z, x=x, y=['ECE', 'MATH', 'CSE', 'COGS'], hoverongaps=False, colorscale='blues'), layout_title_text="Percent Representation of Subjects per Department") fig.show()
font={'color': 'black'})) preventionFig.update_layout(paper_bgcolor='#2b3e50', margin=dict(l=0, r=0, t=35, b=0), font=dict(color='white'), legend=dict(x=0, y=1, bgcolor='rgba(0,0,0,0)', font={'color': 'black'})) heatmapFig = go.Figure( go.Heatmap( z=coronaDf2['Infection/Death/Recovery'], x=coronaDf2['Timestamp'], y=coronaDf2['Province'], xgap=3, # this ygap=3, # and this is used to make the grid-like apperance, colorscale="Reds", hovertemplate= 'Data: %{x}<br>Województwo: %{y}<br>Potwierdzenia: %{z}<extra></extra>' )) heatmapFig.update_layout( paper_bgcolor='#2b3e50', margin=dict(l=0, r=0, t=35, b=0), font=dict(color='white'), ) def destinationsMapPlot(df): coronaDf = df.reset_index()
def heatmap_tab_content(active_tab, value): if active_tab == 'heatmapInfectionTab': coronaDfgrouped = coronaDfcaseI.groupby([ 'Timestamp', 'Province', ]).count().reset_index().sort_values(by=['Province'], ascending=False) if value == 1: heatMapData = coronaDfgrouped elif value == 2: heatMapData = coronaDfGeneral[[ 'Timestamp', 'Province', 'Confirmed' ]].sort_values(by=['Province'], ascending=False) heatMapData.columns = [ 'Timestamp', 'Province', 'Infection/Death/Recovery' ] elif active_tab == 'heatmapDeathTab': coronaDfgrouped = coronaDfcaseD.groupby([ 'Timestamp', 'Province', ]).count().reset_index().sort_values(by=['Province'], ascending=False) if value == 1: heatMapData = coronaDfgrouped elif value == 2: heatMapData = coronaDfGeneral[['Timestamp', 'Province', 'Death' ]].sort_values(by=['Province'], ascending=False) heatMapData.columns = [ 'Timestamp', 'Province', 'Infection/Death/Recovery' ] elif active_tab == 'heatmapRecoveredTab': coronaDfgrouped = coronaDfcaseR.groupby([ 'Timestamp', 'Province', ]).count().reset_index().sort_values(by=['Province'], ascending=False) if value == 1: heatMapData = coronaDfgrouped elif value == 2: heatMapData = coronaDfGeneral[[ 'Timestamp', 'Province', 'Recovered' ]].sort_values(by=['Province'], ascending=False) heatMapData.columns = [ 'Timestamp', 'Province', 'Infection/Death/Recovery' ] elif active_tab == 'heatmapActiveTab': coronaDfgrouped = coronaDfcaseI.groupby([ 'Timestamp', 'Province', ]).count().reset_index().sort_values(by=['Province'], ascending=False) if value == 1: heatMapData = coronaDfgrouped elif value == 2: heatMapData = coronaDfGeneral[['Timestamp', 'Province', 'Active' ]].sort_values(by=['Province'], ascending=False) heatMapData.columns = [ 'Timestamp', 'Province', 'Infection/Death/Recovery' ] heatmapFig = go.Figure() heatmap1 = go.Heatmap( z=heatMapData['Infection/Death/Recovery'], x=heatMapData['Timestamp'], y=heatMapData['Province'], xgap=3, # this ygap=3, # and this is used to make the grid-like apperance, colorscale="Reds", hovertemplate= 'Data: %{x}<br>Województwo: %{y}<br>Potwierdzenia: %{z}<extra></extra>' ) heatmapFig.add_trace(heatmap1) heatmapFig.update_layout( paper_bgcolor='#2b3e50', margin=dict(l=0, r=0, t=35, b=0), font=dict(color='white'), ) if active_tab == 'heatmapInfectionTab': heatmapFig.update_traces( colorscale='Reds', hovertemplate= 'Data: %{x}<br>Województwo: %{y}<br>Potwierdzenia: %{z}<extra></extra>' ) elif active_tab == 'heatmapDeathTab': heatmapFig.update_traces( colorscale='Greys', hovertemplate= 'Data: %{x}<br>Województwo: %{y}<br>Zgony: %{z}<extra></extra>') elif active_tab == 'heatmapRecoveredTab': heatmapFig.update_traces( colorscale='Greens', hovertemplate= 'Data: %{x}<br>Województwo: %{y}<br>Wyleczeni: %{z}<extra></extra>' ) return dcc.Graph(id='heatmapFigId', figure=heatmapFig)
def figure(self, computed_traces=None): """Return a figure object compatible with plotly.graph_objects. Parameters: - computed_traces (dict; optional): The dendrogram traces from another (precomputed) Clustergram component. """ dt, heatmap = None, None if computed_traces is None: ( dt, self._data, self._row_labels, self._column_labels, ) = self._compute_clustered_data() else: # use, if available, the precomputed dendrogram and heatmap # traces (as well as the row and column labels) dt = computed_traces["dendro_traces"] heatmap = computed_traces["heatmap"] self._row_labels = computed_traces["row_labels"] self._column_labels = computed_traces["column_labels"] # this dictionary relates curve numbers (accessible from the # hoverData/clickData props) to cluster numbers cluster_curve_numbers = {} # initialize plot; GM is for group markers # [empty] [col. dendro] [col. dendro] [empty] # [row dendro] [heatmap] [heatmap] [row GM] # [row dendro] [heatmap] [heatmap] [row GM] # [empty] [col. GM] [col. GM] [empty] fig = tools.make_subplots( rows=4, cols=4, specs=[ [{}, { "colspan": 2 }, None, {}], [{ "rowspan": 2 }, { "colspan": 2, "rowspan": 2 }, None, { "rowspan": 2 }], [None, None, None, None], [{}, { "colspan": 2 }, None, {}], ], vertical_spacing=0, horizontal_spacing=0, print_grid=False, ) fig["layout"].update(hovermode="closest") # get the tick values; these will be at the leaves of the # dendrogram tickvals_col = [] tickvals_row = [] # for column dendrogram, leaves are at bottom (y=0) for i in range(len(dt["col"])): xs = dt["col"][i]["x"] ys = dt["col"][i]["y"] # during serialization (e.g., in a dcc.Store, the NaN # values become None and the arrays get turned into lists; # they must be converted back if isinstance(xs, list): xs = np.array(xs, dtype=np.float) dt["col"][i].update(x=xs) if isinstance(ys, list): ys = np.array(ys, dtype=np.float) dt["col"][i].update(y=ys) tickvals_col += [ xs.flatten()[j] for j in range(len(xs.flatten())) if ys.flatten()[j] == 0.0 and xs.flatten()[j] % 10 == 5 ] tickvals_col = list(set(tickvals_col)) # for row dendrogram, leaves are at right(x=0, since we # horizontally flipped it) for i in range(len(dt["row"])): xs = dt["row"][i]["x"] ys = dt["row"][i]["y"] if isinstance(xs, list): xs = np.array(xs, dtype=np.float) dt["row"][i].update(x=xs) if isinstance(ys, list): ys = np.array(ys, dtype=np.float) dt["row"][i].update(y=ys) tickvals_row += [ ys.flatten()[j] for j in range(len(ys.flatten())) if xs.flatten()[j] == 0.0 and ys.flatten()[j] % 10 == 5 ] tickvals_row = list(set(tickvals_row)) # sort so they are in the right order (lowest to highest) tickvals_col.sort() tickvals_row.sort() # update axis settings for dendrograms and heatmap axes = [ "xaxis1", "xaxis2", "xaxis4", "xaxis5", "yaxis1", "yaxis2", "yaxis4", "yaxis5", ] for a in axes: fig["layout"][a].update( type="linear", showline=False, showgrid=False, zeroline=False, mirror=False, fixedrange=False, showticklabels=False, ) (row_dendro_traces, col_dendro_traces) = self._sort_traces(dt["row"], dt["col"]) for i in range(len(col_dendro_traces)): cdt = col_dendro_traces[i] cdt["name"] = "Col Cluster %d" % i cdt["line"] = dict(width=self._line_width[1]) cdt["hoverinfo"] = "y+name" cluster_curve_numbers[len(fig.data)] = ["col", i] fig.append_trace(cdt, 1, 2) # row dendrogram (displays on left side) for i in range(len(row_dendro_traces)): rdt = row_dendro_traces[i] rdt["name"] = "Row Cluster %d" % i rdt["line"] = dict(width=self._line_width[0]) rdt["hoverinfo"] = "x+name" cluster_curve_numbers[len(fig.data)] = ["row", i] fig.append_trace(rdt, 2, 1) # display row dendrogram sideways xaxis4 = fig["layout"]["xaxis4"] # pylint: disable=invalid-sequence-index xaxis4.update(autorange="reversed") # ensure that everything is aligned properly # with the heatmap yaxis4 = fig["layout"]["yaxis4"] # pylint: disable=invalid-sequence-index yaxis4.update(scaleanchor="y5") xaxis2 = fig["layout"]["xaxis2"] # pylint: disable=invalid-sequence-index xaxis2.update(scaleanchor="x5") if len(tickvals_col) == 0: tickvals_col = [ 10 * i + 5 for i in range(len(self._column_labels)) ] # add in all of the labels fig["layout"]["xaxis5"].update( # pylint: disable=invalid-sequence-index tickmode="array", tickvals=tickvals_col, ticktext=self._column_labels, tickfont=self._tick_font, showticklabels=True, side="bottom", showline=False, range=[min(tickvals_col) - 5, max(tickvals_col) + 5] # workaround for autoscale issues above; otherwise # the graph cuts off and must be scaled manually ) if len(tickvals_row) == 0: tickvals_row = [10 * i + 5 for i in range(len(self._row_labels))] fig["layout"]["yaxis5"].update( # pylint: disable=invalid-sequence-index tickmode="array", tickvals=tickvals_row, ticktext=self._row_labels, tickfont=self._tick_font, showticklabels=True, side="right", showline=False, range=[min(tickvals_row), max(tickvals_row)], ) # hide labels, if necessary for l in self._hidden_labels: fig["layout"][l].update(ticks="", showticklabels=False) # recalculate the heatmap, if necessary if heatmap is None: # heatmap heat_data = self._data # symmetrize the heatmap about zero, if necessary if self._center_values: heat_data = np.subtract(heat_data, np.mean(heat_data)) heatmap = go.Heatmap( x=tickvals_col, y=tickvals_row, z=heat_data, colorscale=self._color_map, colorbar={"xpad": 50}, ) fig.append_trace(heatmap, 2, 2) # hide all legends fig["layout"].update(showlegend=False, ) # apply the display ratio row_ratio = 0 col_ratio = 0 # the argument can be either in list form or float form # first is ratio for row; second is ratio for column if self._display_ratio[0] != 0: row_ratio = 0.95 / float(1 + int(1 / self._display_ratio[0])) if self._display_ratio[1] != 0: col_ratio = 0.95 / float(1 + int(1 / self._display_ratio[1])) # the row/column labels take up 0.05 of the graph, and the rest # is taken up by the heatmap and dendrogram for each dimension # row: dendrogram, heatmap, row labels (left-to-right) # column: dendrogram, column labels, heatmap (top-to-bottom) # width adjustment for row dendrogram fig["layout"]["xaxis1"].update( # pylint: disable=invalid-sequence-index domain=[0, 0.95]) fig["layout"]["xaxis2"].update( # pylint: disable=invalid-sequence-index domain=[row_ratio, 0.95], anchor="y4") fig["layout"]["xaxis4"].update( # pylint: disable=invalid-sequence-index domain=[0, row_ratio]) fig["layout"]["xaxis5"].update( # pylint: disable=invalid-sequence-index domain=[row_ratio, 0.95]) # height adjustment for column dendrogram fig["layout"]["yaxis1"].update( # pylint: disable=invalid-sequence-index domain=[1 - col_ratio, 1]) fig["layout"]["yaxis2"].update( # pylint: disable=invalid-sequence-index domain=[1 - col_ratio, 1]) fig["layout"]["yaxis4"].update( # pylint: disable=invalid-sequence-index domain=[0, 0.95 - col_ratio]) fig["layout"]["yaxis5"].update( # pylint: disable=invalid-sequence-index domain=[0, 0.95 - col_ratio]) fig["layout"]["legend"] = dict( # pylint: disable=unsupported-assignment-operation x=0.7, y=0.7) # annotations # axis settings for subplots that will display group labels axes = ["xaxis6", "yaxis6", "xaxis8", "yaxis8"] for a in axes: fig["layout"][a].update( type="linear", showline=False, showgrid=False, zeroline=False, mirror=False, fixedrange=False, showticklabels=False, ) # group labels for row dendrogram fig["layout"]["yaxis6"].update( # pylint: disable=invalid-sequence-index domain=[0, 0.95 - col_ratio], scaleanchor="y5", scaleratio=1) if len(tickvals_row) > 0: fig["layout"]["yaxis6"].update( # pylint: disable=invalid-sequence-index range=[min(tickvals_row), max(tickvals_row)]) # padding between group label line and dendrogram fig["layout"]["xaxis6"].update( # pylint: disable=invalid-sequence-index domain=[0.95, 1], range=[-5, 1]) # group labels for column dendrogram fig["layout"]["xaxis8"].update( # pylint: disable=invalid-sequence-index domain=[row_ratio, 0.95], scaleanchor="x5", scaleratio=1) if len(tickvals_col) > 0: fig["layout"]["xaxis8"].update( # pylint: disable=invalid-sequence-index range=[min(tickvals_col), max(tickvals_col)]) fig["layout"]["yaxis8"].update( # pylint: disable=invalid-sequence-index domain=[0.95 - col_ratio, 1 - col_ratio], range=[-0.5, 0.5]) # get group label annotations and label traces ( row_group_labels, col_group_labels, row_annotations, col_annotations, ) = self._group_label_traces(row_dendro_traces, col_dendro_traces) # add annotations to graph fig["layout"].update(annotations=row_annotations + col_annotations) # add label traces to graph for rgl in row_group_labels: fig.append_trace(rgl, 2, 4) for cgl in col_group_labels: fig.append_trace(cgl, 4, 2) # set background colors fig["layout"].update(paper_bgcolor=self._paper_bg_color, plot_bgcolor=self._plot_bg_color) # finally add height and width fig["layout"].update(height=self._height, width=self._width) computed_traces = { "dendro_traces": dt, "heatmap": heatmap, "row_labels": self._row_labels, "column_labels": self._column_labels, } return (fig, computed_traces, cluster_curve_numbers)
d_bins) hist = np.ones((d_bins - 1, w_bins - 1)) for i in range(d_bins - 1): for k in range(w_bins - 1): sub_inds = np.logical_and( np.logical_and(w_steps[k] <= df["WORKING"], df["WORKING"] < w_steps[k + 1]), np.logical_and(d_steps[i] <= df["DEATH"], df["DEATH"] < d_steps[i + 1])) if np.any(sub_inds): hist[i, k] = np.mean(df.iloc[df.index[sub_inds]]["DURATION"].values) else: hist[i, k] = np.nan import plotly.graph_objects as go fig = go.Figure( data=go.Heatmap(z=hist, x=w_steps, y=d_steps, hoverongaps=False)) #fig = px.imshow(hist #labels=dict(x="Log(Work days lost)", y="Death rate", color="Minimal sick peak [%]"), #x=w_steps, #y=d_steps # ) #fig = px.scatter(x=df["WORKING"], y=df["DEATH"], color= target) #df["SICK_PEAK"]) #df["KNOW_RATE_SICK"]*df["SD_IMPACT"]) #color=df["SD_IMPACT"]*df["KNOW_RATE_SICK"]) #fig = px.scatter_3d(df.iloc[df.index[target]], x="WORKING", y="DEATH", z="SICK_PEAK") #, color=target) #df["SICK_PEAK"]) #df["KNOW_RATE_SICK"]*df["SD_IMPACT"]) #color=df["SD_IMPACT"]*df["KNOW_RATE_SICK"]) fig.show()
row_idx = -int(row[0]) column[row_idx] = row[1]/len(data['i_rssi']) rssi_hist2d.append(column) column = np.zeros(100) hist = np.array(np.unique(Dist, return_counts=True)).T for row in hist: row_idx = int(np.around(row[0]/.1)) column[row_idx] = row[1]/len(Dist) dist_hist2d.append(column) rssi_hist2d = np.array(rssi_hist2d).T dist_hist2d = np.array(dist_hist2d).T maxz = np.max([np.max(rssi_hist2d), np.max(dist_hist2d)]) fig.add_trace(go.Heatmap( x=np.arange(0, 765, 45), y=np.arange(db_lim[0], db_lim[1], -1), z=rssi_hist2d[int(-db_lim[0]):int(-db_lim[1]), :], zmin=0, zmax=maxz), row=1, col=1) fig.add_trace(go.Heatmap( x=np.arange(0, 765, 45), y=np.arange(dist_lim[0], dist_lim[1], .1), z=dist_hist2d[int(dist_lim[0]/.1):int(dist_lim[1]/.1), :], zmin=0, zmax=maxz), row=2, col=1) fig.add_trace(go.Scatter(x=np.arange(0, 765, 45), y=np.array([1]*16), mode='lines', line=ref_line), row=2, col=1) fig.update_layout(title={'text': 'DA14695 Evaluation Board, %s antenna'%(antenna), 'xanchor': 'center', 'yanchor': 'top', 'y': .95, 'x': .5}) fig.update_xaxes(title='Angle (°)', row=2, col=1) fig.update_layout(showlegend=False) fig.update_yaxes(title_text='Initiator RSSI (dBm)', row=1, col=1) fig.update_yaxes(title_text='Calculated distance (m)', row=2, col=1) fig.write_image(os.path.join(output_directory, 'orientation_exp1_%s.png'%(antenna)))
k = 2 # Number of infected m = 20 # Number of group tests xpure = np.zeros([n, 1]) xpure[0:k] = 1 np.random.shuffle(xpure) x = xpure + 0.05 * np.random.randn(n).reshape(-1, 1) xhat1 = Algorithm(x, n, k, m).COMP() xhat2 = Algorithm(x, n, k, m).DD() xhat3 = Algorithm(x, n, k, m).CBP() xhat4 = Algorithm(x, n, k, m).SCOMP() xhat5 = SR(x, n, k, m).xhat() fig = make_subplots(rows=1, cols=6, subplot_titles=("True", "COMP", "DD", "CBP", "SCOMP", "SR"), shared_yaxes=True) for num, pred in enumerate([x, xhat1, xhat2, xhat3, xhat4, xhat5]): fig.add_trace(go.Heatmap(z=pred), row=1, col=num + 1) fig.update_traces(dict(showscale=False)) fig.update_layout(autosize=False, width=800, height=400, margin=dict(l=50, r=50, b=20, t=30)) fig.update_xaxes(showticklabels=False) fig.show()
def plot_signal(idx, data): if len(idx) == 0: raise PreventUpdate figs = make_subplots(rows=2, cols=1, subplot_titles=('Waveform', 'Spectrogram')) try: filename = data[idx[0]]['audio_filepath'] audio, fs = librosa.load(path=filename, sr=None) if 'offset' in data[idx[0]]: audio = audio[int(data[idx[0]]['offset'] * fs):int((data[idx[0]]['offset'] + data[idx[0]]['duration']) * fs)] time_stride = 0.01 hop_length = int(fs * time_stride) n_fft = 512 # linear scale spectrogram s = librosa.stft(y=audio, n_fft=n_fft, hop_length=hop_length) s_db = librosa.power_to_db(S=np.abs(s)**2, ref=np.max, top_db=100) figs.add_trace( go.Scatter( x=np.arange(audio.shape[0]) / fs, y=audio, line={'color': 'green'}, name='Waveform', hovertemplate= 'Time: %{x:.2f} s<br>Amplitude: %{y:.2f}<br><extra></extra>', ), row=1, col=1, ) figs.add_trace( go.Heatmap( z=s_db, colorscale=[ [0, 'rgb(30,62,62)'], [0.5, 'rgb(30,128,128)'], [1, 'rgb(30,255,30)'], ], colorbar=dict(yanchor='middle', lenmode='fraction', y=0.2, len=0.5, ticksuffix=' dB'), dx=time_stride, dy=fs / n_fft / 1000, name='Spectrogram', hovertemplate= 'Time: %{x:.2f} s<br>Frequency: %{y:.2f} kHz<br>Magnitude: %{z:.2f} dB<extra></extra>', ), row=2, col=1, ) figs.update_layout({ 'margin': dict(l=0, r=0, t=20, b=0, pad=0), 'height': 500 }) figs.update_xaxes(title_text='Time, s', row=1, col=1) figs.update_yaxes(title_text='Amplitude', row=1, col=1) figs.update_xaxes(title_text='Time, s', row=2, col=1) figs.update_yaxes(title_text='Frequency, kHz', row=2, col=1) except Exception: pass return figs
def tax_correlation_plot(taxonomy_pk, countmatrix_pk, samples=None, level=3, relative=True): linnean_levels = { y: x for x, y in enumerate([ "kingdom", "phylum", "class", "order", "family", "genus", "species" ]) } if level in linnean_levels: level = linnean_levels[level] elif type(level) == str: level = int(level) tax_result = Result.objects.get(pk=taxonomy_pk) count_matrix_result = Result.objects.get(pk=countmatrix_pk) matrix = count_matrix_result.values.instance_of( Matrix).first().data.get().get_value() if relative: matrix = matrix / matrix.sum(axis=0) else: matrix = matrix.todense() sample_pks = count_matrix_result.samples.order_by("pk") sample_names = count_matrix_result.samples.order_by("pk") if samples: sample_pks = sample_pks.filter(pk__in=samples) sample_names = sample_names.filter(pk__in=samples) sample_pks = list(sample_pks.values_list("pk", flat=True)) sample_names = list(sample_names.values_list("name", flat=True)) tax_df = tax_result.dataframe(value_names=["taxonomic_classification"], additional_fields=["features__pk"]) def format_taxonomy(x): if len(x) <= level: tax_index = len(x) - 1 else: tax_index = level while x[tax_index].endswith("__"): tax_index -= 1 if tax_index > 0: return "; ".join([x[tax_index - 1], x[tax_index]]) else: return x[tax_index] tax_df["value_data"] = tax_df["value_data"].str.split("; ").apply( format_taxonomy) tax_merge = tax_df.groupby("value_data").apply( lambda x: x['features__pk'].unique()) sample_df = pd.DataFrame({'sample': sample_names}) for tax, merge in tax_merge.items(): y = matrix[merge][:, sample_pks].sum(axis=0).tolist()[0], sample_df[tax] = y[0] #retrieve sample values. Find pearson correlation of sample vals to taxa. plot heatmap sqs = Sample.objects.filter(name__in=sample_names) val_names = list( set([ v.signature.get().name for v in Value.objects.non_polymorphic().filter(samples__in=sqs) ])) val_index = {name: i for i, name in enumerate(val_names)} data = [] for s in sqs: s_vals = [np.nan] * len(val_names) for val in s.values.non_polymorphic().filter( signature__name__in=val_names): s_vals[val_index[val.signature.get().name]] = val.data.get().value data.append([s.name] + s_vals) df = pd.DataFrame(data=data, columns=["sample"] + val_names) no_file_info = [ col for col in df.columns if not "file" in col and not "table" in col ] print(no_file_info) df = df[no_file_info] df = df.set_index('sample') joined = sample_df.join(df, on='sample') pearson = joined.corr().filter(no_file_info) fig = go.Figure(data=go.Heatmap(z=pearson.values, x=pearson.columns, y=list(pearson.index), hoverongaps=False)) return plt.plot(fig, output_type="div")
def main(_args: argparse.Namespace): DATA_PATH = pathlib.Path("data/isis_twitter_data.csv") if not DATA_PATH.is_file(): raise FileNotFoundError( f"CSV dataset not found from the path {DATA_PATH}. Please, download and convert it." ) data = read_csv_into_obj(DATA_PATH) users = group_users_by_tweet_meta(data) # Sort by tweets, get first 10 users users_sorted = sort_users(users) # users_sorted_10 = get_slice(users_sorted) # assert 10 == len(users_sorted_10.keys()) if args.user_hashtags: user_h = users_sorted.get(args.user_hashtags) rankings = {} for tweet in user_h.get("tweets"): hashtags = tweet.get_hashtags() for h in hashtags: if not rankings.get(h): rankings[h] = 1 else: rankings[h] += 1 # Top 10 hashtags by selected user sorted_ranks = { k: v for (k, v) in sorted( rankings.items(), key=lambda x: x[1], reverse=True)[:10] } from pprint import pprint pprint(sorted_ranks, sort_dicts=False) # Plot all users, by amount of tweets if args.all and args.plot_tweets: plot_users( users_sorted, "amount_tweets", "User number", "Amount tweets", "Amount of tweets per user, sorted as descending", ) # Plot all users, by amount of tweets, in logarithm scale plot_users( users_sorted, "amount_tweets", "Tweet Frequency", "p(X)", # "Amount of tweets fitted into CCDF, compared to different distribution candidates", plot_type="log", show=args.show) users_sorted_10 = get_slice(users_sorted) if not args.all and args.plot_tweets: plot_users(users_sorted_10, "amount_tweets", "User number", "Amount tweets", "Amount of tweets per user, sorted as descending", use_name=True, show=args.show) assert 10 == len(users_sorted_10.keys()) for sort_by, desc in zip( list(USER_META.keys())[1:], [ "Amount retweets", "Amount use of mentions", "Amount use of hashtags" ], ): users_sorted = sort_users(users, by_meta_value=sort_by) users_sorted_10 = get_slice(users_sorted) plot_users(users_sorted_10, sort_by, "Username", desc, f"{desc} per user, sorted as descending", use_name=True, show=args.show) if args.sentiment: GLOBAL_LOGGER.info("Plotting sentiments of all tweets...") if args.sentiment and args.all: plot_sentiments_with_ternary(data, args.show) elif args.sentiment: for k in list(USER_META.keys())[1:4]: users_combined_10 = [] users_sorted_10 = get_slice(sort_users(users, k)) GLOBAL_LOGGER.info(f"Plotting ternary by {k}") for j in users_sorted_10.keys(): users_combined_10 += users_sorted_10.get(j).get("tweets") plot_sentiments_with_ternary(users_combined_10, args.show, k) # Reduce network node amount by hashtag occurrences in tweets if args.k and args.create_network: # Collect k and degree centrality value pairs # plotted as heatmap later k_degree = [] _table = """\\begin{table*}[ht] \\begin{tabular} { | c | c | c | c | c | c | c | c | c | } \\hline k & Nodes \\# & Edges \\# & Largest component & Diameter & Avg. Clustering Coef. & Avg. Degree & Avg. Closeness & Avg. Betweenness \\\\ \\hline""" for _k in args.k: G = create_network(data, int(_k)) nodes, edges, components, diameter, average_c_coeff, deg_mean, average_closeness_cent, average_betweenness_cent = get_graph_info( G, _k) k_degree.append((_k, [v for v in G.degree()])) tab_row = f"\t\t{_k} & {nodes} & {edges} & {components[0]} & {diameter} & " \ f"{average_c_coeff} & {deg_mean} & {average_closeness_cent} & {average_betweenness_cent} \\\\" _table += "\n" _table += tab_row + "\n\t\t\\hline" _table += """\n \\end{tabular} \\caption {Core measurements of hashtag graph by minimal hashtag pair occurrences (k)} \\label {tab:graphstats-by-k} \\end{table*}""" GLOBAL_LOGGER.info("Following LaTeX table autogenerated:") print(_table) # Example command # python -m analyse -k 1 2 3 4 5 10 15 --show -lDEBUG if args.heatmap: GLOBAL_LOGGER.info( "Plotting heatmap of degree centrality based on k values") # degrees = [sorted(map(lambda x: x[1], v), reverse=True) for (k, v) in k_degree] degrees = [list(map(lambda x: x[1], v)) for (k, v) in k_degree] # hashtags = [list(map(lambda x: x[0], v)) for (k, v) in k_degree] fig = go.Figure(data=go.Heatmap(z=degrees, zmax=100, zmin=1, y=[k[0] for k in k_degree], colorscale='Inferno')) path = "article/figures/hashtag_heatmap_by_k.png" fig.write_image(path) if args.show: fig.show() elif args.create_network: G = create_network(data) # get_graph_info(G) GLOBAL_LOGGER.debug("All nodes and edges added for hashtag graph") if args.show: Gcc = G.subgraph( sorted(nx.connected_components(G), key=len, reverse=True)[0]) pos = nx.spring_layout(Gcc) nx.draw_networkx_nodes(Gcc, pos, node_size=10) nx.draw_networkx_edges(Gcc, pos, alpha=0.4) plt.show() if args.page_rank: GLOBAL_LOGGER.info("Creating plot for PageRank.") pr = nx.pagerank_numpy(G, alpha=0.9) # Sort rank of every key-value pair pr_rank = sorted(pr.items(), key=lambda x: x[1], reverse=True) values = [k[1] for k in pr_rank] amount = range(1, len(values) + 1) plt.scatter(amount, values) plt.xlabel("Rank of hashtag") plt.ylabel("PageRank value") plt.title("PageRank distribution of hashtag popularity") for index, item in enumerate(pr_rank[:10]): GLOBAL_LOGGER.info(f"Rank: {index + 1}, hashtag: {item[0]}") plt.savefig(f'article/figures/pagerank_distribution.png', bbox_inches='tight') if args.show: plt.show() else: plt.close() if args.lcc: clusters = nx.clustering(G) plt.hist(clusters.values(), bins=10) plt.title("Distribution of LCC") plt.xlabel('Clustering') plt.ylabel('Frequency') plt.savefig(f'article/figures/lcc_distribution.png', bbox_inches='tight') if args.show: plt.show() else: plt.close() if args.girvann: if not args.create_network: GLOBAL_LOGGER.error( "--create-network parameter required with girvan-newmann analysis." ) exit(1) GLOBAL_LOGGER.info(f"Starting Girvan-Newmann analysis.") k = 10 # comp = nx.algorithms.community.centrality.girvan_newman(G) # test = 1 # for communities in islice(comp, k): # data = list(sorted(c) for c in communities) # print(data) # print(len(data)) # with open(f"file{test}.txt", "w") as f: # # f.write(str(data)) # json.dump(data, f) # test += 1 with open("file10.txt") as f: data = json.load(f) data_sorted = sorted(data, key=len, reverse=True) print(data_sorted[4]) for d in data_sorted[:10]: print(len(d))
def iplot_error_map( backend: IBMQBackend, figsize: Tuple[int] = (800, 500), show_title: bool = True, remove_badcal_edges: bool = True, background_color: str = 'white', as_widget: bool = False) -> Union[PlotlyFigure, PlotlyWidget]: """Plot the error map of a device. Args: backend: Plot the error map for this backend. figsize: Figure size in pixels. show_title: Whether to show figure title. remove_badcal_edges: Whether to remove bad CX gate calibration data. background_color: Background color, either 'white' or 'black'. as_widget: ``True`` if the figure is to be returned as a ``PlotlyWidget``. Otherwise the figure is to be returned as a ``PlotlyFigure``. Returns: The error map figure. Raises: VisualizationValueError: If an invalid input is received. VisualizationTypeError: If the specified `backend` is a simulator. Example: .. jupyter-execute:: :hide-code: :hide-output: from qiskit.test.ibmq_mock import mock_get_backend mock_get_backend('FakeVigo') .. jupyter-execute:: from qiskit import IBMQ from qiskit.providers.ibmq.visualization import iplot_error_map IBMQ.load_account() provider = IBMQ.get_provider(group='open', project='main') backend = provider.get_backend('ibmq_vigo') iplot_error_map(backend, as_widget=True) """ meas_text_color = '#000000' if background_color == 'white': color_map = HELIX_LIGHT_CMAP text_color = '#000000' plotly_cmap = HELIX_LIGHT elif background_color == 'black': color_map = HELIX_DARK_CMAP text_color = '#FFFFFF' plotly_cmap = HELIX_DARK else: raise VisualizationValueError( '"{}" is not a valid background_color selection.'.format( background_color)) if backend.configuration().simulator: raise VisualizationTypeError( 'Requires a device backend, not a simulator.') config = backend.configuration() n_qubits = config.n_qubits cmap = config.coupling_map if n_qubits in DEVICE_LAYOUTS.keys(): grid_data = DEVICE_LAYOUTS[n_qubits] else: fig = go.Figure() fig.update_layout(showlegend=False, plot_bgcolor=background_color, paper_bgcolor=background_color, width=figsize[0], height=figsize[1], margin=dict(t=60, l=0, r=0, b=0)) out = PlotlyWidget(fig) return out props = backend.properties().to_dict() t1s = [] t2s = [] for qubit_props in props['qubits']: count = 0 for item in qubit_props: if item['name'] == 'T1': t1s.append(item['value']) count += 1 elif item['name'] == 'T2': t2s.append(item['value']) count += 1 if count == 2: break # U2 error rates single_gate_errors = [0] * n_qubits for gate in props['gates']: if gate['gate'] == 'u2': _qubit = gate['qubits'][0] single_gate_errors[_qubit] = gate['parameters'][0]['value'] # Convert to percent single_gate_errors = 100 * np.asarray(single_gate_errors) avg_1q_err = np.mean(single_gate_errors) max_1q_err = max(single_gate_errors) single_norm = mpl.colors.Normalize(vmin=min(single_gate_errors), vmax=max_1q_err) q_colors = [ mpl.colors.rgb2hex(color_map(single_norm(err))) for err in single_gate_errors ] line_colors = [] cx_idx = [] if n_qubits > 1 and cmap: cx_errors = [] for cmap_qubits in cmap: for gate in props['gates']: if gate['qubits'] == cmap_qubits: cx_errors.append(gate['parameters'][0]['value']) break else: continue # Convert to percent cx_errors = 100 * np.asarray(cx_errors) # remove bad cx edges if remove_badcal_edges: cx_idx = np.where(cx_errors != 100.0)[0] else: cx_idx = np.arange(len(cx_errors)) avg_cx_err = np.mean(cx_errors[cx_idx]) for err in cx_errors: if err != 100.0 or not remove_badcal_edges: cx_norm = mpl.colors.Normalize(vmin=min(cx_errors[cx_idx]), vmax=max(cx_errors[cx_idx])) line_colors.append(mpl.colors.rgb2hex(color_map(cx_norm(err)))) else: line_colors.append("#ff0000") # Measurement errors read_err = [] for qubit in range(n_qubits): for item in props['qubits'][qubit]: if item['name'] == 'readout_error': read_err.append(item['value']) read_err = 100 * np.asarray(read_err) avg_read_err = np.mean(read_err) max_read_err = np.max(read_err) if n_qubits < 10: num_left = n_qubits num_right = 0 else: num_left = math.ceil(n_qubits / 2) num_right = n_qubits - num_left x_max = max([d[1] for d in grid_data]) y_max = max([d[0] for d in grid_data]) max_dim = max(x_max, y_max) qubit_size = 32 font_size = 14 offset = 0 if cmap: if y_max / max_dim < 0.33: qubit_size = 24 font_size = 10 offset = 1 if n_qubits > 5: right_meas_title = "Readout Error (%)" else: right_meas_title = None if cmap and cx_idx.size > 0: cx_title = "CNOT Error Rate [Avg. {}%]".format(np.round(avg_cx_err, 3)) else: cx_title = None fig = make_subplots( rows=2, cols=11, row_heights=[0.95, 0.05], vertical_spacing=0.15, specs=[[{ "colspan": 2 }, None, { "colspan": 6 }, None, None, None, None, None, { "colspan": 2 }, None, None], [{ "colspan": 4 }, None, None, None, None, None, { "colspan": 4 }, None, None, None, None]], subplot_titles=("Readout Error (%)", None, right_meas_title, "Hadamard Error Rate [Avg. {}%]".format( np.round(avg_1q_err, 3)), cx_title)) # Add lines for couplings if cmap and n_qubits > 1 and cx_idx.size > 0: for ind, edge in enumerate(cmap): is_symmetric = False if edge[::-1] in cmap: is_symmetric = True y_start = grid_data[edge[0]][0] + offset x_start = grid_data[edge[0]][1] y_end = grid_data[edge[1]][0] + offset x_end = grid_data[edge[1]][1] if is_symmetric: if y_start == y_end: x_end = (x_end - x_start) / 2 + x_start x_mid = x_end y_mid = y_start elif x_start == x_end: y_end = (y_end - y_start) / 2 + y_start x_mid = x_start y_mid = y_end else: x_end = (x_end - x_start) / 2 + x_start y_end = (y_end - y_start) / 2 + y_start x_mid = x_end y_mid = y_end else: if y_start == y_end: x_mid = (x_end - x_start) / 2 + x_start y_mid = y_end elif x_start == x_end: x_mid = x_end y_mid = (y_end - y_start) / 2 + y_start else: x_mid = (x_end - x_start) / 2 + x_start y_mid = (y_end - y_start) / 2 + y_start fig.append_trace(go.Scatter( x=[x_start, x_mid, x_end], y=[-y_start, -y_mid, -y_end], mode="lines", line=dict(width=6, color=line_colors[ind]), hoverinfo='text', hovertext='CX<sub>err</sub>{B}_{A} = {err} %'.format( A=edge[0], B=edge[1], err=np.round(cx_errors[ind], 3))), row=1, col=3) # Add the qubits themselves qubit_text = [] qubit_str = "<b>Qubit {}</b><br>H<sub>err</sub> = {} %" qubit_str += "<br>T1 = {} \u03BCs<br>T2 = {} \u03BCs" for kk in range(n_qubits): qubit_text.append( qubit_str.format(kk, np.round(single_gate_errors[kk], 3), np.round(t1s[kk], 2), np.round(t2s[kk], 2))) if n_qubits > 20: qubit_size = 23 font_size = 11 if n_qubits > 50: qubit_size = 20 font_size = 9 qtext_color = [] for ii in range(n_qubits): if background_color == 'black': if single_gate_errors[ii] > 0.8 * max_1q_err: qtext_color.append('black') else: qtext_color.append('white') else: qtext_color.append('white') fig.append_trace(go.Scatter(x=[d[1] for d in grid_data], y=[-d[0] - offset for d in grid_data], mode="markers+text", marker=go.scatter.Marker(size=qubit_size, color=q_colors, opacity=1), text=[str(ii) for ii in range(n_qubits)], textposition="middle center", textfont=dict(size=font_size, color=qtext_color), hoverinfo="text", hovertext=qubit_text), row=1, col=3) fig.update_xaxes(row=1, col=3, visible=False) _range = None if offset: _range = [-3.5, 0.5] fig.update_yaxes(row=1, col=3, visible=False, range=_range) # H error rate colorbar min_1q_err = min(single_gate_errors) max_1q_err = max(single_gate_errors) if n_qubits > 1: fig.append_trace(go.Heatmap(z=[ np.linspace(min_1q_err, max_1q_err, 100), np.linspace(min_1q_err, max_1q_err, 100) ], colorscale=plotly_cmap, showscale=False, hoverinfo='none'), row=2, col=1) fig.update_yaxes(row=2, col=1, visible=False) fig.update_xaxes(row=2, col=1, tickvals=[0, 49, 99], ticktext=[ np.round(min_1q_err, 3), np.round( (max_1q_err - min_1q_err) / 2 + min_1q_err, 3), np.round(max_1q_err, 3) ]) # CX error rate colorbar if cmap and n_qubits > 1 and cx_idx.size > 0: min_cx_err = min(cx_errors) max_cx_err = max(cx_errors) if min_cx_err == max_cx_err: min_cx_err = 0 # Force more than 1 color. fig.append_trace(go.Heatmap(z=[ np.linspace(min_cx_err, max_cx_err, 100), np.linspace(min_cx_err, max_cx_err, 100) ], colorscale=plotly_cmap, showscale=False, hoverinfo='none'), row=2, col=7) fig.update_yaxes(row=2, col=7, visible=False) min_cx_idx_err = min(cx_errors[cx_idx]) max_cx_idx_err = max(cx_errors[cx_idx]) fig.update_xaxes(row=2, col=7, tickvals=[0, 49, 99], ticktext=[ np.round(min_cx_idx_err, 3), np.round((max_cx_idx_err - min_cx_idx_err) / 2 + min_cx_idx_err, 3), np.round(max_cx_idx_err, 3) ]) hover_text = "<b>Qubit {}</b><br>M<sub>err</sub> = {} %" # Add the left side meas errors for kk in range(num_left - 1, -1, -1): fig.append_trace(go.Bar( x=[read_err[kk]], y=[kk], orientation='h', marker=dict(color='#eedccb'), hoverinfo="text", hoverlabel=dict(font=dict(color=meas_text_color)), hovertext=[hover_text.format(kk, np.round(read_err[kk], 3))]), row=1, col=1) fig.append_trace(go.Scatter(x=[avg_read_err, avg_read_err], y=[-0.25, num_left - 1 + 0.25], mode='lines', hoverinfo='none', line=dict(color=text_color, width=2, dash='dot')), row=1, col=1) fig.update_yaxes(row=1, col=1, tickvals=list(range(num_left)), autorange="reversed") fig.update_xaxes( row=1, col=1, range=[0, 1.1 * max_read_err], tickvals=[0, np.round(avg_read_err, 2), np.round(max_read_err, 2)], showline=True, linewidth=1, linecolor=text_color, tickcolor=text_color, ticks="outside", showgrid=False, zeroline=False) # Add the right side meas errors, if any if num_right: for kk in range(n_qubits - 1, num_left - 1, -1): fig.append_trace(go.Bar( x=[-read_err[kk]], y=[kk], orientation='h', marker=dict(color='#eedccb'), hoverinfo="text", hoverlabel=dict(font=dict(color=meas_text_color)), hovertext=[hover_text.format(kk, np.round(read_err[kk], 3))]), row=1, col=9) fig.append_trace(go.Scatter(x=[-avg_read_err, -avg_read_err], y=[num_left - 0.25, n_qubits - 1 + 0.25], mode='lines', hoverinfo='none', line=dict(color=text_color, width=2, dash='dot')), row=1, col=9) fig.update_yaxes( row=1, col=9, tickvals=list(range(n_qubits - 1, num_left - 1, -1)), side='right', autorange="reversed", ) fig.update_xaxes( row=1, col=9, range=[-1.1 * max_read_err, 0], tickvals=[ 0, -np.round(avg_read_err, 2), -np.round(max_read_err, 2) ], ticktext=[0, np.round(avg_read_err, 2), np.round(max_read_err, 2)], showline=True, linewidth=1, linecolor=text_color, tickcolor=text_color, ticks="outside", showgrid=False, zeroline=False) # Makes the subplot titles smaller than the 16pt default for ann in fig['layout']['annotations']: ann['font'] = dict(size=13) title_text = "{} Error Map".format(backend.name()) if show_title else '' fig.update_layout(showlegend=False, plot_bgcolor=background_color, paper_bgcolor=background_color, width=figsize[0], height=figsize[1], title=dict(text=title_text, x=0.452), title_font_size=20, font=dict(color=text_color), margin=dict(t=60, l=0, r=40, b=0)) if as_widget: return PlotlyWidget(fig) return PlotlyFigure(fig)
def system_error_map(backend, figsize=(None, None), colormap=None, background_color='white', show_title=True, remove_badcal_edges=True, as_widget=False): """Plot the error map of a device. Args: backend (IBMQBackend or FakeBackend or DeviceSimulator or Properties): Plot the error map for a backend. figsize (tuple, optional): Figure size in pixels. colormap (Colormap): A matplotlib colormap. background_color (str, optional): Background color, either 'white' or 'black'. show_title (bool, optional): Whether to show figure title. remove_badcal_edges (bool, optional): Whether to remove bad CX gate calibration data. as_widget (bool, optional): ``True`` if the figure is to be returned as a ``PlotlyWidget``. Otherwise the figure is to be returned as a ``PlotlyFigure``. Returns: PlotlyFigure or PlotlyWidget: The error map figure. Raises: KaleidoscopeError: Invalid input type. Example: .. jupyter-execute:: from qiskit import * from kaleidoscope.qiskit.backends import system_error_map pro = IBMQ.load_account() backend = pro.backends.ibmq_vigo system_error_map(backend) """ if not isinstance( backend, (IBMQBackend, DeviceSimulator, FakeBackend, BackendProperties)): raise KaleidoscopeError( 'Input is not a valid backend or properties object.') if isinstance(backend, BackendProperties): backend = properties_to_pseudobackend(backend) CMAP = BMW PLOTLY_CMAP = cmap_to_plotly(CMAP) if colormap is not None: CMAP = colormap PLOTLY_CMAP = cmap_to_plotly(CMAP) meas_text_color = '#000000' if background_color == 'white': color_map = CMAP text_color = '#000000' plotly_cmap = PLOTLY_CMAP elif background_color == 'black': color_map = CMAP text_color = '#FFFFFF' plotly_cmap = PLOTLY_CMAP else: raise KaleidoscopeError( '"{}" is not a valid background_color selection.'.format( background_color)) if backend.configuration().simulator and not isinstance( backend, DeviceSimulator): raise KaleidoscopeError('Requires a device backend, not a simulator.') config = backend.configuration() n_qubits = config.n_qubits cmap = config.coupling_map if str(n_qubits) in LAYOUTS['layouts'].keys(): kind = 'generic' if backend.name() in LAYOUTS['special_names']: if LAYOUTS['special_names'][backend.name()] in LAYOUTS['layouts'][ str(n_qubits)]: kind = LAYOUTS['special_names'][backend.name()] grid_data = LAYOUTS['layouts'][str(n_qubits)][kind] else: fig = go.Figure() fig.update_layout(showlegend=False, plot_bgcolor=background_color, paper_bgcolor=background_color, width=figsize[0], height=figsize[1], margin=dict(t=60, l=0, r=0, b=0)) out = PlotlyWidget(fig) return out props = backend.properties() freqs = [0] * n_qubits t1s = [0] * n_qubits t2s = [0] * n_qubits alphas = [0] * n_qubits for idx, qubit_props in enumerate(props.qubits): for item in qubit_props: if item.name == 'frequency': freqs[idx] = item.value elif item.name == 'T1': t1s[idx] = item.value elif item.name == 'T2': t2s[idx] = item.value elif item.name == 'anharmonicity': alphas[idx] = item.value # U2 error rates single_gate_errors = [0] * n_qubits single_gate_times = [0] * n_qubits for gate in props.gates: if gate.gate in ['u2', 'sx']: _qubit = gate.qubits[0] for gpar in gate.parameters: if gpar.name == 'gate_error': single_gate_errors[_qubit] = gpar.value elif gpar.name == 'gate_length': single_gate_times[_qubit] = gpar.value # Convert to log10 single_gate_errors = np.log10(np.asarray(single_gate_errors)) avg_1q_err = np.mean(single_gate_errors) max_1q_err = _round_log10_exp(np.max(single_gate_errors), rnd='up', decimals=1) min_1q_err = _round_log10_exp(np.min(single_gate_errors), rnd='down', decimals=1) single_norm = mpl.colors.Normalize(vmin=min_1q_err, vmax=max_1q_err) q_colors = [ mpl.colors.rgb2hex(color_map(single_norm(err))) for err in single_gate_errors ] if n_qubits > 1: line_colors = [] if cmap: cx_errors = [] cx_times = [] for line in cmap: for gate in props.gates: if gate.qubits == line: for gpar in gate.parameters: if gpar.name == 'gate_error': cx_errors.append(gpar.value) elif gpar.name == 'gate_length': cx_times.append(gpar.value) # Convert to array cx_errors = np.log10(np.asarray(cx_errors)) # remove bad cx edges if remove_badcal_edges: cx_idx = np.where(cx_errors != 0.0)[0] else: cx_idx = np.arange(len(cx_errors)) avg_cx_err = np.mean(cx_errors[cx_idx]) min_cx_err = _round_log10_exp(np.min(cx_errors[cx_idx]), rnd='down', decimals=1) max_cx_err = _round_log10_exp(np.max(cx_errors[cx_idx]), rnd='up', decimals=1) cx_norm = mpl.colors.Normalize(vmin=min_cx_err, vmax=max_cx_err) for err in cx_errors: if err != 0.0 or not remove_badcal_edges: line_colors.append( mpl.colors.rgb2hex(color_map(cx_norm(err)))) else: line_colors.append("#ff0000") # Measurement errors read_err = [0] * n_qubits p01_err = [0] * n_qubits p10_err = [0] * n_qubits for qubit in range(n_qubits): for item in props.qubits[qubit]: if item.name == 'readout_error': read_err[qubit] = item.value elif item.name == 'prob_meas0_prep1': p01_err[qubit] = item.value elif item.name == 'prob_meas1_prep0': p10_err[qubit] = item.value read_err = np.asarray(read_err) avg_read_err = np.mean(read_err) max_read_err = np.max(read_err) p01_err = np.asarray(p01_err) p10_err = np.asarray(p10_err) if n_qubits < 10: num_left = n_qubits num_right = 0 else: num_left = math.ceil(n_qubits / 2) num_right = n_qubits - num_left x_max = max([d[1] for d in grid_data]) y_max = max([d[0] for d in grid_data]) max_dim = max(x_max, y_max) qubit_size = 32 font_size = 14 offset = 0 if cmap: if y_max / max_dim < 0.33: qubit_size = 24 font_size = 10 offset = 1 if n_qubits > 5: right_meas_title = "Readout error" else: right_meas_title = None if cmap: cx_title = "CNOT error rate [Avg. {}]".format( '{:.2}\u22C510<sup>{}</sup>'.format(*_pow10_coeffs(avg_cx_err))) else: cx_title = None fig = make_subplots( rows=2, cols=11, row_heights=[0.95, 0.05], vertical_spacing=0.15, specs=[[{ "colspan": 2 }, None, { "colspan": 6 }, None, None, None, None, None, { "colspan": 2 }, None, None], [{ "colspan": 4 }, None, None, None, None, None, { "colspan": 4 }, None, None, None, None]], subplot_titles=("Readout error", None, right_meas_title, "SX error rate [Avg. {}]".format( '{:.2}\u22C510<sup>{}</sup>'.format( *_pow10_coeffs(avg_1q_err))), cx_title)) # Add lines for couplings if cmap and n_qubits > 1: for ind, edge in enumerate(cmap): is_symmetric = False if edge[::-1] in cmap: is_symmetric = True y_start = grid_data[edge[0]][0] + offset x_start = grid_data[edge[0]][1] y_end = grid_data[edge[1]][0] + offset x_end = grid_data[edge[1]][1] if is_symmetric: if y_start == y_end: x_end = (x_end - x_start) / 2 + x_start x_mid = x_end y_mid = y_start elif x_start == x_end: y_end = (y_end - y_start) / 2 + y_start x_mid = x_start y_mid = y_end else: x_end = (x_end - x_start) / 2 + x_start y_end = (y_end - y_start) / 2 + y_start x_mid = x_end y_mid = y_end else: if y_start == y_end: x_mid = (x_end - x_start) / 2 + x_start y_mid = y_end elif x_start == x_end: x_mid = x_end y_mid = (y_end - y_start) / 2 + y_start else: x_mid = (x_end - x_start) / 2 + x_start y_mid = (y_end - y_start) / 2 + y_start cx_str = 'cnot<sub>err</sub> = {err}' cx_str += '<br>𝜏<sub>cx</sub> = {tau} ns' fig.append_trace(go.Scatter( x=[x_start, x_mid, x_end], y=[-y_start, -y_mid, -y_end], mode="lines", line=dict(width=6, color=line_colors[ind]), hoverinfo='text', hovertext=cx_str.format( err='{:.3}\u22C510<sup>{}</sup>'.format( *_pow10_coeffs(cx_errors[ind])), tau=np.round(cx_times[ind], 2))), row=1, col=3) # Add the qubits themselves qubit_text = [] qubit_str = "<b>Qubit {idx}</b>" qubit_str += "<br>freq = {freq} GHz" qubit_str += "<br>T<sub>1</sub> = {t1} \u03BCs" qubit_str += "<br>T<sub>2</sub> = {t2} \u03BCs" qubit_str += "<br>α = {anh} GHz" qubit_str += "<br>sx<sub>err</sub> = {err}" qubit_str += "<br>𝜏<sub>sx</sub> = {tau} ns" for kk in range(n_qubits): qubit_text.append( qubit_str.format( idx=kk, freq=np.round(freqs[kk], 5), t1=np.round(t1s[kk], 2), t2=np.round(t2s[kk], 2), anh=np.round(alphas[kk], 3) if alphas[kk] else 'NA', err='{:.3}\u22C510<sup>{}</sup>'.format( *_pow10_coeffs(single_gate_errors[kk])), tau=np.round(single_gate_times[kk], 2))) if n_qubits > 20: qubit_size = 23 font_size = 11 if n_qubits > 50: qubit_size = 20 font_size = 9 qtext_color = [] for ii in range(n_qubits): qtext_color.append(find_text_color(q_colors[ii])) fig.append_trace(go.Scatter(x=[d[1] for d in grid_data], y=[-d[0] - offset for d in grid_data], mode="markers+text", marker=go.scatter.Marker(size=qubit_size, color=q_colors, opacity=1), text=[str(ii) for ii in range(n_qubits)], textposition="middle center", textfont=dict(size=font_size, color=qtext_color), hoverinfo="text", hovertext=qubit_text), row=1, col=3) fig.update_xaxes(row=1, col=3, visible=False) _range = None if offset: _range = [-3.5, 0.5] fig.update_yaxes(row=1, col=3, visible=False, range=_range) # H error rate colorbar if n_qubits > 1: fig.append_trace(go.Heatmap(z=[ np.linspace(min_1q_err, max_1q_err, 100), np.linspace(min_1q_err, max_1q_err, 100) ], colorscale=plotly_cmap, showscale=False, hoverinfo='none'), row=2, col=1) fig.update_yaxes(row=2, col=1, visible=False) mid_1q_err = _round_log10_exp( (max_1q_err - min_1q_err) / 2 + min_1q_err, rnd='up', decimals=1) fig.update_xaxes(row=2, col=1, tickfont=dict(size=13), tickvals=[0, 49, 99], ticktext=[ '{:.2}\u22C510<sup>{}</sup>'.format( *_pow10_coeffs(min_1q_err)), '{:.2}\u22C510<sup>{}</sup>'.format( *_pow10_coeffs(mid_1q_err)), '{:.2}\u22C510<sup>{}</sup>'.format( *_pow10_coeffs(max_1q_err)), ]) # CX error rate colorbar if cmap and n_qubits > 1: fig.append_trace(go.Heatmap(z=[ np.linspace(min_cx_err, max_cx_err, 100), np.linspace(min_cx_err, max_cx_err, 100) ], colorscale=plotly_cmap, showscale=False, hoverinfo='none'), row=2, col=7) fig.update_yaxes(row=2, col=7, visible=False) mid_cx_err = (max_cx_err - min_cx_err) / 2 + min_cx_err fig.update_xaxes( row=2, col=7, tickfont=dict(size=13), tickvals=[0, 49, 99], ticktext=[ '{:.2}\u22C510<sup>{}</sup>'.format( *_pow10_coeffs(min_cx_err)), '{:.2}\u22C510<sup>{}</sup>'.format( *_pow10_coeffs(mid_cx_err)), '{:.2}\u22C510<sup>{}</sup>'.format(*_pow10_coeffs(max_cx_err)) ]) hover_text = "<b>Qubit {idx}</b>" hover_text += "<br>M<sub>err</sub> = {err}" hover_text += "<br>P<sub>0|1</sub> = {p01}" hover_text += "<br>P<sub>1|0</sub> = {p10}" # Add the left side meas errors for kk in range(num_left - 1, -1, -1): fig.append_trace(go.Bar( x=[read_err[kk]], y=[kk], orientation='h', marker=dict(color='#c7c7c5'), hoverinfo="text", hoverlabel=dict(font=dict(color=meas_text_color)), hovertext=[ hover_text.format(idx=kk, err=np.round(read_err[kk], 4), p01=np.round(p01_err[kk], 4), p10=np.round(p10_err[kk], 4)) ]), row=1, col=1) fig.append_trace(go.Scatter(x=[avg_read_err, avg_read_err], y=[-0.25, num_left - 1 + 0.25], mode='lines', hoverinfo='none', line=dict(color=text_color, width=2, dash='dot')), row=1, col=1) fig.update_yaxes(row=1, col=1, tickvals=list(range(num_left)), autorange="reversed") fig.update_xaxes( row=1, col=1, range=[0, 1.1 * max_read_err], tickvals=[0, np.round(avg_read_err, 2), np.round(max_read_err, 2)], showline=True, linewidth=1, linecolor=text_color, tickcolor=text_color, ticks="outside", showgrid=False, zeroline=False) # Add the right side meas errors, if any if num_right: for kk in range(n_qubits - 1, num_left - 1, -1): fig.append_trace(go.Bar( x=[-read_err[kk]], y=[kk], orientation='h', marker=dict(color='#c7c7c5'), hoverinfo="text", hoverlabel=dict(font=dict(color=meas_text_color)), hovertext=[ hover_text.format(idx=kk, err=np.round(read_err[kk], 4), p01=np.round(p01_err[kk], 4), p10=np.round(p10_err[kk], 4)) ]), row=1, col=9) fig.append_trace(go.Scatter(x=[-avg_read_err, -avg_read_err], y=[num_left - 0.25, n_qubits - 1 + 0.25], mode='lines', hoverinfo='none', line=dict(color=text_color, width=2, dash='dot')), row=1, col=9) fig.update_yaxes( row=1, col=9, tickvals=list(range(n_qubits - 1, num_left - 1, -1)), side='right', autorange="reversed", ) fig.update_xaxes( row=1, col=9, range=[-1.1 * max_read_err, 0], tickvals=[ 0, -np.round(avg_read_err, 2), -np.round(max_read_err, 2) ], ticktext=[0, np.round(avg_read_err, 2), np.round(max_read_err, 2)], showline=True, linewidth=1, linecolor=text_color, tickcolor=text_color, ticks="outside", showgrid=False, zeroline=False) # Makes the subplot titles smaller than the 16pt default for ann in fig['layout']['annotations']: ann['font'] = dict(size=13) title_text = "{} error map".format(backend.name()) if show_title else '' fig.update_layout(showlegend=False, plot_bgcolor=background_color, paper_bgcolor=background_color, width=figsize[0], height=figsize[1], title=dict(text=title_text, x=0.452), title_font_size=20, font=dict(color=text_color), margin=dict(t=60, l=0, r=0, b=0), hoverlabel=dict(font_size=14, font_family="courier,monospace", align='left')) if as_widget: return PlotlyWidget(fig) return PlotlyFigure(fig)
def visualize_matrix(rotor, matrix, frequency=None, **kwargs): """Visualize global matrix. This function gives some visualization of a given matrix, displaying values on a heatmap. Parameters ---------- rotor: rs.Rotor The rotor object. matrix: str String for the desired matrix. frequency: float, optional Excitation frequency. Defaults to rotor speed. kwargs : optional Additional key word arguments can be passed to change the plot layout only (e.g. coloraixs=dict(colorscale="Rainbow"), width=1000, height=800, ...). *See Plotly Python Figure Reference for more information. Returns ------- fig : Plotly graph_objects.Figure() The figure object with the plot. """ A = np.zeros((rotor.ndof, rotor.ndof)) # E will store element's names and contributions to the global matrix E = np.zeros((rotor.ndof, rotor.ndof), dtype=np.object) M, N = E.shape for i in range(M): for j in range(N): E[i, j] = [] for elm in rotor.elements: g_dofs = elm.dof_global_index l_dofs = elm.dof_local_index() try: elm_matrix = getattr(elm, matrix)(frequency) except TypeError: elm_matrix = getattr(elm, matrix)() A[np.ix_(g_dofs, g_dofs)] += elm_matrix for l0, g0 in zip(l_dofs, g_dofs): for l1, g1 in zip(l_dofs, g_dofs): if elm_matrix[l0, l1] != 0: E[g0, g1].append("<br>" + elm.__class__.__name__ + f"(n={elm.n})" + f": {elm_matrix[l0, l1]:.2E}") # list for dofs -> ['x0', 'y0', 'alpha0', 'beta0', 'x1'...] dof_list = [0 for i in range(rotor.ndof)] for elm in rotor.elements: for k, v in elm.dof_global_index._asdict().items(): dof_list[v] = k data = {"row": [], "col": [], "value": [], "pos_value": [], "elements": []} for i, dof_row in enumerate(dof_list): for j, dof_col in enumerate(dof_list): data["row"].append(i) data["col"].append(j) data["value"].append(A[i, j]) data["pos_value"].append(abs(A[i, j])) data["elements"].append(E[i, j]) dof_string_list = [] sub = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉") for d in dof_list: d = d.replace("alpha", "α") d = d.replace("beta", "β") d = d.replace("_", "") d = d.translate(sub) dof_string_list.append(d) x_axis = dof_string_list y_axis = dof_string_list[::-1] fig = go.Figure() fig.add_trace( go.Heatmap( x=x_axis, y=y_axis, z=A[::-1], customdata=np.array(data["elements"]).reshape(A.shape)[::-1], coloraxis="coloraxis", hovertemplate=("<b>Value: %{z:.3e}<b><br>" + "<b>Elements:<b><br> %{customdata}"), name="<b>Matrix {}</b>".format(matrix), )) fig.update_layout( coloraxis=dict( cmin=np.amin(A), cmax=np.amax(A), colorscale="Rainbow", colorbar=dict(title=dict(text="<b>Value</b>", side="top"), exponentformat="power"), ), **kwargs, ) return fig
axis=1) hm_df['scaled_status'] = hm_df.apply(lambda x: scaled_status[x.status], axis=1) pivot_table = pd.pivot_table(hm_df[[ 'equip_id', 'dtime', 'status', 'cycle_dtime', 'status', 'scaled_status' ]], index=['equip_id'], columns=['cycle_dtime'], values='scaled_status', aggfunc='max', fill_value=scaled_status[Status.UNDEF]) equips = [f'місце {m}' for m in pivot_table.index.tolist()] dates = [Misc.str_time(d) for d in pivot_table.columns.tolist()] z = pivot_table.values.tolist() heatmap = go.Heatmap(z=z, x=dates, y=equips, colorscale=colorscale, xgap=2, ygap=2) fig = go.Figure(data=[heatmap]) # fig = ff.create_annotated_heatmap(z=z, x=dates, y=equips, colorscale=colorscale, xgap=2, ygap=2) fig.show() fig.write_image("../tmp/tst.png") # colorbar=dict(thickness=25, # tickvals=[0.1, 0.4, 0.7, 0.9], # ticktext=['aaa', 'bbb', 'ccc', 'ddd']))
import numpy as np import plotly import plotly.graph_objects as go arr = np.load('cache/minimax.npy') arr = arr.reshape((9, 3**9)) fig = go.Figure( go.Heatmap(z=arr, x=np.arange(0, 3**9, 1), y=np.arange(0, 9, 1), colorscale='viridis')) fig.update_layout(title='Minimax Cache visualization', xaxis={'title': 'States'}, yaxis={'title': 'Actions'}) plotly.offline.plot(fig, filename='cache/minimax_visualized.html')
sort=False, ) ] layout = go.Layout(title="Symptoms") fig = go.Figure(data, layout) st.plotly_chart(fig) st.write("# Department symptoms") depsep = df.groupby("department")["symptoms"].value_counts().unstack().fillna( 0) depsep fig = go.Figure(data=go.Heatmap( z=depsep.values, x=depsep.index, y=depsep.columns, hoverongaps=False, colorscale="reds", )) st.plotly_chart(fig) # symptoms df['symptoms'].value_counts() symptoms = df[['id', 'result', 'symptoms' ]].groupby(by=['result', 'symptoms']).count().reset_index() symptoms positive_x = symptoms[symptoms['result'] == 'positive']['symptoms'].values positive_y = symptoms[symptoms['result'] == 'positive']['id'].values
def read_proportions_heatmap(TaXon_table_xlsx, taxonomic_level, path_to_outdirs, width_value, height_value, template, font_size): import PySimpleGUI as sg import pandas as pd import numpy as np import plotly.express as px import plotly.graph_objects as go from pathlib import Path import os, webbrowser TaXon_table_xlsx = Path(TaXon_table_xlsx) TaXon_table_df = pd.read_excel(TaXon_table_xlsx).fillna("unidentified") samples_list = TaXon_table_df.columns.tolist()[10:] Species_read_proportion_dict = {} # check for presence absence data # otherwise abort and print error message pa_test = set([val for sublist in TaXon_table_df[samples_list].values.tolist() for val in sublist]) if pa_test == {1,0}: sg.Popup("Please do not use presence absence data!", title=("Error")) raise RuntimeError ## check for the taxonmic level to analyse if taxonomic_level not in ["ASVs", "ESVs", "OTUs", "zOTUs"]: ## create a y axis title text taxon_title = taxonomic_level.lower() # ask how the to handle missing taxonomy answer = sg.PopupYesNo("Shall missing taxonomy be replaced by the best hit?\n\nYes => Replace missing taxonomy with the best available hit.\nNo => Display missing taxonomy as \'unidentified\'.", title="Plotting strategy") if answer == "Yes": ## replace nan with the best hit taxon_levels_dict = {"Phylum": 1, "Class": 2, "Order": 3, "Family": 4, "Genus": 5, "Species": 6} value_taxonomic_level = taxon_levels_dict[taxonomic_level] best_hit_list = [] for taxon in TaXon_table_df[list(taxon_levels_dict.keys())].values.tolist(): ## human readable range => e.g. from 5 to 0 for species level for test in range(value_taxonomic_level-1,-1,-1): if taxon[test] != "unidentified": best_hit_list.append(taxon[test]) break TaXon_table_df[taxonomic_level] = best_hit_list else: taxon_title = taxonomic_level taxonomic_level = "ID" ############################################################################## ## create a subfolder for better sorting and overview dirName = Path(str(path_to_outdirs) + "/" + "Read_proportions_plots" + "/" + TaXon_table_xlsx.stem + "/") if not os.path.exists(dirName): os.mkdir(dirName) output_pdf = Path(str(dirName) + "/" + taxonomic_level + "_heatmap.pdf") output_html = Path(str(dirName) + "/" + taxonomic_level + "_heatmap.html") output_xlsx = Path(str(dirName) + "/" + taxonomic_level + "_heatmap.xlsx") ############################################################################ ## create the progress bar window layout = [[sg.Text('Progress bar')], [sg.ProgressBar(1000, orientation='h', size=(20, 20), key='progressbar')], [sg.Cancel()]] window_progress_bar = sg.Window('Progress bar', layout, keep_on_top=True) progress_bar = window_progress_bar['progressbar'] progress_update = 0 progress_increase = 1000 / len(samples_list) + 1 ############################################################################ TaXon_table_df_2 = "" for sample in samples_list: df = TaXon_table_df[['ID', "Phylum", "Class", "Order", "Family", "Genus", "Species", sample]] df_2 = df[[sample]]/df[[sample]].sum() df = df.assign(perc=df_2.values * 100) df["perc"] = df.groupby([taxonomic_level])['perc'].transform('sum') df_3 = df.drop_duplicates(subset=[taxonomic_level, 'perc']) df_3 = df_3.drop([sample], axis=1) df_3 = df_3.rename(columns={"perc": sample}) if TaXon_table_df_2 is "": TaXon_table_df_2 = df_3 else: TaXon_table_df_2 = TaXon_table_df_2.join(df_3[[sample]]) ############################################################################ event, values = window_progress_bar.read(timeout=10) if event == 'Cancel' or event is None: window_progress_bar.Close() raise RuntimeError # update bar with loop value +1 so that bar eventually reaches the maximum progress_update += progress_increase progress_bar.UpdateBar(progress_update) ############################################################################ window_progress_bar.Close() ## create plot ## ask if a subplot shall be generated plot_df = TaXon_table_df_2[samples_list] plot_df.index = TaXon_table_df_2[taxonomic_level] ## custom colorscale cs=[ [0, "rgb(220,220,220)"], [0.00001, "rgb(255,255,255)"], [0.05, "rgb(255,255,255)"], [0.05, "rgb(242,242,255)"], [0.1, "rgb(242,242,255)"], [0.1, "rgb(229,229,255)"], [0.15, "rgb(229,229,255)"], [0.15, "rgb(216,216,255)"], [0.2, "rgb(216,216,255)"], [0.2, "rgb(203,203,255)"], [0.25, "rgb(203,203,255)"], [0.25, "rgb(190,190,255)"], [0.3, "rgb(190,190,255)"], [0.3, "rgb(177,177,255)"], [0.35, "rgb(177,177,255)"], [0.35, "rgb(164,164,255)"], [0.4, "rgb(164,164,255)"], [0.4, "rgb(155,155,255)"], [0.45, "rgb(155,155,255)"], [0.45, "rgb(138,138,255)"], [0.5, "rgb(138,138,255)"], [0.5,"rgb(125,125,255)"], [0.55,"rgb(125,125,255)"], [0.55, "rgb(112,112,255)"], [0.6, "rgb(112,112,255)"], [0.6, "rgb(99,99,255)"], [0.65, "rgb(99,99,255)"], [0.65, "rgb(86,86,255)"], [0.7, "rgb(86,86,255)"], [0.7, "rgb(73,73,255)"], [0.75, "rgb(73,73,255)"], [0.75, "rgb(60,60,255)"], [0.8, "rgb(60,60,255)"], [0.8, "rgb(47,47,255)"], [0.85, "rgb(47,47,255)"], [0.85, "rgb(34,34,255)"], [0.9, "rgb(34,34,255)"], [0.9, "rgb(21,21,255)"], [0.95, "rgb(21,21,255)"], [0.95, "rgb(8,8,255)"], [1, "rgb(8,8,255)"], ] if (taxonomic_level == "Species" or taxonomic_level == "Genus"): y_values = ["<i>" + taxon + "</i>" for taxon in plot_df.index.tolist()[::-1]] else: y_values = plot_df.index.tolist()[::-1] ## v2 heatmap fig = go.Figure(data=go.Heatmap( z=plot_df.values.tolist()[::-1], x=plot_df.columns.tolist(), y=y_values, colorscale=cs)) fig.update_layout(width=int(width_value), height=int(height_value), template=template, font_size=font_size, title_font_size=font_size, yaxis_nticks=len(plot_df.index.tolist()), xaxis_nticks=len(plot_df.index.tolist()), legend_title_text='reads (%)') ## write files fig.write_image(str(output_pdf)) fig.write_html(str(output_html)) ## ask to show file answer = sg.PopupYesNo('Show plot?', keep_on_top=True) if answer == "Yes": webbrowser.open('file://' + str(output_html)) ## print closing text closing_text = "Read proportion plots are found in: " + str(path_to_outdirs) + "/Read_proportion_plots/" sg.Popup(closing_text, title="Finished", keep_on_top=True) ## write log from taxontabletools.create_log import ttt_log ttt_log("read proportions heatmap", "analysis", TaXon_table_xlsx.name, output_pdf.name, "", path_to_outdirs)
def plot_map_plotly(cooler_obj, track, chrom, signal="HMM3"): loc_eig = track[track.chrom == chrom].copy().reset_index() masked_signal = loc_eig[loc_eig['E1'] == loc_eig['E1']] indexes_t = np.where(np.abs(np.diff(masked_signal[signal])) > 0)[0] + 1 line_loc = masked_signal.iloc[indexes_t].index.values #indexes of lines arr = cooler_obj.matrix(balance=True).fetch(chrom) x = np.linspace(0, len(arr) - 1, len(arr), dtype=int) y = loc_eig["E1"].values y_sig = loc_eig[signal].values y_sig = y_sig * 2 / np.nanmax(y_sig) - 1 trace1 = go.Heatmap( z=np.log(arr + 1e-5), colorscale="ylorrd", showscale=False) #, color_continuous_scale='YlOrRd', aspect ='equal') #trace1.update_xaxes(side="top") trace2 = go.Scatter(x=x, y=y, xaxis="x", yaxis="y2", mode='lines', marker=dict(color='orange'), showlegend=False) trace2_2 = go.Scatter(x=x, y=y_sig, xaxis="x", yaxis="y2", mode='lines', marker=dict(color='blue'), showlegend=False) trace3 = go.Scatter(x=y, y=x, xaxis="x2", yaxis="y", mode='lines', marker=dict(color='orange'), showlegend=False) trace3_2 = go.Scatter(x=y_sig, y=x, xaxis="x2", yaxis="y", mode='lines', marker=dict(color='blue'), showlegend=False) layout = go.Layout( xaxis=dict( autorange=False, domain=[0.12, 1], side="top" # ,range=[0, len(arr) -1] , range=[80, 200], tickfont=dict(family='Rockwell', color='black', size=6), scaleanchor="y", showgrid=False # ,nticks = 6 # ,tick0 = 0 ), yaxis=dict( domain=[0, 0.88], autorange=False # ,range=[len(arr) -1, 0] , range=[120, 0], tickangle=-90, tickfont=dict(family='Rockwell', color='black', size=6), showgrid=False # ,nticks = 6 # ,tick0 = 0 ), yaxis2=dict(domain=[0.9, 1], tickfont=dict(family='Rockwell', color='black', size=6), range=[min(-1, np.nanmin(y)), max(np.nanmax(y), 1)], tick0=-1, dtick=1, showgrid=False, fixedrange=True), xaxis2=dict(domain=[0, 0.1], autorange=False, range=[max(np.nanmax(y), 1), min(-1, np.nanmin(y))], side="top", tickangle=0, tickfont=dict(family='Rockwell', color='black', size=6), tick0=-1, dtick=1, showgrid=False, fixedrange=True), hovermode="closest", ) fig = go.Figure(data=[trace1, trace2, trace2_2, trace3, trace3_2], layout=layout) ''' Draw Lines to the map ''' for loc in line_loc: fig.add_shape( # Line reference to the axes type="line", xref="x", yref="y", x0=0, y0=loc, x1=len(y) - 1, y1=loc, line=dict( color="black", width=1, ), ) fig.add_shape( # Line reference to the axes type="line", xref="x", yref="y", x0=loc, y0=0, x1=loc, y1=len(y) - 1, line=dict( color="black", width=1, ), ) fig.update_layout(title=dict(text=chrom), width=650, height=650, autosize=False, margin=dict(t=30, b=30, l=30, r=30)) return fig
import plotly.express as px import plotly.graph_objects as go import pandas as pd import numpy as np dist = np.random.multinomial(5, [0.5, 0.5], size=20) probs = [[i, i] for i in range(len(dist))] for row in range(len(dist)): probs[row][0] = dist[row, 0] / dist[row].sum() probs[row][1] = dist[row, 1] / dist[row].sum() go.Figure(data=go.Heatmap(z=list(np.array(probs).transpose()), x=['word ' + str(i + 1) for i in range(len(probs))], y=['positive', 'negative'], colorscale=[[0.0, "rgb(255,255,255)"], [1.0, "rgb(49,54,149)"]], colorbar=dict(title="probability")))
for row in hist: row_idx = int(row[0] / .1) column[row_idx] = row[1] / len(dists) dist_blocked_hist2d.append(column) rssi_blocked_hist2d = np.array(rssi_blocked_hist2d).T dist_blocked_hist2d = np.array(dist_blocked_hist2d).T maxz = np.max([ np.max(rssi_blocked_hist2d), np.max(dist_blocked_hist2d), np.max(rssi_los_hist2d), np.max(dist_los_hist2d) ]) fig.add_trace(go.Heatmap( x=np.arange(1, 3.25, .25), y=np.arange(db_lim[0], db_lim[1], -1), z=rssi_los_hist2d[int(-db_lim[0]):int(-db_lim[1]), :], zmin=0, zmax=maxz), row=1, col=1, secondary_y=False) fig.add_trace(go.Heatmap( x=np.arange(1, 3.25, .25), y=np.arange(dist_lim[0], dist_lim[1], .1), z=dist_los_hist2d[int(dist_lim[0] / .1):int(dist_lim[1] / .1), :], zmin=0, zmax=maxz), row=2, col=1, secondary_y=False) fig.add_trace(go.Heatmap(
next_symbol = symbol_list[j] try: ts = close_dfs[[symbol, next_symbol]].to_numpy() except KeyError: pass try: jh = JohansenResults(ts) except np.linalg.LinAlgError: continue johansen_pair_matrix[i, j] = jh.is_pair if jh.is_pair is not None: pairs.append((symbol, next_symbol)) evecs.append(jh.pi) eigvals.append(jh.eigenvalues) pvals.append(jh.p_val) output_df = pd.DataFrame(index=range(len(pairs))) output_df['Pair'] = pairs output_df['Eigenvectors'] = evecs output_df['Eigenvalues'] = eigvals output_df['pValue'] = pvals output_df.to_pickle('data/johansen_{}.pickle'.format(fund_type)) fig = go.Figure( data=go.Heatmap(z=johansen_pair_matrix, x=symbol_list, y=symbol_list)) fig.show()
for i, seed_id in enumerate(seismic_data["seed_id"]): for j, stream_id in enumerate(seismic_data["stream_id"]): fig.add_trace( go.Scatter( name=str(seed_id.values), x=pd.to_datetime(seismic_data["time"].values), y=seismic_data.sel(seed_id=seed_id, stream_id=stream_id).values, ), row=1, col=1, ) spectrogram = stuett.data.Spectrogram(nfft=512, stride=64, dim="time") spec = spectrogram(seismic_data) # select only one channel spec = spec.sel(seed_id="4D.MH36.A.EHE", stream_id=0) trace_hm = go.Heatmap( x=pd.to_datetime(spec["time"].values), y=spec["frequency"].values, z=np.log(spec.values), colorscale="Jet", hoverinfo="none", colorbar={"title": "Power Spectrum/dB"}, ) fig.add_trace(trace_hm, row=2, col=1) fig.show()
def show_frame_analysis(df_xy, filename, feature_col="fwhm"): df0 = df_xy.set_index(["x_bin", "y_bin"]).unstack(0).iloc[::-1] df1 = df0.stack() # Set text on hover df1["text"] = df1.apply( lambda row: f"fwhm: {row['fwhm']:.2f} px<br>\ radius: {row['chip_r']:.0f} px<br>\ ellipticity: {row['ellipticity']:.3f}<br>\ eccentricity: {row['eccentricity']:.3f}<br>\ major-axis: {row['a']:.3f}<br>\ minor-axis: {row['b']:.3f}<br>\ stars: {row['star_count']}<br>", axis=1, ) df2 = df1["text"].unstack(1).iloc[::-1] # Add quiver for opposite direction df_quiver = df_xy[["x_bin", "y_bin", "vec_u", "vec_v"]] df_quiver["vec_u"] *= -1 df_quiver["vec_v"] *= -1 df_quiver = pd.concat( [df_xy[["x_bin", "y_bin", "vec_u", "vec_v"]], df_quiver]) p = ff.create_quiver( df_quiver["x_bin"], df_quiver["y_bin"], df_quiver["vec_u"], df_quiver["vec_v"], scale=200, scaleratio=1, name="quiver", line_width=1, line=dict(color="#000"), ) zmax = df0[feature_col].values.max() if feature_col == "fwhm": zmin = 1 zmax = max([5, zmax]) elif feature_col == "ellipticity": zmin = 0 zmax = max([0.5, zmax]) elif feature_col == "eccentricity": zmin = 0 zmax = max([1, zmax]) p.add_trace( go.Heatmap( x=df0[feature_col].columns, y=df0.index, z=df0[feature_col].values, name="test", hovertext=df2, colorscale="balance", zmin=zmin, zmax=zmax, colorbar=dict(title=feature_col.upper()), )) p.update_layout( title=f"Frame analysis for<br>{os.path.basename(filename)}", ) return p
def get_heatmaps(site, path): global data macro_cat = { 'geophony': ['Wind', 'Rain', 'River', 'Wave', 'Thunder'], 'biophony': ['Bird', 'Amphibian', 'Insect', 'Mammal', 'Reptile'], 'anthropophony': [ 'Walking', 'Cycling', 'Beep', 'Car', 'Car honk', 'Motorbike', 'Plane', 'Helicoptere', 'Boat', 'Others_motors', 'Shoot', 'Bell', 'Talking', 'Music', 'Dog bark', 'Kitchen sounds', 'Rolling shutter' ] } macro_cat_fr = { 'geophony': ['Vent', 'Pluie', 'Rivière', 'Vague', 'Tonnerre'], 'biophony': ['Oiseau', 'Amphibien', 'Insecte', 'Mammifère', 'Reptile'], 'anthropophony': [ 'Marche', 'Vélo', 'Bip', 'Voiture', 'Klaxon', 'Moto', 'Avion', 'Hélicoptère', 'Bateau', 'Autres_moteurs', 'Tir', 'Cloche', 'Parler', 'Musique', 'Aboiement de chien', 'Bruits de cuisine', 'Volet roulant' ] } # try: # data = pd.read_csv(os.path.join(path, f'tagging_site_{site:04d}.csv')) # except: data = pd.read_csv(os.path.join(path, f'results_{site:04d}.csv')) fig = make_subplots(rows=4, cols=1, shared_xaxes=True, subplot_titles=("<b>Anthropophonie</b>", "<b>Géophonie</b>", "<b>Biophonie</b>", ""), vertical_spacing=0.04) fig.add_trace(go.Heatmap( x=data['datetime'], y=macro_cat_fr['geophony'], z=data[[f'tag_{idx}' for idx in macro_cat['geophony']]].T, coloraxis='coloraxis', name='Géophonie', colorscale='Hot'), row=2, col=1) fig.add_trace(go.Heatmap( x=data['datetime'], y=macro_cat_fr['biophony'], z=data[[f'tag_{idx}' for idx in macro_cat['biophony']]].T, coloraxis='coloraxis', name='Biophonie', colorscale='Hot'), row=3, col=1) fig.add_trace(go.Heatmap( x=data['datetime'], y=macro_cat_fr['anthropophony'], z=data[[f'tag_{idx}' for idx in macro_cat['anthropophony']]].T, coloraxis='coloraxis', name='Anthropophonie', colorscale='viridis'), row=1, col=1) # for idx in range(3): # fig.add_trace(go.Scattergl(x = data['datetime'], y = data['tag_{}'.format(macro_cat['geophony'][idx])], name=macro_cat['geophony'][idx]), row=1, col=1) # fig.add_trace(go.Scattergl(x = data['datetime'], y = data['tag_{}'.format(macro_cat['biophony'][idx])], name=macro_cat['biophony'][idx]), row=2, col=1) # fig.add_trace(go.Scattergl(x = data['datetime'], y = data['tag_{}'.format(macro_cat['anthropophony'][idx])], name=macro_cat['anthropophony'][idx]), row=3, col=1) fig.add_trace(go.Scattergl(x=data['datetime'], y=data['anthropophony'], name='Anthropophonie', line=dict(color='black'), opacity=0.5), row=4, col=1) fig.add_trace(go.Scattergl(x=data['datetime'], y=data['geophony'], name="Géophonie", line=dict(color='blue'), opacity=0.5), row=4, col=1) fig.add_trace(go.Scattergl(x=data['datetime'], y=data['biophony'], name='Biophonie', line=dict(color='green'), opacity=0.5), row=4, col=1) fig.update_layout(margin={ "r": 0, "t": 50, "l": 0, "b": 0 }, coloraxis_colorbar=dict(title="<b>Probabilité</b>", titleside='right', thicknessmode="pixels", thickness=30, lenmode="pixels", len=400, yanchor="bottom", y=0.0, xanchor="right", x=1.1), yaxis={'fixedrange': True}, height=800) fig.update_yaxes({'fixedrange': True}, row=2, col=1) fig.update_yaxes({'fixedrange': True}, row=3, col=1) fig.update_yaxes({'fixedrange': True}, row=4, col=1) return fig, data