Esempio n. 1
0
def time_comp(unwound_data):
    df = pd.DataFrame(pd.io.json.json_normalize(unwound_data))
    p = figure()
    x = df['TEST.DATETIME']
    p.circle(x, df['TEST.VALUE'])
    p.xaxis.formatter = formatters.DatetimeTickFormatter(days=["%Y-%m-%d"])
    #add limits
    if 'LOWER LIMIT' in df.columns:
        p.line(x, df['LOWER LIMIT'], line_color='red')
    if 'NOMINAL' in df.columns:
        p.line(x, df['NOMINAL'], line_color='green')
    if 'UPPER LIMIT' in df.columns:
        p.line(x, df['UPPER LIMIT'], line_color='red')
    script, div = components(p)
    return script, div
Esempio n. 2
0
def box_whisker(data):
    """
    data: mongo list result with fields DATE,LL,NOM,UL,Q1,Q3,MEAN,MAX,MIN
    """
    #https://bokeh.pydata.org/en/latest/docs/gallery/boxplot.html
    #%%
    df = pd.DataFrame(pd.io.json.json_normalize(data))
    df2 = pd.DataFrame(df['STATS.QUARTILES'].tolist(),
                       columns=['Q1', 'Q2', 'Q3'])
    df.pop('STATS.QUARTILES')
    df = pd.concat([df, df2], axis=1)

    figs = []
    for name, group in df.groupby('TESTER_NAME'):
        #somehow combine data for dates
        top = float(
            pd.to_numeric(group.UPPER_LIMIT).mode() +
            group['STATS.STDEV'].min())
        bot = float(
            pd.to_numeric(group.LOWER_LIMIT).mode() -
            group['STATS.STDEV'].min())
        p = figure(title=name, y_range=(bot, top))
        x = group['DATE']

        #stems
        p.segment(x, group['STATS.MAX'], x, group['Q3'])
        p.segment(x, group['STATS.MIN'], x, group['Q1'])

        #boxes
        w = datetime.timedelta(days=0.75)  #lucky guess
        p.vbar(x,
               w,
               group['Q1'],
               group['Q2'],
               fill_color="#E08E79",
               line_color="black")
        p.vbar(x,
               w,
               group['Q2'],
               group['Q3'],
               fill_color="#3B8686",
               line_color="black")

        # whiskers (squares are easier)
        p.square(x, group['STATS.MIN'])
        p.square(x, group['STATS.MAX'])

        #limits
        p.line(x, group['UPPER_LIMIT'], line_color="red")
        p.line(x, group['NOMINAL'], line_color="green")
        p.line(x, group['LOWER_LIMIT'], line_color="red")
        p.xaxis.formatter = formatters.DatetimeTickFormatter(days=["%Y-%m-%d"])
        figs.append(p)
        #%%
    '''    
    #show table
    td = {'TESTER_NAME' : list(df.TESTER_NAME),
            'DATE' : list(df.DATE.astype(str)),
          'LOG_FILE' : list(df.LOG_FILE)}
    source = ColumnDataSource(td)
    cols = [TableColumn(field='TESTER_NAME', title='Tester'),
            TableColumn(field='DATE', title='Date'),
            TableColumn(field='LOG_FILE', title='Log File Link')]
    w = sum([fig.plot_width for fig in figs]) #spans all plots
    table = DataTable(source=source, columns=cols, width=w)
    grid = gridplot([figs, [widgetbox(table)]])
    '''
    grid = gridplot([figs])
    script, div = components(grid)
    return script, div
