Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions fdms/computation/country/annual/external_sector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import pandas as pd

from fdms.utils.mixins import SumAndSpliceMixin
from fdms.utils.splicer import Splicer
from fdms.utils.operators import Operators
from fdms.utils.series import export_to_excel


# STEP 16
class ExternalSector(SumAndSpliceMixin):
def perform_computation(self, result_1, result_3, ameco_h_df):
# TODO: Check the scales of the output variables
splicer = Splicer()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines are not required?

operators = Operators()

# UBYA.1.0.0.0

addends = {'UBYA.1.0.0.0': ['UBRA.1.0.0.0', 'UBTA.1.0.0.0']}
self._sum_and_splice(addends, result_1, ameco_h_df, splice=False)
new_input_df = self.result.set_index(['Country Ameco', 'Variable Code'], drop=True)
new_input_df = pd.concat([new_input_df, result_1, result_3], sort=True)


# UBCA.1.0.0.0

addends = {'UBCA.1.0.0.0': ['UXGS.1.0.0.0', '-UMGS.1.0.0.0', 'UBYA.1.0.0.0']}
self._sum_and_splice(addends, new_input_df, ameco_h_df, splice=False)
new_input_df = self.result.set_index(['Country Ameco', 'Variable Code'], drop=True)
new_input_df = pd.concat([new_input_df, result_1, result_3], sort=True)

# UBLA.1.0.0.0

addends = {'UBLA.1.0.0.0': ['UBCA.1.0.0.0', 'UBKA.1.0.0.0']}
self._sum_and_splice(addends, new_input_df, ameco_h_df, splice=False)

# DXGT.1.0.0.0

dxge_data = self.get_data(new_input_df, 'DXGE.1.0.0.0')
dxgi_data = self.get_data(new_input_df, 'DXGI.1.0.0.0')
dxgt_data = dxge_data + dxgi_data
series_meta = self.get_meta('DXGT.1.0.0.0')
series = pd.Series(series_meta)
series = series.append(dxgt_data)
self.result = self.result.append(series, ignore_index=True, sort=True)
new_input_df = self.result.set_index(['Country Ameco', 'Variable Code'], drop=True)
new_input_df = pd.concat([new_input_df, result_1], sort=True)

# DMGT.1.0.0.0

dmge_data = self.get_data(new_input_df, 'DMGE.1.0.0.0')
dmgi_data = self.get_data(new_input_df, 'DMGI.1.0.0.0')
dmgt_data = dmge_data + dmgi_data
series_meta = self.get_meta('DMGT.1.0.0.0')
series = pd.Series(series_meta)
series = series.append(dmgt_data)
self.result = self.result.append(series, ignore_index=True, sort=True)
new_input_df = self.result.set_index(['Country Ameco', 'Variable Code'], drop=True)
new_input_df = pd.concat([new_input_df, result_1], sort=True)

# DBGT.1.0.0.0

dxgt_data = self.get_data(new_input_df, 'DXGT.1.0.0.0')
dmgt_data = self.get_data(new_input_df, 'DMGT.1.0.0.0')
dbgt_data = dxgt_data - dmgt_data
series_meta = self.get_meta('DBGT.1.0.0.0')
series = pd.Series(series_meta)
series = series.append(dbgt_data)
self.result = self.result.append(series, ignore_index=True, sort=True)

# DBGE.1.0.0.0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix this calculation, it doesn't match the original one

dxge_data = self.get_data(new_input_df, 'DXGT.1.0.0.0')
dmge_data = self.get_data(new_input_df, 'DMGT.1.0.0.0')
dbge_data = dxge_data - dmge_data
series_meta = self.get_meta('DBGE.1.0.0.0')
series = pd.Series(series_meta)
series = series.append(dbge_data)
self.result = self.result.append(series, ignore_index=True, sort=True)

# TODO: DBGI.1.0.0.0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finish the module, perform the last calculation.


self.result.set_index(['Country Ameco', 'Variable Code'], drop=True, inplace=True)
self.apply_scale()

export_to_excel(self.result, step=16)


return self.result
19 changes: 19 additions & 0 deletions fdms/computation/country/annual/fiscal_sector.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ def perform_computation(self, df, ameco_h_df):
'UDGGL.1.0.0.0': ['UDGG.1.0.0.0', ]
}
self._sum_and_splice(addends, df, ameco_h_df)
new_input_df = self.result.set_index(['Country Ameco', 'Variable Code'], drop=True)
new_input_df = pd.concat([new_input_df, df], sort=True)

# UUCG.1.0.0.0

