Exemple #1
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()