Esempio n. 3
0
def index():
    """Get datapoints and attributes and display in a graph.

    The arguments are provided through the GET request.

    Args:
        attribute: string with the attribute to filter by.
        timeIntervallType: string with the type of time intervall to filter by.
        timeArgument: list of string(s) containing the time ranges.
    Returns:
        A rendered HTML site with the graph included.
    """
    attributes = backend.get_attributes()
    current_attribute = None
    filters = {}
    if 'attribute' in request.args:
        filters['Argument'] = request.args['attribute']
        for attribute in attributes:
            if request.args['attribute'] == attribute['name']:
                current_attribute = attribute
                break
    if 'timeIntervallType' in request.args and 'timeArgument' in request.args:
        intervallType = request.args['timeIntervallType']
        time_argument = json.loads(request.args['timeArgument'])
        if intervallType == 'months' and len(time_argument[0]) == 2:
            filters['timeIntervallType'] = 'MONTH'
            filters['timeArgument'] = [time_argument[0]]
        else:
            filters['timeIntervallType'] = 'TIME_INTERVALL'
            if intervallType == 'year':
                date_start = datetime.datetime(int(time_argument[0]), 1, 1)
                date_end = datetime.datetime(int(time_argument[0]), 12, 31)
            elif intervallType == 'months':
                date = datetime.datetime.strptime(time_argument[0], '%Y-%m')
                date_start = datetime.datetime(date.year, date.month, 1)
                days_in_month = calendar.monthrange(date.year, date.month)[1]
                date_end = datetime.datetime(date.year, date.month,
                                             days_in_month)
            else:
                date_start = datetime.datetime.strptime(
                    time_argument[0], '%Y-%m-%d')
                date_end = datetime.datetime.strptime(time_argument[1],
                                                      '%Y-%m-%d')
            date_start = date_start.strftime('%Y-%m-%d')
            date_end = date_end.strftime('%Y-%m-%d')
            filters['timeArgument'] = [date_start, date_end]
    if filters:
        data_points = backend.get_data(filters)
    else:
        data_points = []

    if not current_attribute:
        current_attribute = attributes[0]

    for dp in data_points:
        dp['date'] = datetime.datetime(dp['year'], dp['month'], dp['day'],
                                       dp['time'])

    res = {}
    for dp in data_points:
        for attr in dp:
            if attr in res:
                res[attr].append(dp[attr])
            else:
                res[attr] = [dp[attr]]
    data_points = ColumnDataSource(res)

    if data_points.data:
        p = figure(x_axis_label='Time',
                   y_axis_label=current_attribute['displayName'],
                   x_axis_type='datetime',
                   y_range=[
                       min(0,
                           min(data_points.data['value']) - 1),
                       max(data_points.data['value']) + 1
                   ])

        p.xaxis.ticker = DaysTicker(days=list(range(1, 32)))
        p.xaxis.formatter = formatters.DatetimeTickFormatter(days="%Y-%m-%d")

        p.line(x='date', y='value', line_width=2, source=data_points)
        p.circle(x='date', y='value', size=10, source=data_points)

        p.add_tools(
            HoverTool(tooltips=[('Datetime', '@date{%Y-%m-%d %H:%M:%S}'),
                                ('Value', '@value{0.0} @unit')],
                      formatters={'@date': 'datetime'},
                      mode='vline'))

        js_resources = INLINE.render_js()
        css_resources = INLINE.render_css()

        script, div = components(p)
    else:
        js_resources, css_resources, script, div = '', '', '', ''

    return render_template('index.html',
                           attributes=attributes,
                           selected_attribute=current_attribute,
                           js=js_resources,
                           css=css_resources,
                           script=script,
                           div=div)
	def build_ohlc_components(self):
		plot_dict = {}
		for table in self.ohlc_tables:
			tname = table.__tablename__
			cp = tname[:6]

			df = pd.read_sql_table(
				tname,
				self.db.engine,
				columns=['timestamp','open', 'high','low','close']
			)
			df.drop_duplicates(inplace=True)
			r = pd.date_range(
				start=df.timestamp.min(), end=df.timestamp.max()
			)
			df = df.set_index('timestamp').reindex(r).fillna(np.NaN)
			df.index = df.index.rename('timestamp')

			df['open_inc'],df['high_inc'],df['low_inc'],df['close_inc']=\
			df['open'],    df['high'],    df['low'],    df['close']
			df['open_dec'],df['high_dec'],df['low_dec'],df['close_dec']=\
			df['open'],    df['high'],    df['low'],    df['close']

			inc = df.close > df.open
			dec = df.open > df.close

			df['open_dec'][inc]=None
			df['high_dec'][inc]=None
			df['low_dec'][inc]=None
			df['close_dec'][inc]=None

			df['open_inc'][dec]=None
			df['high_inc'][dec]=None
			df['low_inc'][dec]=None
			df['close_inc'][dec]=None

			source = ColumnDataSource(data=df)
			reference = ColumnDataSource(data=df)

			plot = figure(
				x_axis_type="datetime",
				plot_height=250,
				plot_width=1400,
				toolbar_location="above",
				tools=self.TOOLS,
				# these didn't do much
				# sizing_mode='stretch_width',
				# sizing_mode='stretch_height',
				# sizing_mode='stretch_both',
				# sizing_mode='scale_width',
				# sizing_mode='scale_height',
				# sizing_mode='scale_both',
				# sizing_mode='fixed',
				title=title_dict[cp]
			)
			seg = plot.segment(
				x0='timestamp',
				y0='high',
				x1='timestamp',
				y1='low',
				color='black',
				source=source
			)
			inc_bar = plot.vbar(
				x='timestamp',
				width=20*60*60*1000,
				top='close_inc',
				bottom='open_inc',
				fill_color="#D5E1DD",
				line_color="black",
				source=source
			)
			dec_bar = plot.vbar(
				x='timestamp',
				width=20*60*60*1000,
				top='open_dec',
				bottom='close_dec',
				fill_color="#F2583E",
				line_color="black",
				source=source
			)

			date_range_slider = DateRangeSlider(
				# width=400,
				align='center',
				margin=(25,50,5,50),
				# format="%x",
				format="%B %e, %Y",
				value=(
					df.index.min(),
					df.index.max()
				),
				start=df.index.min(),
				end=df.index.max(),
				background='#343a40',
				bar_color='#f8f9fa',
				step=86400000
			)

			args = dict(
				source=source,
				reference=reference
			)
			code =\
			"""
			const data = source.data;
			const data_ref = reference.data;

			const from_date = this.value[0];
			const to_date = this.value[1];

			var from_pos = data_ref['timestamp'].indexOf(to_date);
			var to_pos = data_ref['timestamp'].indexOf(from_date);

			data['timestamp'] = data_ref['timestamp'].slice(to_pos,from_pos);

			data['high'] = data_ref['high'].slice(to_pos,from_pos);
			data['low'] = data_ref['low'].slice(to_pos,from_pos);

			data['open_inc'] = data_ref['open_inc'].slice(to_pos,from_pos);
			data['high_inc'] = data_ref['high_inc'].slice(to_pos,from_pos);
			data['low_inc'] = data_ref['low_inc'].slice(to_pos,from_pos);
			data['close_inc'] = data_ref['close_inc'].slice(to_pos,from_pos);

			data['open_dec'] = data_ref['open_dec'].slice(to_pos,from_pos);
			data['high_dec'] = data_ref['high_dec'].slice(to_pos,from_pos);
			data['low_dec'] = data_ref['low_dec'].slice(to_pos,from_pos);
			data['close_dec'] = data_ref['close_dec'].slice(to_pos,from_pos);

			source.change.emit();
			"""
			callback = CustomJS(args=args, code=code)
			date_range_slider.js_on_change("value", callback)

			# NaN's sometimes show up in hover tool
			hover_tool = HoverTool(
				tooltips=[
				# https://docs.bokeh.org/en/latest/docs/reference/models/formatters.html#bokeh.models.formatters.DatetimeTickFormatter
					('timestamp', '@{timestamp}{%x}'),
					('open', '@{open}{%0.4f}'),
					('high', '@{high}{%0.4f}'),
					('low', '@{low}{%0.4f}'),
					('close', '@{close}{%0.4f}')
				],
				formatters={
					'@{timestamp}': 'datetime',
					'@{open}': 'printf',
					'@{high}': 'printf',
					'@{low}': 'printf',
					'@{close}': 'printf'

				},
				renderers=[inc_bar,dec_bar]
			)
			plot.add_tools(hover_tool)
			plot.background_fill_color = '#f8f9fa'
			plot.border_fill_color = "#343a40"
			plot.title.text_color = "#f8f9fa"
			plot.xaxis.axis_label_text_color = "#f8f9fa"
			plot.yaxis.axis_label_text_color = "#f8f9fa"
			plot.xaxis.major_label_text_color = "#f8f9fa"
			plot.yaxis.major_label_text_color = "#f8f9fa"
			plot.xgrid.visible = False
			plot.xaxis.formatter = formatters.DatetimeTickFormatter(
				days="%m/%d/%Y",
				months = "%m/%d/%Y"
			)
			# plot.background_fill_color = '#868e96'
			plot_dict[cp] = column(plot, date_range_slider)


		return components(plot_dict)
	def build_components(self, is_closed):
		plot_dict = {}
		for table in self.tables:
			cp = table.__tablename__
			url =f"http://*****:*****@{timestamp}{%d %b %Y %l:%M:%S:%N %P}'),
					('rate', '@{rate}{%0.4f}'),
				],
				formatters={
					'@{timestamp}': 'datetime',
					'@{rate}': 'printf'
				}
			)
			plot.add_tools(hover_tool)
			callback = CustomJS(
				args={'line':line, 'source':source, 'cp':cp}, code="""
				var rates = source.data.rate;
				var first_val = rates[0];
				var last_val = rates[rates.length-1];
				var delta = Number.parseFloat(Math.abs(last_val-first_val)).toFixed(5);
				var increasing = first_val < last_val;
				if (increasing) {
					line.glyph.line_color = 'green';
				} else {
					line.glyph.line_color = 'red';
				}
						var card_class_dict = {
						true: {
						  "card_class":"card-text text-center font-weight-lighter text-success",
						  "new_color": "green",
						  "arrow": "▲"
						},
						false: {
						  "card_class":"card-text text-center font-weight-lighter text-danger",
						  "new_color": "red",
						  "arrow": "▼"
						  }
						}
						var formats = card_class_dict[increasing];

						$('#delta_'+cp)
						.removeClass()
						.addClass(
							  formats['card_class']
						)
						.html(
							formats["arrow"].concat(delta)
						);
						$('#current_'+cp).html(last_val);

				"""
			)
			source.js_on_change('change:data', callback)
			plot_dict[cp] = plot
		return components(plot_dict)