addends = {'UUCG.1.0.0.0': ['UWCG.1.0.0.0', 'UYTGH.1.0.0.0','UYIG.1.0.0.0','UYVG.1.0.0.0','UUOG.1.0.0.0','UCTGI.1.0.0.0','UYTGM.1.0.0.0']}
self._sum_and_splice(addends, new_input_df, ameco_h_df)
new_input_df = self.result.set_index(['Country Ameco', 'Variable Code'], drop=True)
new_input_df = pd.concat([new_input_df, df], sort=True)

# USGG.1.0.0.0

addends = {'USGG.1.0.0.0': ['-URCG.1.0.0.0', '-UUCG.1.0.0.0']}
self._sum_and_splice(addends, new_input_df, ameco_h_df)

# UDGG.1.0.0.0

variable = 'UDGG.1.0.0.0'
series_meta = self.get_meta(variable)
Expand All @@ -84,5 +100,8 @@ def perform_computation(self, df, ameco_h_df):

self.result.set_index(['Country Ameco', 'Variable Code'], drop=True, inplace=True)
self.apply_scale()

export_to_excel(self.result, 'output/outputvars12.txt', 'output/output12.xlsx')

return self.result

44 changes: 44 additions & 0 deletions fdms/computation/country/annual/private_sector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pandas as pd

from fdms.utils.mixins import SumAndSpliceMixin
from fdms.utils.splicer import Splicer
from fdms.utils.operators import Operators
from fdms.utils.series import export_to_excel


# STEP 15
class PrivateSector(SumAndSpliceMixin):
def perform_computation(self, result_1, result_12, result_13, result_14, ameco_h_df):
breakpoint()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to delete all breakpoints

# TODO: Check the scales of the output variables
splicer = Splicer()
operators = Operators()

# USGN.1.0.0.0

addends = {'USGN.1.0.0.0': ['UVGD', 'UBRA', 'UBTA', '-UCPH', '-UCTG']}

self._sum_and_splice(addends, result_1, ameco_h_df, splice=False)
new_input_df = self.result.set_index(['Country Ameco', 'Variable Code'], drop=True)
new_input_df = pd.concat([new_input_df, result_1, result_13, result_14], sort=True)

# UBLP.1.0.0.0

addends = {'UBLP.1.0.0.0': ['UBLC.1.0.0.0', 'UBLH.1.0.0.0']}

self._sum_and_splice(addends, new_input_df, ameco_h_df, splice=False)
new_input_df = self.result.set_index(['Country Ameco', 'Variable Code'], drop=True)
new_input_df = pd.concat([new_input_df, result_1, result_12], sort=True)

# USGP.1.0.0.0

addends = {'USGP.1.0.0.0': ['USGN.1.0.0.0','USGG.1.0.0.0']}

self._sum_and_splice(addends, new_input_df, ameco_h_df, splice=False)

self.result.set_index(['Country Ameco', 'Variable Code'], drop=True, inplace=True)
self.apply_scale()

export_to_excel(self.result, step=15)

return self.result
16 changes: 16 additions & 0 deletions fdms/tests/test_country_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
from fdms.computation.country.annual.fiscal_sector import FiscalSector
from fdms.computation.country.annual.corporate_sector import CorporateSector
from fdms.computation.country.annual.household_sector import HouseholdSector
from fdms.computation.country.annual.private_sector import PrivateSector
from fdms.computation.country.annual.external_sector import ExternalSector

from fdms.config import YEARS
from fdms.config.scale_correction import fix_scales
from fdms.config.variable_groups import NA_VO, T_VO
Expand Down Expand Up @@ -201,6 +204,17 @@ def test_country_calculation_BE(self):
missing_vars = [v for v in variables if v not in list(result_14.loc[self.country].index)]
self.assertFalse(missing_vars)

# STEP 15
step_15 = PrivateSector(scales=self.scales)
result_15 = step_15.perform_computation(self.result_1, result_12, result_13, result_14, self.ameco_df)

# STEP 16
step_16 = ExternalSector(scales=self.scales)
result_16 = step_16.perform_computation(self.result_1, result_3, self.ameco_df)

breakpoint()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another breakpoint



# TODO: Fix all scales
result = pd.concat([self.result_1, result_2, result_3, result_4, result_5, result_6, result_7, result_8,
result_9, result_10, result_11, result_12, result_13, result_14], sort=True)
Expand All @@ -227,3 +241,5 @@ def test_country_calculation_BE(self):
wrong_names = [series.name for series in wrong_series]
res_wrong, exp_wrong = res.loc[wrong_names].copy(), exp.loc[wrong_names].copy()
report_diff(res_wrong, exp_wrong)

#breakpoint()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this comment?

Loading