def test_xy_reader_with_timestamps(self): """Test the `read_xy_values` function with a ``.csv`` file containing timestamps. """ filename = write_tempfile( """"Eastern Standard Time","Response Time","Response Length" "2010/05/19 13:45:50",419,2048 "2010/05/19 13:45:55",315,2048 """) # Test parsing with no gmt_offset or zero_time reader = csv.DictReader(open(filename)) x_values, y_values = utils.read_xy_values( reader, 'Eastern Standard Time', ['Response Time', 'Response Length'], date_format='%Y/%m/%d %H:%M:%S') self.assertEqual(x_values, [ datetime(2010, 5, 19, 13, 45, 50), datetime(2010, 5, 19, 13, 45, 55), ]) self.assertEqual(y_values, { 'Response Length': [2048.0, 2048.0], 'Response Time': [419.0, 315.0], }) # Test parsing with gmt_offset=6 reader = csv.DictReader(open(filename)) x_values, y_values = utils.read_xy_values( reader, 'Eastern Standard Time', ['Response Time', 'Response Length'], date_format='%Y/%m/%d %H:%M:%S', gmt_offset=6) self.assertEqual(x_values, [ datetime(2010, 5, 19, 19, 45, 50), datetime(2010, 5, 19, 19, 45, 55), ]) self.assertEqual(y_values, { 'Response Length': [2048.0, 2048.0], 'Response Time': [419.0, 315.0], }) # Test parsing zero_time reader = csv.DictReader(open(filename)) x_values, y_values = utils.read_xy_values( reader, 'Eastern Standard Time', ['Response Time', 'Response Length'], date_format='%Y/%m/%d %H:%M:%S', zero_time=True) self.assertEqual(x_values, [ datetime(2010, 5, 19, 0, 0, 0), datetime(2010, 5, 19, 0, 0, 5), ]) self.assertEqual(y_values, { 'Response Length': [2048.0, 2048.0], 'Response Time': [419.0, 315.0], }) # Remove the temporary file os.unlink(filename)
def test_xy_reader_without_timestamps(self): """Test the `read_xy_values` function with a ``.csv`` file that does not contain timestamps, only numeric values. """ filename = write_tempfile( """X,Y,Z 0,2,3 1,4,6 2,6,9 3,8,12 4,10,15 """) reader = csv.DictReader(open(filename)) x_values, y_values = utils.read_xy_values( reader, 'X', ['Y', 'Z']) self.assertEqual(x_values, [0.0, 1.0, 2.0, 3.0, 4.0]) self.assertEqual(y_values, { 'Y': [2.0, 4.0, 6.0, 8.0, 10.0], 'Z': [3.0, 6.0, 9.0, 12.0, 15.0], }) # Remove the temporary file os.unlink(filename)
def generate(self): """Generate the graph. """ print("Reading '%s'" % self.csv_file) reader = csv.DictReader(open(self.csv_file, 'r')) # Attempt to match column names x_column, y_columns = utils.matching_xy_fields( self['x'], self['y'], reader.fieldnames) # Do we need to guess what format the date is in? if self['dateformat'] == 'guess': self['dateformat'] = self.guess_date_format(x_column) # Read each row in the .csv file and populate x and y value lists x_values, y_values = utils.read_xy_values( reader, x_column, y_columns, self['dateformat'], self['gmtoffset'], self['zerotime']) # Create the figure and plot self.figure = pylab.figure() self.axes = self.figure.add_subplot(111) self.axes.grid(True) # Label X-axis with provided label, or column name self.axes.set_xlabel(self['xlabel'] or x_column) # Add graph title if provided if self['title']: self.figtitle = self.figure.suptitle(self['title'], fontsize=18) # Do date formatting of axis labels if the X column is a date field if self['dateformat']: self.add_date_labels(min(x_values), max(x_values)) self.figure.autofmt_xdate() # Get the top n by average? if self['top']: y_columns = utils.top_by_average(self['top'], y_columns, y_values, self['drop']) print("********** Top %d columns by average:" % self['top']) print('\n'.join(y_columns)) # Get the top n by peak? elif self['peak']: y_columns = utils.top_by_peak(self['peak'], y_columns, y_values, self['drop']) print("********** Top %d columns by peak:" % self['peak']) print('\n'.join(y_columns)) # Plot lines for all Y columns lines = [] for y_col in y_columns: line = self.axes.plot(x_values, y_values[y_col], self['linestyle']) lines.append(line) # Set Y-limit if provided if self['ymax'] > 0: print("Setting ymax to %s" % self['ymax']) self.axes.set_ylim(0, self['ymax']) # Draw a legend for the figure # Use prefix-based Y axis label? if self['ylabel'] == 'prefix': prefix, labels = utils.strip_prefix(y_columns) self.axes.set_ylabel(prefix) # Use given label (possibly no label) else: labels = [col for col in y_columns] self.axes.set_ylabel(self['ylabel']) # Truncate labels if desired if self['truncate'] > 0: labels = [label[0:self['truncate']] for label in labels] self.legend = pylab.legend( lines, labels, loc='upper center', bbox_to_anchor=(0.5, -0.15), prop={'size': 9}, ncol=3)