def save(root=None, formats=None, **options): """Save the plot in the given formats and clears the figure. For options, see Config. Args: root: string filename root formats: list of string formats options: keyword args used to invoke various pyplot functions """ clf = options.pop('clf', True) config(**options) if formats is None: formats = ['pdf', 'eps'] try: formats.remove('plotly') plotly(clf=False) except ValueError: pass if root: for fmt in formats: save_format(root, fmt) if clf: clear_figure()
def fplotly(fig, username, api_key): axes = fig.axes lines = axes[0].lines xdata = plt.getp(lines[0], 'xdata') ydata = plt.getp(lines[0], 'ydata') py = plotly.plotly(username, api_key) py.plot(xdata,ydata)
def plotAway(data): api_key="hvvum2tdvx" username = "******" py = plotly.plotly(username=username, key=api_key) print py layout = { 'title': 'tweet Competitors', 'autosize': False, 'width': 550, 'height': 550, 'yaxis': {'name': 'Count of Tweets','zeroline':False}, 'xaxis': {'type': 'Clients','zeroline':False}, 'barmode': 'group','bargap': 0.25,'bargroupgap': 0.3, 'bardir': 'v'} #'catagories': cat,' input=[] for a in data: print a.keys() #print input #response = py.plot(input,layout) #url = response['url'] #filename = response['filename'] print filename print response
def update_temp(date, temp): py = plotly.plotly(username='******', key='7ew737sh9i') r = py.plot(date, temp, filename='RPiTempCont', fileopt='extend', layout={'title': 'Raspberry Pi Temperature Status'})
def update_temp(date, temp): py = plotly.plotly(username_or_email='USER', key='KEY') r = py.plot(date, temp, filename='RPiTempCont', fileopt='extend', layout={'title': 'Raspberry Pi Temperature Status'})
def test_combinations(args, graph=False): py = plotly.plotly(username='******', key='uzkqabvlzm', verbose=False) options = [100, 10, 0.9, 4, 0.05, 10000] features = ["skewness", "spectral_rolloff", "energy", "sv", "spread", "centroid", "zcr", "obsi", "kurtosis"] electronic_y = [] classical_y = [] categories = [] print '\t'.join([feature[:2] for feature in features] + ["meta", "acou", "regg", "elec", "class"]) for i in range(1, len(features) + 1): combinations = [list(comb) for comb in itertools.combinations(features, i)] for comb in combinations: comb_name = ', '.join(comb) classifier = Classifier(args['data'], discrete_intervals=options[0], size_rule_generation=options[1], filter_list=comb) top_fitness = classifier.train(req_min_fitness=options[2], gen_select=options[3], mutation_prob=options[4], limit_generations=options[5]) for feature in features: if feature in comb: sys.stdout.write("X\t") else: sys.stdout.write("\t") sys.stdout.write(str(top_fitness['metal']["fitness"])[:4] + "\t") sys.stdout.write(str(top_fitness['acoustic']["fitness"])[:4] + "\t") sys.stdout.write(str(top_fitness['reggae']["fitness"])[:4] + "\t") sys.stdout.write(str(top_fitness['electronic']["fitness"])[:4] + "\t") sys.stdout.write(str(top_fitness['classical']["fitness"])[:4] + "\n") if graph: print "Training ended\nFinal fitness:", top_fitness electronic_y.append(top_fitness['metal']) classical_y.append(top_fitness['classical']) categories.append(comb_name) if len(categories) > 20: electronic = { "name": "Metal", "x": categories, "y": electronic_y, "type": "bar" } classical = { "name": "Classical", "x": categories, "y": classical_y, "type": "bar" } layout = { "barmode": "group", 'xaxis': {'type': 'combination'}, 'catagories': categories } response = py.plot([electronic, classical], layout=layout) print response['url'] electronic_y = [] classical_y = [] categories = []
def update_temp(): py = plotly.plotly(username_or_email='geekskunk', key='m5b6vstz34') i = datetime.datetime.now() proc = subprocess.Popen(["cat", "/sys/class/thermal/thermal_zone0/temp"], stdout=subprocess.PIPE) t = proc.stdout.read() r = py.plot(i.strftime('%Y-%m-%d %H:%M:%S'),float(t)/1000, filename='RPiBaseCPU', fileopt='extend', layout={'title': 'Base Station RPi CPU Temperature History'})
def initPlotly(plotly_un, plotly_key): try: #Plotly handle py = plotly.plotly(username=plotly_un, key=plotly_key) except: print (strftime("[%H:%M:%S]: EXCEPTION ", localtime()) + str(sys.exc_info()[0])) return False return py
def plotClusters(data): ''' Use the plotly API to plot data from clusters. Gets a plot URL from plotly and then uses subprocess to 'open' that URL from the command line. This should open your default web browser. ''' # List of symbols each cluster will be displayed using symbols = ['circle', 'cross', 'triangle-up', 'square'] # Convert data into plotly format. traceList = [] for i, c in enumerate(data): data = [] for p in c.points: data.append(p.coords) # Data trace = {} trace['x'], trace['y'] = zip(*data) trace['marker'] = {} trace['marker']['symbol'] = symbols[i] trace['name'] = "Cluster " + str(i) traceList.append(trace) # Centroid (A trace of length 1) centroid = {} centroid['x'] = [c.centroid.coords[0]] centroid['y'] = [c.centroid.coords[1]] centroid['marker'] = {} centroid['marker']['symbol'] = symbols[i] centroid['marker']['color'] = 'rgb(200,10,10)' centroid['name'] = "Centroid " + str(i) traceList.append(centroid) # Log in to plotly py = plotly(username=PLOTLY_USERNAME, key=PLOTLY_KEY) # Style the chart datastyle = { 'mode': 'markers', 'type': 'scatter', 'marker': { 'line': { 'width': 0 }, 'size': 12, 'opacity': 0.6, 'color': 'rgb(74, 134, 232)' } } resp = py.plot(*traceList, style=datastyle) # Display that plot in a browser cmd = "open " + resp['url'] subprocess.call(cmd, shell=True)
def initPlotly(): p = plotly.plotly(username, api_key) trace_1 = {'x': [], 'y': [], 'stream': {'token': stream_token[0], 'maxpoints': 5000}} trace_2 = {'x': [], 'y': [], 'stream': {'token': stream_token[1], 'maxpoints': 5000}} trace_3 = {'x': [], 'y': [], 'stream': {'token': stream_token[2], 'maxpoints': 5000}} trace_4 = {'x': [], 'y': [], 'stream': {'token': stream_token[3], 'maxpoints': 5000}} print p.plot([trace_1, trace_2, trace_3, trace_4], filename='Aquaponics May 15',fileopt='extend')
def fig_to_plotly(fig, username=None, api_key=None, notebook=False): """Convert a matplotlib figure to plotly dictionary """ renderer = PlotlyRenderer(username=username, api_key=api_key) Exporter(renderer).run(fig) py = plotly.plotly(renderer.username, renderer.api_key) if notebook: return py.iplot(renderer.data, layout=renderer.layout) else: py.plot(renderer.data, layout=renderer.layout)
def main(): filename = sys.argv[1] teams,year = np.loadtxt(filename,unpack=True,usecols=(0,1),dtype=str) v0,v1,v2,v3,v4 = np.loadtxt(filename,unpack=True,usecols=(2,3,4,5,6),dtype=float) ############################################################################ py = plotly.plotly(username_or_email="MatthewBellis", key="d6h4et78v5") ############################################################################ plotly_title = 'NBA stats' plotly_filename = 'Siena_NBA_research' #s={'type':'bubble' ,'jitter':0.1, 'boxpoints':'all'} #s={'type':'box'} #axesstyle = {'range':[datetime.datetime(2014,1,15),datetime.datetime(2014,2,15)]} #axesstyle = {} #l={'title': plotly_title,'xaxis':axesstyle} #colors = colorscale(GnBu, v4) colors = colorscale(diverging, v4) data = [] for i0,i1,i2,i3,i4,i5,i6,c in zip(teams,year,v0,v1,v2,v3,v4,colors): t = ["%s %s" % (i0,i1)] #t = "%s" % (i0) print t d = {'x':i2, 'y':i3,\ 'marker': {'size':20*(i4-min(v2))/(max(v2)-min(v2)), 'opacity':0.9, 'line':{'width':1},'color':c},\ 'type':'scatter','mode':'markers',\ 'text':t} data.append(d) citation = {'showarrow':False, 'font':{'size':10},'xref':'paper','yref':'paper','x':0.00,'y':-0.18,'align':'left',\ 'text':'Data source and inspiration:<br>WHERE DID THIS COME FROM?'} layout = {'showlegend':False,'hovermode':'closest', 'title':'','annotations':[citation],\ 'title':plotly_title,\ 'xaxis':{ 'ticks':'','linecolor':'white','showgrid':False,'zeroline':False, 'title': 'Offensive efficiency', 'nticks':12 }, 'yaxis':{ 'ticks':'','linecolor':'white','showgrid':False,'zeroline':False, 'title': 'FGR', 'nticks':12 }} response = py.plot(data,layout=layout,filename=plotly_filename,fileopt='overwrite') url = response['url'] filename = response['filename'] print response print url print filename
def __init__(self, username, api): """Creates a plotly object Parameters ----------- username: string plotly username api: string plotly api """ super(PlotlyChart, self).__init__() self.plotly = plotly.plotly(username, api)
def iClustergram(data=None, row_labels=None, col_labels=None, row_groups=None, col_groups=None, row_linkage='average', col_linkage='average', row_pdist='euclidean', col_pdist='euclidean', standardize=None, log=False, display_range=3, username='******', apikey='fmnoxd2t2u'): ## preprocess data if log: data = np.log2(data + 1.0) if standardize == 1: # Standardize along the columns of data data = zscore(data, axis=0) elif standardize == 2: # Standardize along the rows of data data = zscore(data, axis=1) ## cluster data: ## compute pdist for rows d1 = dist.pdist(data, metric=row_pdist) D1 = dist.squareform(d1) Y1 = sch.linkage(D1, method=row_linkage, metric=row_pdist) Z1 = sch.dendrogram(Y1, orientation='right') idx1 = Z1['leaves'] ## compute pdist for cols d2 = dist.pdist(data.T, metric=col_pdist) D2 = dist.squareform(d2) Y2 = sch.linkage(D2, method=col_linkage, metric=col_pdist) Z2 = sch.dendrogram(Y2) idx2 = Z2['leaves'] ## transform the orders of data to clustered data data_clustered = data data_clustered = data_clustered[:,idx2] data_clustered = data_clustered[idx1,:] data_to_plot = data_clustered.tolist() ## transform the orders of row and col labels new_row_labels = [] new_col_labels = [] for i in range(data.shape[0]): new_row_labels.append(row_labels[idx1[i]]) for i in range(data.shape[1]): new_col_labels.append(col_labels[idx2[i]]) ## plot clustered data using plotly py = plotly.plotly(username, apikey) d = {} d['x'] = new_row_labels d['y'] = new_col_labels d['z'] = data_to_plot d['type'] = 'heatmap' py.plot([d]) return
def creatStreamingGraph(fileName, ConfigGile,chanID): with open(ConfigGile) as config_file: plotly_user_config = json.load(config_file) username = plotly_user_config['plotly_username'] api_key = plotly_user_config['plotly_api_key'] stream_token = plotly_user_config['plotly_streaming_tokens'][chanID] stream_server = 'http://stream.plot.ly' p = plotly.plotly(username, api_key) layout = {'autosize': True, 'showlegend':True,'legend': {"x" : 1, "y" : 1},"xaxis": {"title":"Time"},"yaxis": {"title":"Temp (C)"}} p.plot([{'x': [],'y': [],"name":fileName,'type': 'scatter', 'mode': 'lines+markers','stream': {'token': stream_token,'maxpoints': 60}}],filename=fileName, fileopt='overwrite',layout = layout, world_readable=True) s = plotly.stream(stream_token) return s
def generate__two_traces(ConfigGile): with open(ConfigGile) as config_file: plotly_user_config = json.load(config_file) username = plotly_user_config['plotly_username'] api_key = plotly_user_config['plotly_api_key'] stream_tokenA = plotly_user_config['plotly_streaming_tokens'][0] stream_tokenB = plotly_user_config['plotly_streaming_tokens'][1] data = [{ 'x': [], 'y': [], "name": "Ambient Temp", 'type': 'scatter', 'mode': 'lines+markers', 'line': { 'color': 'rgba(106, 45, 172, 1)' }, 'stream': { 'token': stream_tokenA, 'maxpoints': 60 } }, { 'x': [], 'y': [], "name": "Water Temp", 'type': 'scatter', 'mode': 'lines+markers', 'line': { 'color': 'rgba(215, 40, 44, 1)' }, 'stream': { 'token': stream_tokenB, 'maxpoints': 60 } }] p = plotly.plotly(username, api_key) p.plot(data, fileopt='overwrite', layout={ 'autosize': True, 'showlegend': True, 'yaxis': { 'range': [0, 40], 'title': 'Temp(C)' }, 'xaxis': { 'title': 'Time' } }) s0 = plotly.stream(stream_tokenA) s1 = plotly.stream(stream_tokenB) return s0, s1
def plotClusters(data): ''' Use the plotly API to plot data from clusters. Gets a plot URL from plotly and then uses subprocess to 'open' that URL from the command line. This should open your default web browser. ''' # List of symbols each cluster will be displayed using symbols = ['circle', 'cross', 'triangle-up', 'square'] # Convert data into plotly format. traceList = [] for i, c in enumerate(data): data = [] for p in c.points: data.append(p.coords) # Data trace = {} trace['x'], trace['y'] = zip(*data) trace['marker'] = {} trace['marker']['symbol'] = symbols[i] trace['name'] = "Cluster " + str(i) traceList.append(trace) # Centroid (A trace of length 1) centroid = {} centroid['x'] = [c.centroid.coords[0]] centroid['y'] = [c.centroid.coords[1]] centroid['marker'] = {} centroid['marker']['symbol'] = symbols[i] centroid['marker']['color'] = 'rgb(200,10,10)' centroid['name'] = "Centroid " + str(i) traceList.append(centroid) # Log in to plotly py = plotly(username=PLOTLY_USERNAME, key=PLOTLY_KEY) # Style the chart datastyle = {'mode':'markers', 'type':'scatter', 'marker':{'line':{'width':0}, 'size':12, 'opacity':0.6, 'color':'rgb(74, 134, 232)'}} resp = py.plot(*traceList, style = datastyle) # Display that plot in a browser cmd = "open " + resp['url'] subprocess.call(cmd, shell=True)
def generate__two_traces(ConfigGile): with open(ConfigGile) as config_file: plotly_user_config = json.load(config_file) username = plotly_user_config['plotly_username'] api_key = plotly_user_config['plotly_api_key'] stream_tokenA = plotly_user_config['plotly_streaming_tokens'][0] stream_tokenB = plotly_user_config['plotly_streaming_tokens'][1] data = [{'x': [], 'y': [],"name":"Ambient Temp", 'type': 'scatter', 'mode': 'lines+markers', 'line': {'color': 'rgba(106, 45, 172, 1)'}, 'stream': {'token': stream_tokenA, 'maxpoints': 60}}, {'x': [], 'y': [],"name":"Water Temp", 'type': 'scatter', 'mode': 'lines+markers', 'line': {'color': 'rgba(215, 40, 44, 1)'}, 'stream': {'token': stream_tokenB, 'maxpoints': 60}}] p = plotly.plotly(username, api_key) p.plot(data, fileopt='overwrite',layout = {'autosize': True, 'showlegend':True,'yaxis': {'range': [0,40],'title':'Temp(C)'},'xaxis': {'title':'Time'}}) s0 = plotly.stream(stream_tokenA) s1 = plotly.stream(stream_tokenB) return s0,s1
def topfiveTags(): listTrends = analytics.trending_concepts() print "in topFive" client =[] listofDicts=[] #list should be 10 long print len(listTrends) for length in range(len(listTrends)): #get client name #print listTrends[length][0] client.append(listTrends[length][0]) colours = ["red","yellow","blue","black","orange","green","purple"] for length in range(len(listTrends)): tags = [] counts =[] for item in listTrends[length][1]: #print item tags.append(item[0]) counts.append(item[1]) listofDicts.append({'name':client[length], 'x': tags, 'y': counts, 'type':'bar', 'marker':{'color': colours[length], 'line': {'color': colours[length], 'width': 3} } }) for c in listofDicts: pprint.pprint(c) api_key="hvvum2tdvx" username = "******" py = plotly.plotly(username=username, key=api_key) layout = { 'title': 'tweet Competitors', 'autosize': False, 'width': 550, 'height': 550, 'yaxis': {'name': 'Count of Tweets','zeroline':False}, 'xaxis': {'type': 'Clients','zeroline':False}, 'barmode': 'group','bargap': 0.05,'bargroupgap': 0.1, 'bardir': 'v'} response = py.plot(listofDicts,layout=layout) url = response['url'] filename = response['filename'] print filename print response
def gen_bar(self, title, dataset): """Generates a plotly barchart from a dataset Returns plotly object of the form: {'url':'', 'filename': '', 'error': 'No data', 'warning': '', 'message': ''}""" if not any(dataset): return {'url':'', 'filename': 'title', 'error': 'No data', 'warning': '', 'message': ''} py = plotly.plotly(plotly_usrn, plotly_secret) py.ioff() data = {'x': list(range(len(dataset))), 'y':dataset, 'type': 'bar'} l = {'autosize': True, 'width': 900, 'height': 600, 'showlegend': False, 'title':title} return py.plot([data], layout=l)
def save_plotly(application, label): """Save a Glue session to a plotly plot This is currently restricted to 1-4 scatterplots or histograms :param application: Glue application to save :param path: Label for the exported plot """ user = application.get_setting('PLOTLY_USER') apikey = application.get_setting('PLOTLY_APIKEY') args, kwargs = build_plotly_call(application) kwargs['filename'] = label logging.getLogger(__name__).debug(args, kwargs) py = plotly.plotly(user, key=apikey) py.plot(args, **kwargs)
def __init__(self, api=None): self.hi = True if not api: self.api = IrrigationAPI() else: self.api = api self.conn = None self.dbTable = None self.printer = None self.apikey = "d8ytNTiS45sNRIVqsluvbDTlW2eSAKxJVUNVamJLUmtJZz0g" self.feedid = 121835 self.feedHist = {} self.py = plotly.plotly(username_or_email='kutenai', key='xhhbxk2swl')
def analyzeResults(options): """ Generate ROC curve and find optimum point given cost matrix """ with open(options.inputFile, 'r') as fh: results = pandas.read_csv(fh) costMatrix = getCostMatrix() vals = genCurveData(results, options.min, options.max, options.step, costMatrix) costs = vals['costs'] print "The lowest expected cost with this ROC curve is: %s" % str(min(costs)) costsArray = numpy.array(costs) minCostIndices = numpy.where(costsArray == costsArray.min()) for minCostIndex in minCostIndices: ind = minCostIndex[0] print "A threshold that gives the minimum cost given the current cost matrix is: %s" % str(vals['thresholds'][ind]) if options.plot: # Get connection to plotly try: plotlyUser = os.environ['PLOTLY_USER_NAME'] plotlyAPIKey = os.environ['PLOTLY_API_KEY'] except KeyError: raise Exception("Plotly user name and api key were not found in " "your environment. Please add:\n" "export PLOTLY_USER_NAME={username}\n" "export PLOTLY_API_KEY={apikey}") py = plotly(username_or_email=plotlyUser, key=plotlyAPIKey, verbose = False) plotROC(py, vals)
def handle(self, *args, **kwargs): now = datetime.now() a_week = timedelta(weeks=1) this_week = [now - a_week, now] qs = ArchiveTrack.objects.filter(Q(play__created__range=this_week))\ .annotate(plays=Count('play')).order_by('-plays') consolodated = {} total = qs.count() y = Year() for count, item in enumerate(qs): year, didsleep = y.get_year(item.spotify_href) if count % 10 == 0: print float(count) / total * 100, '%', y.request_count if not year: pass if year in consolodated: consolodated[year].append(item) else: consolodated[year] = [item] consolodated = collections.OrderedDict(sorted(consolodated.items())) py = plotly.plotly(username_or_email='EricGeorge', key='cp9m5tn1ix') l = {'autosize': False,'width': 1200, 'height': 600, 'showlegend': False, 'xaxis' : {"linecolor": "rgba(0, 0, 0, 0)","ticks":"", "showgrid": True, "zeroline": False, "showticklabels": True}, 'yaxis' : {"linecolor": "rgba(0, 0, 0, 0)", "ticks": "", "showgrid": True, "zeroline": True, "showticklabels": True}} print py.plot([{'y': [len(i) for i in consolodated.values()], 'x': consolodated.keys(), 'type': 'bar'}], layout=l)
def generateThreeTrace(ConfigGile): with open(ConfigGile) as config_file: plotly_user_config = json.load(config_file) username = plotly_user_config['plotly_username'] api_key = plotly_user_config['plotly_api_key'] stream_tokenA = plotly_user_config['plotly_streaming_tokens'][3] stream_tokenB = plotly_user_config['plotly_streaming_tokens'][4] stream_tokenC = plotly_user_config['plotly_streaming_tokens'][5] stream_server = 'http://stream.plot.ly' layout = {'autosize': True, 'showlegend':True,'legend': {"x" : 1, "y" : 1},"xaxis": {"title":"Time"},"yaxis": {"title":"Temp (C)"}} data = [ {'x': [], 'y': [],"name":"DS18B20 1", 'type': 'scatter', 'mode': 'lines+markers', 'line': {'color': 'rgba(106, 45, 172, 1)'}, 'stream': {'token': stream_tokenA, 'maxpoints': 60}}, {'x': [], 'y': [],"name":"DS18B20 2", 'type': 'scatter', 'mode': 'lines+markers', 'line': {'color': 'rgba(215, 40, 44, 1)'}, 'stream': {'token': stream_tokenB, 'maxpoints': 60}}, {'x': [], 'y': [],"name":"MLX90614 1", 'type': 'scatter', 'mode': 'lines+markers', 'line': {'color': 'rgba(106, 240, 44, 1)'}, 'stream': {'token': stream_tokenC, 'maxpoints': 60}}] p = plotly.plotly(username, api_key) p.plot(data, fileopt='overwrite',layout = {'autosize': True, 'showlegend':True,'legend': {"x" : 1, "y" : 1}}) s4 = plotly.stream(stream_tokenA) s5 = plotly.stream(stream_tokenB) s6 = plotly.stream(stream_tokenC) return s4, s5, s6
def __init__(self, source, discrete_intervals=DEFAULT_DISCRETE_INTERVALS, size_rule_generation=20, filter_list=[], log_results=False): """ Inicializa el clasificador. Toma como fuente un archivo csv y puede recibir como parametro opcional la cantidad de intervalos para discretizar los valores de los features. """ super(Classifier, self).__init__() self.source = source self.discrete_intervals = discrete_intervals self.features, self.train_data, self.test_data, self.result_types = self._read_source(filter_list) self.len_features = len(self.features) if log_results: print "Se tienen", self.len_features, "features distintos." self.len_data = len(self.train_data) self.size_rule_generation = size_rule_generation self.len_rules = len(self.result_types) self.best_rules = {} self.py = plotly.plotly(username='******', key='uzkqabvlzm', verbose=False) self.log_results = log_results # Cada regla tiene `size_rule_generation` reglas inicialmente creadas # al azar. # La idea es evolucionar sobre este conjunto de reglas para cada tipo # de reglas. # Una regla devuelve un valor binario, SI es result_type o NO lo es. self.rules = {} for result_type in self.result_types: self.rules[result_type] = [] # Para cada tipo de regla creo `size_rule_generation` reglas al # azar y evaluo de forma prematura con los datos que tienen. for i in range(self.size_rule_generation): rule = Rule(self.features, self.discrete_intervals, result_type) rule_dict = {'rule': rule, 'fitness': self._rule_fitness(rule)} self.rules[result_type].append(rule_dict)
def fig_to_plotly(fig, username=None, api_key=None, notebook=False, resize=False, strip_style=False, verbose=False): """Convert a matplotlib figure to plotly dictionary and send. All available information about matplotlib visualizations are stored within a matplotlib.figure.Figure object. You can create a plot in python using matplotlib, store the figure object, and then pass this object to the fig_to_plotly function. In the background, mplexporter is used to crawl through the mpl figure object for appropriate information. This information is then systematically sent to the PlotlyRenderer which creates the JSON structure used to make plotly visualizations. Finally, these dictionaries are sent to plotly and your browser should open up a new tab for viewing! Optionally, if you're working in IPython, you can set notebook=True and the PlotlyRenderer will call plotly.iplot instead of plotly.plot to have the graph appear directly in the IPython notebook. Note, this function gives the user access to a simple, one-line way to render an mpl figure in plotly. If you need to trouble shoot, you can do this step manually by NOT running this fuction and entereing the following: ============================================================================ from mplexporter import Exporter from mplexporter.renderers import PlotlyRenderer # create an mpl figure and store it under a varialble 'fig' renderer = PlotlyRenderer() exporter = Exporter(renderer) exporter.run(fig) ============================================================================ You can then inspect the JSON structures by accessing these: renderer.layout -- a plotly layout dictionary renderer.data -- a list of plotly data dictionaries Positional arguments: fig -- a matplotlib figure object username -- a valid plotly username ** api_key -- a valid api_key for the above username ** notebook -- an option for use with an IPython notebook ** Don't have a username/api_key? Try looking here: https://plot.ly/plot ** Forgot your api_key? Try signing in and looking here: https://plot.ly/api/python/getting-started """ import plotly renderer = PlotlyRenderer() Exporter(renderer).run(fig) if resize: renderer.resize() if strip_style: renderer.strip_style() py = plotly.plotly(username, api_key) data = renderer.data.get_json() layout = renderer.layout.get_json() if notebook: return py.iplot(data, layout=layout) else: py.plot(data, layout=layout) if verbose: return renderer.msg
#!/usr/bin/env python # -*- coding: utf-8 -*- #import the PySerial library and sleep from the time library import serial import time from time import sleep # Future use import math # Import plotly graph API import plotly py = plotly.plotly(username_or_email="Name", key="PWD") # Import cPickle to manage a file with all data import cPickle as pickle # declare to variables, holding the com port we wish to talk to and the speed port = '/dev/ttyAMA0' baud = 9600 # open a serial connection using the variables above ser = serial.Serial(port=port, baudrate=baud) # wait for a moment before doing anything else sleep(0.2) # clear out the serial input buffer to ensure there are no old messages lying around ser.flushInput() # Minute to keep track in loops minute = int(time.strftime("%M"))
import plotly py = plotly.plotly("iancze", "0ttojbuvyj") import StellarSpectra from StellarSpectra import spectrum from StellarSpectra import constants as C from spectrum import DataSpectrum import numpy as np import sys from astropy.io import ascii myspec = DataSpectrum.open( "/home/ian/Grad/Research/Disks/StellarSpectra/tests/WASP14/WASP-14_2009-06-15_04h13m57s_cb.spec.flux", orders=np.array([22]), ) # Shift wl as close to 0. vz = -15 myspec.wls = myspec.wls * np.sqrt((C.c_kms + vz) / (C.c_kms - vz)) def return_line_labels(wl, tol=1): """Given a wl array, return the nearest n line labels next to the line, that are within tolerance = 1 Ang of each point.""" # for linelist_air.dat, col_starts=[3, 20], col_ends=[17, 28] # for linelist_kurucz.dat, col_starts=[3, 13], col_ends=[10, 20] lines = ascii.read(
#! /usr/bin/python import plotly #username = "******" #api_key = "7abemi2v86" credentials = open( "plotly.credentials", "r" ) ( username, api_key ) = credentials.readlines() username = username[0:-1] api_key = api_key[0:-1] credentials.close() py = plotly.plotly( username=username, key=api_key ) # Read one of the temp logs, break into two arrays def extract( filename, timestamp, temp ): data = open( filename, 'r' ) for line in data: ts, t = line.split( ',' ) ts = ts.strip( '"' ) t = float( t ) timestamp.append( ts ) temp.append( t ) data.close() udtimestamp = [] udtemp = []
import math import plotly import Quandl py = plotly.plotly("kausk", "p24ixqvuzo") import glob def plot(query, name): mydata = Quandl.get(name, authtoken="P4XxRkeVJCEmXmzJuESv", trim_start="2014-03-01", trim_end="2014-03-31", returns="numpy") stockdates = [str(i[0]) for i in mydata] openp = [str(i[1]) for i in mydata] high = [str(i[2]) for i in mydata] low = [str(i[3]) for i in mydata] close = [str(i[4]) for i in mydata] changes = [] for i in close: for j in openp: cf = ((float(i) - float(j)) / (float(j))) changes.append(cf)
def connect_to_ploty(usr='******', api='622ge3l1ww'): ply = plotly.plotly(usr, api) return ply
def main(list_of_list, username, key): """connect to plot.ly API, send data, create graph""" py = plotly.plotly(username, key) py.plot(list_of_list[0], list_of_list[1])
def getLabels(dataSet, centroids): # For each element in the dataset, chose the closest centroid. # Make that centroid the element's label. # Function: Get Centroids # ------------- # Returns k random centroids, each of dimension n. def getCentroids(dataSet, labels, k): pass # Each centroid is the geometric mean of the points that # have that centroid's label. Important: If a centroid is empty (no points have # that centroid's label) you should randomly re-initialize it. def x = np.array([1, 2, 3, 4, 5] y = np.array([8, 8, 8, 8, 8]) z = np.ones((5, 9)) np.sqrt(sum((x - y) ** 2)) np.sqrt(((z-x)**2).sum(axis=0)) # END of solution -a # ------------------ # solution -b # ----------- # Lloyd’s algorithm for performing k-means clustering import numpy as np def cluster_points(X, mu): clusters = {} for x in X: bestmukey = min([(i[0], np.linalg.norm(x-mu[i[0]])) \ for i in enumerate(mu)], key=lambda t:t[1])[0] try: clusters[bestmukey].append(x) except KeyError: clusters[bestmukey] = [x] return clusters def reevaluate_centers(mu, clusters): newmu = [] keys = sorted(clusters.keys()) for k in keys: newmu.append(np.mean(clusters[k], axis = 0)) return newmu def has_converged(mu, oldmu): return (set([tuple(a) for a in mu]) == set([tuple(a) for a in oldmu]) def find_centers(X, K): # Initialize to K random centers oldmu = random.sample(X, K) mu = random.sample(X, K) while not has_converged(mu, oldmu): oldmu = mu # Assign all points in X to clusters clusters = cluster_points(X, mu) # Reevaluate centers mu = reevaluate_centers(oldmu, clusters) return(mu, clusters) ############# import random def init_board(N): X = np.array([(random.uniform(-1, 1), random.uniform(-1, 1)) for i in range(N)]) return X def init_board_gauss(N, k): n = float(N)/k X = [] for i in range(k): c = (random.uniform(-1, 1), random.uniform(-1, 1)) s = random.uniform(0.05,0.5) x = [] while len(x) < n: a, b = np.array([np.random.normal(c[0], s), np.random.normal(c[1], s)]) # Continue drawing points from the distribution in the range [-1,1] if abs(a) < 1 and abs(b) < 1: x.append([a,b]) X.extend(x) X = np.array(X)[:N] return X # END of solution - b # ------------------- # soluton - c // better implementation # ----------- # Full Imports import sys import math import random import subprocess """ To use plotly integration you will need to: 1. Get a username/key from www.plot.ly/api and enter them below 2. Install the plotly module: pip install plotly """ PLOTLY_USERNAME = None PLOTLY_KEY = None if PLOTLY_USERNAME: from plotly import plotly def main(): # How many points are in our dataset? num_points = 10 # For each of those points how many dimensions do they have? dimensions = 2 # Bounds for the values of those points in each dimension lower = 0 upper = 200 # The K in k-means. How many clusters do we assume exist? num_clusters = 3 # When do we say the optimization has 'converged' and stop updating clusters opt_cutoff = 0.5 # Generate some points points = [makeRandomPoint(dimensions, lower, upper) for i in xrange(num_points)] # Cluster those data! clusters = kmeans(points, num_clusters, opt_cutoff) # Print our clusters for i,c in enumerate(clusters): for p in c.points: print " Cluster: ", i, "\t Point :", p # Display clusters using plotly for 2d data # This uses the 'open' command on a URL and may only work on OSX. if dimensions == 2 and PLOTLY_USERNAME: print "Plotting points, launching browser ..." plotClusters(clusters) class Point: ''' An point in n dimensional space ''' def __init__(self, coords): ''' coords - A list of values, one per dimension ''' self.coords = coords self.n = len(coords) def __repr__(self): return str(self.coords) class Cluster: ''' A set of points and their centroid ''' def __init__(self, points): ''' points - A list of point objects ''' if len(points) == 0: raise Exception("ILLEGAL: empty cluster") # The points that belong to this cluster self.points = points # The dimensionality of the points in this cluster self.n = points[0].n # Assert that all points are of the same dimensionality for p in points: if p.n != self.n: raise Exception("ILLEGAL: wrong dimensions") # Set up the initial centroid (this is usually based off one point) self.centroid = self.calculateCentroid() def __repr__(self): ''' String representation of this object ''' return str(self.points) def update(self, points): ''' Returns the distance between the previous centroid and the new after recalculating and storing the new centroid. ''' old_centroid = self.centroid self.points = points self.centroid = self.calculateCentroid() shift = getDistance(old_centroid, self.centroid) return shift def calculateCentroid(self): ''' Finds a virtual center point for a group of n-dimensional points ''' numPoints = len(self.points) # Get a list of all coordinates in this cluster coords = [p.coords for p in self.points] # Reformat that so all x's are together, all y'z etc. unzipped = zip(*coords) # Calculate the mean for each dimension centroid_coords = [math.fsum(dList)/numPoints for dList in unzipped] return Point(centroid_coords) def kmeans(points, k, cutoff): # Pick out k random points to use as our initial centroids initial = random.sample(points, k) # Create k clusters using those centroids clusters = [Cluster([p]) for p in initial] # Loop through the dataset until the clusters stabilize loopCounter = 0 while True: # Create a list of lists to hold the points in each cluster lists = [ [] for c in clusters] clusterCount = len(clusters) # Start counting loops loopCounter += 1 # For every point in the dataset ... for p in points: # Get the distance between that point and the centroid of the first # cluster. smallest_distance = getDistance(p, clusters[0].centroid) # Set the cluster this point belongs to clusterIndex = 0 # For the remainder of the clusters ... for i in range(clusterCount - 1): # calculate the distance of that point to each other cluster's # centroid. distance = getDistance(p, clusters[i+1].centroid) # If it's closer to that cluster's centroid update what we # think the smallest distance is, and set the point to belong # to that cluster if distance < smallest_distance: smallest_distance = distance clusterIndex = i+1 lists[clusterIndex].append(p) # Set our biggest_shift to zero for this iteration biggest_shift = 0.0 # As many times as there are clusters ... for i in range(clusterCount): # Calculate how far the centroid moved in this iteration shift = clusters[i].update(lists[i]) # Keep track of the largest move from all cluster centroid updates biggest_shift = max(biggest_shift, shift) # If the centroids have stopped moving much, say we're done! if biggest_shift < cutoff: print "Converged after %s iterations" % loopCounter break return clusters def getDistance(a, b): ''' Euclidean distance between two n-dimensional points. Note: This can be very slow and does not scale well ''' if a.n != b.n: raise Exception("ILLEGAL: non comparable points") ret = reduce(lambda x,y: x + pow((a.coords[y]-b.coords[y]), 2),range(a.n),0.0) return math.sqrt(ret) def makeRandomPoint(n, lower, upper): ''' Returns a Point object with n dimensions and values between lower and upper in each of those dimensions ''' p = Point([random.uniform(lower, upper) for i in range(n)]) return p def plotClusters(data): ''' Use the plotly API to plot data from clusters. Gets a plot URL from plotly and then uses subprocess to 'open' that URL from the command line. This should open your default web browser. ''' # List of symbols each cluster will be displayed using symbols = ['circle', 'cross', 'triangle-up', 'square'] # Convert data into plotly format. traceList = [] for i, c in enumerate(data): data = [] for p in c.points: data.append(p.coords) # Data trace = {} trace['x'], trace['y'] = zip(*data) trace['marker'] = {} trace['marker']['symbol'] = symbols[i] trace['name'] = "Cluster " + str(i) traceList.append(trace) # Centroid (A trace of length 1) centroid = {} centroid['x'] = [c.centroid.coords[0]] centroid['y'] = [c.centroid.coords[1]] centroid['marker'] = {} centroid['marker']['symbol'] = symbols[i] centroid['marker']['color'] = 'rgb(200,10,10)' centroid['name'] = "Centroid " + str(i) traceList.append(centroid) # Log in to plotly py = plotly(username=PLOTLY_USERNAME, key=PLOTLY_KEY) # Style the chart datastyle = {'mode':'markers', 'type':'scatter', 'marker':{'line':{'width':0}, 'size':12, 'opacity':0.6, 'color':'rgb(74, 134, 232)'}} resp = py.plot(*traceList, style = datastyle) # Display that plot in a browser cmd = "open " + resp['url'] subprocess.call(cmd, shell=True) if __name__ == "__main__": main() # END of soluton - c #------------------- # solution -d # ----------- import os import numpy as np # kmeans clustering algorithm # data = set of data points # k = number of clusters # c = initial list of centroids (if provided) # def kmeans(data, k, c): centroids = [] centroids = randomize_centroids(data, centroids, k) old_centroids = [[] for i in range(k)] iterations = 0 while not (has_converged(centroids, old_centroids, iterations)): iterations += 1 clusters = [[] for i in range(k)] # assign data points to clusters clusters = euclidean_dist(data, centroids, clusters) # recalculate centroids index = 0 for cluster in clusters: old_centroids[index] = centroids[index] centroids[index] = np.mean(cluster, axis=0).tolist() index += 1 print("The total number of data instances is: " + str(len(data))) print("The total number of iterations necessary is: " + str(iterations)) print("The means of each cluster are: " + str(centroids)) print("The clusters are as follows:") for cluster in clusters: print("Cluster with a size of " + str(len(cluster)) + " starts here:") print(np.array(cluster).tolist()) print("Cluster ends here.") return # Calculates euclidean distance between # a data point and all the available cluster # centroids. def euclidean_dist(data, centroids, clusters): for instance in data: # Find which centroid is the closest # to the given data point. mu_index = min([(i[0], np.linalg.norm(instance-centroids[i[0]])) \ for i in enumerate(centroids)], key=lambda t:t[1])[0] try: clusters[mu_index].append(instance) except KeyError: clusters[mu_index] = [instance] # If any cluster is empty then assign one point # from data set randomly so as to not have empty # clusters and 0 means. for cluster in clusters: if not cluster: cluster.append(data[np.random.randint(0, len(data), size=1)].flatten().tolist()) return clusters # randomize initial centroids def randomize_centroids(data, centroids, k): for cluster in range(0, k): centroids.append(data[np.random.randint(0, len(data), size=1)].flatten().tolist()) return centroids # check if clusters have converged def has_converged(centroids, old_centroids, iterations): MAX_ITERATIONS = 1000 if iterations > MAX_ITERATIONS: return True return old_centroids == centroids # END of solution - d # ------------------- # solution -e # ----------- # k-means clustering method to cluster image pixels into k partitions. # Method takes input RGB image and k value, returns a binary image which has # different labels for different segments. In the output image, each # input pixel value is replaced with the mean value it is assigned to. import os from random import randint import time from skimage import io import numpy as np import matplotlib.pyplot as plt # creates RGB pixel object from image pixels class RGB: def __init__(self, rgb_pixel): self.r = int(rgb_pixel[0]) self.g = int(rgb_pixel[1]) self.b = int(rgb_pixel[2]) def __str__(self): return ''.join("r:" + str(self.r) + " g:" + str(self.g) + " b:" + str(self.b)) # creates Centroid object from coordinates and rgb pixel class Centroid: x = 0 y = 0 rgb = RGB(np.zeros(3)) def __init__(self, xy): self.x = xy[0] self.y = xy[1] def __str__(self): return ''.join("x:" + str(self.x) + " y:" + str(self.y) + " rgb:" + str(self.rgb)) # calculate_rgb_distance(pixel, centroid) # input - image and centroid Pixel(r,g,b) # returns - euclidean distance between two rgb pixels def calculate_rgb_distance(pixel, center): p1 = np.array((pixel.r, pixel.g, pixel.b)) p2 = np.array((center.r, center.g, center.b)) return np.linalg.norm(p1 - p2) # pick_random_xy(shape) # input - RGB image matrix shape (rows, columns, rgb) # returns - random pixel(x,y) coordinates def pick_random_xy(shape): return [randint(0, shape[0] - 1), randint(0, shape[1] - 1)] # k_means(image, k) # input - rgb image matrix # - k number of clusters # - difference threshold to stop the algorithm loop(optional) # returns - labelImage, labeled image matrix # - centroids, k centroids after calculation def k_means(image, cluster_count, mean_diff_threshold=5.0): time_start = time.time() # create a matrix of zeros with the same shape to store centroid index assignments for each pixel label_image = np.zeros((image.shape[0], image.shape[1])) print "label count k:", k print "total mean diff threshold:", mean_diff_threshold # k-means algorithm # STEPS: # 0 - Choose k random centroid coordinates # For each pixel x: # 1 - Calculate rgb distance of x from each centroid # 2 - Assign x to closest centroid's cluster # For each centroid c: # 3 - Calculate rgb means of assigned pixels for that c (move the centroid) # repeat until all x are stable # 0 - Choose k random centroid coordinates centroids = [] for i in range(cluster_count): c = Centroid(pick_random_xy(image.shape)) # set random coordinates c.rgb = RGB(image[c.x][c.y]) # set rgb values centroids.append(c) iteration_count = 0 while iteration_count < 10: # repeat until all x are stable, max 10 iterations percent_diff = 0.0 # holds percentage in mean change # k x 3 dimensional array to hold cluster values, will be used to calculate new means cluster_values = np.ones((len(centroids), 3)) cluster_pixel_counts = np.ones((len(centroids))) # 1 - Calculate rgb distance of x from each centroid for x in xrange(image.shape[0]): for y in xrange(image.shape[1]): image_pixel = RGB(image[x][y]) min_dist = 255 min_dist_index = 0 for idx, m in enumerate(centroids): distance = calculate_rgb_distance(image_pixel, m.rgb) if distance < min_dist: min_dist = distance min_dist_index = idx # 2 - Assign pixel to closest centroid's cluster(index) label_image[x][y] = min_dist_index # record pixel values to calculate means cluster_values[min_dist_index][0] += image_pixel.r cluster_values[min_dist_index][1] += image_pixel.g cluster_values[min_dist_index][2] += image_pixel.b cluster_pixel_counts[min_dist_index] += 1 # 3 - Calculate rgb means of assigned pixels for their centroids for idx, m in enumerate(centroids): previous_sum = m.rgb.r + m.rgb.g + m.rgb.b m.rgb.r = int(cluster_values[idx, 0] / cluster_pixel_counts[idx]) m.rgb.g = int(cluster_values[idx, 1] / cluster_pixel_counts[idx]) m.rgb.b = int(cluster_values[idx, 2] / cluster_pixel_counts[idx]) current_sum = m.rgb.r + m.rgb.g + m.rgb.b # total percent change in cluster means if previous_sum > 0: percent_diff += (abs((current_sum - previous_sum) / float(previous_sum))) * 100.0 else: percent_diff = 0.0 iteration_count += 1 print "total percent difference in means", percent_diff, "iteration", iteration_count if mean_diff_threshold > percent_diff > 0.0: print "\nStopped at percent difference in means:", percent_diff print "total iterations:", iteration_count print "total time spent:", '%0.3f ms' % ((time.time() - time_start) * 1000.0) print "\ncalculated cluster means:" for idx, m in enumerate(centroids): print centroids[idx].rgb break return label_image, centroids # load RGB image, alpha channel removed fileName = os.path.join('strawberry.png') inputImage = io.imread(fileName) # number of clusters/centroids k = 4 # run k-means algorithm on input binaryImage, centroid_array = k_means(inputImage, k) outputImage = np.zeros_like(inputImage) # apply cluster centroid means to assigned pixels for x1 in xrange(binaryImage.shape[0]): for y1 in xrange(binaryImage.shape[1]): outputImage[x1][y1][0] = centroid_array[int(binaryImage[x1][y1])].rgb.r outputImage[x1][y1][1] = centroid_array[int(binaryImage[x1][y1])].rgb.g outputImage[x1][y1][2] = centroid_array[int(binaryImage[x1][y1])].rgb.b # create plots and save output image fig = plt.figure() imgOrig = plt.subplot2grid((2, 2), (0, 0)) imgOrig.imshow(inputImage, interpolation='nearest') imgOrig.set_title('Original Image') imgLabeled = plt.subplot2grid((2, 2), (0, 1)) imgLabeled.imshow(binaryImage, cmap=plt.cm.gray, interpolation='nearest') imgLabeled.set_title('Labeled Image') imgOutput = plt.subplot2grid((2, 2), (1, 0), colspan=2) imgOutput.imshow(outputImage, interpolation='nearest') imgOutput.set_title('Output Image') plt.savefig('label-' + str(k) + '-output.png') plt.show()
cursor = connection.cursor() cursor.execute('select * from test_table') for row in cursor.fetchall(): name.append(str(row[0])) age.append(row[1]) # append lists to a list all_data.extend([name, age]) # close cursor, connection cursor.close() return all_data if __name__ == '__main__': # Instantiate new_db = TestDatabase("test_database") # call our instance methods new_db.create() all_data = new_db.search() print all_data == [['Bobby', 'Michael', 'John'], [10, 20, 20]] py = plotly.plotly("GET_YOUR_OWN_USERNAME", "GET_YOUR_OWN_KEY") py.plot(all_data[0], all_data[1]) py.plot([name][age])
print(helpful_msg) c.logger.error(helpful_msg) raise e # Extract the credentials from the config file username = plotly_user_config["plotly_username"] api_key = plotly_user_config["plotly_api_key"] stream_tokens = plotly_user_config["plotly_streaming_tokens"] if "show_this_many_points" in plotly_user_config: maxpoints = plotly_user_config["show_this_many_points"] else: maxpoints = 500 # Make the initializing plotly request # Pass all stream tokens from the config file into this plot p = plotly.plotly(username, api_key) p.ioff() res = p.plot( [{"x": [], "y": [], "stream": {"token": token, "maxpoints": maxpoints}} for token in stream_tokens], filename="Stream from Yun", fileopt="overwrite", ) if "stream-status" in res and "All Streams Go!" in res["stream-status"]: status = "Plot initialized at: {url}\nwith token(s): {tokens}".format( url=res["url"], tokens=", ".join(stream_tokens) ) print(status) c.logger.info(status) else: status = "Error initializing plot: {msg}".format(msg=res["error"])
import plotly import csv import time import datetime from WeatherUndergroundForecast import WeatherUndergroundForecast # This script will download the lastest forecast data every hour and upload the results to plotly for viewing KEY_FILE = 'keys/plotly_api_key' with open(KEY_FILE,'r') as api_key: plotly_username = csv.reader(api_key).next()[1] plotly_api = csv.reader(api_key).next()[1] py = plotly.plotly(plotly_username, plotly_api) HOME_STATE = 'MA' HOME_TOWN = 'Wilmington' wundergnd = WeatherUndergroundForecast() wundergnd.get_api_keyi_from_file('keys/weather_underground_api_key') wundergnd.state = HOME_STATE wundergnd.town = HOME_TOWN while True: print "Updating at ",datetime.datetime.now() wundergnd.updateForecast() # Temperature times,temps = wundergnd.get_temp_forecast()
def call_plotly(list_of_list,username,key): """connect to plot.ly API, send data, create graph""" py = plotly.plotly(username,key) py.plot(list_of_list[0],list_of_list[1])
import os from bottle import run, template, get, post, request from plotly import plotly from numpy import * from random import randint py = plotly(username='******', key='qqli2tgsw5') index_html = '''View your plot results at {{ plot_url }}''' @get('/plot') def form(): return '''<h2>Graph via Plot.ly</h2> <form method="POST" action="/plot"> X1 min: <input name="x1_min" type="text" /> X1 max: <input name="x1_max" type="text" /><br/> X2 min: <input name="x2_min" type="text" /> X2 max: <input name="x2_max" type="text" /><br/> X3 min: <input name="x3_min" type="text" /> X3 max: <input name="x3_max" type="text" /><br/> <input type="submit" /> </form>''' @post('/plot') def submit(): x1_min = int(request.forms.get('x1_min')) x1_max = int(request.forms.get('x1_max')) x2_min = int(request.forms.get('x2_min')) x2_max = int(request.forms.get('x2_max'))
def main(argv): total = len(argv) if total < 3: print "Utilization: python topicmodelling.py <input_csv_file> <saved_models_filename> <no_topics>" exit(0) i_file = str(argv[1]) filename = str(argv[2]) no_topics = argv[3] if os.path.isfile(i_file): # Read articles from tsv articles = read_articles(i_file) # Pull out the titles/abstracts of each article # articleDocs = [x["title"] for x in articles] articleDocs = [x["abstract"] for x in articles] # articleDocs = [" ".join([x["title"], x["abstract"]]) for x in articles] # stopword list stopwords = stop_word_list() # Process them to clean bag of words # articleDocs = process_articles(articleDocs,stopwords) articleDocs = process_articles_gensim(articleDocs) # create gensim dict,corpus with the tweet bodies dictionary, corpus = put_in_dict_corpus(filename, articleDocs) # create TF-IDF model and corpus model, corpus_tfidf = tf_idf(filename, corpus) # Running LDA and printing results #comment just to run lsi myLDA = lda(filename, corpus, dictionary, int(no_topics)) LDAtopics = myLDA.show_topics(topics=int(no_topics), topn=20, formatted=False) i = 0 lda_topics_words = {} for t in LDAtopics: print "LDA Topic %i" % (i) print "=======" topic_score = [] for w in t: topic_score.append((w[1], w[0])) print "%-*s %f" % (15, w[1], w[0]) lda_topics_words[i] = topic_score i += 1 # Running LSI and printing results myLSI = lsi(filename, corpus_tfidf, dictionary, int(no_topics)) LSItopics = myLSI.show_topics(num_words=20, formatted=False) i = 0 lsi_topics_words = {} for t in LSItopics: print "LSI Topic %i" % (i) print "=======" topic_score = [] for w in t: topic_score.append((w[1], w[0])) print "%-*s %f" % (15, w[1], w[0]) lsi_topics_words[i] = topic_score i += 1 # Running HDP and printing results myHDP = hdp(filename, corpus_tfidf, dictionary) myHDP.print_topics(topics=20, topn=10) n_t = list(xrange(int(no_topics))) topic_doc_count_lda = dict((t, 0) for t in n_t) topic_doc_count_lsi = dict((t, 0) for t in n_t) for i in range(0, len(articles)): print "\n" print "=" * 80 print "Document Title: ", articles[i]["title"] print "Document Abstract: ", articles[i]["abstract"] print "Processed Document: ", articleDocs[i] print "TF_IDF Corpus: ", str(corpus_tfidf[i]) print "Matrix format: " + str(corpus[i]) print "Topic probability mixture: " + str(myLDA[corpus[i]]) # comment just to run lsi print "LDA:" print "Topic probability mixture: " + str(myLDA[corpus[i]]) prob_topic_no_lda = sorted(myLDA[corpus_tfidf[i]], key=lambda tup: tup[1], reverse=True) for j, each_lda in enumerate(prob_topic_no_lda[:2]): j += 1 print "%d Maximally probable topic LDA: topic #%s" % (j, str(each_lda[0])) # print 'Topic words: %s' % myLDA.print_topic(each_lda[0],4) print "Topic words: %s" % show_top_topics(lda_topics_words, each_lda[0], 4) if str(each_lda[0]) in topic_doc_count_lda: topic_doc_count_lda[str(each_lda[0])] += 1 else: topic_doc_count_lda[str(each_lda[0])] = 1 print "-" * 80 print "LSI:" if myLSI[corpus[i]] != []: print "Topic probability mixture: " + str(myLSI[corpus_tfidf[i]]) prob_topic_no_lsi = sorted(myLSI[corpus_tfidf[i]], key=lambda tup: tup[1], reverse=True) for k, each_lsi in enumerate(prob_topic_no_lsi[:2]): k += 1 print "%d Maximally probable topic LSI: topic #%s" % (k, str(each_lsi[0])) # print 'Topic words: %s' % myLSI.print_topic(each_lsi[0],4) print "Topic words: %s" % show_top_topics(lsi_topics_words, each_lsi[0], 4) if str(each_lsi[0]) in topic_doc_count_lsi: topic_doc_count_lsi[str(each_lsi[0])] += 1 else: topic_doc_count_lsi[str(each_lsi[0])] = 1 else: print "No LSI model due to blank tfidf-corpus" print "=" * 80 print "=" * 80 print "=" * 80 py = plotly.plotly("somyamohanty", "94wksibtod") print "LDA documents/topic" x_lda = [] y_lda = [] label_lda = [] for k, v in topic_doc_count_lda.items(): print "Topic %s in LDA models has %s number of documents" % (str(k), str(v)) label = show_top_topics_plot(lda_topics_words, int(k), 4) topic_str = "Topic - " + str(k) x_lda.append(topic_str) y_lda.append(v) label_lda.append(label) layout = { "title": "LDA Topic-Document Distribution - " + str(no_topics) + " topics", "xaxis": {"title": "Topic"}, "yaxis": {"title": "Number of Document"}, } py.iplot( [{"x": x_lda, "y": y_lda, "type": "bar", "name": "Number of Docs", "text": label_lda}], layout=layout, filename="lda_" + str(no_topics), fileopt="overwrite", width=1000, height=650, ) lda_plot_dict = {"x": x_lda, "y": y_lda, "labels": label_lda} print "\n" print "LSI documents/topic" x_lsi = [] y_lsi = [] label_lsi = [] for k, v in topic_doc_count_lsi.items(): print "Topic %s in LSI models has %s number of documents" % (str(k), str(v)) label = show_top_topics_plot(lsi_topics_words, int(k), 4) topic_str = "Topic - " + str(k) x_lsi.append(topic_str) y_lsi.append(v) label_lsi.append(label) layout = { "title": "LSI Topic-Document Distribution - " + str(no_topics) + " topics", "xaxis": {"title": "Topic"}, "yaxis": {"title": "Number of Document"}, "xref": "paper", "yref": "paper", "showarrow": False, "x": 0, "y": 0, } py.iplot( [{"x": x_lsi, "y": y_lsi, "type": "bar", "name": "Number of Docs", "text": label_lsi}], layout=layout, filename="lsi_" + str(no_topics), fileopt="overwrite", width=1000, height=650, ) lsi_plot_dict = {"x": x_lsi, "y": y_lsi, "labels": label_lsi} plot_data = {"lda": lda_plot_dict, "lsi": lsi_plot_dict} with open("50_def_alpha_plot_data.json", "w") as f: f.write(json.dumps(plot_data)) f.close()
import plotly import numpy from scipy import signal py = plotly.plotly('gurio', '4y7balp80g') def autocorr(x): result = numpy.correlate(x, x, mode='full') return result[result.size/2:] x_args = [] y_args = [] prev_day_args = [] diff = [] day_num = 1 cur_day = '' prev_day = '' sum_per_day = 0 lagged_sum = 0 mean_price = 0 amount_of_ticks = 0 ticks_per_day = 0 value_per_ticks = 2 inp_line_args_array = [] big_lag = 24 small_lag = 6 file = open('USDJPY_130101_131231', 'r') parameters = file.readline()[:-2].split(',')
#!/usr/bin/python import datetime import serial import RPi.GPIO as GPIO import plotly tokens = ['token1', 'token2'] username = '******' api_key = 'plotly_api_key_here' p = plotly.plotly(username, api_key) streams = [plotly.stream(token) for token in tokens] # Initialize plot with stream tokens print p.plot([{ 'x': [], 'y': [], 'type': 'scatter', 'stream': { 'token': token, 'maxpoints': 40 } } for token in tokens], filename='Atlas Streaming Multiplexer', fileopt='overwrite') # Set multiplexer to read 00 by setting S0 and S1 to low GPIO.cleanup() GPIO.setmode(GPIO.BOARD) S0_pin = 18 S1_pin = 22
"type":"scatter", "name":"Stoping times averages (%s)" % rang, "line":{"opacity": 0.3, "color":"gray", "width":3, "dash":"solid" }, } #------------------------------------------------------------------------------- #response = plotly.signup(username, email) #Signup #loggin username = str(raw_input("Username: "******"e-mail: ")) key = str(raw_input("Key: ")) py = plotly.plotly(username, key) response = py.plot(collatzTrace,collatzAverages) py.style(collatzTraceStyles,collatzAveragesStyles) url = response['url'] filename = response['filename'] if url: os.system('firefox %s' % url) #os.system('explorer %s' % url) #os.system('google-chrome %s' % url) #This https://plot.ly/~GermanJimenez/0
import os import plotly py = plotly.plotly(username=os.getenv("PLOTLY_USERNAME"), key=os.getenv("PLOTLY_APIKEY")) categories = ["1GB Server", "30GB Server"] CloudServers = { "name": "Cloud Servers", "x": categories, "y": [ 867.2285, 138.175333333333, ], "type": "bar" } PerfCloudServers = { "name": "Performance Cloud Servers", "x": categories, "y": [ 819.282833333333, 114.945666666667, ], "type": "bar" } PerfCloudServersHVM = { "name": "Performance Cloud Servers (HVM)", "x": categories,
import plotly import csv import time import datetime from WeatherUndergroundForecast import WeatherUndergroundForecast # This script will download the lastest forecast data every hour and upload the results to plotly for viewing KEY_FILE = 'keys/plotly_api_key' with open(KEY_FILE, 'r') as api_key: plotly_username = csv.reader(api_key).next()[1] plotly_api = csv.reader(api_key).next()[1] py = plotly.plotly(plotly_username, plotly_api) HOME_STATE = 'MA' HOME_TOWN = 'Wilmington' wundergnd = WeatherUndergroundForecast() wundergnd.get_api_keyi_from_file('keys/weather_underground_api_key') wundergnd.state = HOME_STATE wundergnd.town = HOME_TOWN while True: print "Updating at ", datetime.datetime.now() wundergnd.updateForecast() # Temperature times, temps = wundergnd.get_temp_forecast() timesfl, feelslike = wundergnd.get_feels_like_temp_forecast() # Snowfall times_snow, snow = wundergnd.get_snow_forecast()
# 1) Setup plot.ly # <markdowncell> # Import plot.ly with python api [see https://plot.ly/api/python/getting-started ], # sign in, create shortcut to plot.ly object and check version: # <codecell> import plotly username = '******' # my username api_key = 'a35m7g6el5' # my api key py = plotly.plotly(username, api_key) # shortcut to plot.ly object plotly.__version__ # check ploty.ly version # <markdowncell> # Define a function to embed plot.ly graph in IPython notebook [ref. http://nbviewer.ipython.org/github/plotly/IPython-plotly/blob/master/Plotly%20Quickstart.ipynb ]: # <codecell> from IPython.display import HTML def show_plot(url, width=1000, height=500): s = '<iframe height="%s" id="igraph" scrolling="no" seamless="seamless" src="%s" width="%s"></iframe>' %\ (height+50, "/".join(map(str,[url, width, height])), width+50) return HTML(s)
import plotly py = plotly.plotly('JohnnyLam', 'T4JhRZWvDWXlqWl0EXxv') a = (1, 2, 3, 4, 5) b = (2, 3, 5, 6, 1) py.plot(a, b)
import plotly py = plotly.plotly(username='******', key='**put your api key here**') y=0 t=0 dt=0.01 g=9.8 v=4 yp=[] tp=[] while y>=0: v=v-g*dt y=y+v*dt t=t+dt yp=yp+[y] tp=tp+[t] response=py.plot(tp,yp) url=response['url'] filename=response['filename'] print(url) print(filename)
# SPI port on the ADC to the GPIO Pins on the Raspi SPICLK = 18 SPIMISO = 23 SPIMOSI = 24 SPICS = 25 # set up the SPI interface pins GPIO.setup(SPIMOSI, GPIO.OUT) GPIO.setup(SPIMISO, GPIO.IN) GPIO.setup(SPICLK, GPIO.OUT) GPIO.setup(SPICS, GPIO.OUT) # temperature sensor connected channel 0 of mcp3008 adcnum = 0 #init plotly py = plotly.plotly(USERNAME, API_KEY) response = py.plot([{ 'x': [], 'y': [], 'name': TEMP_LEGEND, 'type': chart_type }], filename=FILENAME, fileopt='extend', layout=LAYOUT) os.system('clear') print(b.GREEN + "========================================================") print("Successfully Posted to Plot.ly! Here is your Graph info:") print("========================================================\n")
import plotly py = plotly.plotly("dkens", "qcskp0copu") from numpy import * # generate some random sets of data y0 = random.randn(100) / 5. + 0.5 x0 = random.randn(100) / 5. + 0.5 y1 = random.standard_cauchy(20) x1 = random.standard_cauchy(20) x = concatenate([x0, x1]) y = concatenate([y0, y1]) scatter0 = { 'x': x0, 'y': y0, 'name': 'normal', 'type': 'scatter', 'mode': 'markers', 'marker': { 'size': 4, 'opacity': 0.5 } } scatter1 = { 'x': x1, 'y': y1, 'name': 'cauchy', 'type': 'scatter',
import os import plotly py = plotly.plotly(username=os.getenv("PLOTLY_USERNAME"), key=os.getenv("PLOTLY_APIKEY")) categories = ["Dhrystone", "Whetstone", "Execl Throughput", "File Copy 1024", "File Copy 256", "File Copy 4096", "Pipe Throughput", "Pipe-based Ctx Switch", "Process Creation", "Shell Scripts (1)", "Shell Scripts (8)", "Syscall Overhead", "System Score", ] CloudServers = {"name": "Cloud Servers", "x": categories, "y": [2070.26666666667, 549.466666666667, 197.75, 574.25, 371.916666666667, 1026.91666666667, 298.933333333333, 74.8666666666667,
import plotly from pickle import load from numpy import array, mean, vstack py = plotly.plotly("Mehlins", "vq0aqwkoz4") with open('fixed5', 'rb') as f: results = load(f) bests1 = mean(array([result['best_fitnesses'] for result in results]), axis=0) averages1 = mean(array([result['average_fitnesses'] for result in results]), axis=0) final_bests1 = [result['best_fitnesses'][-1] for result in results] with open('adapt', 'rb') as f: results = load(f) bests2 = mean(array([result['best_fitnesses'] for result in results]), axis=0) averages2 = mean(array([result['average_fitnesses'] for result in results]), axis=0) final_bests2 = [result['best_fitnesses'][-1] for result in results] with open('sigmas', 'rb') as f: results = load(f) sigmas = mean(array(results), axis=0) indexes = range(len(bests1) + 1) trace1 = {'x': indexes, 'y': bests1} trace2 = {'x': indexes, 'y': averages1} trace3 = {'x': indexes, 'y': bests2} trace4 = {'x': indexes, 'y': averages2} trace5 = {'x': indexes, 'y': sigmas}