def _process_and_display_trends(self, query, objects_expected): from qtools.lib.nstats import moving_average_by_interval from numpy import histogram import numpy as np group_by_plate = self.form_result['group_by_plate'] if objects_expected: results = execute_built_query(query, group_by_plate, True, self.form_result['metric'][1]) else: results = execute_built_query(query, group_by_plate, False) epoch_results = [(float(stat), time.mktime(dt.timetuple()), id, name) for stat, dt, id, name in results if stat is not None] exclude_func = create_exclude_function(self.form_result) filtered_results = [tup for tup in epoch_results if exclude_func(tup[0])] c.yaxis_title = fl.comparable_metric_display(MetricPattern.from_python(self.form_result['metric'])) c.mean_value = 'N/A' c.std_value = 'N/A' c.metric_name = fl.comparable_metric_display(MetricPattern.from_python(self.form_result['metric'])) c.table_data =[] if group_by_plate: moving_average = 10 epoch_url_results = [(stat, stamp*1000, url(controller='plate', action='view', id=id), name) for stat, stamp, id, name in filtered_results] else: moving_average = 50 epoch_url_results = [(stat, stamp*1000, url(controller='well', action='view', id=id), name) for stat, stamp, id, name in filtered_results] if epoch_url_results: c.stats = {} stats, time_points, urls, names = zip(*epoch_url_results) c.stats = h.literal(json.dumps(zip(time_points, stats))) c.table_data = zip( names, stats, time_points ) #update if we can. c.mean_value = str( np.mean( stats ) ) c.std_value = str( np.std( stats ) ) INTERVAL = 24*60*60*1000 ma = moving_average_by_interval(stats, time_points, INTERVAL, 10) c.moving_avg = h.literal(json.dumps(ma)) c.urls = h.literal(json.dumps(urls)) c.names = h.literal(json.dumps(names)) # get histogram of points by day #raise Exception, len(ma) density, edges = histogram(time_points, bins=len(ma), range=(ma[0][0], ma[-1][0])) c.densities = h.literal(json.dumps(zip([m[0] for m in ma], [int(d) for d in density]))) c.max_density_axis = 3*max(density) else: c.stats = {} c.moving_avg = {} c.urls = {} c.names = {} c.densities = {} c.max_density_axis = 3 return render('/trend/results.html')
def _get_qcc_text(self): query_str = "Results for " if ( self.form_result['metric'] ): query_str += fl.comparable_metric_display(MetricPattern.from_python(self.form_result['metric'])) query_str += ' metric' if ( self.form_result['start_date'] ): query_str += ', after ' + str( self.form_result['start_date'] ) if ( self.form_result['end_date'] ): query_str += ', before ' + str( self.form_result['end_date'] ) if ( self.form_result['group_by_plate'] ): query_str += ', averged by plate' if ( self.form_result['plate_type'] ): query_str += ', filtered by plate type "%s"' % dict(fl.all_plate_types_field()['options'])[ self.form_result['plate_type'] ] if ( 'plate_name' in self.form_result and self.form_result['plate_name'] ): query_str += ', filtered by plate name "%s"' % self.form_result['plate_name'] if ( 'sample_name' in self.form_result and self.form_result['sample_name'] ): query_str += ', filtered by sample name "%s"' % self.form_result['sample_name'] if ( 'assay_name' in self.form_result and self.form_result['assay_name'] ): query_str += ', filtered by assay name "%s"' % self.form_result['assay_name'] if ( 'reader' in self.form_result and self.form_result['reader'] ): query_str += ', filtered by reader "%s"' % dict(fl.box2_field(exclude_fluidics_modules=True)['options'])[ int( self.form_result['reader']) ] if( 'reader_category' in self.form_result and self.form_result['reader_category'] ): query_str += ', filtered by reader "%s"' % dict(reader_category_field()['options'])[ self.form_result['reader_category'] ] xaxis_str = 'Plates'; if ( self.form_result['group_by_plate'] ): xaxis_str = 'DR names'; elif 'reader' in self.form_result and self.form_result['reader']: xaxis_str = 'Plates on %s' % dict(fl.box2_field(exclude_fluidics_modules=True)['options'])[ int( self.form_result['reader'] ) ] return query_str, xaxis_str
def _process_and_display_qcc(self, query, objects_expected): """ This procuces the data for java scirpt display of qcc """ import numpy as np #average results across plates? group_by_plate = self.form_result['group_by_plate'] if objects_expected: results = execute_built_query(query, group_by_plate, True, self.form_result['metric'][1]) else: results = execute_built_query(query, group_by_plate, False) epoch_results = [(float(stat), time.mktime(dt.timetuple()), id, name, DR) for stat, dt, id, name, DR in results if stat is not None] exclude_func = create_exclude_function(self.form_result) filtered_results = [tup for tup in epoch_results if exclude_func(tup[0])] c.yaxis_title = fl.comparable_metric_display(MetricPattern.from_python(self.form_result['metric'])) c.y_label = fl.comparable_metric_display(MetricPattern.from_python(self.form_result['metric'])) c.explanation, c.x_label = self._get_qcc_text() if group_by_plate: epoch_url_results = []; for i in range( len( filtered_results ) ): stat, stamp, id, name, DR = filtered_results[i] url_str = url(controller='plate', action='view', id=id) epoch_url_results.append( (stat,i,url_str,name,DR) ) #epoch_url_results = [(stat, stamp, url(controller='plate', action='view', id=id), name) for stat, stamp, id, name in filtered_results] else: epoch_url_results = []; for i in range( len( filtered_results ) ): stat, stamp, id, name, plate = filtered_results[i] url_str = url(controller='well', action='view', id=id) epoch_url_results.append( (stat,i,url_str,name,plate) ) #epoch_url_results = [(stat, stamp, url(controller='well', action='view', id=id), name) for stat, stamp, id, name in filtered_results] if epoch_url_results: c.stats = {} stats, time_points, urls, names, xTickLabels = zip(*epoch_url_results) xTickLabels = [ x + " " for x in xTickLabels] c.stats = h.literal(json.dumps(zip(time_points, stats))) stat_mean = np.mean( stats ) stat_standard_dev = np.std( stats ) #stat_standard_error = np.std( stats ) / np.sqrt( len(stats) ) ucl = stat_mean + 3 * stat_standard_dev lcl = stat_mean - 3 * stat_standard_dev mv = [[epoch_url_results[0][1],stat_mean],[epoch_url_results[-1][1],stat_mean]] c.mean_value = h.literal(json.dumps(mv)) xaxis_start = epoch_url_results[0][1] xaxis_end = epoch_url_results[-1][1] c.ucl = h.literal(json.dumps([[xaxis_start,ucl],[xaxis_end,ucl]] )) c.lcl = h.literal(json.dumps([[xaxis_start,lcl],[xaxis_end,lcl]] )) axis_offset = 0.05*(max(stats)-min(stats)) if self.form_result['lower_yaxis']: c.min_y_axis = float( self.form_result['lower_yaxis'] ) else: c.min_y_axis = min( stats ) - axis_offset if self.form_result['upper_yaxis']: c.max_y_axis = float( self.form_result['upper_yaxis'] ) else: c.max_y_axis = max( stats ) + axis_offset c.mv_label = "Center Line (mean): %f" % stat_mean c.ucl_label = "UCL (mean + 3std): %f" % ucl c.lcl_label = "LCL (mean - 3std): %f" % lcl # add user defined limits if presented if self.form_result['upper_spec']: upper_spec = float(self.form_result['upper_spec']) c.upper_spec = h.literal(json.dumps([[xaxis_start,upper_spec],[xaxis_end,upper_spec]] )) c.upper_spec_label = 'User defined upper line: %f' % upper_spec else: c.upper_spec = 'null' c.upper_spec_label = '' if self.form_result['lower_spec']: lower_spec = float(self.form_result['lower_spec']) c.lower_spec = h.literal(json.dumps([[xaxis_start,lower_spec],[xaxis_end,lower_spec]] )) c.lower_spec_label = 'User defined lower line: %f' % lower_spec else: c.lower_spec = 'null' c.lower_spec_label = '' c.urls = h.literal(json.dumps(urls)) c.names = h.literal(json.dumps(names)) c.TickData = h.literal(json.dumps(zip(range(len(xTickLabels)),xTickLabels))) else: c.stats = {} c.mean_value = {} c.ucl = {} c.lcl = {} c.mv_label = '' c.ucl_label = '' c.lcl_label = '' c.urls = {} c.names = {} c.TickData = {} c.min_y_axis = 0 c.max_y_axis = 3 c.upper_spec = 'null' c.upper_spec_label = '' c.lower_spec = 'null' c.lower_spec_label = '' return render('/qcCharts/results.html')