def create_trend_graphs(top_n): header_h = gd['reports'][2] header_c = gd['reports'][3] phrases = gd.values[:, 9] # all trends imp_change = gd.values[:, 10] data_h, data_c = {}, {} text_h, text_c = [], [] hot, cold = 0, 0 # counters for each list plot_h = figure(plot_width=600, plot_height=400, title=header_h) plot_c = figure(plot_width=600, plot_height=400, title=header_c) for i in range(len(phrases)): if hot == top_n and cold == top_n: break phrase = phrases[i] change = float(imp_change[i]) if phrase in white_list: phrase = cut_phrase(phrase) if change > 0: if hot < top_n: data_h[phrase] = change text_h.append('+' + str(("%.1f" % change)) + '%') hot += 1 elif cold < top_n: data_c[phrase] = change text_c.append(str(("%.1f" % change)) + '%') cold += 1 if len(data_h.keys()) > 0: phrases_h = sorted(data_h, key=data_h.get) changes_h = sorted(data_h.values()) source = ColumnDataSource( dict(y=phrases_h, x=changes_h, text=text_h[::-1])) title = header_h plot_h = figure(plot_width=600, plot_height=400, tools="save", title=title, x_range=ranges.Range1d(start=0, end=max(changes_h) + 20), y_range=source.data["y"], tooltips='@y<br>change: @x') labels = LabelSet(x='x', y='y', text='text', level='glyph', x_offset=5, y_offset=0, source=source, render_mode='canvas', text_color="#1d6d34") plot_h.hbar(source=source, right='x', y='y', left=0, height=0.5, color="#1d6d34") plot_h.add_layout(labels) plot_h.xaxis.visible = False if len(data_c.keys()) > 0: phrases_c = sorted(data_c, key=data_c.get) changes_c = sorted(data_c.values()) source = ColumnDataSource( dict(y=phrases_c[::-1], x=changes_c[::-1], text=text_c[::-1])) plot_c = figure(plot_width=600, plot_height=400, tools="save", title=header_c, x_range=ranges.Range1d(start=min(changes_c) - 10, end=20), y_range=source.data["y"], tooltips='@y<br>change: @x') labels = LabelSet(x=0, y='y', text='text', level='glyph', x_offset=5, y_offset=0, source=source, render_mode='canvas', text_color="#912605") plot_c.hbar(source=source, right='x', y='y', left=0, height=0.5, color="#912605") plot_c.add_layout(labels) plot_c.xaxis.visible = False return [plot_h, plot_c]
def build_bar(score, col, source): x_label = "Protocol" y_label = score title = "Comparison (today): " + score if score != "Activity Score": rangepre = ranges.Range1d(start=0, end=125) else: rangepre = ranges.Range1d(start=0, end=100) plot = figure(plot_width=1120, plot_height=500, tools="save", background_fill_color="grey", background_fill_alpha=.1, x_axis_label=x_label, y_axis_label=y_label, title=title, x_range=source.data["protocol"], y_range=rangepre) plot.min_border_bottom = 120 plot.left[0].formatter.use_scientific = False if score != "Activity Score": plot.vbar(x='protocol', top=col, bottom=0, width=0.5, color='#5DA5DA', source=source) labels = LabelSet(x='protocol', y=col, text=col, level='glyph', x_offset=-13, y_offset=0.5, source=source, render_mode='canvas', text_font='arial', text_font_size='8.5pt') else: plot.vbar(x='protocol', top=col, bottom=0, width=0.5, color='#FAA43A', source=source) labels = LabelSet(x='protocol', y=col, text=col, level='glyph', x_offset=-6, y_offset=0.5, source=source, render_mode='canvas', text_font='arial', text_font_size='8.5pt') plot.add_layout(labels) return plot
def __init__(self, W, H, YaxisLabel): Curtailment = 1 self.source = ColumnDataSource( dict(x=['Smart', 'Unity', 'Disabled'], y=[0, 0, 0], c=['red', 'blue', 'green'])) self.Plot = figure(plot_width=W, plot_height=H, tools="save", x_range=self.source.data["x"], y_range=ranges.Range1d( start=0, end=max(self.source.data["y"]) * 1.2)) self.Plot.toolbar.logo = None # self.Plot.xaxis.axis_label = 'Time [h]' self.Plot.yaxis.axis_label = YaxisLabel self.Plot.vbar(source=self.source, x='x', top='y', bottom=0, width=0.6, color='c', alpha=0.5) self.final_layout = self.Plot return
def features_bar(order, xs, ys, yerrs): feature_dict = { 'gdp': 'GDP', 'population': "Population", 'md2do': "MD:DO Ratio", 'nurses_never': "Nurse Comm. Nev.", 'nurses_usually': "Nurses Comm. Usual.", 'nurses_always': "Nurses Comm. Alw.", 'doctors_never': "Docs Comm. Nev.", 'doctors_usually': "Docs Comm. Usual.", 'doctors_always': "Docs Comm. Alw.", 'error_disc': "Error Disclosure", 'time_after_treatment': "Time All. Aft. Treatment", 'time_after_discovery': "Time All. Aft. Discovery", 'max_time': "Max. Time All.", 'cap': "P.O. Cap", 'cap_death_injury': "P.O. Cap (Death/Serious Injury)" } p = figure(plot_width=400, plot_height=600, y_axis_label='Feature Importance', y_range=ranges.Range1d(start=0, end=0.35)) p.yaxis.axis_label_text_font_size = "15pt" base, lower, upper = [], [], [] err_xs = [] err_ys_low = [] err_ys_up = [] #xs=range(X.shape[1]) #ys=importances[indices] #yerrs=std[indices] for x, y, yerr in zip(xs, ys, yerrs): err_xs.append(x) err_ys_low.append(y - yerr) err_ys_up.append(y + yerr) base = xs lower = err_ys_low upper = err_ys_up source_error = ColumnDataSource( data=dict(base=base, x=order, lower=lower, upper=upper, values=ys)) p.vbar(source=source_error, x='base', top='values', bottom=0, width=0.8, color='firebrick') p.add_layout( Whisker(source=source_error, base="base", upper="upper", lower="lower", level='overlay')) p.xaxis.ticker = base label_dict = {} for num, label in zip(base, order): label_dict[num] = feature_dict[label] p.xaxis.major_label_overrides = label_dict p.xaxis.major_label_orientation = np.pi / 4 p.xaxis.major_label_text_font_size = "12pt" return (p)
def create_custom_trends_graph(): header = gd['reports'][4] phrases = gd.values[:, 9] imp_change = gd.values[:, 10] phrases_h, changes_h = [], [] phrases_c, changes_c = [], [] text_h, text_c = [], [] custom_count = 0 for i in range(len(phrases)): phrase = phrases[i] change = float(imp_change[i]) if phrase in custom_nps: if custom_count > 30: logger.warning("too many custom phrases (>30). Showing partial list") break phrase = cut_phrase(phrase) if change > 0: phrases_h.append(phrase) changes_h.append(change) text_h.append('+' + str(("%.1f" % change)) + '%') else: phrases_c.append(phrase) changes_c.append(change) text_c.append(str(("%.1f" % change)) + '%') custom_count += 1 changes = changes_h + changes_c text = text_h + text_c trends = phrases_h + phrases_c colors = [] if len(changes_h) > 0: for i in range(len(changes_h)): colors.append("#1d6d34") if len(changes_c) > 0: for i in range(len(changes_c)): colors.append("#912605") if len(changes) < 10: # pad with 10 blanks changes += [0] * (10 - len(changes)) text += ' ' * (10 - len(text)) trends += [str((10 - len(trends)) - i) for i in range(0, (10 - len(trends)))] colors += ['white'] * (10 - len(colors)) source = ColumnDataSource( dict(y=trends[::-1], x=changes[::-1], colors=colors[::-1], text=text[::-1])) plot = figure(title=header, plot_width=600, plot_height=400, tools="save", x_range=ranges.Range1d(start=min(changes) - 10, end=max(changes) + 20), y_range=source.data["y"], tooltips='@y<br>change: @x') labels = LabelSet(x=max(changes) + 5, y='y', text='text', level='glyph', x_offset=0, y_offset=-10, source=source, render_mode='canvas') plot.hbar(source=source, right='x', y='y', left=0, height=0.5, color='colors') plot.add_layout(labels) return plot
def display_results(results): #parsed_results_filters, parsed_results_ensembles = parse_results(results) averaged_results = { key: np.round(np.average(results[key]), decimals=4) for key in results } output_file("bar_sorted.html") x = [key for key in averaged_results] y = np.array([averaged_results[key] for key in averaged_results]) args = np.argsort(y) x = np.array(x)[args] y = y[args] source = ColumnDataSource(dict(x=x, y=y)) x_label = "Filter_Ensemble set" y_label = "Performance" title = "Performance bar plot" p = figure(plot_width=1100, plot_height=400, tools="save", x_axis_label=x_label, y_axis_label=y_label, title=title, x_minor_ticks=2, x_range=source.data["x"], y_range=ranges.Range1d(start=np.min(y) - .25, end=np.max(y) + 0.05)) labels = LabelSet(x='x', y='y', text='y', level='glyph', x_offset=-50, y_offset=0, source=source, render_mode='canvas') p.vbar(source=source, x='x', top='y', bottom=0, width=0.3, color=PuBu[7][2]) p.add_layout(labels) show(p)
def __init__(self, W, H, YaxisLabel, L1, L2, ZeroBase = False): self.Source = ColumnDataSource(dict(x=['Smart', 'Unity', 'Disabled'], y=[0, 0, 0], z=[0, 0, 0])) self.Plot = figure(plot_width=W, plot_height=H, tools="save", x_range=self.Source.data["x"], y_range=ranges.Range1d(start=0, end=max(self.Source.data["z"]) * 1.2)) self.Plot.toolbar.logo = None # self.Plot.xaxis.axis_label = 'Time [h]' self.Plot.yaxis.axis_label = YaxisLabel if not ZeroBase: self.Plot.vbar(source=self.Source, x='x', top='y', bottom=0, width=0.6, color='blue', alpha=0.5 ,legend = L1) self.Plot.vbar(source=self.Source, x='x', top='z', bottom='y', width=0.6, color='red', alpha=0.5,legend = L2) else: self.Plot.vbar(source=self.Source, x='x', top='z', bottom=0, width=0.6, color='green', alpha=0.5, legend=L1) self.Plot.vbar(source=self.Source, x='x', top=0, bottom='y', width=0.6, color='red', alpha=0.5, legend=L2) self.final_layout = self.Plot return
def __init__(self, W, H): Curtailment = 1 self.source = ColumnDataSource(dict(x=['Smart'], y=[0])) self.Plot = figure(plot_width=W, plot_height=H, tools="save", x_range=self.source.data["x"], y_range=ranges.Range1d( start=0, end=max(self.source.data["y"]) * 1.2)) self.Plot.toolbar.logo = None #self.Plot.xaxis.axis_label = 'Time [h]' self.Plot.yaxis.axis_label = 'Curtailed Energy [kWh]' self.Plot.vbar(source=self.source, x='x', top='y', bottom=0, width=0.4, color='red', alpha=0.5) self.final_layout = self.Plot return
from bokeh.plotting import figure from bokeh.plotting import show source = ColumnDataSource(dict(x=['DC', 'Marval'], y=[100, 98])) x_label = "" y_label = "Ratings" title = "Comic Book Ratings" p = figure(plot_width=500, plot_height=300, tools="reset, wheel_zoom, box_zoom, save", x_axis_label=x_label, y_axis_label=y_label, title=title, x_minor_ticks=2, x_range=source.data["x"], y_range=ranges.Range1d(start=0, end=120)) labels = LabelSet(x='x', y='y', text='y', level='glyph', x_offset=-13.5, y_offset=0, source=source, render_mode='canvas') p.vbar(source=source, x='x', top='y', bottom=0, width=0.3, color=PuBu[7][2]) p.add_layout(labels) show(p)
def index(): if request.method == "GET": df = pd.read_csv('issue_comments_jupyter_copy.csv', sep=',') df['org'] = df['org'].astype('str') df['repo'] = df['repo'].astype('str') df['comments'] = df['comments'].astype('str') df['user'] = df['user'].astype('str') df['comment_creation_date'] = pd.to_datetime( df['comment_creation_date']) # https://stackoverflow.com/questions/39401481/how-to-add-data-labels-to-a-bar-chart-in-bokeh ####Start code for Plot1- Comments per Repo (in Jupyter Org) #### counts_per_repo = df.groupby( ['org', 'repo'], as_index=False).count().sort_values('number') repos = counts_per_repo.repo.values numbers = counts_per_repo.number.values source = ColumnDataSource(dict(y=repos.tolist(), x=numbers.tolist())) plot1 = figure( x_axis_label='No. Comments', y_axis_label="Repository", title='Count of Jupyter GitHub Comments per Repo', x_range=ranges.Range1d( start=0, end=((counts_per_repo.number.max() + 1000) / 1000) * 1000), y_range=source.data['y'], ) plot1.hbar(source=source, y='y', height=.50, right='x', left=0) # print "Created plot" #plot.hbar(data.index, data['Close']) #print "plot.line" #script, div = components(plot1) #return render_template('index.html', script=script, div=div) ####Start code for Plot2 Comments per User (in Jupyter Org)#### counts_per_user = df.groupby( ['user'], as_index=False).count().sort_values('number', ascending=False) user = counts_per_user.user.values numbers = counts_per_user.number.values source = ColumnDataSource(dict(y=user.tolist(), x=numbers.tolist())) plot2 = figure( x_axis_label='No. Comments', y_axis_label="User Name", title='Count of Jupyter GitHub Comments per User (Top 50)', x_range=ranges.Range1d( start=0, end=((counts_per_user.number.max() + 1000) / 1000) * 1000), y_range=source.data['y'][:50], ) plot2.hbar(source=source, y='y', height=0.5, right='x', left=0) ####Start code for Plot3 TSNE Scatter Plot#### source = ColumnDataSource(df) hover = HoverTool(tooltips=[("term", "@term")]) plot3 = figure(plot_width=900, plot_height=700, title='Comments Map by t-SNE', tools='pan,wheel_zoom,box_zoom,reset,hover,previewsave') #plot.add_tools(hover) plot3.circle(x='x', y='y', color='blue', source=source) p2column = column([plot2]) r = row([plot1, p2column]) #layout = column(p1,p2) - added below from https://campus.datacamp.com/courses/interactive-data-visualization-with-bokeh/layouts-interactions-and-annotations?ex=2 script1, div1 = components(r) #script2, div2 = components(plot2) #script3, div3 = components(plot3) comment_script, comment_div = components(get_comment_clusters(df)) try: age, happyface = dill.load(open('happyface.dill', 'rb')) except: age = datetime.datetime.now() + datetime.timedelta(days=2) if (age - datetime.datetime.now()).total_seconds() > 60 * 60 * 24: happyface = happyfacer(df) dill.dump((datetime.datetime.now(), happyface), open('happyface.dill', 'wb')) return render_template('index.html', script1=script1, div1=div1, happyface=happyface, comment_script=comment_script, comment_div=comment_div)
text_font_size='18px') plot.add_layout(labelx) plot.add_layout(labely) plot.add_layout(labelz) div11 = Div(text=""" Source of data: <a href="https://github.com/datadesk/california-coronavirus-data/blob/master/latimes-state-totals.csvv">latimes-state-totals.csv</a>, from <a href="https://www.latimes.com/projects/california-coronavirus-cases-tracking-outbreak/">Los Angeles Times</a> """) div12 = Div(text=""" Last update: {} """.format(ds['date'].max())) plot2 = figure(plot_height=600, plot_width=1200, x_range=races, \ tools="", toolbar_location=None, title="Covid-19 Cases by Races", \ x_axis_label="Races", y_axis_label="Percentage", y_range=ranges.Range1d(start=0, end=1)) plot2.vbar(x='x', top='cases', width=0.8, source=s3, legend_field='x',\ fill_color=factor_cmap('x', palette=Spectral6, factors=races)) plot2.text(x='x', y='cases', text='num_cases', source=s3, x_offset=-15) plot2.text(x='x', y='cases', text='cases_text', source=s3, x_offset=-15, y_offset=-15) div21 = Div(text=""" Source of data: <a href="https://github.com/datadesk/california-coronavirus-data/blob/master/cdph-race-ethnicity.csv">cdph-race-ethnicity.csv</a>, from <a href="https://www.cdph.ca.gov/Programs/CID/DCDC/Pages/COVID-19/Race-Ethnicity.aspx">California Department of Public Health</a> """)
def SalesData(request): if request.method == 'POST': isbn = request.POST.get('isbn') if isbn == "": return render(request, 'blog/predict.html') else: username = isbn # HTML text is assigned to the html_str variable. Ideally this will be writen to the file ISBN2.html html_str = """{% extends 'blog/base.html' %}{% block content %}{% load staticfiles %}<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><script type="text/javascript" src="http://jqueryrotate.googlecode.com/svn/trunk/jQueryRotate.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><div class="container"><div class="panel-group"><div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a data-toggle="collapse" href="#collapse1">Current Edition</a></h4></div><div id="collapse1" class="panel-collapse collapse"><div class="panel-body"><br/><div class="bs-example" data-example-id="table table-bordered" id ="table table-bordered"><table class= "table table-bordered"> <thead> """ # ISBN2.HTML file is opened in the below with condition with open( 'U:\\django_test\\mysitetest\\blog\\templates\\blog\ISBN2.html', 'w') as myfile: # with the file open, myfile.write(html_str) myfile.write( "<th class=" "col-md-1" ">" + " " + "</th><th class=" "col-md-1" ">Jan</th> <th>Feb</th> <th>Mar</th> <th>Apr</th> <th>May</th> <th>Jun</th> <th>Jul</th> <th>Aug</th> <th>Sep</th> <th>Oct</th> <th>Nov</th> <th>Dec</th></tr> </thead> <tbody> <tr> " ) with open( 'C:\\Users\\prasadv\\Desktop\\Stock Opt\\Clustering output file\ISBN_Data_SOMCluster_Period_nrm.csv', 'rt') as f: reader = pd.read_csv(f, delimiter=',') df1 = pd.DataFrame( reader, columns=[ '', 'ISBN', 'PMG', 'PMC', 'Medium', 'Extended Medium', 'Edition Type', 'First Sale Time', 'First Sale Month', 'Sale Period(in Months)', 'Quarter', 'First Year Sales', 'Second Year Sales', 'Third Year Sales', 'Fourth Year Sales', 'Fifth Year Sales', 'Supply Site : L', 'Supply Site : R', 'Range Sales Cluster', 'Previous ISBN', 'Previous Edition Cluster', 'SOM Cluster', 'Cluster : 2-12', 'Cluster : 1-12', 'Cluster : 13-24', 'Cluster : 25-36', 'Cluster : 37-48', 'Cluster : 49-60', 'Cluster : 1-24', 'Cluster : 1-36', 'Cluster : 1-48', 'Cluster : 1-60', 'Period : 1', 'Period : 2', 'Period : 3', 'Period : 4', 'Period : 5', 'Period : 6', 'Period : 7', 'Period : 8', 'Period : 9', 'Period : 10', 'Period : 11', 'Period : 12', 'Period : 13', 'Period : 14', 'Period : 15', 'Period : 16', 'Period : 17', 'Period : 18', 'Period : 19', 'Period : 20', 'Period : 21', 'Period : 22', 'Period : 23', 'Period : 24', 'Period : 25', 'Period : 26', 'Period : 27', 'Period : 28', 'Period : 29', 'Period : 30', 'Period : 31', 'Period : 32', 'Period : 33', 'Period : 34', 'Period : 35', 'Period : 36', 'Period : 37', 'Period : 38', 'Period : 39', 'Period : 40', 'Period : 41', 'Period : 42', 'Period : 43', 'Period : 44', 'Period : 45', 'Period : 46', 'Period : 47', 'Period : 48', 'Period : 49', 'Period : 50', 'Period : 51', 'Period : 52', 'Period : 53', 'Period : 54', 'Period : 55', 'Period : 56', 'Period : 57', 'Period : 58', 'Period : 59', 'Period : 60', 'Period : 61', 'Period : 62', 'Period : 63', 'Period : 64', 'Period : 65', 'Period : 66', 'Period : 67', 'Period : 68', 'Period : 69', 'Period : 70', 'Period : 71', 'Period : 72', 'Period : 73', 'Period : 74', 'Period : 75', 'Period : 76', 'Period : 77', 'Period : 78', 'Period : 79', 'Period : 80', 'Period : 81', 'Period : 82', 'Period : 83', 'Period : 84', 'Period : 85', 'Period : 86', 'Period : 87', 'Period : 88', 'Period : 89', 'Period : 90', 'Period : 91', 'Period : 92', 'Period : 93', 'Period : 94', 'Period : 95', 'Period : 96', 'Period : 97', 'Period : 98', 'Period : 99', 'Period : 100', 'Period : 101', 'Period : 102', 'Period : 103', 'Period : 104', 'Period : 105', 'Period : 106', 'Period : 107', 'Period : 108', 'Period : 109', 'Period : 110', 'Period : 111', 'Period : 112', 'Period : 113', 'Period : 114', 'Total Sales' ]) GG = df1[df1['ISBN'].notnull() & (df1['ISBN'] == float(username))] lista = GG.loc[:, 'Period : 1': 'Period : 114'] #lista is the range starting from p1 to p# lista = lista.values.tolist() lista = [item for sublist in lista for item in sublist] lista = list(map(int, lista)) max_num = max(list(map(int, lista))) listb = GG.loc[:, 'First Sale Month'] # taking only the month from the list row listc = GG.loc[:, 'First Sale Time'] listc = ''.join(listc.tolist()) listc = listc.split('/') listc = listc[0] listb = listb.get_value(listb.index[0]) myfile.write("<td>" + listc + "</td>") date1 = datetime.strptime(str(listc) + "-1-1", "%Y-%m-%d") if int(listb) == 1: a = 12 b = (len(lista) / 12) if isinstance(b, float) == True: b = math.ceil(b) for j in range(1, b): if a == 12: parts = lista[0:a] pd_new = pd.DataFrame(parts) Part_for_chart = parts parts = [ '<td>' + str(s) + '</td>' for s in parts ] str1 = ''.join(parts) myfile.write(str1 + "</tr>") a = a + 12 else: parts = lista[a - 12:a] if (len(parts)) != 12: lengthofparts = 12 - (len(parts)) g = [ parts.append(0) for z in range(0, lengthofparts) ] pd_new = pd_new.append(parts) for x in parts: Part_for_chart.append(x) parts = [ '<td>' + str(s) + '</td>' for s in parts ] str1 = ''.join(parts) listd = '<td>' + str(int(listc) + 1) + '</td>' myfile.write(listd + str1 + "</tr>") a = a + 12 listc = int(listc) + 1 else: for i in range(0, listb): lista.insert(0, 0) a = 12 b = (len(lista) / 12) if isinstance(b, float) == True: b = math.ceil(b) for j in range(1, b): if a == 12: parts = lista[0:a] pd_new = pd.DataFrame(parts) Part_for_chart = parts parts = [ '<td>' + str(s) + '</td>' for s in parts ] str1 = ''.join(parts) myfile.write(str1 + "</tr>") a = a + 12 else: parts = lista[a:a + 12] if (len(parts)) != 12: lengthofparts = 12 - (len(parts)) g = [ parts.append(0) for z in range(0, lengthofparts) ] pd_new = pd_new.append(parts) for x in parts: Part_for_chart.append(x) parts = [ '<td>' + str(s) + '</td>' for s in parts ] str1 = ''.join(parts) listd = '<td>' + str(int(listc) + 1) + '</td>' myfile.write(listd + str1 + "</tr>") a = a + 12 listc = int(listc) + 1 date2 = datetime.strptime( str(listc + 1) + "-1-01", "%Y-%m-%d") months_str = calendar.month_name months = [] while date1 < date2: month = date1.month year = date1.year month_str = months_str[month][0:3] months.append("{0}-{1}".format(month_str, str(year)[-2:])) next_month = month + 1 if month != 12 else 1 next_year = year + 1 if next_month == 1 else year date1 = date1.replace(month=next_month, year=next_year) source = ColumnDataSource(data=dict(height=Part_for_chart, weight=months, names=Part_for_chart)) output_file( "U:\\django_test\\mysitetest\\blog\\templates\\svg\\bar.svg" ) HoverTool( tooltips=[ ('date', '@date{%F}'), ('close', '$@{adj close}{%0.2f}'), # use @{ } for field names with spaces ('volume', '@volume{0.00 a}'), ], # display a tooltip whenever the cursor is vertically in line with a glyph mode='vline') p = figure(plot_width=1500, title=username + '- (Current Edition)', tools="", x_range=source.data["weight"], y_range=ranges.Range1d(start=0, end=(max_num + 100)), toolbar_location=None) p.line(months, Part_for_chart, line_width=2) labels = LabelSet(x='weight', y='height', text='height', level='glyph', x_offset=-13.5, y_offset=0, source=source, text_font_size="10pt", render_mode='canvas') p.xaxis.major_label_orientation = math.pi / 2 p.add_layout(labels) save(p) myfile.write( """<table class="inlineTable"><div id="chart" >{% include "svg/bar.svg" %}</div></table><div class="panel-footer">@Copyright Elsevier 2017. Created by [email protected]</div></div></div></div></div></div></div>""" ) myfile.write("</tr>{% endblock content %}") myfile.close() return render( request, 'blog/ISBN2.html', )
p5 = figure(plot_width=dfs.default_plot_width, plot_height=dfs.default_plot_height, sizing_mode=dfs.default_plot_sizing_mode, x_axis_label=dfs.string_axis_labels[5][0], y_axis_label=dfs.string_axis_labels[5][1]) p6 = figure(plot_width=dfs.default_plot_width, plot_height=dfs.default_plot_height, sizing_mode=dfs.default_plot_sizing_mode, x_axis_label=dfs.string_axis_labels[6][0], y_axis_label=dfs.string_axis_labels[6][1]) p7 = figure(plot_width=dfs.default_plot_width, plot_height=dfs.default_plot_height, sizing_mode=dfs.default_plot_sizing_mode, x_axis_label=dfs.string_axis_labels[7][0], y_axis_label=dfs.string_axis_labels[7][1], x_range=ranges.Range1d(start=dfs.default_start_wavelength, end=dfs.default_stop_wavelength)) figures = [p0, p1, p2, p3, p4, p5, p6, p7] # assemble graph tabs tab0 = Panel(child=p0, title=dfs.string_calculate_types[0]) tab1 = Panel(child=p1, title=dfs.string_calculate_types[1]) tab2 = Panel(child=p2, title=dfs.string_calculate_types[2]) tab3 = Panel(child=p3, title=dfs.string_calculate_types[3].title()) tab4 = Panel(child=p4, title=dfs.string_calculate_types[4].title()) tab5 = Panel(child=p5, title=dfs.string_calculate_types[5].title()) tab6 = Panel(child=p6, title=dfs.string_calculate_types[6]) tab7 = Panel(child=p7, title=dfs.string_calculate_types[7].title()) tabs = Tabs(tabs=[tab0, tab1, tab2, tab3, tab4, tab5, tab6, tab7], name='tabs') # string_title[10] # snr
def dashboard(df, sim_time=180, lat=43.223628, lon=-90.294633, outfile='dashboard.html'): #Isolate the individual housing states df_onlyState = df[[ 'inspection_get', 'claim_get', 'fema_get', 'sba_get', 'permit_get', 'repair_get', 'home_get', 'occupy_get' ]] #list of statuses in correct order for future reference statuses = [ 'inspection_get', 'claim_get', 'fema_get', 'sba_get', 'permit_get', 'repair_get', 'home_get', 'occupy_get' ] NUM_HOMES = df.shape[0] #the total number of entities NUM_CAT = len(statuses) #the total number of categories # assign colors to the sequential housing statuses. # colors are map-optimised, from ColorBrewer # palettes will scale to size automatically, max size is 20 colors_only = bokeh.palettes.d3['Category20'][NUM_CAT] colors = {} for i in range(0, len(statuses)): colors[statuses[i]] = colors_only[i] # create a list to hold the status of all homes by day home_status_list = [] for i in range(0, sim_time): single_home_status = np.empty(shape=[2860, 1], dtype=object) curr_max = i curr = 0 for row in df_onlyState.itertuples(index=False): #convert the row into a dictionary for key-value analysis row_asDict = row._asdict() #find the most recent status time of the home within currMax. ignores None and nan try: mostRecentTime = max(value for name, value in row_asDict.items() if value is not None and value < curr_max) #reverse key-value to determine actual status key = next(key for key, value in row_asDict.items() if value == mostRecentTime) except ValueError: key = 'no_status' single_home_status[curr] = key curr += 1 home_status_list.append( pd.Series(data=single_home_status.ravel(), name=i)) # create a single DataFrame for the home statuses at every unit of simulation time home_status = pd.concat(home_status_list, axis=1) # dataframe for number of homes with a given status at every point of simulation time status_count_list = [] for time in range(0, sim_time): status_count_list.append( pd.Series(data=home_status[time].value_counts(), name=str(time))) #concatenate and fill NaN with zeroes. re-index rows to correct order status_count_df = pd.concat(status_count_list, axis=1).fillna(value=0).reindex(statuses) #create a new dataframe with just the colors for the map #current colors: white to dark green, where dark green = home_get home_status_colors = home_status.replace(colors) # Interactive Barplot # Standalone HTML file using CustomJS #wrangle the data into a data source for the ColumnDataSource to work properly with Javascript per_day = status_count_df.transpose().values.tolist() data = dict({str(i): v for i, v in enumerate(per_day)}) data['x'] = statuses #add the statuses to the data source data['y'] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] #dummy column for CustomJS to overwrite data['colors'] = colors_only source = ColumnDataSource(data) output_file(outfile) #plot setup barplot = figure( plot_width=800, plot_height=600, tools='pan, wheel_zoom, reset, save', x_axis_label='Status', x_range=source.data['x'], y_range=ranges.Range1d(start=0, end=len(data['y'])), title="Number of Households by Status at Specified Time (Slider)") barplot.vbar(source=source, x='x', top='y', color='colors', width=0.6) bar_hover = HoverTool(tooltips=[('num', '@y')]) barplot.add_tools(bar_hover) lat = 43.223628 lon = -90.294633 #Map Setup map_options = GMapOptions(lat=lat, lng=lon, scale_control=True, map_type="roadmap", zoom=16) mapplot = GMapPlot(x_range=Range1d(), y_range=Range1d(), map_options=map_options) mapplot.title.text = "Locations of Modeled Homes (Marker Size Proportional to Original Damage State; Colors Defined Below)" # Set up tool tip with household stories hover = HoverTool() hover.tooltips = [("Household's Story:", "@story")] mapplot.add_tools(PanTool(), WheelZoomTool(), ResetTool(), hover, SaveTool()) mapplot.min_border_top = 50 mapplot.min_border_bottom = 0 #set Google Maps API key mapplot.api_key = "AIzaSyDedFXgNZ71_xOGmVE_eAhElFJOuVweB1Y" #data wrangling for JS interaction home_status_formap = pd.concat( [home_status_colors.copy(), df['latitude'], df['longitude']], axis=1) home_status_formap['y'] = np.nan #dummy column home_status_formap['story'] = df['story'] home_status_formap.columns = home_status_formap.columns.astype(str) # Reclassify damage states at start of simulation for each household to set # marker/circle size for mapping damage_size = dict({ 'None': 10, 'Slight': 15, 'Moderate': 20, 'Extensive': 25, 'Complete': 30 }) home_status_formap['circle_size'] = df['damage_state_start'].replace( list(damage_size.keys()), list(damage_size.values())) mapsource = ColumnDataSource(home_status_formap) circle = Circle(x="longitude", y="latitude", size='circle_size', fill_color="y", fill_alpha=0.8, line_color='black') mapplot.add_glyph(mapsource, circle) # Javascript callback to enable and link interactivity between the two plots. callback = CustomJS(args=dict(s1=source, s2=mapsource), code=""" console.log(' changed selected time', cb_obj.value); var data = s1.data; var data2 = s2.data; data['y'] = data[cb_obj.value]; data2['y'] = data2[cb_obj.value]; s1.change.emit() s2.change.emit() """) #Line Graph setup line_plot = figure( title='Number of Households By Status vs. Simulated Time', y_range=ranges.Range1d(start=int(NUM_HOMES * 0.1), end=int(NUM_HOMES * 1.5)), tools='pan, wheel_zoom, reset, save') all_line_data = status_count_df.values.tolist() day_range = np.linspace(0, sim_time, num=sim_time).tolist() # Generate a vertical bar to indicate current time within the line graph # Line is generated to 10% above the number of homes and 10% below zero currtime_list = { 'x': [0, 0], 'y': [int(NUM_HOMES * 1.1), int(NUM_HOMES * -0.1)] } #dummy column for js callback for i in range(0, sim_time): currtime_list[str(i)] = [i, i] currtime_source = ColumnDataSource(currtime_list) # Create set of line plots with ability to turn individual lines on/off for data, name, color in zip(all_line_data, statuses, colors_only): line_data = pd.DataFrame(data).values.tolist() line_plot.line(day_range, line_data, color=color, alpha=0.8, legend=name, line_width=2) line_plot.line(x='x', y='y', source=currtime_source, line_color='red') line_plot.legend.location = "top_left" line_plot.legend.click_policy = "hide" # Requires Bokeh 0.12.7 # Javascript callback to enable and link interactivity between the two plots. callback = CustomJS(args=dict(s1=source, s2=mapsource, s3=currtime_source), code=""" console.log(' changed selected time', cb_obj.value); var data = s1.data; var data2 = s2.data; var data3 = s3.data; data['y'] = data[cb_obj.value]; data2['y'] = data2[cb_obj.value]; data3['x'] = data3[cb_obj.value]; s1.change.emit(); s2.change.emit(); s3.change.emit(); """) #slider time_slider = Slider(start=0, end=sim_time, value=0, step=1, callback=callback, title='DAY') #show entire layout show( layout([[mapplot], [line_plot, barplot], time_slider], sizing_mode='stretch_both'))
def crime_chart(contents): temp_list = [] crime_dict = {} crime_x = [] crime_y = [] index = input( "What do you want to view?\n\tType '1' to view crime by date\n\tType '2' to view crime by type\n\tType '3' to view crime by district\n\tType '4' to view crime by time of day\n--> " ) if index == "1": index = 1 x_string = "Date" y_string = "Number of incidents" elif index == "2": index = 3 x_string = "Type of crime" y_string = "Number of incidents" elif index == "3": index = 5 x_string = "District" y_string = "Number of incidents" elif index == "4": index = 2 x_string = "Tme of day" y_string = "Number of incidents" # print(contents[1][index][:2]) # exit() # creates a temporary list to sort so the x values are in order for i in range(len(contents)): if contents[i][index] not in temp_list: temp_list.append(contents[i][index]) temp_list.sort() # adds the item to the dictionary with a count of "1" for i in range(len(temp_list)): if temp_list[i] not in crime_dict: crime_dict[temp_list[i]] = 1 # counts the number of key items and updates the dictionary for key in crime_dict: for j in range(len(contents)): if contents[j][index] == key: crime_dict[key] += 1 # creates lists out of the key/value pairs for key in crime_dict: crime_x.append(key) crime_y.append(crime_dict[key]) # creates the chart output_file("omaha_crime.html") source = ColumnDataSource(dict(x=crime_x, y=crime_y)) x_label = x_string y_label = y_string title = "Crime in Omaha, Nebraska" plot = figure(plot_width=4000, plot_height=600, tools="save, pan, zoom_in, zoom_out, reset", x_axis_label=x_label, y_axis_label=y_label, title=title, x_minor_ticks=0, x_range=source.data["x"], y_range=ranges.Range1d(start=0, end=max(crime_y) + (max(crime_y) / 10))) plot.xaxis.major_label_orientation = math.pi / 2 labels = LabelSet(x="x", y="y", text="y", level="glyph", x_offset=-15, y_offset=5, source=source, render_mode="canvas", text_font_size="8pt") plot.vbar(source=source, x="x", top="y", bottom=0, width=0.75, color="firebrick", fill_alpha=0.5) plot.add_layout(labels) show(plot)
def visualize(self): # Set up the output file output_file(self._outputFileName) ## BARPLOT ## per_day = self._stateCounts.transpose().values.tolist() data = dict({str(i): v for i, v in enumerate(per_day)}) data[ 'x'] = self._desiredStates_ns #add the statuses to the data source data['y'] = [0.0 for i in range(len(self._desiredStates_ns)) ] #dummy column for CustomJS to overwrite data['colorsOnly'] = self._colorsOnly source = ColumnDataSource(data) #plot setup barplot = figure(plot_width=800, plot_height=600, tools='pan', x_axis_label='Status', x_range=source.data['x'], y_range=ranges.Range1d( start=0, end=int(self._filteredNumHomes * 1.1)), title="Number of Homes by Status at Current Day") barplot.vbar(source=source, x='x', top='y', width=0.6, fill_color='colorsOnly', line_color=None) bar_hover = HoverTool(tooltips=[('num', '@y')]) barplot.yaxis.axis_label = "Number of Homes" barplot.add_tools(bar_hover) ## MAPS ## mapHoverInfo = self._mapHoverOptions options_html = "" for option in mapHoverInfo: options_html += "<span style=\"font-weight: bold;\">%s: </span><span>%s<br></span>" % ( str(option), "@" + str(option)) mapHoverInfo_html = "<div style=\"width: 450px\">" + options_html + "</div>" map_hover = HoverTool(tooltips=mapHoverInfo_html) #get average lat, long mean_lat = self._filteredData['latitude'].mean() mean_long = self._filteredData['longitude'].mean() #get the zip area name if self._desiredZipcode is None: areaData = self._zipSearch.by_coordinate(mean_lat, mean_long, returns=1)[0] areaName = "Greater " + areaData['City'] + " Area" else: areaData = self._zipSearch.by_zipcode(self._desiredZipcode) areaName = areaData['City'] + ", " + str(areaData['Zipcode']) map_options = GMapOptions(lat=mean_lat, lng=mean_long, map_type="roadmap") mapplot = GMapPlot(x_range=ranges.Range1d(), y_range=ranges.Range1d(), map_options=map_options) mapplot.title.text = areaName mapplot.add_tools(PanTool(), WheelZoomTool(), map_hover) #set Google Maps API key mapplot.api_key = "AIzaSyAr5Z6tbpyDQLPyD4PQmrxvqn6VEN_3vnU" #data wrangling for JS interaction home_data_for_map_list = [ self._allHomeStateColors.copy(), self._filteredData['latitude'], self._filteredData['longitude'] ] for option in self._mapHoverOptions: home_data_for_map_list += [self._filteredData[str(option)]] home_status_colors_formap = pd.concat(home_data_for_map_list, axis=1) initialDamageStateData = self._filteredData[ 'damage_state_start'].replace(self._damageStates) home_status_colors_formap = pd.concat( [home_status_colors_formap, initialDamageStateData], axis=1) home_status_colors_formap['y'] = np.nan #dummy column home_status_colors_formap.columns = home_status_colors_formap.columns.astype( str) mapsource = ColumnDataSource(home_status_colors_formap) circle = Circle(x="longitude", y="latitude", size='damage_state_start', fill_color="y", fill_alpha=0.8, line_color=None) mapplot.add_glyph(mapsource, circle) ## LINE GRAPH ## # LINE GRAPH - CURRENT TIME INDICATOR # # Generate a vertical bar to indicate current time within the line graph # Line is generated to 10% above the number of homes and 10% below zero currtime_list = { 'x': [0, 0], 'y': [ int(self._filteredNumHomes * 1.1), int(self._filteredNumHomes * -0.1) ] } #dummy column for js callback for i in range(0, self._simTime): currtime_list[str(i)] = [i, i] currtime_source = ColumnDataSource(currtime_list) # LINE GRAPH - DATA # line_plot = figure(title='Overall House Status vs Time', y_range=ranges.Range1d( start=int(self._filteredNumHomes * 0.1), end=int(self._filteredNumHomes * 1.5))) all_line_data = self._stateCounts.values.tolist() day_range = np.linspace(1, self._simTime - 2, num=self._simTime - 1).tolist() for data, name, color in zip(all_line_data, self._statuses, self._colorsOnly): line_data = pd.DataFrame(data).values.tolist() line_plot.line(day_range, line_data, color=color, alpha=0.8, legend=name, line_width=2) line_plot.line(x='x', y='y', source=currtime_source, line_color='red') line_plot.legend.location = "top_center" line_plot.legend.click_policy = "hide" line_plot.legend.orientation = "horizontal" line_plot.yaxis.axis_label = "Number of Homes" line_plot.xaxis.axis_label = "Day" # Requires Bokeh 0.12.7 # Javascript callback to enable and link interactivity between the two plots. callback = CustomJS(args=dict(s1=source, s2=mapsource, s3=currtime_source), code=""" console.log(' changed selected time', cb_obj.value); var data = s1.data; var data2 = s2.data; var data3 = s3.data; data['y'] = data[cb_obj.value]; data2['y'] = data2[cb_obj.value]; data3['x'] = data3[cb_obj.value]; s1.change.emit(); s2.change.emit(); s3.change.emit(); """) ## SLIDER ## # This slider manages one callback which updates all three graphics. time_slider = Slider(start=1, end=self._simTime - 1, value=0, step=1, callback=callback, title='DAY') show( gridplot([[mapplot], [line_plot, barplot], [time_slider]], sizing_mode='stretch_both'))
def returnBarGraph(df, title=' ', y_label='score/percentage', label_suffix='', palette=paletteR): pp = pprint.PrettyPrinter(indent=4) # All but first column are the categories categories = list(df.columns)[1:] # Convert every value to an int for cat in categories: df = df.astype({cat: int}) # Content of the first column are the subcategories per category subcats = list(df.iloc[:, 0]) x = [(category, subcat) for category in categories for subcat in subcats] values = [] for cat in categories: values.extend(df[cat].tolist()) value_labels = [] for value in values: value_labels.append(str(value) + label_suffix) source = ColumnDataSource(data=dict(x=x, y=values, labels=value_labels)) p = figure(x_range=FactorRange(*x), y_range=ranges.Range1d(start=0, end=105), y_minor_ticks=10, y_axis_label=y_label, plot_height=800, plot_width=1280, title=title, title_location='above', toolbar_location=None, tools="") # min_border_top labels = LabelSet(x='x', y='y', text='labels', level='glyph', x_offset=7, y_offset=5, angle=90, angle_units='deg', source=source, render_mode='canvas', text_font_size="9pt") p.vbar(x='x', top='y', width=0.9, source=source, line_color="white", fill_color=factor_cmap('x', palette=palette, factors=subcats, start=1)) p.add_layout(labels) p.x_range.range_padding = 0.05 p.title.align = 'center' p.title.text_font_size = "12pt" p.title.text_font_style = "bold" p.xaxis.major_label_orientation = math.pi / 2 p.xgrid.grid_line_color = None return p
y_f = pop_sum_df_f['age'].loc[min_year].tolist() right_f = pop_sum_df_f['ratio'].loc[min_year].tolist() pop_sum_race_df = pop_sum_race_df.round(2) x = pop_sum_race_df['race_ethn'].loc[min_year].tolist() y = pop_sum_race_df['ratio'].loc[min_year].tolist() source = ColumnDataSource(data=dict(y_m=y_m, right_m=right_m, y_f=y_f, right_f=right_f)) source2 = ColumnDataSource(data=dict(x=x, y=y)) # Set up plot plot = figure(plot_height=400, plot_width=800, title="Population", tools="crosshair,pan,reset,save,wheel_zoom", y_axis_label = "Age", x_axis_label = "Percentage of the total population", x_range=ranges.Range1d(start=-.01, end=.01), y_range=ranges.Range1d(start=-1, end=102)) glyph1 = HBar(y="y_m", right="right_m", left=0, height=0.5, fill_color="blue", name="Male") plot.add_glyph(source, glyph1) glyph2 = HBar(y="y_f", right="right_f", left=0, height=0.5, fill_color="orange", name="Female") plot.add_glyph(source, glyph2) plot.xaxis.bounds = (-.01, .01) plot7 = figure(plot_height=400, plot_width=800, title="Population distribution by race", tools="crosshair,pan,reset,save,wheel_zoom", x_axis_label = "race", y_axis_label = "percentage", x_minor_ticks=2,
def city_info(): firstRun = True runMode = 'w' cityOutput=[] #populated termLineHeader for single city cityList=[] #List of cities/inputs submitted by the user cityBatch=[] #Array of cityOutputs justHeader=[] #Header data = dict(request.form) #gets flask data from the user input form and puts it in a dict for x in data.values(): cityList.append(x) tempDuplicateCheck=[] duplicateEntries=[] for city in cityList: if city in tempDuplicateCheck: duplicateEntries.append(city) else: tempDuplicateCheck.append(city) firstRun, runMode, cityOutput, cityList, justHeader = order.orderOfOps(firstRun,runMode, menuChoice=city, cityList = cityList, requestor="flask_app") cityBatch.append(cityOutput) #puts the termLineHeader for each city into a batch # Gets header information for city_info page (particularly important if first city is not valid) unpopulatedTLH = search_array.termLineHeader("NoCity", "72") justHeader= [item[2] for item in unpopulatedTLH] # Determines which of the inputs are considered valid, blank, or had no return info so it can # inform the user on city_info.html. Also, creates and cleans the estimatedpopulationList and # populationDensityList for graphs while looping through cityBatch validCitiesList = [] estimatedPopulationList = [] populationDensityList = [] for city in cityBatch: if len(city)>0: validCitiesList.append(city[0][4]) estimatedPopulationList.append(city[9][4]) populationDensityList.append(city[10][4]) estimatedPopulationList = graph_data.cleanPopInput(estimatedPopulationList) populationDensityList = graph_data.cleanPopDensityInput(populationDensityList) entriesWithNoReturn =[] entriesWithNoText=[] for item in cityList: if len(item)>0 and item.title() not in validCitiesList: entriesWithNoReturn.append(item) if len(item)<1: entriesWithNoText.append(item) #Creating the Estimated Population, Population density Bokeh plots if there is at least one validCity if len(validCitiesList)>0: source = ColumnDataSource(dict(x=validCitiesList,y=estimatedPopulationList)) x_label = "City" y_label = "Estimated Population" title = "Estimated Population (not all cities have this info)" plot1 = figure(plot_width=175*len(validCitiesList), plot_height=500, tools="save", x_axis_label = x_label, y_axis_label = y_label, title=title, x_minor_ticks=2, x_range = source.data["x"], y_range= ranges.Range1d(start=0,end=max(estimatedPopulationList)+5000)) plot1.xaxis.major_label_orientation = 45 labels = LabelSet(x='x', y='y', text='y', level='glyph', x_offset=-13.5, y_offset=0, source=source, render_mode='canvas') plot1.vbar(source=source,x='x',top='y',bottom=0,width=0.3,color="green") plot1.add_layout(labels) yaxis = plot1.select(dict(type=Axis, layout="left"))[0] yaxis.formatter.use_scientific = False script1, div1 = components(plot1) # Return HTML components to embed a Bokeh plot. The data for the plot is stored directly in the returned HTML. #Creating the Population Density Bokeh plot source = ColumnDataSource(dict(x=validCitiesList,y=populationDensityList)) x_label = "City" y_label = "Population Density per sq/mi" title = "Population Density" plot2 = figure(plot_width=175*len(validCitiesList), plot_height=500, tools="save", x_axis_label = x_label, y_axis_label = y_label, title=title, x_minor_ticks=2, x_range = source.data["x"], y_range= ranges.Range1d(start=0,end=max(populationDensityList)+1000)) plot2.xaxis.major_label_orientation = 45 labels = LabelSet(x='x', y='y', text='y', level='glyph', x_offset=-13.5, y_offset=0, source=source, render_mode='canvas') plot2.vbar(source=source,x='x',top='y',bottom=0,width=0.3,color="blue") plot2.add_layout(labels) script2, div2 = components(plot2) else: script1="" div1="" script2="" div2="" #provide minified BokehJS from library static directory js_resources = INLINE.render_js() css_resources = INLINE.render_css() return render_template("city_info.html", cityList=cityList, cityBatch = cityBatch, justHeader=justHeader, entriesWithNoReturn=entriesWithNoReturn, entriesWithNoText=entriesWithNoText, validCitiesList=validCitiesList, plot_script1=script1, plot_div1=div1, plot_script2=script2, plot_div2=div2, js_resources=js_resources, css_resources=css_resources, duplicateEntries=duplicateEntries, cache_timeout=0)
def SalesData(request): if request.method == 'POST': isbn = request.POST.get('isbn') if isbn == "": return render(request, 'blog/predict.html') else: username = isbn # HTML text is assigned to the html_str variable. Ideally this will be writen to the file ISBN2.html html_str = """{% extends 'blog/base.html' %}{% block content %}{% load staticfiles %}<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><script type="text/javascript" src="http://jqueryrotate.googlecode.com/svn/trunk/jQueryRotate.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><div class="container"><div class="panel-group">""" # ISBN2.HTML file is opened in the below with condition with open('U:\\django_test\\mysitetest\\blog\\templates\\blog\ISBN2.html', 'w') as myfile: # with the file open, myfile.write(html_str) global Part_for_chart #with open('C:\\Users\\prasadv\\Desktop\\Stock Opt\\Clustering output file\ISBN_Data_SOMCluster_Period_nrm.csv','rt') as f: with open('C:\\Users\\prasadv\\Desktop\\Stock Opt\\Clustering output file\Consolidation_meta.csv','rt') as f: reader = pd.read_csv(f,delimiter = ',') # the csv file is read here df=pd.DataFrame(reader) # the csv file is converted to a dataframe new_header = df.iloc[0] # headers are pulled from the read file, and passed to new_header variable df1 = df[1:] df1.rename(columns = new_header) # Adding headers to the dataframe GG = df1.loc[(df1['ISBN'] == username)] #searching the ISBN in the dataframe print(GG) previous_isbn = GG.filter(items=['Previous EDITION(-1)']) previous_isbn = previous_isbn.values.tolist() previous_isbn = [item for sublist in previous_isbn for item in sublist ] print(previous_isbn) previous_isbn_1 = GG.filter(items=['Previous EDITION(-2)']) previous_isbn_1 = previous_isbn_1.values.tolist() previous_isbn_1 = [item for sublist in previous_isbn_1 for item in sublist ] print(previous_isbn_1) if previous_isbn!= []: GG1 = df1.loc[(df1['PIN'] == previous_isbn)] print(GG1) if previous_isbn_1 != []: GG2 = df1.loc[(df1['PIN'] == previous_isbn_1)] print(GG2) temp_meta = GG.filter(items=['ISBN','US_PUB_DATE','PMG','PMC','Medium','TITLE_DISC','AUTHOR']) #filtering the variable GG with the column for meta data str1 = temp_meta.values.tolist() # converting the dataframe to list str1 = [item for sublist in str1 for item in sublist ] #reitering the convertion from dataframe to list # writing the HTML tags for metadata table. and assining the values from str1 variable str_html_temp ="""<table class= "table table-bordered"><thead> <th class="col-sm-2" style="background-color:lightgrey;"> ISBN (0)</th> <td class="col-sm-2">""" + str(str1[0]) + """</td><th class="col-sm-2" style="background-color:lightgrey;"> PMG</th> <td class="col-sm-2">""" + str(str1[2])+ """</td><tr><th class="col-sm-2" style="background-color:lightgrey;"> Pub Date</th> <td class="col-sm-2">""" + str(str1[1]) +"""</td><th class="col-sm-2" style="background-color:lightgrey;"> PMC</th> <td class="col-sm-2">""" + str(str1[3]) + """</td></tr><tr><th class="col-sm-2" style="background-color:lightgrey;"> Medium</th> <td class="col-sm-2">""" + str(str1[4]) + """</td><th class="col-sm-2" style="background-color:lightgrey;"> Author</th> <td class="col-sm-2">""" + str(str1[6]) + """</td></tr><tr><th class="col-sm-2" style="background-color:lightgrey;">Title</th><td class="col-sm-2">""" + str(str1[5]) + """</td></tr></table>""" # writing the HTML tages to ISBN2 file, these html tags are for meta data, with collapsable window myfile.write("""<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a data-toggle="collapse" href="#collapse1">MetaData</a></h4></div><div id="collapse1" class="panel-collapse collapse"><div class="panel-body">""" +str_html_temp + """</div></div>""") # writing the HTML tages to ISBN2 file, this sets up the table for current edition sales data myfile.write("""<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a data-toggle="collapse" href="#collapse2">Current Edition</a></h4></div><div id="collapse2" class="panel-collapse collapse"><div class="panel-body"><br/><div class="bs-example" data-example-id="table table-bordered" id ="table table-bordered"><table class= "table table-bordered"> <thead> <th class=""col-md-1"">""" + "Year" + """</th><th class=""col-md-1"">Jan</th> <th>Feb</th> <th>Mar</th> <th>Apr</th> <th>May</th> <th>Jun</th> <th>Jul</th> <th>Aug</th> <th>Sep</th> <th>Oct</th> <th>Nov</th> <th>Dec</th></tr> </thead> <tbody> <tr> """) lista = GG.filter(regex=("Period.*")) # using regex, getting all period sales lista = lista.values.tolist() # removing the headers from the dataframe lista = [item for sublist in lista for item in sublist ] # converting dataframe to list lista = list(map(int, lista)) # converting the list variables to integer max_num = max(list(map(int, lista))) # getting the max number for future use listb = GG.loc[:,'Month'] # taking only the month from the list row listc = GG.loc[:,'First Sale Month'] #taking the first sale month for preparing the table listc=''.join(listc.tolist()) date1 = listc # this is used for creating a running date period for sales table listc = listc.split('/') #spliting the date listc = int(listc[2]) #Getting only the year listb = listb.get_value(listb.index[0]) # getting the month value myfile.write("<td>" + str(listc) + "</td>") # writing the year to the ISBN2 file # if the first sale month is january then the below set of the codes works if int(listb) == 1: #condintion to check the first sale month equal to 1 a = 12 #future use b = (len(lista) / 12) #Dividing the length of the period to get how many years the sales period has if isinstance(b, float) == True: # if the division results in value with decimal places, rounding off the decimal b = math.ceil(b) for j in range(1, b): # running the below loop based on number of years if a == 12: # this condition is to check whether its the first year of sale, as it has to be handled differently parts = lista[0:a] #assigning the values to the parts. This variable will be used for compliation purpose pd_new = pd.DataFrame(parts)#Converting the parts variable to dataframe Part_for_chart = parts # assigning values to variable which will be used for charts parts = ['<td>' + str(s) + '</td>' for s in parts] # adding HTML tags for every element in the dataframe str1 = ''.join(parts) # converting the dataframe into list myfile.write(str1 + "</tr>")# writing the str1 list to ISBN2 file a = a + 12 else: parts = lista[a - 12:a] #adding the values to the parts if (len(parts)) != 12: #in the last year of sales, it will not have 12 month data, hence its checked here. and if its true "0" will be added in the below code lengthofparts = 12 - (len(parts)) g = [parts.append(0) for z in range(0, lengthofparts)] pd_new = pd_new.append(parts) for x in parts: # here appending every value into Part_for_chart variable. Part_for_chart.append(x) parts = ['<td>' + str(s) + '</td>' for s in parts]# adding html tags for sales table str1 = ''.join(parts) listd = '<td>' + str(int(listc) + 1) + '</td>' # adding the year to the table with HTML tags myfile.write(listd + str1 + "</tr>") #wRITING THE tags to the ISBN2 file a = a + 12 listc = int(listc) + 1 date2 = datetime.strptime(str(int(listc) + 1) + "-1-01", "%Y-%m-%d") # date2 variable to capture the last year else: for i in range(0, listb): # is the title doesnt have a sales start month as 1 then its pushed here lista.insert(0, 0) # 0's are inserted based the month number a = 12 #future use b = (len(lista) / 12) #same purpose as above. please refer line #96 to 119 for the below codes commentary if isinstance(b, float) == True: b = math.ceil(b) for j in range(1, b): if a == 12: parts = lista[0:a] pd_new = pd.DataFrame(parts) Part_for_chart = parts parts = ['<td>' + str(s) + '</td>' for s in parts] str1 = ''.join(parts) myfile.write(str1 + "</tr>") a = a + 12 else: parts = lista[a:a + 12] if (len(parts)) != 12: lengthofparts = 12 - (len(parts)) g = [parts.append(0) for z in range(0, lengthofparts)] pd_new = pd_new.append(parts) for x in parts: Part_for_chart.append(x) parts = ['<td>' + str(s) + '</td>' for s in parts] str1 = ''.join(parts) listd = '<td>' + str(int(listc) + 1) + '</td>' myfile.write(listd + str1 + "</tr>") a = a + 12 listc = int(listc) + 1 # please refer line # 96 to 119 for commentary print(listc) date2 = datetime.strptime(str(int(listc) + 1) + "-1-01", "%Y-%m-%d") # date2 variable to capture the last year #date2 = datetime.strptime(str(listc + 1) + "-1-01", "%Y-%m-%d") # date2 variable will be used for creating the consecutive months and years for chart months_str = calendar.month_name months = [] date1 = datetime.strptime(date1,"%d/%m/%Y") # below while loop will generate consecutive # of months and years which is stored into months variable while date1 < date2: month = date1.month # month is stored here. year = date1.year # year is stored here month_str = months_str[month][0:3] # month num is converted to string 1 is converted to Jan months.append("{0}-{1}".format(month_str, str(year)[-2:])) # month string is appended to months variable next_month = month + 1 if month != 12 else 1 next_year = year + 1 if next_month == 1 else year date1 = date1.replace(month=next_month, year=next_year) source = ColumnDataSource(data=dict(height=Part_for_chart, weight=months, names=Part_for_chart)) output_file("U:\\django_test\\mysitetest\\blog\\templates\\svg\\bar.svg") p = figure(plot_width=1500, title=username+'- (Current Edition)', tools="", x_range=source.data["weight"], y_range=ranges.Range1d(start=0, end=(max_num + 100)), toolbar_location=None) p.line(months, Part_for_chart, line_width=2) labels = LabelSet(x='weight', y='height', text='height', level='glyph', x_offset=-13.5, y_offset=0, source=source, text_font_size="10pt", render_mode='canvas') p.xaxis.major_label_orientation = math.pi / 2 p.add_layout(labels) save(p) myfile.write( """<table class="inlineTable"><div id="chart" >{% include "svg/bar.svg" %}</div></table><div class="panel-footer">@Copyright Elsevier 2017. Created by [email protected]</div>{% block test %}{% endblock %}</div></div></div></div></div></div>""") myfile.write("</tr>{% endblock content %}") myfile.close() return render(request, 'blog/ISBN2.html', ) return render(request, 'blog/test_to_extend.html')
def SalesData(request): if request.method == 'POST': isbn = request.POST.get('isbn') if isbn == "": return render(request, 'blog/predict.html') else: username = isbn # HTML text is assigned to the html_str variable. Ideally this will be writen to the file ISBN2.html html_str = """{% extends 'blog/base.html' %}{% block content %}{% load staticfiles %}<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><script type="text/javascript" src="http://jqueryrotate.googlecode.com/svn/trunk/jQueryRotate.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script><div class="container"><div class="panel-group"><div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a data-toggle="collapse" href="#collapse1">Current Edition</a></h4></div><div id="collapse1" class="panel-collapse collapse"><div class="panel-body"><br/><div class="bs-example" data-example-id="table table-bordered" id ="table table-bordered"><table class= "table-striped"> <thead> """ # ISBN2.HTML file is opened in the below with condition with open( 'U:\\django_test\\mysitetest\\blog\\templates\\blog\ISBN2.html', 'w') as myfile: # with the file open, myfile.write(html_str) myfile.write( "<th class=" "col-md-1" ">" + " " + "</th><th class=" "col-md-1" ">Jan</th> <th>Feb</th> <th>Mar</th> <th>Apr</th> <th>May</th> <th>Jun</th> <th>Jul</th> <th>Aug</th> <th>Sep</th> <th>Oct</th> <th>Nov</th> <th>Dec</th></tr> </thead> <tbody> <tr> " ) with open( 'C:\\Users\\prasadv\\Desktop\\Stock Opt\\Clustering output file\ISBN_Data_SOMCluster_Period_nrm.csv', 'rt') as f: reader = csv.reader(f, delimiter=',') for row in reader: for field in row: if field == username: lista = row[ 32: -1] # lista is the range starting from p1 to p# max_num = max(list(map(int, row[32:-1]))) print(max_num) listb = [ int(i[0]) for i in row[8:9] ] # taking only the month from the list row listb = listb[ 0] # listb is the month of the ISBN. here its converted from list to integer listc = row[7:8] # Taking the year listc = listc[0] listc = listc.split('/') listc = listc[0] myfile.write("<td>" + listc + "</td>") date1 = datetime.strptime( str(listc) + "-1-1", "%Y-%m-%d") if listb == 1: a = 12 b = (len(lista) / 12) if isinstance(b, float) == True: b = math.ceil(b) for j in range(1, b): if a == 12: parts = lista[0:a] pd_new = pd.DataFrame(parts) Part_for_chart = parts parts = [ '<td>' + str(s) + '</td>' for s in parts ] str1 = ''.join(parts) myfile.write(str1 + "</tr>") a = a + 12 else: parts = lista[a - 12:a] if (len(parts)) != 12: lengthofparts = 12 - ( len(parts)) g = [ parts.append(0) for z in range( 0, lengthofparts) ] pd_new = pd_new.append(parts) for x in parts: Part_for_chart.append(x) parts = [ '<td>' + str(s) + '</td>' for s in parts ] str1 = ''.join(parts) listd = '<td>' + str( int(listc) + 1) + '</td>' myfile.write(listd + str1 + "</tr>") a = a + 12 listc = int(listc) + 1 else: for i in range(0, listb): lista.insert(0, 0) a = 12 b = (len(lista) / 12) if isinstance(b, float) == True: b = math.ceil(b) for j in range(1, b): if a == 12: parts = lista[0:a] pd_new = pd.DataFrame(parts) Part_for_chart = parts parts = [ '<td>' + str(s) + '</td>' for s in parts ] str1 = ''.join(parts) myfile.write(str1 + "</tr>") a = a + 12 else: parts = lista[a:a + 12] if (len(parts)) != 12: lengthofparts = 12 - ( len(parts)) g = [ parts.append(0) for z in range( 0, lengthofparts) ] pd_new = pd_new.append(parts) for x in parts: Part_for_chart.append(x) parts = [ '<td>' + str(s) + '</td>' for s in parts ] str1 = ''.join(parts) listd = '<td>' + str( int(listc) + 1) + '</td>' myfile.write(listd + str1 + "</tr>") a = a + 12 listc = int(listc) + 1 date2 = datetime.strptime( str(listc + 1) + "-1-01", "%Y-%m-%d") months_str = calendar.month_name months = [] while date1 < date2: month = date1.month year = date1.year month_str = months_str[month][0:3] months.append("{0}-{1}".format( month_str, str(year)[-2:])) next_month = month + 1 if month != 12 else 1 next_year = year + 1 if next_month == 1 else year date1 = date1.replace(month=next_month, year=next_year) source = ColumnDataSource( data=dict(height=Part_for_chart, weight=months, names=Part_for_chart)) output_file( "U:\\django_test\\mysitetest\\blog\\templates\\svg\\bar.svg" ) HoverTool( tooltips=[ ('date', '@date{%F}'), ('close', '$@{adj close}{%0.2f}'), # use @{ } for field names with spaces ('volume', '@volume{0.00 a}'), ], # display a tooltip whenever the cursor is vertically in line with a glyph mode='vline') p = figure( plot_width=1500, title=username + '- (Current Edition)', tools="", x_range=source.data["weight"], y_range=ranges.Range1d(start=0, end=(max_num + 100)), toolbar_location=None) p.line(months, Part_for_chart, line_width=2) labels = LabelSet(x='weight', y='height', text='height', level='glyph', x_offset=-13.5, y_offset=0, source=source, text_font_size="10pt", render_mode='canvas') p.xaxis.major_label_orientation = math.pi / 2 p.add_layout(labels) save(p) myfile.write( """<table class="inlineTable"><div id="chart" >{% include "svg/bar.svg" %}</div></table><div class="panel-footer">@Copyright Elsevier 2017. Created by [email protected]</div></div></div></div></div></div></div>""" ) myfile.write("</tr>{% endblock content %}") myfile.close() return render( request, 'blog/ISBN2.html', )
def create_graph(results_df): # Create Data Source y_axis = ['Min', 'Avg', 'Max', '90th', '95th', '99th'] x_axis = [ round(results_df['Transfer_Time'].min(), 2), round(results_df['Transfer_Time'].mean(), 2), round(results_df['Transfer_Time'].max(), 2), round(results_df['Transfer_Time'].quantile(0.90), 2), round(results_df['Transfer_Time'].quantile(0.95), 2), round(results_df['Transfer_Time'].quantile(0.99), 2) ] # Create Source for 2nd Graph graph2_source = ColumnDataSource(dict(y=y_axis, right=x_axis)) # Write results to csv aggregate_results_df = pd.DataFrame.from_dict( dict(Metrics=y_axis, Value=x_axis)) # Save Dataframe to csv agg_report_name = "Perf_Summary_" + time.strftime("%H%M%S") + ".csv" aggregate_results_df.to_csv(agg_report_name, sep=',', index=False) print( "Performance Metrics Summary report generated! Please check file: {}". format(agg_report_name)) # Plot 1st Graph x_axis_graph1 = results_df['File_Number'].tolist() temp_list = results_df['Transfer_Time'].tolist() y_axis_graph1 = [round(elem, 2) for elem in temp_list] graph1_source = ColumnDataSource(dict(x=x_axis_graph1, y=y_axis_graph1)) transfer_rate_time_graph = figure( x_range=x_axis_graph1, plot_width=1900, plot_height=400, y_axis_label="Time Taken to Transfer File(secs)", toolbar_location="below", tools="save") # Bar Graph transfer_rate_time_graph.vbar(x='x', top='y', bottom=0, source=graph1_source, width=0.3, color="#FFBF00") # Line Graph transfer_rate_time_graph.line(x='x', y='y', source=graph1_source, line_width=2, color="#FFFF00") # Format axises transfer_rate_time_graph_final = set_graph_and_legend_properties( transfer_rate_time_graph, "Transfer Rate Time (sec)") transfer_rate_time_graph_final.xaxis.major_label_orientation = pi / 4 # Add Tool - Hovertool transfer_rate_time_graph_final.add_tools(set_hover_tool_tips_graph1()) # Format x-axis labels # transfer_rate_time_graph_final.xaxis.formatter = PrintfTickFormatter(format="File_%1.0f") # Plot 2nd Graph # Create Fig aggregates_graph = figure(y_range=y_axis, x_range=ranges.Range1d( start=0, end=results_df['Transfer_Time'].max() + 2), toolbar_location='below', plot_width=900, plot_height=500, tools="save") # Add Labels labels = LabelSet(x='right', y='y', text='right', source=graph2_source, level='glyph', x_offset=5, y_offset=5, render_mode='canvas', text_color='white') # Create hBar aggregates_graph.hbar(y='y', right='right', source=graph2_source, left=0, height=0.5) # Format axises and add labels aggregates_graph_final = set_graph_and_legend_properties( aggregates_graph, "Performance Metrics (secs)") aggregates_graph_final.add_layout(labels) # output to static HTML file graph_file_name = "perf_test_report_" + time.strftime("%H%M%S") + ".html" output_file(graph_file_name) # Save Graph save(column(transfer_rate_time_graph_final, aggregates_graph_final)) # Success Message print("Graph generated successfully! Please check file: {}".format( graph_file_name))
def using_gtvt(ra, dec, instrument, targetName='noName', ephFileName=None, output='bokeh'): """Plot the visibility (at a range of position angles) against time. Parameters ---------- ra : str The RA of the target (in degrees) hh:mm:ss.s or dd:mm:ss.s or representing a float dec : str The Dec of the target (in degrees) hh:mm:ss.s or dd:mm:ss.s or representing a float instrument : str Name of the instrument. Can either be (case-sensitive): 'NIRISS', 'NIRCam', 'MIRI', 'FGS', or 'NIRSpec' ephFileName : str The filename of the ephemeris file. output : str Switches on plotting with Bokeh. Parameter value must be 'bokeh'. Returns ------- paGood : float The good position angle. paBad : float The bad position angle. gd : matplotlib.dates object The gregorian date. fig : bokeh.plotting.figure object The plotted figure. """ # Getting calculations from GTVT (General Target Visibility Tool) tab = get_table(ra, dec) gd = tab['Date'] paMin = tab[str(instrument) + ' min'] paMax = tab[str(instrument) + ' max'] paNom = tab[str(instrument) + ' nom'] v3min = tab['V3PA min'] v3max = tab['V3PA max'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~NOTE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Addressing NIRSpec issue* # *the issue that NIRSpec's angle goes beyond 360 degrees with some targs, # thus resetting back to 0 degrees, which can make the plot look weird # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ index = np.arange(0, len(paNom), 1) for idx in index: try: a1 = paNom[idx] b1 = paNom[idx + 1] if (np.isfinite(a1)) & (np.isfinite(b1)): delta = np.abs(a1 - b1) if delta > 250: gd = np.insert(gd, idx + 1, np.nan) paMin = np.insert(paMin, idx + 1, np.nan) paMax = np.insert(paMax, idx + 1, np.nan) paNom = np.insert(paNom, idx + 1, np.nan) v3min = np.insert(v3min, idx + 1, np.nan) v3max = np.insert(v3min, idx + 1, np.nan) except BaseException: pass # Setting up HoverTool parameters & other variables COLOR = 'green' TOOLS = 'pan, wheel_zoom, box_zoom, reset, save' SOURCE = ColumnDataSource( data=dict(pamin=paMin, panom=paNom, pamax=paMax, date=gd)) TOOLTIPS = [('Date', '@date{%F}'), ('Maximum Aperture PA', '@pamax'), ('Nominal Aperture PA', '@panom'), ('Minimum Aperture PA', '@pamin')] # Time to plot if output == 'bokeh': fig = figure(tools=TOOLS, plot_width=800, plot_height=400, x_axis_type='datetime', title='{} Visibility with {}'.format( targetName, instrument)) # Draw the curve and PA min/max circles try: nom = fig.line('date', 'panom', line_color=COLOR, legend_label='Nominal Aperture PA', alpha=.5, source=SOURCE) except AttributeError: nom = fig.line('date', 'panom', line_color=COLOR, legend='Nominal Aperture PA', alpha=.5, source=SOURCE) fig.circle('date', 'pamin', color=COLOR, size=1, source=SOURCE) fig.circle('date', 'pamax', color=COLOR, size=1, source=SOURCE) # Adding HoverTool fig.add_tools( HoverTool(renderers=[nom], tooltips=TOOLTIPS, formatters={'date': 'datetime'}, mode='vline')) # Plot formatting fig.xaxis.axis_label = 'Date' fig.yaxis.axis_label = 'Aperture Position Angle (degrees)' fig.y_range = ranges.Range1d(0, 360) # Making the output table # Creating new lists w/o the NaN values v3minnan, v3maxnan, paNomnan, paMinnan, paMaxnan, gdnan, mjds = \ [], [], [], [], [], [], [] for vmin, vmax, pnom, pmin, pmax, date in zip(v3min, v3max, paNom, paMin, paMax, gd): if np.isfinite(pmin): v3minnan.append(vmin) v3maxnan.append(vmax) paNomnan.append(pnom) paMinnan.append(pmin) paMaxnan.append(pmax) gdnan.append(date) # Adding MJD column mjdnan = [] for date in gdnan: t = Time(str(date), format='iso') mjd = t.mjd mjdnan.append(mjd) # Adding lists to a table object table = Table( [v3minnan, v3maxnan, paMinnan, paMaxnan, paNomnan, gdnan, mjdnan], names=('min_V3_PA', 'max_V3_PA', 'min_Aperture_PA', 'max_Aperture_PA', 'nom_Aperture_PA', 'Gregorian', 'MJD')) # Getting bad PAs allPAs = np.arange(0, 360, 1) badPAs = [] for pa in allPAs: if (pa not in np.round(paMinnan)) & \ (pa not in np.round(paMaxnan)) & \ (pa not in np.round(paNomnan)): badPAs.append(pa) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~NOTE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # This addresses a bokeh shading issue that accidentally shades # accessible PAs (e.g: trappist-1b) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ remove_pa = [] for badpa in badPAs: for panom in paNomnan: diff = np.abs(badpa - panom) if diff < 7: remove_pa.append(badpa) for pa in np.unique(remove_pa): badPAs.remove(pa) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~NOTE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Grouping the bad PAs into lists within the badPAs list. # This will make bad PA shading easier in the contamination Bokeh plot # (sossContamFig.py) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ badPAs = np.sort(badPAs) if len(badPAs > 0): grouped_badPAs = [[badPAs[0]]] for idx in range(1, len(badPAs)): if ((badPAs[idx - 1] + 1) == badPAs[idx]): grouped_badPAs[len(grouped_badPAs) - 1].append(badPAs[idx]) elif ((badPAs[idx - 1] + 1) < badPAs[idx]): grouped_badPAs.append([badPAs[idx]]) grouped_badPAs = np.asarray(grouped_badPAs) else: # Accounting for targets with 100% visibility grouped_badPAs = np.asarray([]) return paMin, paMax, gd, fig, table, grouped_badPAs
############################################ source1 = ColumnDataSource(dict(x=x1, y=y1)) x_label1 = "Palavras" y_label1 = "qtd" title1 = "Palavras mais utilizadas pela Valéria." plot1 = figure(plot_width=800, plot_height=600, tools="save", x_axis_label=x_label1, y_axis_label=y_label1, title=title1, x_minor_ticks=2, x_range=source1.data["x"], y_range=ranges.Range1d(start=0, end=max(y1) + 300)) labels1 = LabelSet(x='x', y='y', text='y', level='glyph', x_offset=-10, y_offset=0, source=source1, render_mode='canvas') plot1.vbar(source=source1, x='x', top='y', bottom=0, width=0.8,
name.append(v.strip()) elif k == "share": share.append(v) elif k == "short": name[i] = v.strip() elif k == "color": color.append(v.strip()) # 1. exercise source = ColumnDataSource(data=dict(x=name, y=share, color=color)) p1 = figure(title="Bar plot of share of parties", x_axis_label="Party", y_axis_label="Share", x_range=name, y_range=ranges.Range1d(start=0, end=31), plot_width=6000) labels = LabelSet(x='x', y='y', text='y', level='glyph', x_offset=-13.5, y_offset=0, source=source, render_mode='canvas') p1.vbar(x='x', top='y', color='color', line_color='black',
def tap_callback(self, attr, old, new): if len(new["1d"]["indices"]) > 0: new_data = {} new_data['observation'] = [0] new_data['NUTS_ID'] = ['0'] new_data['unit'] = [' '] new_data['color'] = ['#000000'] old_data = self.current_map_CDS.data temporal_data = {'NUTS_ID': [], 'observations': [], 'periods': [], 'units': [], 'colors': []} for indices in new["1d"]["indices"]: new_data['observation'].append(old_data['observation'][indices]) new_data['unit'].append(old_data['unit'][indices]) new_data['NUTS_ID'].append(old_data['NUTS_ID'][indices]) new_data['color'].append(self.color_by_id[old_data['NUTS_ID'][indices]]) raw_index = self.current_dataset.loc[:, :][ self.current_dataset.loc[:, 'NUTS_ID'] == old_data['NUTS_ID'][indices]].index[0] observations = self.current_dataset.loc[raw_index, :]['OBSERVATIONS'][self.id_select.value] sorted_obs = sorted(observations, key=lambda k: k['period']) periods = [] for timestamp in [k['period'] for k in sorted_obs]: s = timestamp.split('-') periods.append(date(int(s[0]), int(s[1]), int(s[2]))) temporal_data['NUTS_ID'].append(old_data['NUTS_ID'][indices]) temporal_data['periods'].append(periods) temporal_data['observations'].append([float(k['value']) for k in sorted_obs]) temporal_data['units'].append([k['unit'] for k in sorted_obs]) temporal_data['colors'].append(PuBu[7][2]) testdata_source = ColumnDataSource(new_data) # dont work with to large datasets x_label = "Region" y_label = "Selected Indicator in {}".format(new_data['unit'][1]) title = "Visualisation" p2 = figure(plot_width=500, plot_height=300, tools="save", x_axis_label=x_label, y_axis_label=y_label, title=title, x_minor_ticks=2, x_range=testdata_source.data["NUTS_ID"], y_range=ranges.Range1d(start=min(testdata_source.data['observation']), end=max(testdata_source.data['observation']))) labels = LabelSet(x='NUTS_ID', y='observation', text='observation', level='glyph', x_offset=-13.5, y_offset=0, source=testdata_source, render_mode='canvas') p2.vbar(source=testdata_source, x='NUTS_ID', top='observation', bottom=0, width=0.3, color='color') p2.toolbar.logo = None p3 = figure(plot_width=500, plot_height=300, tools="save", x_axis_type='datetime', y_axis_label=y_label, x_axis_label='Period', title='Time Series' ) p3.toolbar.logo = None for index, value in enumerate(temporal_data['NUTS_ID']): tmp_CDS = ColumnDataSource( {'NUTS_ID': [value]*len(temporal_data['observations'][index]), 'observations': temporal_data['observations'][index], 'periods': temporal_data['periods'][index], 'units': temporal_data['units'][index] }) p3.line(x='periods', y='observations', color=self.color_by_id[value], source=tmp_CDS) da = pd.DataFrame(np.diff(np.log(tmp_CDS.data['observations']))) #da.index = tmp_CDS.data['periods'] #model = pf.GARCH(p=1, q=1, data=da) #model.adjust_prior(1, pf.TruncatedNormal(0.01, 0.5, lower=0.0, upper=1.0)) #model.adjust_prior(2, pf.TruncatedNormal(0.97, 0.5, lower=0.0, upper=1.0)) #res = model.fit('M-H', nsims=20000) #print('success') #pre_data = model.predict(10) #print(pre_data) self.layout.children[2] = column(Spacer(height=144), p2, p3) else: style = {"font-size": "20px"} unit = self.current_map_CDS.data['unit'][0] mean = np.mean(self.current_map_CDS.data['observation']) deviation = np.std(self.current_map_CDS.data['observation']) p_mean_title = Div(text="Mean:", style=style) p_mean_value = Div(text="{:10,.3} {}".format(mean, unit), style=style) p_deviation_title = Paragraph(text="Standard Deviation:", style=style) p_deviation_value = Paragraph(text="{:10,.3} {}".format(deviation, unit), style=style) hist, edges = np.histogram( self.current_map_CDS.data['observation'], density=True, bins=30) p2 = figure(height=300) p2.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], color=PuBu[7][2]) self.layout.children[2] = column( row(p_mean_title, p_mean_value), row(p_deviation_title, p_deviation_value), Spacer(height=46), p2, width=500)
x = pop_sum_race_df['race_ethn'].loc[min_year].tolist() y = pop_sum_race_df['ratio'].loc[min_year].tolist() source = ColumnDataSource( data=dict(y_m=y_m, right_m=right_m, y_f=y_f, right_f=right_f)) source2 = ColumnDataSource(data=dict(x=x, y=y)) # Set up plot plot = figure(plot_height=400, plot_width=800, title="Population", tools="crosshair,pan,reset,save,wheel_zoom", y_axis_label="Age", x_axis_label="Percentage of the total population", x_range=ranges.Range1d(start=-.01, end=.01), y_range=ranges.Range1d(start=-1, end=102)) glyph1 = HBar(y="y_m", right="right_m", left=0, height=0.5, fill_color="blue", name="Male") plot.add_glyph(source, glyph1) glyph2 = HBar(y="y_f", right="right_f", left=0, height=0.5, fill_color="orange",
def crime_in_neighborhood(data): crime_dict = {} x_values = [] # neighborhood names y_values = [] # number of occurrances # creates a dictionary, gives each location one incident for i in range(len(data)): if data[i][4] not in y_values: crime_dict[data[i][4]] = 1 # counts the number of incidents and updates the dictionary for key in crime_dict: for j in range(len(data)): if data[j][4] == key: crime_dict[key] += 1 # creates lists of the two items for x and y values for key in crime_dict: x_values.append(key) y_values.append(crime_dict[key]) # this creates the chart using Bokeh output_file("crime_data.html") source = ColumnDataSource(dict(x=x_values, y=y_values)) x_label = "Neighborhoods" y_label = "Number of crime incidents" title = "Crime in Portland, Oregon" plot = figure(plot_width=4000, plot_height=700, tools="save, pan, zoom_in, zoom_out, reset", x_axis_label=x_label, y_axis_label=y_label, title=title, x_minor_ticks=0, x_range=source.data["x"], y_range=ranges.Range1d(start=0, end=13000)) plot.xaxis.major_label_orientation = math.pi / 3 labels = LabelSet(x="x", y="y", text="y", level="glyph", x_offset=-15, y_offset=5, source=source, render_mode="canvas", text_font_size="8pt") plot.vbar(source=source, x="x", top="y", bottom=0, width=0.75, color="firebrick", fill_alpha=0.5) plot.add_layout(labels) show(plot)
def test(a, Ed, edition_curr_prev, L_R): GG = a temp_meta = GG.filter(items=[ 'ISBN', 'US_PUB_DATE', 'PMG', 'PMC', 'Medium', 'TITLE_DISC', 'AUTHOR' ]) #filtering the variable GG with the column for meta data str1 = temp_meta.values.tolist() # converting the dataframe to list str1 = [item for sublist in str1 for item in sublist ] #reitering the convertion from dataframe to list # writing the HTML tages to ISBN2 file, these html tags are for meta data, with collapsable window str_html_previous = "" if L_R == "L": str_html_previous = ( """<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a data-toggle="collapse" href="#collapse""" + str(Ed) + L_R + """"> """ + edition_curr_prev + """</a></h4></div><div id="collapse""" + str(Ed) + L_R + """" class="collapse""" + str(Ed) + L_R + """ panel-collapse collapse in">""") else: str_html_previous = ("""<div id="collapse""" + str(Ed) + L_R + """" class="collapse""" + str(Ed) + L_R + """ panel-collapse collapse">""") str_html_previous += ( """ <div class="panel-body"><br/><div class="bs-example" data-example-id="table table-bordered" id ="table table-bordered"><table class= "table table-bordered"> <thead> <th class=""col-md-1"">""" + "Year" + """</th><th class=""col-md-1"">Jan</th> <th>Feb</th> <th>Mar</th> <th>Apr</th> <th>May</th> <th>Jun</th> <th>Jul</th> <th>Aug</th> <th>Sep</th> <th>Oct</th> <th>Nov</th> <th>Dec</th></tr> </thead> <tbody> <tr> """ ) lista = GG.filter( regex=("Period :*")) # using regex, getting all period sales lista = lista.values.tolist() # removing the headers from the dataframe lista = [item for sublist in lista for item in sublist] # converting dataframe to list lista = list(map(int, lista)) # converting the list variables to integer max_num = max(list(map(int, lista))) # getting the max number for future use listb = GG.loc[:, 'Month'] # taking only the month from the list row listc = GG.loc[:, 'First Sale Month'] #taking the first sale month for preparing the table listc = ''.join((listc.tolist())) date1 = "1/" + listc[ 0: 8] # this is used for creating a running date period for sales table[] listc = listc.split("/") listc = int(listc[1]) listb = listb.get_value(listb.index[0]) # getting the month value str_html_previous += ("<td>" + str(listc) + "</td>" ) # writing the year to the ISBN2 file # if the first sale month is january then the below set of the codes works if int(listb) != 1: #condintion to check the first sale month equal to 1 for i in range( 0, listb ): # is the title doesnt have a sales start month as 1 then its pushed here lista.insert(0, 0) # 0's are inserted based the month number a = 12 #future use b = ( len(lista) / 12 ) #Dividing the length of the period to get how many years the sales period has if isinstance( b, float ) == True: # if the division results in value with decimal places, rounding off the decimal b = math.ceil(b) for j in range(1, b): # running the below loop based on number of years if a == 12: # this condition is to check whether its the first year of sale, as it has to be handled differently parts = lista[ 0: a] #assigning the values to the parts. This variable will be used for compliation purpose pd_new = pd.DataFrame( parts) #Converting the parts variable to dataframe Part_for_chart = parts # assigning values to variable which will be used for charts parts = ['<td>' + str(s) + '</td>' for s in parts ] # adding HTML tags for every element in the dataframe str1 = ''.join(parts) # converting the dataframe into list str_html_previous += (str1 + "</tr>" ) # writing the str1 list to ISBN2 file #myfile.write a = a + 12 else: parts = lista[a - 12:a] #adding the values to the parts if ( len(parts) ) != 12: #in the last year of sales, it will not have 12 month data, hence its checked here. and if its true "0" will be added in the below code lengthofparts = 12 - (len(parts)) g = [parts.append(0) for z in range(0, lengthofparts)] pd_new = pd_new.append(parts) for x in parts: # here appending every value into Part_for_chart variable. Part_for_chart.append(x) parts = ['<td>' + str(s) + '</td>' for s in parts] # adding html tags for sales table str1 = ''.join(parts) listd = '<td>' + str( int(listc) + 1) + '</td>' # adding the year to the table with HTML tags str_html_previous += (listd + str1 + "</tr>" ) #wRITING THE tags to the ISBN2 file #myfile.write a = a + 12 listc = int(listc) listc = listc + 1 #print(listc) date2 = datetime.strptime( str(int(listc) + 1) + "-1-01", "%Y-%m-%d") # date2 variable to capture the last year months_str = calendar.month_name months = [] date1 = datetime.strptime(date1, "%d/%m/%Y") # below while loop will generate consecutive # of months and years which is stored into months variable while date1 < date2: month = date1.month # month is stored here. year = date1.year # year is stored here month_str = months_str[month][ 0:3] # month num is converted to string 1 is converted to Jan months.append("{0}-{1}".format( month_str, str(year)[-2:])) # month string is appended to months variable next_month = month + 1 if month != 12 else 1 next_year = year + 1 if next_month == 1 else year date1 = date1.replace(month=next_month, year=next_year) source = ColumnDataSource( data=dict(height=Part_for_chart, weight=months, names=Part_for_chart)) output_file("U:\\django_test\\mysitetest\\blog\\templates\\svg\\bar" + str(Ed) + L_R + ".svg") p = figure(plot_width=1500, title=L_R + '- (' + edition_curr_prev + ')', tools="", x_range=source.data["weight"], y_range=ranges.Range1d(start=0, end=(max_num + 100)), toolbar_location=None) p.line(months, Part_for_chart, line_width=2) labels = LabelSet(x='weight', y='height', text='height', level='glyph', x_offset=-13.5, y_offset=0, source=source, text_font_size="10pt", render_mode='canvas') p.xaxis.major_label_orientation = math.pi / 2 p.add_layout(labels) save(p) #show(p) #str_html_previous += ("""<table class="inlineTable"><div id="chart" >{% include "svg/bar"""+ str(Ed) + L_R +""".svg" %}</div></table><div class="col-md-9"></div></div></div></div></div></div>""") #str_html_previous += ("""<table class="inlineTable"><div id="chart" >{% include "svg/bar"""+ str(Ed) + L_R +""".svg" %}</div></table><div class="col-md-9"></div></div></div></div>""") str_html_previous += ( """<table class="inlineTable"><div id="chart" ><svg viewBox="0 0 60 55" width="100" height="50"><use xlink:href=""svg/bar""" + str(Ed) + L_R + """.svg""/> </svg></div></table><div class="col-md-9"></div></div></div></div>""" ) str_html_previous += ("</tr>") return str_html_previous
def using_gtvt(ra, dec, instrument, ephFileName=None, output='bokeh'): """Plot the visibility (at a range of position angles) against time. Parameters ---------- ra : float The RA of the target. dec : float The Dec of the target. instrument : str Name of the instrument. Can either be (case-sensitive): 'NIRISS', 'NIRCam', 'MIRI', 'FGS', or 'NIRSpec' ephFileName : str The filename of the ephemeris file. output : str Switches on plotting with Bokeh. Parameter value must be 'bokeh'. Returns ------- paGood : float The good position angle. paBad : float The bad position angle. gd : matplotlib.dates object The gregorian date. fig : bokeh.plotting.figure object The plotted figure. """ # getting calculations from GTVT (General Target Visibility Tool) tab = get_table(ra, dec) gd = tab['Date'] paMin = tab[str(instrument) + ' min'] paMax = tab[str(instrument) + ' max'] paNom = tab[str(instrument) + ' nom'] v3min = tab['V3PA min'] v3max = tab['V3PA max'] # addressing NIRSpec issue* # *the issue that NIRSpec's angle goes beyond 360 degrees with some targs, # thus resetting back to 0 degrees, which can make the plot look weird index = np.arange(0, len(paNom), 1) for idx in index: try: a1 = paNom[idx] b1 = paNom[idx + 1] if (np.isfinite(a1) == True) & (np.isfinite(b1) == True): delta = np.abs(a1 - b1) if delta > 250: print(a1, b1, delta) gd = np.insert(gd, idx + 1, np.nan) paMin = np.insert(paMin, idx + 1, np.nan) paMax = np.insert(paMax, idx + 1, np.nan) paNom = np.insert(paNom, idx + 1, np.nan) v3min = np.insert(v3min, idx + 1, np.nan) v3max = np.insert(v3min, idx + 1, np.nan) except: pass # Setting up HoverTool parameters & other variables COLOR = 'green' TOOLS = 'pan, wheel_zoom, box_zoom, reset, save' SOURCE = ColumnDataSource(data=dict(pamin=paMin,\ panom=paNom,\ pamax=paMax,\ date=gd)) TOOLTIPS = [('Date','@date{%F}'),\ ('Maximum Aperture PA', '@pamax'),\ ('Nominal Aperture PA', '@panom'),\ ('Minimum Aperture PA', '@pamin')] # Time to plot if output == 'bokeh': fig = figure(tools=TOOLS,\ plot_width=800,\ plot_height=400,\ x_axis_type='datetime',\ title='Target Visibility with '+str(instrument)) # Draw the curve and PA min/max patch fig.circle('date', 'panom', color=COLOR, size=1, legend='Nominal Aperture PA',\ source=SOURCE, alpha=.5) fig.circle('date', 'pamin', color=COLOR, size=1, source=SOURCE) fig.circle('date', 'pamax', color=COLOR, size=1, source=SOURCE) # Adding HoverTool fig.add_tools(HoverTool(tooltips=TOOLTIPS,\ formatters={'date':'datetime'},\ mode='vline')) # Plot formatting fig.xaxis.axis_label = 'Date' fig.yaxis.axis_label = 'Position Angle (degrees)' fig.y_range = ranges.Range1d(0, 360) # Making the output table # Creating new lists w/o the NaN values v3minnan, v3maxnan, paNomnan, paMinnan, paMaxnan, gdnan, mjds = \ [], [], [], [], [], [], [] for vmin, vmax, pnom, pmin, pmax, date in zip(v3min, v3max, paNom, paMin, paMax, gd): if np.isfinite(pmin) == True: v3minnan.append(vmin) v3maxnan.append(vmax) paNomnan.append(pnom) paMinnan.append(pmin) paMaxnan.append(pmax) gdnan.append(date) # Adding MJD column mjdnan = [] for date in gdnan: t = Time(str(date), format='iso') mjd = t.mjd mjdnan.append(mjd) # Adding lists to a table object table = Table([v3minnan, v3maxnan, paMinnan, paMaxnan, paNomnan, gdnan, mjdnan],\ names=('#min_V3_PA', 'max_V3_PA','min_Aperture_PA',\ 'max_Aperture_PA', 'nom_Aperture_PA', 'Gregorian', 'MJD')) return paMin, paMax, gd, fig, table