Exemple #1
0
def graph_table(filename, title, vector,
                base_year, is_intensities, collapse=[]):

    plot = GNUPlot(filename, title, "usa")

    data = {}
    sortby = {}
    other_category = "Other"
    max_year = max(config.STUDY_YEARS)

    other_category_numer = dict((year, 0) for year in config.STUDY_YEARS)
    other_category_denom = dict((year, 0) for year in config.STUDY_YEARS)

    for group in vector.keys():
        numerator = vector[group]

        if is_intensities:
            denominator = pce_dollars[group]
        else:
            denominator = dict((year, 1) for year in config.STUDY_YEARS)

        base_value = numerator[base_year] / denominator[base_year]

        if group in collapse:
            for year in config.STUDY_YEARS:
                other_category_numer[year] += numerator[year]

                # adding 1 for each year is fine since it's relative to the base
                other_category_denom[year] += denominator[year]

        else:
            data[group] = {}

            for year in config.STUDY_YEARS:
                data[group][year] = numerator[year] / denominator[year]

            sortby[ data[group][max_year] / data[group][base_year] ] = group

    if len(other_category_numer):
        data[other_category] = dict(
            (year, other_category_numer[year] / other_category_denom[year]) \
                for year in config.STUDY_YEARS)
        sortby[ data[other_category][max_year] /\
                    data[other_category][base_year] ] = other_category

    sorted_groups = [sortby[key] for key in sorted(sortby.keys(), reverse=True)]

    plot.add_custom_setup("set style data linespoints")
    plot.add_custom_setup("unset colorbox")
    plot.add_custom_setup("set grid")

    min_x = 5 * math.floor(min(config.STUDY_YEARS) / 5)
    max_x = 5 * math.ceil(max(config.STUDY_YEARS) / 5) + 5 # room for labels
    plot.add_custom_setup("set xrange [ %d : %d ]" % (min_x, max_x))

    interval = 1 / (len(sorted_groups) - 1)
    for i in range(len(sorted_groups)):
        plot.add_custom_setup("set style lines %d lc palette frac %.2f lw 2"
                              % (i + 1,  i * interval))
    series_values = []

    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        if group in bea.short_nipa:
            group_name = bea.short_nipa[group]
        else:
            group_name = group

        series_values.append(group_name)
        plot.suppress_title(group_name)

        base_value = data[group][base_year]
        for year in config.STUDY_YEARS:
            print(group, year, data[group][year], base_value, data[group][year] / base_value)
            plot.set_value(group_name, year,
                           data[group][year] / base_value * 100)

        plot.set_series_style(group_name, "linestyle %d" % (i + 1))

    plot.series_values = series_values
    plot.write_tables()
    plot.get_axis_specs() # make sure max_y is adjusted

    prev_pos = None
    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        if group in bea.short_nipa:
            group_name = bea.short_nipa[group]
        else:
            group_name = group

        ending_value = data[group][max_year] / data[group][base_year] * 100
        position = ending_value / plot.max_y + 0.01 # line up baseline

        # space labels out by at least 0.04 so they don't overlap
        if prev_pos is not None and prev_pos - position < 0.03:
            position = prev_pos - 0.03

        plot.add_custom_setup(
            "set label %d '%s' at graph 0.81, %.2f font 'Arial,8'"
            % (i + 1, group_name, position))

        prev_pos = position

    plot.generate_plot()
Exemple #2
0
        btu = row[1]  # billion Btu
        elec_plot.set_value(elec_sources[source], year, btu / 1000)  # tBtu
        elec_iplot.set_value(elec_sources[source], year, btu / gdp_value)

    stmt = db.prepare(
        "select source, use_btu " + \
        "  from %s.seds_us_%d " % (config.EIA_SCHEMA, year) +
        " where (source = 'LO' and sector = 'TC') " +
        "    or (source = 'TE' and sector = 'EI')")
    result = stmt()
    for row in result:
        if row[0] == "LO":
            losses = row[1]
        else:
            total = row[1]

    print(year, losses / total)

    # not now, the values are too flat over time to see anything
    #elec_iplot.set_overlay_value(year, 1 - losses / total)

ff_plot.write_tables()
ff_plot.generate_plot()
elec_plot.write_tables()
elec_plot.generate_plot()

ff_iplot.write_tables()
ff_iplot.generate_plot()
elec_iplot.write_tables()
elec_iplot.generate_plot()
Exemple #3
0
        btu = row[1] # billion Btu
        elec_plot.set_value(elec_sources[source], year, btu / 1000) # tBtu
        elec_iplot.set_value(elec_sources[source], year, btu / gdp_value)

    stmt = db.prepare(
        "select source, use_btu " + \
        "  from %s.seds_us_%d " % (config.EIA_SCHEMA, year) +
        " where (source = 'LO' and sector = 'TC') " + 
        "    or (source = 'TE' and sector = 'EI')")
    result = stmt()
    for row in result:
        if row[0] == "LO":
            losses = row[1]
        else:
            total = row[1]

    print(year, losses / total)

    # not now, the values are too flat over time to see anything
    #elec_iplot.set_overlay_value(year, 1 - losses / total)

