예제 #1
0
파일: test_utils.py 프로젝트: a-e/csvsee
    def test_top_by_avg(self):
        """Test the `top_by_avg` function.
        """
        data = {
            'a': [2, 2, 2, 2, 2],
            'b': [1, 2, 2, 2, 2],
            'c': [1, 1, 2, 2, 2],
            'd': [1, 1, 1, 2, 2],
            'e': [1, 1, 1, 1, 2],
        }
        top3 = utils.top_by_average(3, data.keys(), data)
        self.assertEqual(top3, ['a', 'b', 'c'])

        bottom3 = utils.top_by_average(3, data.keys(), data, drop=2)
        self.assertEqual(bottom3, ['c', 'd', 'e'])
예제 #2
0
파일: graph.py 프로젝트: a-e/csvsee
    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)