def annotation_heatmap(self, ax, X, annotation, extent=None, label='Gradient', imshow_kwargs=None, **annotation_kwargs): imshow_kwargs.setdefault('label', label) imshow_kwargs.setdefault('showscale', True) imshow = self.imshow(ax, X, extent, **imshow_kwargs) X = X - X.min() X /= X.max() / 2. X -= 1 x = np.linspace(extent[0], extent[1], X.shape[0]) y = np.linspace(extent[2], extent[3], X.shape[1]) annotations = Annotations() for n, row in enumerate(annotation): for m, val in enumerate(row): var = X[n][m] annotations.append( Annotation( text=str(val), x=x[m], y=y[n], xref='x1', yref='y1', font=dict( color='white' if np.abs(var) > 0.8 else 'black', size=10), opacity=.5, showarrow=False, )) return imshow, annotations
def display_age_distribution(data, title): ''' Display the age distribution. Parameters : - data: Serie of age - title: title of the graph ''' data_fig = [go.Histogram(x=data, histnorm='count', name='runners')] layout = go.Layout(title=title, xaxis=dict(title='Age', titlefont=dict(size=18)), yaxis=dict(title='Number of runners', titlefont=dict(size=18)), showlegend=False, annotations=[ Annotation(y=120, x=75, text='mean age : ' + str(round(data.mean(), 2)), showarrow=False) ]) fig = go.Figure(data=data_fig, layout=layout) plotly.offline.iplot(fig) return fig
def plot_axis_lines(self, ax, X, color=Tango.colorsHex['mediumBlue'], label=None, marker_kwargs=None, **kwargs): if X.shape[1] == 1: annotations = Annotations() for i, row in enumerate(X): annotations.append( Annotation( text='', x=row[0], y=0, yref='paper', ax=0, ay=20, arrowhead=2, arrowsize=1, arrowwidth=2, arrowcolor=color, showarrow=True, #showlegend=i==0, #label=label, )) return annotations elif X.shape[1] == 2: marker_kwargs.setdefault('symbol', 'diamond') opacity = kwargs.pop('opacity', .8) return Scatter3d(x=X[:, 0], y=X[:, 1], z=np.zeros(X.shape[0]), mode='markers', projection=dict(z=dict(show=True, opacity=opacity)), marker=Marker(color=color, **marker_kwargs or {}), opacity=0, name=label, showlegend=label is not None, **kwargs)
def make_sbplt_anno(sbplt_in, x_in, y_in, name): return Annotation(x=np.mean(x_in), y=y_in[-1], text=name, xanchor='center', align='center', font=Font(size=14), showarrow=False, xref='x{}'.format(sbplt_in), yref='y{}'.format(sbplt_in))
def make_score_anno(sbplt_in, x_in, y_in, score): return Annotation( x=x_in.max() - 0.95, # had to tweak these from y=y_in.min() + 0, # from original text=('%.2f' % score).lstrip('0'), align='right', font=Font(size=15), showarrow=False, xref='x{}'.format(sbplt_in), yref='y{}'.format(sbplt_in))
def plot_age_distribution(df, age_column_name='age', sex_column_name='sex'): ''' This function displays the distribution of runners according to their age. Parameters: - df: DataFrame containing information about runners - age_column_name: Name of column containing age of runners ''' # Calculation of age distribution statistics by gender statistics = [] all_genders = ['all'] all_genders.extend(df[sex_column_name].unique()) for sex in all_genders: if sex == 'all': ages = df[age_column_name] else: ages = df[df[sex_column_name] == sex][age_column_name] statistics.append('<b>Mean age of ' + sex + ' runners: ' + str(round(np.mean(ages), 2)) + ' (STD: ' + str(round(np.std(ages), 2)) + ')</b>') data = [go.Histogram(x=df[age_column_name])] annotations = [ Annotation(y=1, x=1, text='<br>'.join(statistics), xref='paper', yref='paper', showarrow=False) ] shapes = [{ 'type': 'line', 'yref': 'paper', 'x0': np.mean(df[age_column_name]), 'y0': 0, 'x1': np.mean(df[age_column_name]), 'y1': 1, 'line': { 'color': '#f44242', 'width': 2, 'dash': 'dash' } }] figure = study_utils.create_plotly_legends_and_layout( data, title='Age distribution of runners', x_name='Age', y_name='Number of runners', barmode='group', bargap=0.25, annotations=annotations, shapes=shapes) plotly.offline.iplot(figure) return figure
def make_annotations(pos, text, colorVertex, font_size=14, font_color='rgb(25,25,25)'): L=len(pos[0]) if len(text)!=L: raise ValueError('The lists pos and text must have the same len') annotations = Annotations() for k in range(L): f = getColorText(colorVertex[k]) annotations.append( Annotation( #text=text[k], x=pos[0][k], y=pos[1][k], xref='x1', yref='y1', font=dict(color= f, size=font_size), showarrow=False) ) return annotations
def day_start(x, ts): x *= 2 annotation = Annotation(xref='x', yref='paper', y=0.9, x=x, text=ts.strftime('%a, %d.%m'), showarrow=False, xanchor='left') line = { 'type': 'line', 'xref': 'x', 'yref': 'paper', 'y0': 0, 'y1': 1, 'x0': x, 'x1': x, 'line': { 'width': 1 } } return line, annotation
def generate_all_evolution_figures(df, age_categories, sex_categories): ''' This function generates all evolution figures according sets of age categories and sex categories. Final Dict has the following pattern: { <age_category_1: { <sex_1>: <Plotly figure [, <sex_2>: <Plotly figure, ...] } [, <age_category_2: { <sex_1>: <Plotly figure [, <sex_2>: <Plotly figure, ...] }, ...] } Parameters - df: DataFrame containing records about runners - age_categories: Array containing age categories to be displayed (if 'All'/'all', no filter is done on df) - sex_categories: Array containing sex categories to be displayed (if 'All'/'all', no filter is done on df) Return - figures: Dict containing all evolution figures ''' # We define the considered runnings and the years interval runnings = {'column_name': 'distance (km)', 'values': OrderedDict([(10, {'name': '10 km', 'color': KM_10_COLOR}), (21, {'name': 'Semi-marathon', 'color': KM_21_COLOR}), (42, {'name': 'Marathon', 'color': KM_42_COLOR})])} year_values = [year for year in df['year'].unique() if year] # We define options and the final Dict figures = {} options = {'title': 'Evolution of number of participants over years for runnings of Lausanne Marathon', 'x_name': 'Years', 'x_values': year_values, 'y_format': 'f'} for age_category in age_categories: # We select data according to age category if age_category.lower() == 'all': data = df else: data = df[df['age category'] == age_category] figures[age_category] = {} for sex_category in sex_categories: # We select data according to sex category if sex_category.lower() == 'all': data_final = data else: data_final = data[data['sex'] == sex_category.lower()] lines = [] for km, attributes in runnings['values'].items(): data_running = data_final[data_final[runnings['column_name']] == km] line = go.Scatter(x = year_values, y = [len(data_running[data_running['year'] == y]) for y in year_values], mode = 'lines', name = attributes['name'], marker={'color': attributes['color']}) lines.append(line) annotations = [Annotation(y=1.1, text='Age category: ' + age_category + ' Sex category: ' + sex_category + ' runners', xref='paper', yref='paper', showarrow=False)] figure = study_utils.create_plotly_legends_and_layout(data=lines, **options, annotations=annotations) figures[age_category][sex_category] = figure return figures
def schedule(soln, sc, offset=False): """Summary Args: arr (TYPE): Description dep (TYPE): Description rs (TYPE): Description offset (int, optional): Description Returns: TYPE: Description """ arr = [soln.a[0]] dep = [soln.d[0]] traces = [] x_arr = [] y_arr = [] text_arr = [] x_dep = [] y_dep = [] text_dep = [] tooltip = '<b>Route</b>: {}<br><b>Bus</b>: {}<br>'\ '<b>Arrival</b>: {}<br><b>Departure</b>: {}' for r, (arr_r, dep_r) in enumerate(zip(arr, dep)): x_arr += arr_r y_arr += [r] * len(arr_r) text_arr += [ tooltip.format(sc.routes[r].name, k, arr, dep) for k, (arr, dep) in enumerate(zip(arr_r, dep_r)) ] x_dep += dep_r y_dep += [r] * len(dep_r) text_dep += [ tooltip.format(sc.routes[r].name, k, arr, dep) for k, (arr, dep) in enumerate(zip(arr_r, dep_r)) ] traces.append( Scatter(x=x_arr, y=[-y - offset for y in y_arr], mode='markers', marker=dict(size=20, color=[0] * len(x_arr), colorscale='RdBu'), hoverinfo='text', text=text_arr, opacity=.3, showlegend=False)) traces.append( Scatter(x=x_dep, y=[-y - offset for y in y_dep], mode='markers', marker=dict(symbol='triangle-right', size=10, color=[colors[r] for r in y_dep]), hoverinfo='text', text=text_dep, showlegend=False)) if type(offset) is bool: for route in sc.routes: traces.append( Scatter(x=[sc.plan_horizon * 2, sc.plan_horizon * 2], y=[route.id, route.id], mode='lines+markers', marker=dict(size=10, color=[colors[route.id], colors[route.id]]), line=dict(width=5, color=colors[route.id]), showlegend=True, name='Route %s' % route.name)) lo = alter_layout( dict( hovermode='closest', height=30 * (len(sc.routes) + 1) + 30 + 90, yaxis=dict(zeroline=False, showgrid=False, tickvals=[-x for x in range(len(sc.routes))], ticktext=[r.name for r in sc.routes], showticklabels=True, autorange=False, range=[-len(sc.routes), 1]), xaxis=dict( showgrid=True, # autotick=False, dtick=10, showticklabels=True, range=[-1, sc.plan_horizon + 1], autorange=False), annotations=Annotations([ Annotation(x=0.5, y=-85.0 / 30 / (len(sc.routes) + 1), showarrow=False, text='%s_%s_%s' % (soln.model.timestamp, soln.model.__class__.__name__, sc.name), align='center', xref='paper', yref='paper', font=dict(family='SF Mono', size=11, color='rgba(0,0,0,.2)')) ]))) return Figure(data=traces, layout=lo) else: return (traces, zip([-y - offset for y in range(len(sc.routes))], [r.name for r in sc.routes]))
def generate_teams_evolution_figures(data, title='Evolution of teams performance over the years', runnings=None, team_column_name='team', year_column_name='year', min_years=6, nb_teams=8, threshold_bins_size=50, display_annotations=True): ''' This function generate teams_evolution figures for all runnings. Final Dict has the following pattern: { <running_1: { <Plotly figure> } [, <running_2: { <Plotly figure> }, ...] } Parameters - data: DataFrame containing results - title: Title of figure - runnings: Dict containing name of column containing runnings (key: column_name) and set of runnings (key: values, value: dict() with key: value in column, value: name of running) By default, None. If None, default values will be set by function. - team_column_name: Name of column containing teams (by default, 'teams') - year_column_name: Name of column containing year associated to a given result (by default, 'year') - min_years: Minimum of participations when considering a team (by default, 6) - nb_teams: Number of teams to consider among teams with number of participations > min_years (by default, 8) Note: Teams are filtered by number of participants. - threshold_bins_size: Maximum size of a bin (by default, 25) Note: Size of bin is related to number of participants of a considered team and for a given year. If None, no limitation is used. - display_annotations: Boolean used to display annotations (by default, True) Return - figures: Dict containing all teams evolution figures ''' # Default runnings if not runnings: runnings = {'column_name': 'distance (km)', 'values': {10: '10 km', 21: 'Semi-marathon', 42: 'Marathon'}} figures = {} # Loop over runnings for key, value in runnings['values'].items(): # We retrieve data related to current running filtered_data = data[data[runnings['column_name']] == key] # We retrieve names of the <nb_teams> most important groups with at least <min_years> participations in Lausanne Marathon top_teams = filtered_data.groupby(team_column_name).filter(lambda x: x[year_column_name].nunique() >= min_years).groupby('team').size().sort_values(ascending=False).nlargest(nb_teams) # We keep only data linked with such groups data_top_teams = filtered_data[filtered_data[team_column_name].isin(top_teams.index.values)] # We finally groupby teams after complete filter groups_top_teams = data_top_teams.groupby(team_column_name) # We generate colors for each group and we initialize array that will contain traces colors = study_utils.generate_colors_palette(groups_top_teams.groups) traces = [] # Loop over groups for name, groups in groups_top_teams: x_values, y_values, size_values, texts = [], [], [], [] # Loop over participation years for current group for year, results in groups.groupby(year_column_name): x_values.append(year) y = study_utils.compute_average_time(results) y_values.append(y) text = '<b>Team: ' + name + '</b><br>Average time: ' + y.strftime('%H:%M:%S') + '<br>Participants: ' + str(len(results)) + '<br>Median age: ' + str(int(results['age'].median())) texts.append(text) size = len(results) if not threshold_bins_size or (len(results) < threshold_bins_size) else threshold_bins_size size_values.append(size) trace = go.Scatter(x=x_values, y=y_values, name=name, mode='lines+markers', hoverinfo='text', text=texts, marker=dict(size=size_values, color=colors[name], line=dict(width = 1.5, color = 'rgb(0, 0, 0)'))) traces.append(trace) # For each running, we create annotations if asked by user, we set multiple options accordingly and we store figure if display_annotations: annotations = [Annotation(y=1.1, text='Running: ' + str(value) + ' | Top teams: ' + str(nb_teams) + ' | Minimum participations: ' + str(min_years) + ' | Maximum bins size: ' + str(threshold_bins_size), xref='paper', yref='paper', showarrow=False)] else: annotations = None options = {'title': title, 'hovermode': 'closest', 'x_name': 'Year', 'y_name': 'Median time', 'y_type': 'time', 'y_format': '%H:%M:%S', 'annotations': annotations} figure = study_utils.create_plotly_legends_and_layout(data=traces, **options) figures[value] = figure return figures
step = 1. / n_channels kwargs = dict(domain=[1 - step, 1], showticklabels=False, zeroline=False, showgrid=False) # create objects for layout and traces layout = Layout(yaxis=YAxis(kwargs), showlegend=False) traces = [Scatter(x=times, y=data.T[:, 0])] # loop over the channels for ii in range(1, n_channels): kwargs.update(domain=[1 - (ii + 1) * step, 1 - ii * step]) layout.update({'yaxis%d' % (ii + 1): YAxis(kwargs), 'showlegend': False}) traces.append(Scatter(x=times, y=data.T[:, ii], yaxis='y%d' % (ii + 1))) # add channel names using Annotations annotations = Annotations([Annotation(x=-0.06, y=0, xref='paper', yref='y%d' % (ii + 1), text=ch_name, font=Font(size=9), showarrow=False) for ii, ch_name in enumerate(ch_names)]) layout.update(annotations=annotations) # set the size of the figure and plot it layout.update(autosize=False, width=1000, height=600) fig = Figure(data=Data(traces), layout=layout) py.iplot(fig, filename='shared xaxis') # We can look at the list of bad channels from the ``info`` dictionary # In[16]: raw.info['bads']
def travelTime(sc, route=False, max_time=False, offset=False): traces = [] tooltip = '<b>Route</b>: {}<br><b>Bus</b>: {}<br>'\ '<b>Arrival</b>: {}<br><b>Departure</b>: {}' for r in range(len(sc.theta)): if r == route: for s in range(len(sc.theta[r])): traces.append( Scatter(x=range(len(sc.theta[r][s])), y=sc.theta[r][s], name="%s (%s-%s)" % (sc.routes.routes[r], sc.routes.routes[r].stops[s], sc.routes.routes[r].stops[s + 1]), line=dict(shape='hv'), showlegend=True)) # if type(offset) is bool: # for route in sc.routes: # traces.append(Scatter( # x=[sc.plan_horizon*2, sc.plan_horizon*2], # y=[route.id, route.id], # mode='lines+markers', # marker=dict( # size=10, # color=[colors[route.id], colors[route.id]] # ), line=dict( # width=5, # color=colors[route.id] # ), showlegend=True, # name='Route %s' % route.name)) max_tt, min_tt = (max(max(max(sc.theta))), min(min(min(sc.theta)))) lo = alter_layout( dict( height=(max_tt - min_tt) * 70, yaxis=dict( zeroline=False, showgrid=False, tickvals=range(0, max_tt + 1), ticktext=map(str, range(0, max_tt + 1)), showticklabels=True, # autorange=False, range=[0, max_tt + 1], title="Travel time(minutes)"), xaxis=dict( showgrid=True, # autotick=False, dtick=10, showticklabels=True, range=[ -1, (sc.plan_horizon + 1) if not max_time else max_time ], autorange=False, title='Time(minutes)'), annotations=Annotations([ Annotation(x=0.5, y=0, showarrow=False, text=sc.name, align='center', xref='paper', yref='paper', font=dict(family='SF Mono', size=11, color='rgba(0,0,0,.2)')) ]))) if len(traces) == 1: traces.append( Scatter(x=[-100], y=[0], line=dict(shape='hv'), showlegend=False)) return Figure(data=traces, layout=alter_layout(dict()))
def draw_igraph(self, title, colors=None): (Xn, Yn, Zn), (Xe, Ye, Ze) = self.get_3d_position() trace1 = Scatter3d(x=Xe, y=Ye, z=Ze, mode='lines', line=Line(color='rgb(125,125,125)', width=1, dash=True), hoverinfo='none') trace2 = Scatter3d( x=Xn, y=Yn, z=Zn, mode='markers', name='callers', marker=Marker( symbol='dot', size=[100 * x + 5 for x in list(self.betweenness.values())], color=self.colors, colorscale='Rainbow', opacity=0.5), text=self.nodes, hoverinfo='text') axis = dict(showbackground=False, showline=False, zeroline=False, showgrid=False, showticklabels=False, title='') layout = Layout( title=title, width=1000, height=1000, showlegend=False, scene=Scene( xaxis=XAxis(axis), yaxis=YAxis(axis), zaxis=ZAxis(axis), ), margin=Margin(t=100), hovermode='closest', annotations=Annotations([ Annotation(showarrow=False, text="Data source: ???", xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='bottom', font=Font(size=14)) ]), ) data = Data([trace1, trace2]) fig = Figure(data=data, layout=layout) iplot(fig, filename='Call Network')
def plot_gender_distributions(df): ''' This functions displays graph representing the gender distribution of Canton of Vaud and Lausanne Marathon 2016 for comparison. Parameters - df: DataFrame containing information on runners for Lausanne Marathon 2016 Return - figure: Plotly figure ''' # Building of DataFrame for ploting CANTON_VAUD = 'Canton of Vaud' LAUSANNE_MARATHON = 'Lausanne Marathon' total_runners = len(df) total_runners_male = len(df[df['sex'] == 'male']) total_runners_female = len(df[df['sex'] == 'female']) vaud_information_population = pd.Series({ 'male': TOTAL_RESIDENT_MALE / TOTAL_RESIDENT_VAUD * 100, 'female': TOTAL_RESIDENT_FEMALE / TOTAL_RESIDENT_VAUD * 100 }) marathon_information_runner = pd.Series({ 'male': total_runners_male / total_runners * 100, 'female': total_runners_female / total_runners * 100 }) information_population = pd.DataFrame({ CANTON_VAUD: vaud_information_population, LAUSANNE_MARATHON: marathon_information_runner }) information_population.sort_index(axis=0, level=None, ascending=False, inplace=True) text_vaud = [ '<b>' + CANTON_VAUD + '</b><br>' + str(TOTAL_RESIDENT_MALE) + ' residents', '<b>' + CANTON_VAUD + '</b><br>' + str(TOTAL_RESIDENT_FEMALE) + ' residents' ] text_marathon = [ '<b>' + LAUSANNE_MARATHON + '</b><br>' + str(total_runners_male) + ' runners', '<b>' + LAUSANNE_MARATHON + '</b><br>' + str(total_runners_female) + ' runners' ] vaud_trace = go.Bar(x=information_population.index.values, y=information_population[CANTON_VAUD], name=CANTON_VAUD, hoverinfo='text', text=text_vaud) marathon_trace = go.Bar(x=information_population.index.values, y=information_population[LAUSANNE_MARATHON], name=LAUSANNE_MARATHON, hoverinfo='text', text=text_marathon) data = [vaud_trace, marathon_trace] annotations = [ Annotation(y=1.1, text='Total residents: ' + str(TOTAL_RESIDENT_VAUD) + ' | Total runners: ' + str(total_runners), xref='paper', yref='paper', showarrow=False) ] figure = study_utils.create_plotly_legends_and_layout( data, title='Gender distribution Lausanne Marathon vs Canton of Vaud', x_name='Gender', y_name='Percentage (%)', barmode='group', annotations=annotations) plotly.offline.iplot(figure) return figure
def plot_speed_distribution_by_running(data, runnings=None, title='Speed distribution by running', speed_column_name='speed (m/s)', sex_column_name='sex'): ''' This function plots, for each running, the distribution of ages of runners based on the genders of participants. Parameters - data: DataFrame to use during generation of the distribution - runnings: Dict containing name of column containing runnings (key: column_name) and set of runnings (key: values, value: dict() with following keys: name, color) By default, None. If None, default values will be set by function. - title: Title of the graph (by default, 'Speed distribution by running') - speed_column_name: Name of the column containing age of participants('age' or 'age category', by default, 'speed (m/s)') - sex_column_name: Name of the column containing sex of participants (by default, 'sex') Return - figure: Plotly figure title, x axis name, statistics, column to select, categories or not, title of xaxis, title of yaxis (not modified) ''' if not runnings: runnings = { 'column_name': 'distance (km)', 'values': OrderedDict([(10, { 'name': '10 km', 'color': KM_10_COLOR, 'position': 1 }), (21, { 'name': 'Semi-marathon', 'color': KM_21_COLOR, 'position': 2 }), (42, { 'name': 'Marathon', 'color': KM_42_COLOR, 'position': 3 })]) } colors = { 'female': FEMALE_COLOR, 'male': MALE_COLOR, 'all': ALL_GENDERS_COLOR } statistics = {} with study_utils.ignore_stdout(): figure = tools.make_subplots( rows=3, cols=1, shared_xaxes=True, subplot_titles=([ attributes['name'] for km, attributes in runnings['values'].items() ])) for key, attributes in runnings['values'].items(): filtered_df = data[data[runnings['column_name']] == key] statistics[attributes['name']] = 'Total: ' + str( len(filtered_df)) + ' runners<br>Max: ' + str( round(np.max(filtered_df[speed_column_name]), 2)) + ' m/s<br>Min: ' + str( round(np.min(filtered_df[speed_column_name]), 2) ) + ' m/s<br>Median: ' + str( round(np.median(filtered_df[speed_column_name]), 2)) + ' m/s | SD: ' + str( round( np.std(filtered_df[speed_column_name]), 2)) + ' m/s' for sex in np.concatenate( (filtered_df[sex_column_name].unique(), ['all']), axis=0): if sex == 'all': x = filtered_df[speed_column_name] else: x = filtered_df[filtered_df[sex_column_name] == sex][speed_column_name] figure.append_trace( go.Histogram(xbins={ 'start': math.floor(np.min(data[speed_column_name])), 'end': math.ceil(np.max(data[speed_column_name])), 'size': 0.1 }, x=x, name=sex.capitalize() + ' runners', legendgroup=sex, showlegend=(attributes['position'] == 1), marker={'color': colors[sex]}, opacity=0.75), attributes['position'], 1) # Format of axes and layout figure.layout.xaxis1.update(title='Speed (m/s)', tickformat='.1f') figure.layout.yaxis2.update(title='Number of participants') figure.layout.update(title=title, barmode='stack', bargroupgap=0.1, bargap=0, margin=go.Margin(t=100, b=50, l=50, r=50)) # Add of statistics # Trick: We use position of subtitles annotations to create the ones related to statistics annotations_statistics = [] for annotation in figure['layout']['annotations']: annotations_statistics.append( Annotation(y=annotation['y'] - 0.12, x=1, align='left', text=statistics[annotation['text']], xref='paper', yref='paper', yanchor='bottom', showarrow=False)) figure['layout']['annotations'].extend(annotations_statistics) plotly.offline.iplot(figure) return figure
showlegend=False, scene=Scene( xaxis=XAxis(axis), yaxis=YAxis(axis), zaxis=ZAxis(axis), ), margin=Margin( t=100 ), hovermode='closest', annotations=Annotations([ Annotation( showarrow=False, text="", xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='bottom', font=Font( size=14 ) ) ]), ) data=Data([trace1, trace2]) fig=Figure(data=data, layout=layout) py.plot(fig, filename='Crawler')
def Generate_3DModel(idList, labelList, percentage): network = pd.read_csv("network.csv") L = len(network['TF']) #Values=[network['importance'][k] for k in range(L)] #G=ig.Graph(Edges, directed=False) #layt=G.layout('kk', dim=3) G = Create_Graph(idList, labelList, percentage, netthreshold) N = len(list(G.node())) #--> Numero de nodos V = list(G.node()) # lista con todos los nodos #Edges=[(network['TF'][k], network['target'][k]) for k in range(L)] Edges = list(G.edges()) #layt=nx.spectral_layout(G,dim=3) #layt=nx.spring_layout(G, dim=3) #layt=nx.fruchterman_reingold_layout(G,dim=3) #layt=laytdict.values() #g=nx.Graph() #g.add_nodes_from(V) #g.add_edges_from(Edges) layt = nx.fruchterman_reingold_layout(G, dim=3) #layt = nx.circular_layout(G,scale=10,dim=3) #layt=nx.spring_layout(G,dim=3) laytN = list(layt.values()) Xn = [laytN[k][0] for k in range(N)] # x-coordinates of nodes Yn = [laytN[k][1] for k in range(N)] # y-coordinates Zn = [laytN[k][2] for k in range(N)] # z-coordinates Xe = [] Ye = [] Ze = [] for e in Edges: Xe += [layt[e[0]][0], layt[e[1]][0], None] # x-coordinates of edge ends Ye += [layt[e[0]][1], layt[e[1]][1], None] Ze += [layt[e[0]][2], layt[e[1]][2], None] trace1 = Scatter3d(x=Xe, y=Ye, z=Ze, mode='lines', line=Line(color='rgb(125,125,125)', width=1), hoverinfo='none') trace2 = Scatter3d(x=Xn, y=Yn, z=Zn, mode='markers+text', textposition='top', name='genes', marker=Marker(symbol='dot', size=6, color='#6959CD', colorscale='Viridis', line=Line(color='rgb(50,50,50)', width=1)), text=V, hoverinfo='text') #for node, adjacencies in enumerate(G.adjacency()): #trace2['marker']['color'].append(len(adjacencies)) #node_info = 'Number of connections: '+str(len(adjacencies)) #trace2['text'].append(node_info) axis = dict(showbackground=False, showline=False, zeroline=False, showgrid=False, showticklabels=False, title='') fig = Figure(data=Data([trace1, trace2]), layout=Layout( title="Network (3D visualization)", width=1000, height=1000, showlegend=False, scene=Scene( xaxis=XAxis(axis), yaxis=YAxis(axis), zaxis=ZAxis(axis), ), margin=Margin(t=100), hovermode='closest', annotations=Annotations([ Annotation(showarrow=False, text="", xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='bottom', font=Font(size=14)) ]), )) py.iplot(fig, filename='networkx3D')
def add_annotations(df, *args, **kwargs): if 'annotations' in df.plotly['layout']: df.plotly['layout']['annotations'].append(Annotation(*args, **kwargs)) else: df.plotly['layout']['annotations'] = [Annotation(*args, **kwargs)] return df
def treeplot(decision_tree): """""" if len(decision_tree.nodes) == 0: return '' g = igraph.Graph() g.add_vertices(len(decision_tree.nodes)) g.add_edges(decision_tree.edges) # Define posições no espaço para a árvore layout = g.layout_reingold_tilford(mode="out", root=[0], rootlevel=None) # Cria os vértices da árvore Xn = [-node[0] for node in layout] Yn = [-node[1] for node in layout] colors = [ '#333' if node['type'] == 'decision' else '#CACACA' for node in decision_tree.nodes ] nodes = Scatter(x=Xn, y=Yn, mode='markers', marker=dict(symbol='square', size=40, color=colors), text=None, hoverinfo='none') # Escreve rótulos nos vértices annotations = Annotations() for node in decision_tree.nodes: color = '#FFF' if node['type'] == 'decision' else '#000' label = node['label'] if node[ 'type'] == 'decision' else node['label'] + '?' a = Annotation(text=label, x=Xn[node['id']], y=Yn[node['id']], xref='x1', yref='y1', font=dict( color=color, size=9, family='"Open Sans", verdana, arial, sans-serif'), showarrow=False) annotations.append(a) # Cria as arestas da árvore Xe = [] Ye = [] for edge in decision_tree.edges: Xe += [Xn[edge[0]], Xn[edge[1]], None] Ye += [Yn[edge[0]], Yn[edge[1]], None] lines = Scatter(x=Xe, y=Ye, mode='lines+markers+text', line=dict(color='#333', width=2), hoverinfo='none') # Escreve rótulos nas arestas for edge in decision_tree.edges: label = decision_tree.nodes[edge[1]]['value'] X = (Xn[edge[0]] + Xn[edge[1]]) / 2 Y = (Yn[edge[0]] * 1.2 + Yn[edge[1]] * 0.8) / 2 a = Annotation(text=label, x=X, y=Y, xref='x1', yref='y1', font=dict( color='#333', size=14, family='"Open Sans", verdana, arial, sans-serif'), showarrow=False) annotations.append(a) # Cria gráfico data = Data([lines, nodes]) axis = dict(showline=False, zeroline=False, showgrid=False, showticklabels=False) layout = dict(title='', annotations=annotations, font=dict(size=12, family='"Open Sans", verdana, arial, sans-serif'), showlegend=False, xaxis=XAxis(axis), yaxis=YAxis(axis), margin=dict(l=0, r=0, b=0, t=0), hovermode='closest', paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)') fig = dict(data=data, layout=layout) return plot(fig, filename='Decision-Tree', output_type='div', include_plotlyjs=False)
def draw_plotly(model, met_names, pairs, fluxes): if not pairs: return G = nx.DiGraph() G.add_edges_from(pairs) if fluxes and type(list(fluxes.values())[0]) == tuple: fl = {} for i in fluxes: fl[i] = fluxes[i][1] producing = set(make_pairs.direct_producers(model, met_names, fl)) consuming = set(make_pairs.direct_consumers(model, met_names, fl)) else: producing = set(make_pairs.direct_producers(model, met_names, fluxes)) consuming = set(make_pairs.direct_consumers(model, met_names, fluxes)) val_map = { 'reaction': '#666699', 'metabolite': '#666699', 'producing': '#006600', 'both': '#663300', 'consuming': '#ff0000', 'observed': "#0066cc", 'path': '#ff66ff' } """ val_map = {'reaction': 'b', 'metabolite': 'c', 'producing': 'g', 'both': 'y', 'consuming': 'r', 'observed': 'm', 'path': 'm'} """ node_colors = [] for node in G.nodes(): if node in met_names: color = val_map['observed'] elif node in producing & consuming: color = val_map['both'] elif node in producing: color = val_map['producing'] elif node in consuming: color = val_map['consuming'] elif node in model.reactions: color = val_map['reaction'] else: color = val_map['metabolite'] node_colors.append(color) node_types = [ 'observed metabolites' if node in met_names else 'producing/consuming reactions' if node in producing & consuming else 'producing reactions' if node in producing else 'consuming reactions' if node in consuming else 'reactions' if node in model.reactions else 'metabolites' for node in G.nodes() ] #node_ids = {node:i for i, node in enumerate(G.nodes())} node_labels = {} for node in G.nodes(): if node in model.reactions: f = fluxes[node] if type(f) == tuple: node_labels[node] = node + " :" + "{:.3f}\n{:.3f}".format( round(f[0], 3), round(f[1], 3)) else: node_labels[node] = node + " :" + "{:.3f}".format( round(fluxes[node], 3)) else: node_labels[node] = node node_sizes = [20 if a in model.reactions else 10 for a in G.nodes()] #pos = nx.nx_pydot.graphviz_layout(G, prog="dot") pos = nx.fruchterman_reingold_layout(G) N = G.nodes() E = G.edges() Xv = [pos[k][0] for k in pos] Yv = [pos[k][1] for k in pos] labels = [node_labels[l] for l in node_labels] #full_labels = [model.reactions[a] if a in model.reactions else model.metabolites[a] for a in G.nodes()] full_labels = [] for i, node in enumerate(G.nodes()): if node in model.reactions: f = fluxes[node] if type(f) == tuple: full_labels.append( model.reactions[node] + " :" + "{:.3f}\n{:.3f}".format(round(f[0], 3), round(f[1], 3))) else: full_labels.append(model.reactions[node] + " :" + "{:.3f}".format(round(fluxes[node], 3))) else: full_labels.append(model.metabolites[node]) Xed = [] Yed = [] for edge in E: Xed += [pos[edge[0]][0], pos[edge[1]][0], None] Yed += [pos[edge[0]][1], pos[edge[1]][1], None] edge_trace = Scatter(x=Xed, y=Yed, mode='lines+markers', line=Line(color='rgb(210,210,210)', width=1), hoverinfo='none') traces = [edge_trace] """ node_trace = Scatter(x=Xv[1:3], y=Yv[1:3], mode='markers', name='dot', marker=Marker(symbol='dot', size=node_sizes[1:3], color= node_colors[1:3], line=Line(color='rgb(50,50,50)', width=0.5) ), text=labels[1:3], hoverinfo='text' ) traces.append(node_trace) node_trace = Scatter(x=Xv[3:], y=Yv[3:], mode='markers', name='dot', marker=Marker(symbol='dot', size=node_sizes[3:], color= node_colors[3:], line=Line(color='rgb(50,50,50)', width=0.5) ), text=labels[3:], hoverinfo='text' ) traces.append(node_trace) """ N_len = len(N) for i in range(N_len): traces.append( Scatter(x=[Xv[i]], y=[Yv[i]], mode='markers', name=node_types[i], marker=Marker(symbol='dot', size=[node_sizes[i]], color=[node_colors[i]], line=Line(color='rgb(50,50,50)', width=0.5)), text=[full_labels[i]], hoverinfo='text')) annot = "" #"This networkx.Graph has the Fruchterman-Reingold layout<br>Code:"+"<a href='http://nbviewer.ipython.org/gist/empet/07ea33b2e4e0b84193bd'> [2]</a>" annotations = Annotations([ Annotation( showarrow=False, text=labels[i], xref='x1', yref='y1', x=Xv[i], #+1, y=Yv[i], #+1, xanchor='left', yanchor='bottom', font=Font(size=14)) for i in range(N_len) ] + [ Annotation( showarrow=False, text=labels[0], xref='x1', yref='y1', x=Xv[0], #+1, y=Yv[0], #+1, xanchor='left', yanchor='bottom', font=Font(size=14)) ]) #print(annotations) axis = dict( showline=False, # hide axis line, grid, ticklabels and title zeroline=False, showgrid=False, showticklabels=False, title='') layout = Layout( title='', titlefont=dict(size=16), showlegend=False, #showlegend=True, hovermode='closest', margin=dict(b=20, l=5, r=5, t=40), annotations=annotations, xaxis=XAxis(axis), yaxis=YAxis(axis)) data1 = Data(traces) #Data([edge_trace, node_trace]) fig1 = Figure(data=data1, layout=layout) fig1['layout']['annotations'][0]['text'] = annot #py.iplot(fig1, filename='Coautorship-network-nx') plotly.offline.plot(fig1)
def plot_runners_teams_individual_distribution_according_to_running_type( df, title='Team/individual runners composition', runnings=None, team_column_name='profile'): ''' This function displays the distribution of participants according to their profiles (individual runners/runners in team) for the different runnings. Parameters - df: DataFrame containing data - title: Title of the graph (by default, 'Team/individual runners composition') - runnings: Dict containing name of column containing runnings (key: column_name) and set of runnings (key: values, value: dict() with following keys: name, color) By default, None. If None, default values will be set by function. - team_column_name: Name of column containing type of participants (by default, 'profile') Return - figure: Plotly figure ''' if not runnings: runnings = { 'column_name': 'distance (km)', 'values': OrderedDict([(10, { 'name': '10 km', 'color': KM_10_COLOR }), (21, { 'name': 'Semi-marathon', 'color': KM_21_COLOR }), (42, { 'name': 'Marathon', 'color': KM_42_COLOR })]) } data = [] annotations_texts = [] for key, attributes in runnings['values'].items(): filtered_df = df[df[runnings['column_name']] == key] nb_runners_running = len(filtered_df) x_values, y_values, texts = [[] for i in range(3)] for profile in filtered_df[team_column_name].unique(): x_values.append(profile) nb_runners = len( filtered_df[filtered_df[team_column_name] == profile]) y_values.append(nb_runners) texts.append('<b>' + attributes['name'] + '</b><br>' + profile.capitalize() + ' runners: ' + str(nb_runners) + ' (' + '{:.1f}%'.format(nb_runners * 100 / nb_runners_running) + ')') annotations_texts.append(attributes['name'] + ': ' + str(nb_runners_running) + ' runners') data.append( go.Bar(x=x_values, y=y_values, name=attributes['name'], text=texts, hoverinfo='text', marker={'color': attributes['color']})) annotations = [ Annotation(y=1.1, x=0, text=' | '.join(annotations_texts), xref='paper', yref='paper', showarrow=False) ] figure = study_utils.create_plotly_legends_and_layout( data, title=title, x_name='Composition', y_name='Number of runners', barmode='group', annotations=annotations) plotly.offline.iplot(figure) return figure
def generate_all_performance_figures(df, age_categories, sex_categories, performance_criteria): ''' This function generates all performance figures according sets of age categories and sex categories and a set of performance criteria. Final Dict has the following pattern: { <age_category_1: { <sex_1>: { <performance_criterion_1>: <Plotly figure> [, <performance_criterion_2>: <Plotly figure> , ...] } [, <sex_2>: { <performance_criterion_1>: <Plotly figure> [, <performance_criterion_2>: <Plotly figure> , ...] }, ...] } [, <age_category_2: { <sex_1>: { <performance_criterion_1>: <Plotly figure> [, <performance_criterion_2>: <Plotly figure> , ...] } [, <sex_2>: { <performance_criterion_1>: <Plotly figure> [, <performance_criterion_2>: <Plotly figure> , ...] }, ...] }, ...] } Parameters - df: DataFrame containing records about runners - age_categories: Array containing age categories to be displayed (if 'All'/'all', no filter is done on df) - sex_categories: Array containing sex categories to be displayed (if 'All'/'all', no filter is done on df) - performance_criteria: Array containing performance criteria to consider Return - figures: Dict containing all performance figures ''' # We define the considered runnings and the years interval, as colors for boxplots runnings = {10: '10 km', 21: 'Semi-marathon', 42: 'Marathon'} year_values = [year for year in df['year'].unique() if year] colors = {'10 km': KM_10_COLOR, 'Semi-marathon': KM_21_COLOR, 'Marathon': KM_42_COLOR} # We define options and the final Dict figures = {} default_options = {'title': 'Performance over years for runnings of Lausanne Marathon', 'x_name': 'Years', 'x_values': year_values, 'boxmode': 'group'} time_options = {'y_name': 'Time', 'y_type': 'date', 'y_format': '%H:%M:%S'} speed_options = {'y_name': 'Speed (m/s)'} time_options.update(default_options) speed_options.update(default_options) for age_category in age_categories: # We select data according to age category if age_category.lower() == 'all': data = df else: data = df[df['age category'] == age_category] figures[age_category] = {} for sex_category in sex_categories: # We select data according to sex category if sex_category.lower() == 'all': data_final = data else: data_final = data[data['sex'] == sex_category.lower()] figures[age_category][sex_category] = {} annotations = [Annotation(y=1.1, text='Age category: ' + age_category + ' Sex category: ' + sex_category + ' runners', xref='paper', yref='paper', showarrow=False)] # We create a figure for each performance criterion for performance_criterion in performance_criteria: criterion = performance_criterion.lower() boxplots = study_utils.create_plotly_boxplots(data=data_final, x='year', y=criterion, hue='distance (km)', hue_names=runnings, colors=colors) if criterion == 'time': figure = study_utils.create_plotly_legends_and_layout(data=boxplots, **time_options, annotations=annotations) elif criterion == 'speed (m/s)': figure = study_utils.create_plotly_legends_and_layout(data=boxplots, **speed_options, annotations=annotations) else: # By default, two specific criteria are allowed: 'time' and 'speed (m/s)'. If any other criterion is provided, we throw an exception. raise ValueError('Invalid performance criterion encountered. Performance criterion must be either \'Time\' or \'Speed (m/s)\'') figures[age_category][sex_category][performance_criterion] = figure return figures
def connectivity(soln, sc, offset=False): """Summary Args: c (TYPE): Description arr (TYPE): Description dep (TYPE): Description rs (TYPE): Description ss (TYPE): Description o (TYPE): Description offset (int, optional): Description Returns: TYPE: Description """ traces = [] t = 0 ticktext = [] x_arr = [] y_arr = [] r_arr = [] text_arr = [] x_dep = [] y_dep = [] r_dep = [] text_dep = [] for r, v1 in soln.c.iteritems(): for rp, v2 in v1.iteritems(): for s, cs in v2.iteritems(): station_r = sc.routes[r].stops.index(s) station_rp = sc.routes[rp].stops.index(s) x_arr += [a for a in soln.a[r][station_r]] y_arr += [t] * len(soln.a[r][station_r]) r_arr += [r] * len(soln.a[r][station_r]) for k in range(sc.routes[r].n_buses): kp = min([ kp if cs[k][kp] == 1 else 999 for kp in range(sc.routes[rp].n_buses) ]) if kp == 999: text_arr.append('<b>Arrival</b>: %s<br>' '<b>Connects</b>: NC' % soln.a[r][station_r][k]) else: text_arr.append( '<b>Arrival</b>: %s<br>' '<b>Connects</b>: %s-%s' % (soln.a[r][station_r][k], sc.routes[rp].name, kp)) x_dep += [d for d in soln.d[rp][station_rp]] y_dep += [t] * sc.routes[rp].n_buses r_dep += [rp] * sc.routes[rp].n_buses text_dep += [ '{}-{}'.format(sc.routes[rp].name, kp) for kp in range(sc.routes[rp].n_buses) ] ticktext.append('%s-%s (%s)' % (sc.routes[r].name, sc.routes[rp].name, s)) t += 1 traces.append( Scatter(x=x_arr, y=[-y - offset for y in y_arr], mode='markers', marker=dict(size=20, color=[colors[r] for r in r_arr]), hoverinfo='text', text=text_arr, showlegend=False)) traces.append( Scatter(x=x_dep, y=[-y - offset for y in y_dep], mode='markers', marker=dict(symbol='triangle-right', size=10, color=[colors[r] for r in r_dep]), hoverinfo='text', text=text_dep, showlegend=False)) if type(offset) is bool: for route in sc.routes: traces.append( Scatter(x=[sc.plan_horizon * 2, sc.plan_horizon * 2], y=[route.id, route.id], mode='lines+markers', marker=dict(size=10, color=[colors[route.id], colors[route.id]]), line=dict(width=5, color=colors[route.id]), showlegend=True, name='Route %s' % route.name)) lo = alter_layout( dict( hovermode='closest', height=90 + 30 + 30 * (t + 1), yaxis=dict(zeroline=False, showgrid=False, tickvals=[-x for x in range(t)], ticktext=ticktext, showticklabels=True, autorange=False, range=[-t, 1]), xaxis=dict( autorange=False, range=[-1, sc.plan_horizon + 1], showgrid=True, # autotick=False, dtick=10, showticklabels=True), annotations=Annotations([ Annotation(x=0.5, y=-85.0 / 30 / (t + 1), showarrow=False, text='%s_%s_%s' % (soln.model.timestamp, soln.model.__class__.__name__, sc.name), align='center', xref='paper', yref='paper', font=dict(family='SF Mono', size=11, color='rgba(0,0,0,.2)')) ]))) return Figure(data=traces, layout=lo) else: return traces, zip([-y - offset for y in range(t)], ticktext)
def draw3Dnx(graph=None, out_path=None, perc_threshold=None, positions_array=None, positions_dict=None, plot_title='', plot_description='', colorscale='Set3', notebook_mode=True, auto_open=False): """Draws a given graph in 3D""" if graph is None or nx.is_empty(graph): raise ValueError('input graph can not be empty!') # graph = nx.random_geometric_graph(200, 0.05) if notebook_mode: init_notebook_mode() marker_size = 7 marker_edge_width = 2 link_width = 2 colorbar_title = 'Node Connections' hover_description = '# connections: ' position_attr = ['x', 'y', 'z'] if positions_array is not None: for node in graph.nodes(): for ix, attr in enumerate(position_attr): graph.nodes[node][attr] = positions_array[node][ix] elif positions_dict is not None: for node in graph.nodes(): for attr in position_attr: graph.nodes[node][attr] = positions_array[node][attr] for attr in position_attr: if not nx.get_node_attributes(graph, attr): raise ValueError( 'Position attribute {} missing. ' 'Add it to graph or supply with one of the position inputs'. format(attr)) edge_threshold = -np.Inf if perc_threshold is not None: eval_distr = np.array( list(nx.get_edge_attributes(graph, 'weight').values())) try: edge_threshold = np.percentile(eval_distr, perc_threshold) except: print('threshold to prune edges can not be determined.') traceback.print_exc() return edge_trace = Scatter3d( x=[], y=[], z=[], mode='lines', line=Line(width=marker_edge_width, color='#888'), hoverinfo='none', ) def get_position(gnode): """Helper to retun the x, y, z coords of a node""" return gnode['x'], gnode['y'], gnode['z'] for src, dest in graph.edges(): # adding only the strongest edges if perc_threshold is None or graph.get_edge_data( src, dest)['weight'] > edge_threshold: x0, y0, z0 = get_position(graph.nodes[src]) # ['position'] x1, y1, z1 = get_position(graph.nodes[dest]) # ['position'] edge_trace['x'].extend([x0, x1, None]) edge_trace['y'].extend([y0, y1, None]) edge_trace['z'].extend([z0, z1, None]) # empty lists here will be appended with data to be plotted node_trace = Scatter3d(x=[], y=[], z=[], text=[], mode='markers', hoverinfo='text', marker=Marker( symbol='dot', showscale=True, colorscale=colorscale, reversescale=True, color=[], size=marker_size, colorbar=dict(thickness=15, title=colorbar_title, xanchor='left', titleside='right'), )) # setting nodal positions and info for ix, node in enumerate(graph.nodes()): x, y, z = get_position(graph.nodes[node]) node_trace['x'].append(x) node_trace['y'].append(y) node_trace['z'].append(z) node_trace['text'].append(node) node_trace['marker']['color'].append(ix) axis = dict(showbackground=False, showline=False, zeroline=False, showgrid=False, showticklabels=False, title='') scene = Scene(xaxis=XAxis(axis), yaxis=YAxis(axis), zaxis=ZAxis(axis)) annotations = Annotations([ Annotation( showarrow=False, text=plot_description, xref='paper', yref='paper', x=0, y=0.1, # z=0.05, xanchor='left', yanchor='bottom', # zanchor='bottom', font=Font(size=14)) ]) layout = Layout( title=plot_title, titlefont=dict(size=16), # width=1000, # height=1000, showlegend=False, hovermode='closest', scene=scene, margin=Margin(t=100), # margin=dict(b=20,l=5,r=5,t=40), annotations=annotations, ) fig_data = Data([edge_trace, node_trace]) fig = Figure(data=fig_data, layout=layout) if out_path is None and auto_open is False: auto_open = True if notebook_mode: iplot(fig, filename=out_path) else: plot(fig, filename=out_path, auto_open=auto_open) return fig
def trip(soln, sc, offset=False): """Summary Args: gr (TYPE): Description p (TYPE): Description arr (TYPE): Description dep (TYPE): Description stations (TYPE): Description omega (TYPE): Description alpha (TYPE): Description offset (int, optional): Description Returns: TYPE: Description """ traces = [] for group in sc.groups: sp = group.origin for r, rp, s in zip(group.routes[:-1], group.routes[1:], group.stops): station_r = r.stops.index(s) station_rp = rp.stops.index(s) for k, kp in [(k, kp) for k in range(r.n_buses) for kp in range(rp.n_buses)]: if soln.p[r.id][rp.id][s][k][kp][group.id] == 1: traces.append( Scatter( x=[ soln.d[r.id][r.stops.index(sp)][k], soln.a[r.id][station_r][k] ], y=[-offset - group.id, -offset - group.id], mode='lines+markers', marker=dict(size=10, color=[colors[r.id], colors[r.id]]), line=dict(width=5, color=colors[r.id]), hoverinfo='text', text=[ '<b>Bus</b>: R%s-%s<br><b>Departs</b>: %s' % (r.id, k, soln.d[r.id][r.stops.index(sp)][k]), '<b>Bus</b>: R%s-%s<br><b>Arrives</b>: %s' '<br><b>Transfer time</b>: %s' % (r.id, k, soln.a[r.id][station_r][k], sc.omega[r.id][s][rp.id]) ], showlegend=False)) break sp = s traces.append( Scatter(x=[ soln.d[rp.id][rp.stops.index(sp)][kp], min(soln.d[rp.id][rp.stops.index(sp)][kp] + 5, sc.plan_horizon) ], y=[-offset - group.id, -offset - group.id], mode='lines', line=dict(width=5, color=colors[rp.id], dash="dot"), hoverinfo='text', text=[ '<b>Bus</b>: R%s-%s<br><b>Departs</b>: %s' % (rp.id, kp, soln.d[rp.id][rp.stops.index(sp)][kp]), '' ], showlegend=False)) traces.append( Scatter(x=[soln.d[rp.id][rp.stops.index(sp)][kp]], y=[-offset - group.id], mode='markers', marker=dict(size=10, color=[colors[rp.id], colors[rp.id]]), hoverinfo='text', text=[ '<b>Bus</b>: R%s-%s<br><b>Departs</b>: %s' % (rp.id, kp, soln.d[rp.id][rp.stops.index(sp)][kp]), '' ], showlegend=False)) traces.append( Scatter(x=[g.alpha for g in sc.groups], y=[-y - offset for y in range(len(sc.groups))], mode='markers', marker=dict(size=10, color=[0] * len(sc.groups), colorscale='RdBu', cmin=-1, cmax=1), hoverinfo='text', showlegend=False)) if type(offset) is bool: for route in sc.routes: traces.append( Scatter(x=[sc.plan_horizon * 2, sc.plan_horizon * 2], y=[route.id, route.id], mode='lines+markers', marker=dict(size=10, color=[colors[route.id], colors[route.id]]), line=dict(width=5, color=colors[route.id]), showlegend=True, name='Route %s' % route.name)) lo = alter_layout( dict( hovermode='closest', height=90 + 30 + 30 * (len(sc.groups) + 1), yaxis=dict( zeroline=False, showgrid=False, tickvals=[-x for x in range(len(sc.groups))], # ticktext=[('G%d (' % (g.id, )) + '-'.join([g.origin] + g.stops) + '-)' # for g in sc.groups], ticktext=[ '-'.join([g.origin] + g.stops) + '-S6' for g in sc.groups ], showticklabels=True, autorange=False, range=[-len(sc.groups), 1]), xaxis=dict( autorange=False, range=[-1, sc.plan_horizon + 1], showgrid=True, # autotick=False, dtick=10, showticklabels=True), annotations=Annotations([ Annotation(x=0.5, y=-85.0 / 30 / (len(sc.groups) + 1), showarrow=False, text='%s_%s_%s' % (soln.model.timestamp, soln.model.__class__.__name__, sc.name), align='center', xref='paper', yref='paper', font=dict(family='SF Mono', size=11, color='rgba(0,0,0,.2)')) ]))) return Figure(data=traces, layout=lo) else: return (traces, zip([-y - offset for y in range(len(sc.groups))], [('G%d (' % (g.id, )) + '-'.join([g.origin] + g.stops) + '-)' for g in sc.groups]))
prediction = slope * (xi[-1]) + intercept # Creating the dataset, and generating the plot trace1 = Scatter(x=xi, y=y, mode='lines', marker=Marker(color='rgb(255, 127, 14)'), name='Data') trace2 = Scatter(x=[xi[-1] + 1], y=[prediction], mode='markers', marker=Marker(color='rgb(31, 119, 180)'), name='Fit') annotation = Annotation(x=3.5, y=23.5, text='$R^2 = 0.9551,\\Y = 0.716X + 19.18$', showarrow=False, font=Font(size=16)) layout = Layout(title='Linear Fit in Python', plot_bgcolor='rgb(229, 229, 229)', xaxis=XAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), yaxis=YAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), annotations=[annotation]) to_scatter = [trace1, trace2] plotly.offline.plot({"data": to_scatter, "layout": layout})
def comprehensive(soln, sc): traces_all = [] ticks_all = [] n_routes = 0 n_transfers = 0 trs, tks = schedule(soln, sc, offset=0) traces_all += trs ticks_all += tks n_routes = len(tks) trs, tks = connectivity(soln, sc, offset=len(ticks_all) + 1) traces_all += trs ticks_all += tks n_transfers = len(tks) trs, tks = trip(soln, sc, offset=len(ticks_all) + 2) traces_all += trs ticks_all += tks traces_all.append( Scatter(x=[0, sc.plan_horizon, None, 0, sc.plan_horizon], y=[ -n_routes, -n_routes, None, -n_transfers - n_routes - 1, -n_transfers - n_routes - 1 ], mode='lines', line=dict(color='rgba(100,100,100,.3)', width=2, dash='dot'), showlegend=False)) traces_all.append( Scatter(x=[sc.plan_horizon - 14] * 3, y=[0, -n_routes - 1, -n_routes - n_transfers - 2], mode='text', text=['<b>Schedule</b>', '<b>Transfers</b>', '<b>Trips</b>'], textposition='middlright', textfont={ 'size': 13, 'color': '#333333' }, showlegend=False)) for route in sc.routes: traces_all.append( Scatter(x=[sc.plan_horizon * 2, sc.plan_horizon * 2], y=[route.id, route.id], mode='lines+markers', marker=dict(size=10, color=[colors[route.id], colors[route.id]]), line=dict(width=5, color=colors[route.id]), showlegend=True, name='Route %s' % route.name)) lo = alter_layout( dict( hovermode='closest', height=90 + 30 + 30 * (len(ticks_all) + 3), yaxis=dict(zeroline=False, showgrid=False, tickvals=zip(*ticks_all)[0], ticktext=zip(*ticks_all)[1], showticklabels=True, autorange='False', range=[-len(ticks_all) - 2, 1]), xaxis=dict( autorange=False, range=[-1, sc.plan_horizon + 1], showgrid=True, # autotick=False, dtick=10, showticklabels=True), annotations=Annotations([ Annotation(x=0.5, y=-85.0 / 30 / (len(ticks_all) + 3), showarrow=False, text='%s_%s_%s' % (soln.model.timestamp, soln.model.__class__.__name__, sc.name), align='center', xref='paper', yref='paper', font=dict(family='SF Mono', size=11, color='rgba(0,0,0,.2)')) ]))) return Figure(data=traces_all, layout=lo)
yaxis='y1') ]), layout=Layout(width=640, height=480, autosize=False, margin=Margin(l=80, r=63, b=47, t=47, pad=0), hovermode='closest', showlegend=False, annotations=Annotations([ Annotation(x=0.000997987927565, y=0.996414507772, text='top-left', xref='paper', yref='paper', showarrow=False, align='left', font=Font(size=12.0, color='#000000'), opacity=1, xanchor='left', yanchor='top'), Annotation(x=0.000997987927565, y=0.00358549222798, text='bottom-left', xref='paper', yref='paper', align='left', showarrow=False, font=Font(size=12.0, color='#000000'), opacity=1,
def GeneRegulationNetwork(self, netthreshold, config, netconfig): # Transpose the dataframe to get correct format to create the network dfT = self.dfz.transpose() # Get all the TF Gene names tf_names = list(dfT) # Create a Dask Client, just in case we want parellalize the algorithm client = Client(processes=False) # create dataframe network with columns --> TF, target Gene, Importance if netconfig == 1: network = grnboost2(expression_data=dfT, tf_names=tf_names, client_or_address=client) print("grnboost2") else: network = genie3(expression_data=dfT, tf_names=tf_names, client_or_address=client) # We put a threshold because we have a lot of conections and we want to obtain a clear graph with the most representatives conected genes limit = network.index.size * netthreshold G = nx.from_pandas_edgelist(network.head(int(limit)), 'TF', 'target', ['importance'], create_using=nx.Graph(directed=False)) N = len(list(G.node())) # number of genes nodes V = list(G.node()) # list of genes nodes Edges = list(G.edges()) layt = { 1: nx.fruchterman_reingold_layout(G, dim=3), 2: nx.circular_layout(G, dim=3) }.get(config, nx.circular_layout(G, dim=3)) laytN = list(layt.values()) Xn = [laytN[k][0] for k in range(N)] # x-coordinates of nodes Yn = [laytN[k][1] for k in range(N)] # y-coordinates Zn = [laytN[k][2] for k in range(N)] # z-coordinates Xe = [] Ye = [] Ze = [] for e in Edges: Xe += [layt[e[0]][0], layt[e[1]][0], None] # x-coordinates of edge ends Ye += [layt[e[0]][1], layt[e[1]][1], None] Ze += [layt[e[0]][2], layt[e[1]][2], None] trace1 = Scatter3d(x=Xe, y=Ye, z=Ze, mode='lines', line=Line(color='rgb(125,125,125)', width=1), hoverinfo='none') trace2 = Scatter3d(x=Xn, y=Yn, z=Zn, mode='markers+text', textposition='top center', name='genes', marker=Marker(symbol='circle', size=3, color='#6959CD', colorscale='Viridis', line=Line(color='rgb(50,50,50)', width=1)), text=V, hoverinfo='text') axis = dict(showbackground=False, showline=False, zeroline=False, showgrid=False, showticklabels=False, title='') fig = Figure(data=Data([trace1, trace2]), layout=Layout( title="Gene Regulatory Network", width=1000, height=1000, showlegend=False, scene=Scene( xaxis=XAxis(axis), yaxis=YAxis(axis), zaxis=ZAxis(axis), ), margin=Margin(t=100), hovermode='closest', annotations=Annotations([ Annotation(showarrow=False, text="Khaos Research Group", xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='bottom', font=Font(size=20)) ]), )) plotly.offline.plot(fig, filename='3DNetworkx_.html', auto_open=True) script = plot(fig, output_type='div', include_plotlyjs=False, show_link=True) #print(script) return script