ff_plot.write_tables()
ff_plot.generate_plot()
elec_plot.write_tables()
elec_plot.generate_plot()

ff_iplot.write_tables()
ff_iplot.generate_plot()
elec_iplot.write_tables()
elec_iplot.generate_plot()
Exemple #4
0
def graph_table(filename, title, vector, base_year, intensity_divisor,
                sector_groups):

    plot = GNUPlot(filename, title, "usa")

    data = {}
    sortby = {}

    numerators = {}
    denominators = {}

    for key in sector_groups.keys():
        data[key] = {}
        denominators[key] = dict((year, 0) for year in config.STUDY_YEARS)
        numerators[key] = dict((year, 0) for year in config.STUDY_YEARS)

    max_year = max(config.STUDY_YEARS)
    for sector in vector.keys():
        print(sector)
        numerator = vector[sector]

        for (key, sectors) in sector_groups.items():
            if sector in sectors:
                for year in config.STUDY_YEARS:
                    if intensity_divisor is not None:
                        denominators[key][year] += \
                            intensity_divisor[sector][year]
                    else:
                        denominators[key][year] += 1

                    numerators[key][year] += numerator[year]
                break

    for key in sector_groups.keys():
        print(key, denominators[key])
        data[key] = dict(
            (year, numerators[key][year] / denominators[key][year])
            for year in config.STUDY_YEARS)
        sortby[data[key][max_year] / data[key][base_year]] = key

    sorted_groups = [
        sortby[key] for key in sorted(sortby.keys(), reverse=True)
    ]

    plot.add_custom_setup("set style data linespoints")
    plot.add_custom_setup("unset colorbox")
    plot.add_custom_setup("set grid")

    min_x = 5 * math.floor(min(config.STUDY_YEARS) / 5)
    max_x = 5 * math.ceil(max(config.STUDY_YEARS) / 5) + 5  # room for labels
    plot.add_custom_setup("set xrange [ %d : %d ]" % (min_x, max_x))

    interval = 1 / (len(sorted_groups) - 1)
    for i in range(len(sorted_groups)):
        plot.add_custom_setup("set style lines %d lc palette frac %.2f lw 2" %
                              (i + 1, i * interval))
    series_values = []

    def name_for_group(group):
        if group in bea.short_nipa:
            group_name = bea.short_nipa[group]
        else:
            group_name = group
        return group_name

    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        group_name = name_for_group(group)

        series_values.append(group_name)
        plot.suppress_title(group_name)
        base_value = data[group][base_year]
        for year in config.STUDY_YEARS:
            plot.set_value(group_name, year,
                           data[group][year] / base_value * 100)

        plot.set_series_style(group_name, "linestyle %d" % (i + 1))

    plot.series_values = series_values
    plot.write_tables()
    plot.get_axis_specs()  # make sure max_y is adjusted

    prev_pos = None
    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        group_name = name_for_group(group)

        ending_value = data[group][max_year] / data[group][base_year] * 100
        position = ending_value / plot.max_y + 0.01  # line up baseline

        # space labels out by at least 0.04 so they don't overlap
        if prev_pos is not None and prev_pos - position < 0.03:
            position = prev_pos - 0.03

        plot.add_custom_setup(
            "set label %d '%s' at graph 0.81, %.2f font 'Arial,8'" %
            (i + 1, group_name, position))

        prev_pos = position

    plot.generate_plot()
