예제 #1
0
def population_diagnostic(simulation):
    figures_directory = create_or_get_figures_directory(simulation)
    directory = os.path.dirname(simulation.data_source.output_path)
    uniform_weight = simulation.uniform_weight

    population = None
    for csv_file in ['births', 'deaths', 'migrants']:
        simulation_data_frame = pandas.read_csv(os.path.join(directory, csv_file + '.csv'))
        simulation_data_frame.period = (simulation_data_frame['period'] / 100).round().astype(int)
        simulation_data_frame.set_index('period', inplace = True)

        insee_data_frame = pandas.DataFrame({
            '{}_insee'.format(csv_file): get_insee_projection(csv_file, 'total', function = 'sum')
            })
        insee_data_frame.index.name = 'period'

        data_frame = pandas.concat([simulation_data_frame * uniform_weight, insee_data_frame], axis = 1)

        if population is not None:
            population = pandas.concat([population, data_frame], axis = 1)
        else:
            population = data_frame

    population.dropna(inplace = True)
    population.index.name = 'year'
    ax = population.plot(
        title = 'Components of population growth',
        style = ['k-', 'k--', 'g-', 'g--', 'b-', 'b--'],
        xticks = [period for period in list(population.index.astype(int))],
        )
    fig = ax.get_figure()
    plt.gca().xaxis.set_major_formatter(FuncFormatter(lambda x, _: int(x)))
    fig.savefig(os.path.join(figures_directory, 'population_growth_compoennts.png'))
    del ax, fig
    return population
예제 #2
0
def plot_dependance_csv(simulation):
    figures_directory = create_or_get_figures_directory(simulation)

    panel_simulation = extract_dependance_csv(simulation)
    panel_simulation = panel_simulation.squeeze() / 1000
    panel_simulation.index.name = None
    panel_simulation.rename(
        columns = dict(
            male = "hommes",
            female = "femmes",
            ),
        inplace = True
        )
    plt.figure()
    ax = panel_simulation.plot(
        xticks = [period for period in list(panel_simulation.index.astype(int))],
        cmap = 'PuBu'
        )
    ax.set_ylabel(u"effectifs en milliers")
    ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.2), ncol=3)
    plt.gca().xaxis.set_major_formatter(FuncFormatter(lambda x, _: int(x)))
    fig = ax.get_figure()

    fig.savefig(os.path.join(figures_directory, 'dependance.png'))
    del ax, fig
예제 #3
0
def plot_ratio_demographique(simulation):
    figures_directory = create_or_get_figures_directory(simulation)

    panel_simulation = extract_population_by_age_csv(simulation)
    panel_simulation.drop('total', axis = 'major', inplace = True)

    def separate(age):
        age = int(age)
        if age <= 19:
            return 'kids'
        elif age <= 59:
            return 'active'
        elif age > 59:
            return 'retired'

    ratio_60 = panel_simulation['total'].groupby(separate, axis = 0).sum()
    ratio_60 = ratio_60.loc['retired', :]/ratio_60.loc['active', :]
예제 #4
0
def plot_population2(simulation):
    figures_directory = create_or_get_figures_directory(simulation)

    panel_simulation = extract_population_csv(simulation)

    for gender in ['total', 'male', 'female']:
        data_frame_insee = get_data_frame_insee(gender)
        data_frame_simulation = panel_simulation[gender].drop('total')
        data_frame_simulation.index = data_frame_simulation.index.astype(int)
        data_frame_simulation.sort_index(inplace = True)

        absolute_diff = (
            data_frame_simulation - data_frame_insee[data_frame_simulation.columns]
            )

        relative_diff = (
            data_frame_simulation - data_frame_insee[data_frame_simulation.columns]
            ) / data_frame_insee[data_frame_simulation.columns]
        plt.figure()
        ax = relative_diff.T.plot(title = gender)
        fig = ax.get_figure()
        fig.savefig(os.path.join(figures_directory, 'population_rel_diff_{}.png'.format(gender)))
        del ax, fig

    for gender in ['total', 'male', 'female']:
        data_frame_insee_total = get_data_frame_insee(gender).sum()
        data_frame_simulation = panel_simulation[gender].drop('total')
        data_frame_simulation.index = data_frame_simulation.index.astype(int)
        data_frame_simulation.sort_index(inplace = True)
        data_frame_simulation_total = data_frame_simulation.sum()

        data_frame_insee_total = data_frame_insee_total.loc[data_frame_simulation_total.index].copy()

        plt.figure()
        ax = data_frame_insee_total.plot(label = 'insee', style = 'b-')
        data_frame_simulation_total.plot(label = 'til', style = 'r-', ax = ax)
        ax.legend()
        fig = ax.get_figure()
        fig.savefig(os.path.join(figures_directory, 'population_{}.png'.format(gender)))
        del ax, fig