Esempio n. 6
0
class Graph_Create():
    colors = [
    "#c6e3cb",
    "#9ed5cd",
    "#83cacf",
    "#47aed0",
    "#3984b6",
    "#2c5a9c",
    "#1e3082",
    ]
    name_o_days = [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
    ]
    
    dem = Dem()
    dem.create_week_pop()
    dem.create_week_volume()
    dem.create_week_density()
    days = [1]

    time_format = formatters.DatetimeTickFormatter()
    time_format.hours = ["%H:%M"]
    datetime = dt.datetime(2018,6,1,6)
    one_hour = dt.timedelta(hours=1)
    one_day = dt.timedelta(days=1)
    time_var =[datetime]



    def week_graphs(self):
        p = figure( width = 1200,height=650,title="Week",x_axis_type="datetime", logo = None,tools="pan, wheel_zoom,reset", active_drag="pan")#fill week    
        for day in range(7):
            self.time_var = [self.datetime]
            hours_open = len(self.dem.week_pop[day])-1
            for x in range(hours_open):
                self.datetime = self.datetime+self.one_hour
                self.time_var.append(self.datetime)
            # datetime= datetime+one_day
            self.datetime = self.datetime - hours_open*self.one_hour
            source = ColumnDataSource(dict(
                x=self.time_var,
                y=self.dem.week_pop[day],
                volume=self.dem.week_volume[day],
                density=self.dem.week_density[day],


            ))
            hover = HoverTool()
            hover.tooltips = [
                ("(x,y)","($x,$y)"),
                ("volume","@volume"),
                ("density","@density")

            ]
            
            
            p.xaxis.formatter= self.time_format
            p.yaxis.axis_label = "Population"
            p.xaxis.axis_label = "Time"
            p.circle("x","y",source=source,legend="{0}".format(self.name_o_days[day]),color = self.colors[day])
            p.line("x","y",source=source,color = self.colors[day])
        p.tools.append(hover)
        show(p)
    
    def week_single_graphs(self): 
        for day in range(7):
            p = figure( width = 1200,height=650,title="{0}".format(self.name_o_days[day]),x_axis_type="datetime", logo = None,tools="pan, wheel_zoom,reset", active_drag="pan")#fill week 
            self.time_var = [self.datetime]
            hours_open = len(self.dem.week_pop[day])-1
            for x in range(hours_open):
                self.datetime = self.datetime+self.one_hour
                self.time_var.append(self.datetime)
            # datetime= datetime+one_day
            self.datetime = self.datetime - hours_open*self.one_hour
            source = ColumnDataSource(dict(
                x=self.time_var,
                y=self.dem.week_pop[day],
                volume=self.dem.week_volume[day],
                density=self.dem.week_density[day],


            ))
            hover = HoverTool()
            hover.tooltips = [
                ("(x,y)","($x,$y)"),
                ("volume","@volume"),
                ("density","@density")

            ]
            
            
            p.xaxis.formatter= self.time_format
            p.yaxis.axis_label = "Population"
            p.xaxis.axis_label = "Time"
            p.circle("x","y",source=source,legend="{0}".format(self.name_o_days[day]),color = self.colors[day])
            p.line("x","y",source=source,color = self.colors[day])
            p.tools.append(hover)
            show(p)