def test_update_list_make_copies_true(): trace1 = Scatter(x=[1, 2, 3], y=[2, 1, 2]) trace2 = Scatter(x=[1, 2, 3], y=[3, 2, 1]) data = Data([trace1, trace2]) update = dict(x=[2, 3, 4], y=[1, 2, 3], line=Line()) data.update(update, make_copies=True) assert data[0]['line'] is not data[1]['line']
def test_update_list(): trace1 = Scatter(x=[1, 2, 3], y=[2, 1, 2]) trace2 = Scatter(x=[1, 2, 3], y=[3, 2, 1]) data = Data([trace1, trace2]) update = dict(x=[2, 3, 4], y=[1, 2, 3]) data.update(update) assert data[0] == Scatter(x=[2, 3, 4], y=[1, 2, 3]) assert data[1] == Scatter(x=[2, 3, 4], y=[1, 2, 3])
def test_update_list_empty(): trace1 = Scatter(x=[1, 2, 3], y=[2, 1, 2]) trace2 = Scatter(x=[1, 2, 3], y=[3, 2, 1]) data = Data([trace1, trace2]) data.update([]) print(data.to_string()) assert data[0] == Scatter(x=[1, 2, 3], y=[2, 1, 2]) assert data[1] == Scatter(x=[1, 2, 3], y=[3, 2, 1])
def test_flatten_repeated_trace_names(self): dl = Data([Scatter(name='thesame', x=[1, 2, 3]) for _ in range(3)]) data = dl.get_data(flatten=True) comp_data = { 'thesame.x': [1, 2, 3], 'thesame_1.x': [1, 2, 3], 'thesame_2.x': [1, 2, 3] } self.assertEqual(data, comp_data)
def test_validate(): data = Data() data.validate() data += [{'type': 'scatter'}] data.validate() data += [{}, {}, {}] data.validate()
def make_report_plotly(counts, suffix, report): for count in counts: run_test( report, count ) for category, results in report.iteritems(): #formatter = results['formatter'] scale = results['scale'] unit = results['unit'] bars = [] for name, values in results.iteritems(): if name in ['title', 'scale', 'unit']: continue #values = map(formatter, map(lambda x: x * scale, values)) values = map(lambda x: x * scale, values) bar = Bar( x=counts, y=values, name=name ) bars.append(bar) layout = Layout(title=results['title'], font=Font(family='Raleway, sans-serif'), showlegend=True, barmode='group', bargap=0.15, bargroupgap=0.1, legend=Legend(x=0, y=1.0), xaxis=XAxis(title='Num Sites', type='category'), yaxis=YAxis(title=results['unit']) ) data = Data(bars) fig = Figure(data=data, layout=layout) outpath = '../images/%s%s.png' % (category, suffix) py.image.save_as(fig, outpath) print "Wrote", outpath
def test_figure_json_encoding(self): df = pd.DataFrame(columns=["col 1"], data=[1, 2, 3]) s1 = Scatter3d(x=numeric_list, y=np_list, z=mixed_list) s2 = Scatter(x=df["col 1"]) data = Data([s1, s2]) figure = Figure(data=data) js1 = _json.dumps(s1, cls=utils.PlotlyJSONEncoder, sort_keys=True) js2 = _json.dumps(s2, cls=utils.PlotlyJSONEncoder, sort_keys=True) assert ( js1 == '{"type": "scatter3d", "x": [1, 2, 3], ' '"y": [1, 2, 3, null, null, null, "2014-01-05T00:00:00"], ' '"z": [1, "A", "2014-01-05T00:00:00", ' '"2014-01-05T01:01:01", "2014-01-05T01:01:01.000001"]}' ) assert js2 == '{"type": "scatter", "x": [1, 2, 3]}' # Test JSON encoding works _json.dumps(data, cls=utils.PlotlyJSONEncoder, sort_keys=True) _json.dumps(figure, cls=utils.PlotlyJSONEncoder, sort_keys=True) # Test data wasn't mutated np_array = np.array([1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)]) for k in range(len(np_array)): if k in [3, 4]: # check NaN assert np.isnan(np_list[k]) and np.isnan(np_array[k]) else: # non-NaN assert np_list[k] == np_array[k] assert set(data[0]["z"]) == set( [ 1, "A", dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1), dt(2014, 1, 5, 1, 1, 1, 1), ] )
def plot(self): """ Plot the figure. Call this last.""" traces = [] for dataset in self.xy_datasets: kwargs = {} if 'name' in dataset and dataset['name'] is not None: kwargs['name'] = dataset['name'] else: kwargs['showlegend'] = False traces.append(Scatter( x = dataset['x'], y = dataset['y'], mode = 'lines', **kwargs )) data = Data(traces) plotly.offline.iplot({ 'data': data, 'layout': self.makeLayout() })
def test_spacing(): expected = Figure(data=Data(), layout=Layout( xaxis1=XAxis(domain=[0.0, 0.3], anchor='y1'), xaxis2=XAxis(domain=[0.35, 0.6499999999999999], anchor='y2'), xaxis3=XAxis(domain=[0.7, 1.0], anchor='y3'), xaxis4=XAxis(domain=[0.0, 0.3], anchor='y4'), xaxis5=XAxis(domain=[0.35, 0.6499999999999999], anchor='y5'), xaxis6=XAxis(domain=[0.7, 1.0], anchor='y6'), yaxis1=YAxis(domain=[0.0, 0.45], anchor='x1'), yaxis2=YAxis(domain=[0.0, 0.45], anchor='x2'), yaxis3=YAxis(domain=[0.0, 0.45], anchor='x3'), yaxis4=YAxis(domain=[0.55, 1.0], anchor='x4'), yaxis5=YAxis(domain=[0.55, 1.0], anchor='x5'), yaxis6=YAxis(domain=[0.55, 1.0], anchor='x6'))) fig = tls.get_subplots(2, 3, horizontal_spacing=.05, vertical_spacing=.1) assert fig == expected
def plotConfusionMatrix(self, data, normalize=True): """ Plots the confusion matrix of the input dataframe. @param data (pandas DF) The confusion matrix. @param normalize (bool) True will normalize the confusion matrix values for the total number of actual classifications per label. Thus the cm values are 0.0 to 1.0. """ xyzData = self.interpretConfusionMatrixData(data, normalize) data = Data([ Heatmap(z=xyzData["z"], x=xyzData["x"], y=xyzData["y"], colorscale='YIGnBu') ]) layout = Layout( title='Confusion matrix for ' + self.experimentName, xaxis=XAxis(title='Predicted label', side='top', titlefont=Font(family='Courier New, monospace', size=18, color='#7f7f7f')), yaxis=YAxis(title='True label', titlefont=Font(family='Courier New, monospace', size=18, color='#7f7f7f'), autorange='reversed'), barmode='overlay', autosize=True, width=1000, height=1000, margin=Margin(l=200, r=80, b=80, t=450)) fig = Figure(data=data, layout=layout) plot_url = py.plot(fig) print "Confusion matrix URL: ", plot_url
def plot_macd(day_csv, short_period, long_period, remote=False): day_rb = pd.read_csv(day_csv, index_col=0) day_rb.index = pd.DatetimeIndex(day_rb.index) day_rb = day_rb['close'] index = list(range(day_rb.shape[0])) macd = moving_average_convergence_divergence(day_rb.values, short_period, long_period) t = {"x": index, "y": day_rb, "name": 'origin_close', "type": "scatter"} t1 = { "x": index, "y": macd, "name": '%s_%s_%s_days' % ('MACD', long_period, short_period), "type": "scatter" } data = Data([t, t1]) layout = {"xaxis": {"tickangle": 30}} fig = Figure(data=data, layout=layout) if remote: url = plotly.plotly.plot(fig, filename='MACD.html') print('url: ', url) else: plotly.offline.plot(fig, filename='MACD.html')
def plot(traces): scatterObjs = [] #make scatter objects for trace in traces: #print trace.getName(), la_branches if int(trace.getName()) in la_branches: scatterObjs.append( Scatter( x=trace.getUpdate(), y=trace.getNoApptValue(), mode='lines', #name=branch_names[trace.getName()] name=getOfficeName(trace.getName( )) #read these from file. TODO: rename trace.getName? )) data = Data(scatterObjs) layout = Layout( title='Central LA DMV Non-Apointment Wait Times: ' + datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d')) fig = Figure(data=data, layout=layout) #TODO: trap plotly ConnectionError x = py.plot(fig, filename='DMV', auto_open=False)
def render(self): """ Plot the figure. Call this last.""" traces = [] for dataset in self.xy_datasets: kwargs = {} if 'name' in dataset and dataset['name'] is not None: kwargs['name'] = dataset['name'] self.zindex = kwargs['name'] else: kwargs['showlegend'] = False zvals = np.full(np.size(dataset['x']), self.zindex) traces.append( Scatter3d(x=dataset['x'], z=dataset['y'], y=zvals, mode='lines', **kwargs)) if not isinstance(self.zindex, str): self.zindex += 1 data = Data(traces) plotly.offline.iplot({'data': data, 'layout': self.makeLayout()})
def make_chart(ticker): from plotly.graph_objs import Scatter, Data, Line import requests from datetime import datetime import pandas as pd ticker = ticker.upper() url = 'https://min-api.cryptocompare.com/data/histoday?fsym={}&tsym=USDT&limit=119&aggregate=1'.format(ticker) response = requests.get(url) obj = response.json()['Data'] df = pd.DataFrame(obj, columns = ['time', 'low', 'high', 'open', 'close', 'volume']) df.sort_values(by = ['time'], inplace = True) dates = df['time'].map(datetime.fromtimestamp) df.loc[:, 'time'] = dates df.loc[:, 'moving'] = df['close'].rolling(window = 20).mean() labels = dates.map('{:%Y-%m-%d}'.format)[19:].values price = Scatter(x= labels, y = df.loc[19:, 'close'], line = Line(width = 2, color = 'blue'), name = ticker) moving = Scatter(x= labels, y = df.loc[19:, 'moving'], line = Line(width = 2, color = 'orange'), name = '20 Day Moving Avg') data = Data([price, moving]) return data
def test_append_scatter(): expected = Figure( data=Data([Scatter(x=[1, 2, 3], y=[2, 3, 4], xaxis='x5', yaxis='y5')]), layout=Layout( xaxis1=XAxis(domain=[0.0, 0.2888888888888889], anchor='y1'), xaxis2=XAxis(domain=[0.35555555555555557, 0.6444444444444445], anchor='y2'), xaxis3=XAxis(domain=[0.7111111111111111, 1.0], anchor='y3'), xaxis4=XAxis(domain=[0.0, 0.2888888888888889], anchor='y4'), xaxis5=XAxis(domain=[0.35555555555555557, 0.6444444444444445], anchor='y5'), xaxis6=XAxis(domain=[0.7111111111111111, 1.0], anchor='y6'), yaxis1=YAxis(domain=[0.575, 1.0], anchor='x1'), yaxis2=YAxis(domain=[0.575, 1.0], anchor='x2'), yaxis3=YAxis(domain=[0.575, 1.0], anchor='x3'), yaxis4=YAxis(domain=[0.0, 0.425], anchor='x4'), yaxis5=YAxis(domain=[0.0, 0.425], anchor='x5'), yaxis6=YAxis(domain=[0.0, 0.425], anchor='x6'))) trace = Scatter(x=[1, 2, 3], y=[2, 3, 4]) fig = tls.make_subplots(rows=2, cols=3) fig.append_trace(trace, 2, 2) assert fig == expected
def initLy(self): now = dt.datetime.now() self.year, self.month, self.day = now.year, now.month, now.day for i in range(len(self.streams)): # Configure streams self.streams[i] = py.Stream(self.stream_ids[i]) # Initialize traces self.traces[i] = Scatter(x=[], y=[], name=self.tlabels[i], stream=dict(token=self.stream_ids[i])) self.data = Data(self.traces) self.fig = Figure(data=self.data) self.unique_url = py.plot(self.fig, filename='T80S_TM_%04i%02i%02i' % (self.year, self.month, self.day)) # ,fileopt='extended') self.openStreams()
def plot_vectors(days, peak="PM"): _scaler = 6 config_plotly() color_scale = colorlover.scales['5']['qual']['Dark2'] data = list() top_size = days['7'].max() for index, row in days.iterrows(): desc = str(index) + " cluster_id" + str(row['cluster_id']) data.append( Scattermapbox( lat=[row["Start_Lat_mean" + peak], row["End_Lat_mean" + peak]], lon=[row["Start_Lon_mean" + peak], row["End_Lon_mean" + peak]], mode='lines', text=desc, name=desc, opacity=0.7, line=Line(width=int(_scaler * row['7'] / top_size), color=color_scale[int(row['cluster_id'])]))) data = Data(data) layout = Layout( showlegend=False, autosize=True, hovermode='closest', mapbox=dict(accesstoken=PLOTLY_API_KEY, bearing=0, center=dict(lat=row["Start_Lat_mean" + peak], lon=row["Start_Lon_mean" + peak]), pitch=0, zoom=11), ) fig = dict(data=data, layout=layout) py.plot(fig, filename='Vectors ' + peak)
def test_append_scatter3d(): expected = Figure( data=Data([ Scatter3d(x=[1, 2, 3], y=[2, 3, 4], z=[1, 2, 3], scene="scene1"), Scatter3d(x=[1, 2, 3], y=[2, 3, 4], z=[1, 2, 3], scene="scene2"), ]), layout=Layout( scene1=Scene(domain={ "y": [0.575, 1.0], "x": [0.0, 1.0] }), scene2=Scene(domain={ "y": [0.0, 0.425], "x": [0.0, 1.0] }), ), ) fig = tls.make_subplots(rows=2, cols=1, specs=[[{ "is_3d": True }], [{ "is_3d": True }]]) trace = Scatter3d(x=[1, 2, 3], y=[2, 3, 4], z=[1, 2, 3]) fig.append_trace(trace, 1, 1) fig.append_trace(trace, 2, 1) d1, d2 = strip_dict_params(fig["data"][0], expected["data"][0]) assert d1 == d2 d1, d2 = strip_dict_params(fig["data"][1], expected["data"][1]) assert d1 == d2 d1, d2 = strip_dict_params(fig["layout"], expected["layout"]) assert d1 == d2
def plotCumulativeAccuracies(self, classificationAccuracies, trainSize): """ Creates scatter plots that show the accuracy for each category at a certain training size @param classificationAccuracies (dict) Maps a category label to a list of lists of accuracies. Each item in the key is a list of accuracies for a specific training size, ordered by increasing training size. @param trainSize (list) Sizes of training sets for trials. """ # Convert list of list of accuracies to list of means classificationSummaries = [ (label, map(numpy.mean, acc)) for label, acc in classificationAccuracies.iteritems() ] data = [] sizes = sorted(set(trainSize)) for label, summary in classificationSummaries: data.append(Scatter(x=sizes, y=summary, name=label)) data = Data(data) layout = Layout( title='Cumulative Accuracies for ' + self.experimentName, xaxis=XAxis(title='Training size', titlefont=Font(family='Courier New, monospace', size=18, color='#7f7f7f')), yaxis=YAxis(title='Accuracy', titlefont=Font(family='Courier New, monospace', size=18, color='#7f7f7f'))) fig = Figure(data=data, layout=layout) plot_url = py.plot(fig) print "Cumulative Accuracies URL: ", plot_url
def plot_cluster(days, param="End", peak="AM", marker_size_scale=30): config_plotly() color_scale = colorlover.scales['5']['qual']['Set1'] data = list() top_size = days['7'].max() for cluster in days.cluster_id.unique(): data.append( Scattermapbox( lat=days[days.cluster_id == cluster][param + "_Lat_mean" + peak], lon=days[days.cluster_id == cluster][param + "_Lon_mean" + peak], mode='markers', marker=Marker(size=days[days.cluster_id == cluster]['7'] / top_size * marker_size_scale, color=color_scale[cluster]), text="CL: " + str(cluster), hoverinfo='text')) data = Data(data) layout = Layout( title='Clusters centres of gravity:' + param + peak, autosize=True, hovermode='closest', showlegend=False, mapbox=dict(accesstoken=PLOTLY_API_KEY, bearing=0, center=dict(lat=40, lon=-73), pitch=0, zoom=7, style='light'), ) fig = dict(data=data, layout=layout) py.plot(fig, filename='Clusters:' + param + peak)
def plot_frame(dataframe, uname=None, api_key=None, mode='lines', line={}): try: import plotly.plotly as ply from plotly.graph_objs import Scatter, Layout, Data, Figure except ImportError: raise InvalidOperationException("Please install the Python " "plotly bindings") if uname and api_key: ply.sign_in(uname, api_key) c1 = dataframe.ch1 c2 = dataframe.ch2 x = list(range(len(c1))) t1 = Scatter(x=x, y=c1, mode=mode, line=line) t2 = Scatter(x=x, y=c2, mode=mode, line=line) layout = Layout(title="Moku:Lab Frame Grab") data = Data([t1, t2]) fig = Figure(data=data, layout=layout) return ply.plot(fig)
def graph_tots(df, column, column_name, user, pl): import pandas as pd from plotly.graph_objs import Scatter, Data, Line, Figure from datetime import datetime ts = df[column] * user.get_mult() #To guarentee the most recent amount matches the PL display if column == 'tpl': date = datetime.now().strftime('%Y%m%d%H%M%S%f') ts = ts.append(pd.Series([pl.tpl], index=[date])) dates = pd.to_datetime(ts.index, format='%Y%m%d%H%M%S%f') price = Scatter(x=dates, y=ts, line=Line(width=2, color='blue'), name=column_name) layout = dict(title=column_name + ' History') data = Data([price]) fig = Figure(data=data, layout=layout) return fig
def main(): vars = sklearn.datasets.load_boston()['feature_names'] boston = sklearn.datasets.load_boston()['data'] X_idx = np.where(vars == 'DIS')[0][0] Y_idx = np.where(vars == 'NOX')[0][0] X = boston[:, X_idx] Y = boston[:, Y_idx] X_mean = np.mean(X) Y_mean = np.mean(Y) # See Introduction to Statistical Learning Ch 3.1 (Fig 3.4) numer = sum((x - X_mean) * (y - Y_mean) for x, y in zip(X, Y)) denom = sum((x - X_mean)**2 for x, y in zip(X, Y)) beta_1 = numer / denom beta_0 = Y_mean - beta_1 * X_mean eqn = 'Y(hat) = %.3f %f.3x' % (beta_0, beta_1) print 'Determined the below line of best fit...' print eqn trace = Scatter(x=X, y=Y, mode='markers') name = 'Determined this regression: %s' % eqn plot_url = py.plot(Data([trace]), filename=name)
def plot(timeseries, startdate, samplingfreq, plotname): """ Plot a list of dictionaries, representing sampled (top) topics """ py.sign_in(PLOTLYID, PLOTLYPASS) timestamps = len(timeseries) times = [None] * timestamps scatterdata = dict() for i in range(timestamps): times[i] = startdate + dt.timedelta(seconds=samplingfreq*i) for timestamp in range(timestamps): for k in timeseries[timestamp]: if not k in scatterdata: scatterdata[k] = [0] * timestamps scatterdata[k][timestamp] = timeseries[timestamp][k] msg("Coversion to time series finished successfully.\n") msg("Number of ticks: %d\n", timestamps) msg("Number of unique nouns: %d\n", len(scatterdata)) data = Data([Scatter(x=times, y=scatterdata[topic], name=topic) for topic in scatterdata]) return py.plot(data, filename=plotname)
from __future__ import absolute_import from plotly.graph_objs import (Data, Figure, Font, Layout, Line, Margin, Marker, Scatter, XAxis, YAxis) D = dict(x1=[0, 1, 2, 3, 4, 5], y1=[10, 20, 50, 80, 100, 200], x2=[0, 1, 2, 3, 4, 5, 6], y2=[1, 4, 8, 16, 32, 64, 128]) SIMPLE_LINE = Figure(data=Data([ Scatter(x=[0.0, 1.0, 2.0, 3.0, 4.0, 5.0], y=[10.0, 20.0, 50.0, 80.0, 100.0, 200.0], name='simple', mode='lines', line=Line(dash='solid', color='rgba (0, 0, 255, 1)', width=1.0), xaxis='x1', 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, xaxis1=XAxis(domain=[0.0, 1.0], range=[0.0, 5.0], type='linear', showline=True, ticks='inside',
from __future__ import absolute_import from plotly.graph_objs import (Annotation, Annotations, Data, Figure, Font, Layout, Line, Margin, Scatter, XAxis, YAxis) ANNOTATIONS = Figure(data=Data([ Scatter(x=[0.0, 1.0, 2.0], y=[1.0, 2.0, 3.0], name='_line0', mode='lines', line=Line(dash='solid', color='rgba (0, 0, 255, 1)', width=1.0), xaxis='x1', yaxis='y1'), Scatter(x=[0.0, 1.0, 2.0], y=[3.0, 2.0, 1.0], name='_line1', mode='lines', line=Line(dash='solid', color='rgba (0, 0, 255, 1)', width=1.0), xaxis='x1', 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,
def plot_data(data_dict): ''' Plots the data on the Plotly Framework. ''' py.sign_in(plotly_username, plotly_api_key) tls.set_credentials_file(username=plotly_username, api_key=plotly_api_key) layout = Layout( showlegend=True, autosize=True, height=800, width=800, title="MAP", xaxis=XAxis( zerolinewidth=4, gridwidth=1, showgrid=True, zerolinecolor="#969696", gridcolor="#bdbdbd", linecolor="#636363", mirror=True, zeroline=False, showline=True, linewidth=6, type="linear", range=[0, data_dict["length"]], autorange=False, autotick=False, dtick=15, tickangle=-45, title="X co-ordinate" ), yaxis=YAxis( zerolinewidth=4, gridwidth=1, showgrid=True, zerolinecolor="#969696", gridcolor="#bdbdbd", linecolor="#636363", mirror=True, zeroline=False, showline=True, linewidth=6, type="linear", range=[data_dict["width"], 0], autorange=False, autotick=False, dtick=15, tickangle=-45, title="Y co-ordinate" ) ) mac_history_data = data_dict['data'] processed_data = [] for mac, p_data in mac_history_data.items(): if len(p_data): p_data = sorted(p_data, key=lambda x:x[0]) color = color_generator() plot_data = Scatter( x=[x[1] for x in p_data], y=[y[2] for y in p_data], mode='lines + text', text=list(range(1, len(p_data) + 1)), name=mac, marker=Marker(color=color), opacity="0.6", legendgroup = mac, ) processed_data.append(plot_data) startData = Scatter( x=[p_data[0][1]], y=[p_data[0][2]], mode='markers', marker=Marker(color=color, size="10", symbol = "triangle-left"), showlegend=False, text=["Start point " + mac], legendgroup=mac, ) processed_data.append(startData) endData = Scatter( x=[p_data[-1][1]], y=[p_data[-1][2]], mode='markers', marker=Marker(color=color, size="10"), showlegend=False, text=["End point " + mac], legendgroup=mac, ) processed_data.append(endData) data = Data(processed_data) fig = Figure(data=data, layout=layout) py.plot(fig, filename='Sample Code For History Of Clients ')
width=[1, 4, 8, 16, 32, 64, 128], multi_left=[0, 10, 20, 30, 40, 50], multi_height=[1, 4, 8, 16, 32, 64], multi_bottom=[15, 30, 45, 60, 75, 90], multi_width=[30, 60, 20, 50, 60, 30] ) VERTICAL_BAR = Figure( data=Data([ Bar( x=[0.40000000000000002, 1.3999999999999999, 2.3999999999999999, 3.3999999999999999, 4.4000000000000004, 5.4000000000000004], y=[10.0, 20.0, 50.0, 80.0, 100.0, 200.0], orientation='v', marker=Marker( line=Line( width=1.0 ), color='#0000FF' ), opacity=1, xaxis='x1', yaxis='y1' ) ]), layout=Layout( width=640, height=480, autosize=False, margin=Margin( l=80, r=63, b=47,
sess.run(tf.global_variables_initializer()) train_accuracy = [] for i in range(1000): batch = mnist.train.next_batch(50) train_accuracy.append(accuracy.eval(feed_dict={x: batch[0], y_: batch[1]})) print("step %d, training accuracy %g" % (i, train_accuracy[-1])) train_step.run(feed_dict={x: batch[0], y_: batch[1]}) def fit_f(x, a, b, c, d): return a * x**0.5 + b * x**0.25 + c * x**0.125 + d * x**0.0625 fit_args, _ = curve_fit(fit_f, list(range(len(train_accuracy))), train_accuracy) train_accuracy_smoothed = [ fit_f(x, *fit_args) for x in range(len(train_accuracy)) ] plot(Data([ Scatter(y=train_accuracy, name='Train accuracy'), Scatter(y=train_accuracy_smoothed, line=dict(shape='spline'), name='Train accuracy (smoothed)'), ]), image='svg') print("test accuracy %g" % accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels }))
def get_k_means_pca(matrix, k, max_iter, init_method, n_init, tolerance, metric_dist, file_names, folder_path): """ Generate an array of centroid index based on the active files. Args: number_only_matrix: a numpy matrix without file names and word matrix: a python matrix representing the counts of words in files k: int, k-value max_iter: int, maximum number of iterations init_method: str, method of initialization: 'k++' or 'random' n_init: int, number of iterations with different centroids tolerance: float, relative tolerance, inertia to declare convergence DocTermSparseMatrix: sparse matrix of the word counts metric_dist: str, method of the distance metrics Returns: best_index: an array of the cluster index for each sample silhouette_score: float, silhouette score color_chart: string, list delimited by # of colors to use """ """Parameters for KMeans (SKlearn)""" # n_clusters: int, optional, default: 8 # namely, K; number of clusters to form OR # number of centroids to generate # # max_iter : int # Maximum number of iterations of the k-means algorithm # for a single run # # n_init : int, optional, default: 10 # Number of time the k-means algorithm will be run with # different centroid seeds # # init : 'k-means++', 'random' or an ndarray # method for initialization; # 'k-means++': selects initial cluster centers for k-mean # clustering in a smart way to speed up convergence # # precompute_distances : boolean # tol : float, optional default: 1e-4 # Relative tolerance w.r.t. inertia to declare convergence # # n_jobs : int # The number of jobs to use for the computation # -1 : all CPUs are used # 1 : no parallel computing code is used at all; # useful for debugging # For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. # -2 : all CPUs but one are used. number_only_matrix = matrix.tolist() inequality = '≤' # need to reset matplotlib (if hierarchical was called prior, this clears # previous dendrogram from showing in PCA graph) plt.figure() # get color gradient color_list = plt.cm.Dark2(np.linspace(0, 1, k)) # make color gradient a list color_list = color_list.tolist() # remove the a value from the rgba lists for rgba in color_list: del rgba[-1] rgb_tuples = [] # convert to tuples and put in a list for i in range(0, len(color_list)): rgb_tuples.append(tuple(color_list[i])) # coordinates for each cluster reduced_data = PCA(n_components=2).fit_transform(matrix) # n_init statically set to 300 for now. Probably should be determined # based on number of active files kmeans = KMeans(init=init_method, n_clusters=k, n_init=n_init, tol=tolerance, max_iter=max_iter) kmeans_index = kmeans.fit_predict(reduced_data) best_index = kmeans_index.tolist() colored_points = [] # make list of color for each point for i in range(0, len(best_index)): colored_points.append(rgb_tuples[best_index[i]]) # split x and y coordinates xs, ys = reduced_data[:, 0], reduced_data[:, 1] # plot and label points for x, y, name, color in zip(xs, ys, file_names, colored_points): plt.scatter(x, y, c=color, s=40) plt.text(x, y, name, color=color) # save the plot plt.savefig(pathjoin(folder_path, constants.KMEANS_GRAPH_FILENAME)) # close the plot so next one doesn't plot over the last one plt.close() # trap bad silhouette score input if k <= 2: silhouette_score = "N/A [Not available for K " + inequality + " 2]" elif k > (matrix.shape[0] - 1): silhouette_score = \ 'N/A [Not available if (K value) > (number of active files -1)]' else: kmeans.fit(number_only_matrix) labels = kmeans.labels_ # for silhouette score silhouette_score = get_silhouette_on_k_means(labels, matrix, metric_dist) # make a string of rgb tuples to send to the javascript separated by # # cause jinja hates lists of strings color_chart = '' for i in range(0, len(color_list)): for j in range(0, 3): # Browser needs rgb tuples with int values 0-255 we have rgb tuples # of floats 0-1 color_list[i][j] = int(color_list[i][j] * 255) temp = tuple(color_list[i]) temp2 = "rgb" + str(temp) + "#" color_chart += temp2 colors = color_chart.split("#") plotly_colors = [] for i in range(0, len(best_index)): new_color = colors[best_index[i]] plotly_colors.append(new_color) from plotly.graph_objs import Scatter, Data trace = Scatter(x=xs, y=ys, text=file_names, textfont=dict(color=plotly_colors), name=file_names, mode='markers+text', marker=dict(color=plotly_colors, line=dict(width=1, )), textposition='right') data = Data([trace]) small_layout = dict(margin={ 'l': 50, 'r': 50, 'b': 50, 't': 50, 'pad': 5 }, width=500, height=450, hovermode='closest') big_layout = dict(hovermode='closest') from plotly.offline import plot html = """ <html><head><meta charset="utf-8" /></head><body> ___ </body></html> """ sm_div = plot({ "data": data, "layout": small_layout }, output_type='div', show_link=False, auto_open=False) lg_div = plot({ "data": data, "layout": big_layout }, output_type='div', show_link=False, auto_open=False) sm_div = sm_div.replace('displayModeBar:"hover"', 'displayModeBar:true') sm_div = sm_div.replace("modeBarButtonsToRemove:[]", "modeBarButtonsToRemove:['sendDataToCloud']") sm_div = sm_div.replace("displaylogo:!0", "displaylogo:0") sm_div = sm_div.replace("displaylogo:!0", "displaylogo:0") sm_html = html.replace("___", sm_div) html_file = open(pathjoin(folder_path, constants.PCA_SMALL_GRAPH_FILENAME), "w", encoding='utf-8') html_file.write(sm_html) html_file.close() lg_div = lg_div.replace('displayModeBar:"hover"', 'displayModeBar:true') lg_div = lg_div.replace("modeBarButtonsToRemove:[]", "modeBarButtonsToRemove:['sendDataToCloud']") lg_div = lg_div.replace("displaylogo:!0", "displaylogo:0") lg_html = html.replace("___", lg_div) html_file = open(pathjoin(folder_path, constants.PCA_BIG_GRAPH_FILENAME), "w", encoding='utf-8') html_file.write(lg_html) html_file.close() # integer ndarray with shape (n_samples,) -- label[i] is the code or index # of the centroid the i'th observation is closest to return best_index, silhouette_score, color_chart
def draw_traces(traces, on_map=False, map_style='basic', showlegend=True, figsize=1, aspectratio='auto'): """ plots all the traces (which can be created using :func:`create_bus_trace`, :func:`create_line_trace`, :func:`create_trafo_trace`) to PLOTLY (see https://plot.ly/python/) INPUT: **traces** - list of dicts which correspond to plotly traces generated using: `create_bus_trace`, `create_line_trace`, `create_trafo_trace` OPTIONAL: **on_map** (bool, False) - enables using mapbox plot in plotly **map_style** (str, 'basic') - enables using mapbox plot in plotly - 'streets' - 'bright' - 'light' - 'dark' - 'satellite' **showlegend** (bool, 'True') - enables legend display **figsize** (float, 1) - aspectratio is multiplied by it in order to get final image size **aspectratio** (tuple, 'auto') - when 'auto' it preserves original aspect ratio of the network geodata any custom aspectration can be given as a tuple, e.g. (1.2, 1) """ if on_map: try: on_map = _on_map_test(traces[0]['x'][0], traces[0]['y'][0]) except: logger.warning( "Test if geo-data are in lat/long cannot be performed using geopy -> " "eventual plot errors are possible.") if on_map is False: logger.warning( "Existing geodata are not real lat/lon geographical coordinates. -> " "plot on maps is not possible.\n" "Use geo_data_to_latlong(net, projection) to transform geodata from specific projection." ) if on_map: # change traces for mapbox # change trace_type to scattermapbox and rename x to lat and y to lon for trace in traces: trace['lat'] = trace.pop('x') trace['lon'] = trace.pop('y') trace['type'] = 'scattermapbox' # setting Figure object fig = Figure( data=Data(traces), # edge_trace layout=Layout( titlefont=dict(size=16), showlegend=showlegend, autosize=True if aspectratio is 'auto' else False, hovermode='closest', margin=dict(b=5, l=5, r=5, t=5), # annotations=[dict( # text="", # showarrow=False, # xref="paper", yref="paper", # x=0.005, y=-0.002)], xaxis=XAxis(showgrid=False, zeroline=False, showticklabels=False), yaxis=YAxis(showgrid=False, zeroline=False, showticklabels=False), # legend=dict(x=0, y=1.0) ), ) # check if geodata are real geographycal lat/lon coordinates using geopy if on_map: try: mapbox_access_token = _get_mapbox_token() except Exception: logger.exception( 'mapbox token required for map plots. ' 'Get Mapbox token by signing in to https://www.mapbox.com/.\n' 'After getting a token, set it to pandapower using:\n' 'pandapower.plotting.plotly.mapbox_plot.set_mapbox_token(\'<token>\')' ) raise MapboxTokenMissing fig['layout']['mapbox'] = dict( accesstoken=mapbox_access_token, bearing=0, center=dict(lat=pd.Series(traces[0]['lat']).dropna().mean(), lon=pd.Series(traces[0]['lon']).dropna().mean()), style=map_style, pitch=0, zoom=11) # default aspectratio: if on_map use auto, else use 'original' aspectratio = 'original' if not on_map and aspectratio is 'auto' else aspectratio if aspectratio is not 'auto': if aspectratio is 'original': # TODO improve this workaround for getting original aspectratio xs = [] ys = [] for trace in traces: xs += trace['x'] ys += trace['y'] x_dropna = pd.Series(xs).dropna() y_dropna = pd.Series(ys).dropna() xrange = x_dropna.max() - x_dropna.min() yrange = y_dropna.max() - y_dropna.min() ratio = xrange / yrange if ratio < 1: aspectratio = (ratio, 1.) else: aspectratio = (1., 1 / ratio) aspectratio = np.array(aspectratio) / max(aspectratio) fig['layout']['width'], fig['layout']['height'] = ([ ar * figsize * 700 for ar in aspectratio ]) # check if called from ipynb or not in order to consider appropriate plot function if _in_ipynb(): from plotly.offline import init_notebook_mode, iplot as plot init_notebook_mode() else: from plotly.offline import plot as plot plot(fig)
def generate_grnets(idList, labelList, percentage, netthreshold): #path = os.getcwd() dfz = load_gexpressions(idList, labelList, percentage) ######################## dfinvert = dfz.transpose() # Compute the correlation matrix #corr = dfinvert.corr() #Netdata=pd.read_csv("network.csv") labels = list(dfinvert) #D=nx.to_networkx_graph(data,'TF','target') #limit=Netdata.index.size*netthreshold #Creamos nuestro grafo y almacenamos la posicion #G=nx.from_pandas_edgelist(Netdata.head(int(limit)), 'TF', 'target',['importance'], create_using=nx.Graph() ) #print(nx.info(G)) #G=nx.from_numpy_matrix(data,'TF', 'target' ) #pos=nx.get_node_attributes(Grafo,'pos') ################################################## G = Create_Graph(idList, labelList, percentage, netthreshold) print(G.neighbors('HLA-DRB4')) #pos = nx.spectral_layout(G) pos = nx.spring_layout(G) #pos = nx.kamada_kawai_layout(G) #nx.draw_networkx_nodes(G,pos) #dmin=1 #ncenter='HLA-DRB4' #for n in pos: # x,y=pos[n] # d=(x-0.5)**2+(y-0.5)**2 # if d<dmin: # ncenter=n # dmin=d # p=nx.single_source_shortest_path_length(G,ncenter) #Creamos las aristas de union entre nodos edge_trace = Scatter(x=[], y=[], line=Line(width=1, color='#888'), hoverinfo='text', mode='lines') for edge in G.edges(): x0, y0 = pos[edge[0]] x1, y1 = pos[edge[1]] edge_trace['x'] += [x0, x1, None] edge_trace['y'] += [y0, y1, None] node_trace = Scatter( x=[], y=[], text= labels, # text=labels Le proporciono los nombres de genes a los nodos mode='markers+text', textposition='top', hoverinfo=[], marker=Marker( showscale=True, # colorscale options # 'Greys' | 'Greens' | 'Bluered' | 'Hot' | 'Picnic' | 'Portland' | # Jet' | 'RdBu' | 'Blackbody' | 'Earth' | 'Electric' | 'YIOrRd' | 'YIGnBu' colorscale='YIOrRd', reversescale=True, color=[], size=20, colorbar=dict(thickness=20, title='Node Connections', xanchor='left', titleside='right'), line=dict(width=4))) for node in G.nodes(): x, y = pos[node] node_trace['x'].append(x) node_trace['y'].append(y) #proporcionamos el color a nuestro grafo for node, adjacencies in enumerate(G.adjacency()): node_trace['marker']['color'].append(len(adjacencies)) node_info = 'Number of connections: ' + str(len(adjacencies)) node_trace['text'].append(node_info) #for edge, adyacencias in enumerate(G.adjacency()): # edge_info= 'Number of connections: '+str(len(adjacencies)) # edge_trace['hoverinfo'].append(edge_info) fig = Figure(data=Data([edge_trace, node_trace]), layout=Layout(title='<br>Network graph made with Python', titlefont=dict(size=16), showlegend=False, hovermode='closest', margin=dict(b=20, l=5, r=5, t=40), annotations=[ dict(text="Gene Regulatory Network", showarrow=False, xref="paper", yref="paper", x=0.005, y=-0.002) ], xaxis=XAxis(showgrid=False, zeroline=False, showticklabels=False), yaxis=YAxis(showgrid=False, zeroline=False, showticklabels=False))) py.iplot(fig, filename='networkx')
def generate_grnetsGenes(idList, labelList, percentage, netthreshold): path = os.getcwd().split('arboreum')[0] dfz = load_gexpressions(idList, labelList, percentage) #transpongo la matrix para obtener los genes en forma de columnas networkdata = dfz.transpose() #print (networkdata.keys()) #print (list(networkdata)) #aplicando GRNBOOST2 obtengo un dataframe con el siguiente formato: ['TF', 'target', 'importance'], para así #poder utilizarlo en nuestra red de inferencias genicas #network = grnboost2(expression_data=networkdata, tf_names=list(networkdata)) # Vamos a leer un dataframe con el formato ['TF', 'target', 'importance'], que seria el resultado de aplicar el algoritmo de gnrboost2 data = pd.read_csv("network.csv") data.head() netdata = pd.read_csv("network_2.csv") netdata.head() labels = list(networkdata) limit = data.index.size * netthreshold #Creamos nuestro grafo y almacenamos la posicion G = nx.from_pandas_edgelist(data.head(int(limit)), 'TF', 'target', ['importance'], create_using=nx.Graph()) print(nx.info(G)) sorted(nx.connected_components(G), key=len, reverse=True) pos = nx.spring_layout(G) #pos = nx.circular_layout(G) #pos = nx.shell_layout(G) #pos = nx.nx_pydot.pydot_layout(G) edge_trace = Scatter(x=[], y=[], line=Line(width=0.5, color='#888'), hoverinfo='none', mode='lines') for edge in G.edges(): x0, y0 = pos[edge[0]] x1, y1 = pos[edge[1]] edge_trace['x'] += [x0, x1, None] edge_trace['y'] += [y0, y1, None] node_trace = Scatter(x=[], y=[], mode='markers+text', text=labels, textposition='top', marker=Marker(size=10)) for node in G.nodes(): x, y = pos[node] node_trace['x'].append(x) node_trace['y'].append(y) fig = Figure(data=Data([edge_trace, node_trace]), layout=Layout(title='Gene Regulatory Network', showlegend=False, xaxis=XAxis(showgrid=False, zeroline=False, showticklabels=False), yaxis=YAxis(showgrid=False, zeroline=False, showticklabels=False))) py.iplot(fig, filename='RedGenica')
#!/usr/bin/env python import plotly.plotly as py import plotly.tools as tls from plotly.graph_objs import Stream, Data, Figure, Scatter, Layout stream_id = tls.get_credentials_file()['stream_ids'][1] stream = Stream(token=stream_id, maxpoints=50) trace = Scatter(x=[], y=[], mode='lines+markers', stream=stream) data = Data([trace]) layout = Layout(title='Streaming test') figure = Figure(data=data, layout=layout) unique_url = py.plot(figure, filename='stream_test', auto_open=False) print('Plot URL: {0}'.format(unique_url))
xanchor='left', titleside='right'), line=dict(width=2))) for node in G.nodes(): x, y = G.node[node]['pos'] node_trace['x'].append(x) node_trace['y'].append(y) for node, adjacencies in enumerate(G.adjacency_list()): node_trace['marker']['color'].append(len(adjacencies)) node_info = '# of connections: ' + str(len(adjacencies)) node_trace['text'].append(node_info) fig = Figure( data=Data([edge_trace, node_trace]), layout=Layout( title='<br>Network graph made with Python', titlefont=dict(size=16), showlegend=False, hovermode='closest', margin=dict(b=20, l=5, r=5, t=40), annotations=[ dict( text= "Python code: <a href='https://plot.ly/ipython-notebooks/network-graphs/'> https://plot.ly/ipython-notebooks/network-graphs/</a>", showarrow=False, xref="paper", yref="paper", x=0.005, y=-0.002)
def fig2(): # (!) Set 'size' values to be proportional to rendered area, # instead of diameter. This makes the range of bubble sizes smaller sizemode = 'area' # (!) Set a reference for 'size' values (i.e. a population-to-pixel scaling). # Here the max bubble area will be on the order of 100 pixels sizeref = df['Population'].max() / 1e2 ** 2 colors = { 'Asia': "rgb(255,65,54)", 'Europe': "rgb(133,20,75)", 'Africa': "rgb(0,116,217)", 'North America': "rgb(255,133,27)", 'South America': "rgb(23,190,207)", 'Antarctica': "rgb(61,153,112)", 'Oceania': "rgb(255,220,0)", } # Define a hover-text generating function (returns a list of strings) def make_text(X): return 'Country: %s\ <br>Life Expectancy: %s years\ <br>Population: %s million' \ % (X['Name'], X['LifeExpectancy'], X['Population'] / 1e6) # Define a trace-generating function (returns a Scatter object) def make_trace(X, continent, sizes, color): return Scatter( x=X['GNP'], # GDP on the x-xaxis y=X['LifeExpectancy'], # life Exp on th y-axis name=continent, # label continent names on hover mode='markers', # (!) point markers only on this plot text=X.apply(make_text, axis=1).tolist(), marker=Marker( color=color, # marker color size=sizes, # (!) marker sizes (sizes is a list) sizeref=sizeref, # link sizeref sizemode=sizemode, # link sizemode opacity=0.6, # (!) partly transparent markers line=Line(width=3, color="white") # marker borders ) ) # Initialize data object data = Data() # Group data frame by continent sub-dataframe (named X), # make one trace object per continent and append to data object for continent, X in df.groupby('Continent'): sizes = X['Population'] # get population array color = colors[continent] # get bubble color data.append( make_trace(X, continent, sizes, color) # append trace to data object ) # Set plot and axis titles title = "Life expectancy vs GNP from MySQL world database (bubble chart)" x_title = "Gross National Product" y_title = "Life Expectancy [in years]" # Define a dictionary of axis style options axis_style = dict( type='log', zeroline=False, # remove thick zero line gridcolor='#FFFFFF', # white grid lines ticks='outside', # draw ticks outside axes ticklen=8, # tick length tickwidth=1.5 # and width ) # Make layout object layout = Layout( title=title, # set plot title plot_bgcolor='#EFECEA', # set plot color to grey hovermode="closest", xaxis=XAxis( axis_style, # add axis style dictionary title=x_title, # x-axis title range=[2.0, 7.2], # log of min and max x limits ), yaxis=YAxis( axis_style, # add axis style dictionary title=y_title, # y-axis title ) ) # Make Figure object fig = Figure(data=data, layout=layout) # (@) Send to Plotly and show in notebook py.iplot(fig, filename='s3_life-gdp')
# In[10]: boroughs['COUNT'] = 1 borough_groups = boroughs.groupby('borough') # In[11]: borough_groups.sum().index # In[12]: data = Data([Bar(y=borough_groups.sum()['COUNT'], x=borough_groups.sum().index)]) layout = Layout(xaxis=XAxis(title="Borough"), yaxis=YAxis(title='Accident Count')) fig = Figure(data=data, layout=layout) # In[13]: py.iplot(fig) # In[14]: dates_borough = store.select("nypd", "columns=['date', 'borough']").sort('date') # In[15]:
def test_validate_error(): data = Data() data.append({'not-a-key': 'anything'}) data.validate()