Exemple #5
0
def do_plots():
    for (name, measurements) in config.env_series_names.items():
        data = {}
        for year in config.STUDY_YEARS:
            strings = {
                "schema": config.WIOD_SCHEMA,
                "year": year,
                "fd_sectors": sqlhelper.set_repr(config.default_fd_sectors),
                "measurements": sqlhelper.set_repr(measurements),
                "nipa_schema": usa.config.NIPA_SCHEMA,
                }
    
            stmt = db.prepare(
                """SELECT a.country, a.series, b.gdp,
                          a.series / b.gdp as intensity
                     FROM (SELECT country, sum(value) as series
                             FROM %(schema)s.env_%(year)d
                            WHERE industry = 'total'
                              AND measurement in %(measurements)s
                            GROUP BY country) a,
                          (SELECT aa.country, sum(value) * deflator as gdp
                             FROM %(schema)s.indbyind_%(year)d aa,
                                  (SELECT 100 / gdp as deflator
                                     FROM %(nipa_schema)s.implicit_price_deflators
                                    WHERE year = $1) bb
                            WHERE to_ind in %(fd_sectors)s
                            GROUP BY aa.country, deflator) b
                    WHERE a.country = b.country
                      AND a.series is not null
                    ORDER BY a.series / b.gdp""" % strings)
    
            for row in stmt(year):
                country = row[0]
                intensity = row[3]
                if country not in data:
                    data[country] = {}
                data[country][year] = intensity
    
        slopes = {}
        for (country, country_data) in data.items():
            n = len(country_data.keys())
    
            if n < 2:
                continue
    
            sum_y = sum(country_data.values())
            sum_x = sum(country_data.keys())
            slope = (n * sum([k * v for (k, v) in country_data.items()]) \
                     - sum_x * sum_y) / \
                    (n * sum([k * k for k in country_data.keys()]) - sum_x)
    
            slopes[country] = slope * 1000000
    
        years = "%d-%d" % (config.STUDY_YEARS[0], config.STUDY_YEARS[-1])
        i = 0
        binsize = 8
        plot = None
        for (country, slope) in sorted(slopes.items(), key=lambda x: x[1]):
            if i % binsize == 0:
                if plot is not None:
                    plot.write_tables()
                    plot.generate_plot()
    
                tier = i / binsize + 1
                plot = GNUPlot("tier%d" % tier, "",
                               #"%s intensity from %s, tier %d" \
                               #    % (name, years, tier),
                               "wiod-%s" % name.replace(" ", "-"))
    
                plot.legend("width -5")
    
            for year in config.STUDY_YEARS:
                if year in data[country]:
                    plot.set_value(
                        "%s (%.2f)" % (config.countries[country], slope),
                        year,
                        data[country][year])
    
            i += 1
    
        if plot is not None:
            plot.write_tables()
            plot.generate_plot()
Exemple #6
0
def graph_table(filename, title, vector,
                base_year, intensity_divisor, sector_groups):

    plot = GNUPlot(filename, title, "usa")

    data = {}
    sortby = {}

    numerators = {}
    denominators = {}

    for key in sector_groups.keys():
        data[key] = {}
        denominators[key] = dict((year, 0) for year in config.STUDY_YEARS)
        numerators[key] = dict((year, 0) for year in config.STUDY_YEARS)

    max_year = max(config.STUDY_YEARS)
    for sector in vector.keys():
        print(sector)
        numerator = vector[sector]

        for (key, sectors) in sector_groups.items():
            if sector in sectors:
                for year in config.STUDY_YEARS:
                    if intensity_divisor is not None:
                        denominators[key][year] += \
                            intensity_divisor[sector][year]
                    else:
                        denominators[key][year] += 1

                    numerators[key][year] += numerator[year]
                break

    for key in sector_groups.keys():
        print(key, denominators[key])
        data[key] = dict(
            (year, numerators[key][year] / denominators[key][year])
            for year in config.STUDY_YEARS)
        sortby[ data[key][max_year] / data[key][base_year] ] = key

    sorted_groups = [sortby[key] for key in sorted(sortby.keys(), reverse=True)]

    plot.add_custom_setup("set style data linespoints")
    plot.add_custom_setup("unset colorbox")
    plot.add_custom_setup("set grid")

    min_x = 5 * math.floor(min(config.STUDY_YEARS) / 5)
    max_x = 5 * math.ceil(max(config.STUDY_YEARS) / 5) + 5 # room for labels
    plot.add_custom_setup("set xrange [ %d : %d ]" % (min_x, max_x))

    interval = 1 / (len(sorted_groups) - 1)
    for i in range(len(sorted_groups)):
        plot.add_custom_setup("set style lines %d lc palette frac %.2f lw 2"
                              % (i + 1,  i * interval))
    series_values = []

    def name_for_group(group):
        if group in bea.short_nipa:
            group_name = bea.short_nipa[group]
        else:
            group_name = group
        return group_name

    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        group_name = name_for_group(group)

        series_values.append(group_name)
        plot.suppress_title(group_name)
        base_value = data[group][base_year]
        for year in config.STUDY_YEARS:
            plot.set_value(group_name, year,
                           data[group][year] / base_value * 100)

        plot.set_series_style(group_name, "linestyle %d" % (i + 1))

    plot.series_values = series_values
    plot.write_tables()
    plot.get_axis_specs() # make sure max_y is adjusted

    prev_pos = None
    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        group_name = name_for_group(group)

        ending_value = data[group][max_year] / data[group][base_year] * 100
        position = ending_value / plot.max_y + 0.01 # line up baseline

        # space labels out by at least 0.04 so they don't overlap
        if prev_pos is not None and prev_pos - position < 0.03:
            position = prev_pos - 0.03

        plot.add_custom_setup(
            "set label %d '%s' at graph 0.81, %.2f font 'Arial,8'"
            % (i + 1, group_name, position))

        prev_pos = position

    plot.generate_plot()