Skip to content

Commit ba4f04f

Browse files
authored
Format files using DocumentFormat
1 parent 5b77419 commit ba4f04f

8 files changed

+194
-183
lines changed

src/MimiRFFSPs.jl

+4-5
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ function get_model()
3131

3232
set_dimension!(m, :time, 1750:2300)
3333

34-
add_comp!(m, SPs, :rffsp, first = 2020, last = 2300)
34+
add_comp!(m, SPs, :rffsp, first=2020, last=2300)
3535

36-
all_countries = CSVFiles.load(joinpath(@__DIR__, "..", "data", "keys", "MimiRFFSPs_ISO3.csv")) |> DataFrame
36+
all_countries = CSVFiles.load(joinpath(@__DIR__, "..", "data", "keys", "MimiRFFSPs_ISO3.csv")) |> DataFrame
3737

3838
set_dimension!(m, :country, all_countries.ISO3)
3939

@@ -42,10 +42,9 @@ function get_model()
4242
return m
4343
end
4444

45-
function get_mcs(sampling_ids::Union{Vector{Int}, Nothing} = nothing)
45+
function get_mcs(sampling_ids::Union{Vector{Int},Nothing}=nothing)
4646
# define the Monte Carlo Simulation and add some simple random variables
47-
mcs = @defsim begin
48-
end
47+
mcs = @defsim begin end
4948

5049
distrib = isnothing(sampling_ids) ? Mimi.EmpiricalDistribution(collect(1:10_000)) : Mimi.SampleStore(sampling_ids)
5150
Mimi.add_RV!(mcs, :socio_id_rv, distrib)

src/components/RegionAggregatorSum.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@defcomp RegionAggregatorSum begin
2-
2+
33
inputregions = Index()
44
outputregions = Index()
55

@@ -12,17 +12,17 @@
1212
input = Parameter(index=[time, inputregions])
1313
output = Variable(index=[time, outputregions])
1414

15-
function init(p,v,d)
15+
function init(p, v, d)
1616
idxs = indexin(p.input_output_mapping, p.output_region_names)
1717
!isnothing(findfirst(i -> isnothing(i), idxs)) ? error("All provided region names in the RegionAggregatorSum's input_output_mapping Parameter must exist in the output_region_names Parameter.") : nothing
1818
v.input_output_mapping_int[:] = idxs
1919
end
2020

21-
function run_timestep(p,v,d,t)
21+
function run_timestep(p, v, d, t)
2222
v.output[t, :] .= 0.
2323

2424
for i in d.inputregions
25-
v.output[t, v.input_output_mapping_int[i]] += p.input[t,i]
25+
v.output[t, v.input_output_mapping_int[i]] += p.input[t, i]
2626
end
2727
end
2828
end

src/components/SPs.jl

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# (10/25/2021) BEA Table 1.1.9, line 1 GDP annual values as linked here: https://apps.bea.gov/iTable/iTable.cfm?reqid=19&step=3&isuri=1&select_all_years=0&nipa_table_list=13&series=a&first_year=2005&last_year=2020&scale=-99&categories=survey&thetable=
2-
const pricelevel_2011_to_2005 = 87.504/98.164
2+
const pricelevel_2011_to_2005 = 87.504 / 98.164
33

44
function fill_socioeconomics!(source_Year, source_Country, source_Pop, source_GDP, population, gdp, country_lookup, start_year, end_year)
55
for i in 1:length(source_Year)
@@ -60,50 +60,50 @@ end
6060
end_year = Parameter{Int}(default=Int(2300)) # year (annual) data should end
6161
country_names = Parameter{String}(index=[country]) # need the names of the countries from the dimension
6262
id = Parameter{Int64}(default=Int(6546)) # the sample (out of 10,000) to be used
63-
64-
population = Variable(index=[time, country], unit="million")
65-
population_global = Variable(index=[time], unit="million")
66-
deathrate = Variable(index=[time, country], unit="deaths/1000 persons/yr")
67-
gdp = Variable(index=[time, country], unit="billion US\$2005/yr")
68-
gdp_global = Variable(index=[time], unit="billion US\$2005/yr")
69-
70-
population1990 = Variable(index=[country], unit = "million")
71-
gdp1990 = Variable(index=[country], unit = unit="billion US\$2005/yr")
72-
73-
co2_emissions = Variable(index=[time], unit="GtC/yr")
74-
ch4_emissions = Variable(index=[time], unit="MtCH4/yr")
75-
n2o_emissions = Variable(index=[time], unit="MtN2/yr")
76-
77-
function init(p,v,d)
63+
64+
population = Variable(index=[time, country], unit="million")
65+
population_global = Variable(index=[time], unit="million")
66+
deathrate = Variable(index=[time, country], unit="deaths/1000 persons/yr")
67+
gdp = Variable(index=[time, country], unit="billion US\$2005/yr")
68+
gdp_global = Variable(index=[time], unit="billion US\$2005/yr")
69+
70+
population1990 = Variable(index=[country], unit="million")
71+
gdp1990 = Variable(index=[country], unit=unit = "billion US\$2005/yr")
72+
73+
co2_emissions = Variable(index=[time], unit="GtC/yr")
74+
ch4_emissions = Variable(index=[time], unit="MtCH4/yr")
75+
n2o_emissions = Variable(index=[time], unit="MtN2/yr")
76+
77+
function init(p, v, d)
7878

7979
# add countrys to a dictionary where each country key has a value holding it's
8080
# index in country_names
81-
country_lookup = Dict{String,Int}(name=>i for (i,name) in enumerate(p.country_names))
81+
country_lookup = Dict{String,Int}(name => i for (i, name) in enumerate(p.country_names))
8282
country_indices = d.country::Vector{Int} # helper for type stable country indices
8383

8484
# ----------------------------------------------------------------------
8585
# Socioeconomic Data
8686
# population in millions of individuals
8787
# GDP in billions of $2005 USD
88-
88+
8989
# Load Feather File
9090
t = Arrow.Table(joinpath(datadep"rffsps_v5", "pop_income", "rffsp_pop_income_run_$(p.id).feather"))
9191
fill_socioeconomics!(t.Year, t.Country, t.Pop, t.GDP, v.population, v.gdp, country_lookup, p.start_year, p.end_year)
9292

9393
for year in p.start_year:5:p.end_year-5, country in country_indices
9494
year_as_timestep = TimestepIndex(year - p.start_year + 1)
95-
pop_interpolator = LinearInterpolation(Float64[year, year+5], [log(v.population[year_as_timestep,country]), log(v.population[year_as_timestep+5,country])])
96-
gdp_interpolator = LinearInterpolation(Float64[year, year+5], [log(v.gdp[year_as_timestep,country]), log(v.gdp[year_as_timestep+5,country])])
95+
pop_interpolator = LinearInterpolation(Float64[year, year+5], [log(v.population[year_as_timestep, country]), log(v.population[year_as_timestep+5, country])])
96+
gdp_interpolator = LinearInterpolation(Float64[year, year+5], [log(v.gdp[year_as_timestep, country]), log(v.gdp[year_as_timestep+5, country])])
9797
for year2 in year+1:year+4
9898
year2_as_timestep = TimestepIndex(year2 - p.start_year + 1)
99-
v.population[year2_as_timestep,country] = exp(pop_interpolator[year2])
100-
v.gdp[year2_as_timestep,country] = exp(gdp_interpolator[year2])
99+
v.population[year2_as_timestep, country] = exp(pop_interpolator[year2])
100+
v.gdp[year2_as_timestep, country] = exp(gdp_interpolator[year2])
101101
end
102102
end
103-
103+
104104
# add global data for future accessibility and quality control
105-
v.gdp_global[:,:] = sum(v.gdp[:,:], dims = 2) # sum across countries, which are the second dimension
106-
v.population_global[:,:] = sum(v.population[:,:], dims = 2) # sum across countries, which are the second dimension
105+
v.gdp_global[:, :] = sum(v.gdp[:, :], dims=2) # sum across countries, which are the second dimension
106+
v.population_global[:, :] = sum(v.population[:, :], dims=2) # sum across countries, which are the second dimension
107107

108108
# ----------------------------------------------------------------------
109109
# Death Rate Data
@@ -115,7 +115,7 @@ end
115115
g_datasets[:pop_trajectory_key] = (load(joinpath(datadep"rffsps_v5", "sample_numbers", "sampled_pop_trajectory_numbers.csv")) |> DataFrame).x
116116
end
117117
deathrate_trajectory_id = convert(Int64, g_datasets[:pop_trajectory_key][p.id])
118-
118+
119119
# Load Feather File
120120
t = Arrow.Table(joinpath(datadep"rffsps_v5", "death_rates", "rffsp_death_rates_run_$(deathrate_trajectory_id).feather"))
121121
fill_deathrates!(t.Year, t.ISO3, t.DeathRate, v.deathrate, country_lookup, p.start_year, p.end_year)
@@ -126,7 +126,7 @@ end
126126
# carbon dioxide emissions in GtC
127127
# nitrous oxide emissions in MtN2
128128
# methane emissions in MtCH4
129-
129+
130130
# add data to the global dataset if it's not there
131131
if !haskey(g_datasets, :ch4)
132132
g_datasets[:ch4] = load(joinpath(datadep"rffsps_v5", "emissions", "rffsp_ch4_emissions.csv")) |> DataFrame
@@ -147,13 +147,13 @@ end
147147
# Population and GDP 1990 Values
148148

149149
if !haskey(g_datasets, :ypc1990)
150-
g_datasets[:ypc1990] = load(joinpath(datadep"rffsps_v5", "ypc1990", "rffsp_ypc1990.csv")) |>
151-
DataFrame |>
152-
i -> insertcols!(i, :sample => 1:10_000) |>
153-
i -> stack(i, Not(:sample)) |>
154-
DataFrame |>
155-
i -> rename!(i, [:sample, :country, :value]) |>
156-
DataFrame
150+
g_datasets[:ypc1990] = load(joinpath(datadep"rffsps_v5", "ypc1990", "rffsp_ypc1990.csv")) |>
151+
DataFrame |>
152+
i -> insertcols!(i, :sample => 1:10_000) |>
153+
i -> stack(i, Not(:sample)) |>
154+
DataFrame |>
155+
i -> rename!(i, [:sample, :country, :value]) |>
156+
DataFrame
157157
end
158158
if !haskey(g_datasets, :pop1990)
159159
g_datasets[:pop1990] = load(joinpath(@__DIR__, "..", "..", "data/population1990.csv")) |> DataFrame
@@ -164,7 +164,7 @@ end
164164

165165
end
166166

167-
function run_timestep(p,v,d,t)
167+
function run_timestep(p, v, d, t)
168168

169169
if !(gettime(t) in p.start_year:p.end_year)
170170
error("Cannot run SP component in year $(gettime(t)), SP data is not available for this model and year.")

test/runtests.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ end
1515

1616
@testset "API" begin
1717
include("test_API.jl")
18-
end
18+
end

test/test_API.jl

+66-66
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ update_param!(m, :rffsp, :id, id)
1919
run(m)
2020

2121
# check emissions
22-
ch4 = load(joinpath(datadep"rffsps_v5", "emissions", "rffsp_ch4_emissions.csv")) |>
23-
DataFrame |> @filter(_.year in collect(2020:2300)) |> @filter(_.sample == id) |> DataFrame
24-
n2o = load(joinpath(datadep"rffsps_v5", "emissions", "rffsp_n2o_emissions.csv")) |>
25-
DataFrame |> @filter(_.year in collect(2020:2300)) |> @filter(_.sample == id) |> DataFrame
26-
co2 = load(joinpath(datadep"rffsps_v5", "emissions", "rffsp_co2_emissions.csv")) |>
27-
DataFrame |> @filter(_.year in collect(2020:2300)) |> @filter(_.sample == id) |> DataFrame
22+
ch4 = load(joinpath(datadep"rffsps_v5", "emissions", "rffsp_ch4_emissions.csv")) |>
23+
DataFrame |> @filter(_.year in collect(2020:2300)) |> @filter(_.sample == id) |> DataFrame
24+
n2o = load(joinpath(datadep"rffsps_v5", "emissions", "rffsp_n2o_emissions.csv")) |>
25+
DataFrame |> @filter(_.year in collect(2020:2300)) |> @filter(_.sample == id) |> DataFrame
26+
co2 = load(joinpath(datadep"rffsps_v5", "emissions", "rffsp_co2_emissions.csv")) |>
27+
DataFrame |> @filter(_.year in collect(2020:2300)) |> @filter(_.sample == id) |> DataFrame
2828

2929
@test m[:rffsp, :co2_emissions][findfirst(i -> i == 2020, collect(1750:2300)):end] co2.value atol = tolerance
3030
@test m[:rffsp, :ch4_emissions][findfirst(i -> i == 2020, collect(1750:2300)):end] ch4.value atol = tolerance
@@ -33,101 +33,101 @@ co2 = load(joinpath(datadep"rffsps_v5", "emissions", "rffsp_co2_emissions.csv"))
3333
# check socioeconomics
3434

3535
t = Arrow.Table(joinpath(datadep"rffsps_v5", "pop_income", "rffsp_pop_income_run_$id.feather"))
36-
socio_df = DataFrame( :Year => copy(t.Year),
37-
:Country => copy(t.Country),
38-
:Pop => copy(t.Pop),
39-
:GDP => copy(t.GDP)
40-
) |>
41-
@filter(_.Year in collect(2020:5:2300)) |>
42-
DataFrame
36+
socio_df = DataFrame(:Year => copy(t.Year),
37+
:Country => copy(t.Country),
38+
:Pop => copy(t.Pop),
39+
:GDP => copy(t.GDP)
40+
) |>
41+
@filter(_.Year in collect(2020:5:2300)) |>
42+
DataFrame
4343

4444
for country in all_countries.ISO3
4545

4646
pop_data_model = getdataframe(m, :rffsp, :population) |>
47-
@filter(_.time in collect(2020:5:2300) && _.country == country) |>
48-
DataFrame |>
49-
@orderby(:time) |>
50-
DataFrame
47+
@filter(_.time in collect(2020:5:2300) && _.country == country) |>
48+
DataFrame |>
49+
@orderby(:time) |>
50+
DataFrame
5151

5252
gdp_data_model = getdataframe(m, :rffsp, :gdp) |>
53-
@filter(_.time in collect(2020:5:2300) && _.country == country) |>
54-
DataFrame |>
55-
@orderby(:time) |>
56-
DataFrame
53+
@filter(_.time in collect(2020:5:2300) && _.country == country) |>
54+
DataFrame |>
55+
@orderby(:time) |>
56+
DataFrame
5757

5858
socio_df_country = socio_df |>
59-
@filter(_.Year in collect(2020:5:2300) && _.Country == country) |>
60-
DataFrame |>
61-
@orderby(:Year) |>
62-
DataFrame
59+
@filter(_.Year in collect(2020:5:2300) && _.Country == country) |>
60+
DataFrame |>
61+
@orderby(:Year) |>
62+
DataFrame
6363

64-
@test pop_data_model.population socio_df_country.Pop ./ 1e3 atol = tolerance
65-
@test gdp_data_model.gdp socio_df_country.GDP ./ 1e3 .* MimiRFFSPs.pricelevel_2011_to_2005 atol = tolerance
64+
@test pop_data_model.population socio_df_country.Pop ./ 1e3 atol = tolerance
65+
@test gdp_data_model.gdp socio_df_country.GDP ./ 1e3 .* MimiRFFSPs.pricelevel_2011_to_2005 atol = tolerance
6666
end
6767

6868
socio_gdf = groupby(socio_df, :Year)
6969

7070
model_population_global = getdataframe(m, :rffsp, :population_global) |> @filter(_.time in collect(2020:5:2300)) |> @orderby(:time) |> DataFrame
71-
model_gdp_global = getdataframe(m, :rffsp, :gdp_global) |> @filter(_.time in collect(2020:5:2300)) |> @orderby(:time)|> DataFrame
71+
model_gdp_global = getdataframe(m, :rffsp, :gdp_global) |> @filter(_.time in collect(2020:5:2300)) |> @orderby(:time) |> DataFrame
7272

73-
@test model_population_global.population_global (combine(socio_gdf, :Pop => sum).Pop_sum ./ 1e3) atol = tolerance
74-
@test model_gdp_global.gdp_global (combine(socio_gdf, :GDP => sum).GDP_sum ./ 1e3 .* MimiRFFSPs.pricelevel_2011_to_2005) atol = 1e-7 # slightly higher tolerance
73+
@test model_population_global.population_global (combine(socio_gdf, :Pop => sum).Pop_sum ./ 1e3) atol = tolerance
74+
@test model_gdp_global.gdp_global (combine(socio_gdf, :GDP => sum).GDP_sum ./ 1e3 .* MimiRFFSPs.pricelevel_2011_to_2005) atol = 1e-7 # slightly higher tolerance
7575

7676
# check death rate
7777
pop_trajectory_key = (load(joinpath(datadep"rffsps_v5", "sample_numbers", "sampled_pop_trajectory_numbers.csv")) |> DataFrame).x
7878
deathrate_trajectory_id = convert(Int64, pop_trajectory_key[id])
79-
79+
8080
# Load Feather File
8181
original_years = collect(2023:5:2300)
8282
t = Arrow.Table(joinpath(datadep"rffsps_v5", "death_rates", "rffsp_death_rates_run_$(deathrate_trajectory_id).feather"))
83-
deathrate_df = DataFrame(:Year => copy(t.Year),
84-
:Country => copy(t.ISO3),
85-
:DeathRate => copy(t.DeathRate)
86-
) |>
87-
@filter(_.Year in original_years) |>
88-
DataFrame
83+
deathrate_df = DataFrame(:Year => copy(t.Year),
84+
:Country => copy(t.ISO3),
85+
:DeathRate => copy(t.DeathRate)
86+
) |>
87+
@filter(_.Year in original_years) |>
88+
DataFrame
8989

9090
for country in all_countries.ISO3
9191

9292
deathrate_data_model = getdataframe(m, :rffsp, :deathrate) |>
93-
@filter(_.time in original_years && _.country == country) |>
94-
DataFrame |>
95-
@orderby(:time) |>
96-
DataFrame
97-
98-
deathrate_df_country = deathrate_df |>
99-
@filter(_.Year in original_years && _.Country == country) |>
100-
DataFrame |>
101-
@orderby(:Year) |>
102-
DataFrame
103-
104-
@test deathrate_data_model.deathrate deathrate_df_country.DeathRate atol = tolerance
93+
@filter(_.time in original_years && _.country == country) |>
94+
DataFrame |>
95+
@orderby(:time) |>
96+
DataFrame
97+
98+
deathrate_df_country = deathrate_df |>
99+
@filter(_.Year in original_years && _.Country == country) |>
100+
DataFrame |>
101+
@orderby(:Year) |>
102+
DataFrame
103+
104+
@test deathrate_data_model.deathrate deathrate_df_country.DeathRate atol = tolerance
105105
end
106106

107107
# check pop 1990
108108

109-
population1990 = load(joinpath(@__DIR__, "..", "data", "population1990.csv")) |>
110-
DataFrame |>
111-
@orderby(_.ISO3) |>
112-
DataFrame
109+
population1990 = load(joinpath(@__DIR__, "..", "data", "population1990.csv")) |>
110+
DataFrame |>
111+
@orderby(_.ISO3) |>
112+
DataFrame
113113

114114
@test m[:rffsp, :population1990] population1990.Population atol = tolerance
115115

116116
# check gdp 1990
117117

118-
ypc1990 = load(joinpath(datadep"rffsps_v5", "ypc1990", "rffsp_ypc1990.csv")) |>
119-
DataFrame |>
120-
i -> insertcols!(i, :sample => 1:10_000) |>
121-
i -> stack(i, Not(:sample)) |>
122-
DataFrame |>
123-
@filter(_.sample == id) |>
124-
DataFrame |>
125-
@orderby(_.variable) |>
126-
DataFrame
127-
118+
ypc1990 = load(joinpath(datadep"rffsps_v5", "ypc1990", "rffsp_ypc1990.csv")) |>
119+
DataFrame |>
120+
i -> insertcols!(i, :sample => 1:10_000) |>
121+
i -> stack(i, Not(:sample)) |>
122+
DataFrame |>
123+
@filter(_.sample == id) |>
124+
DataFrame |>
125+
@orderby(_.variable) |>
126+
DataFrame
127+
128128
gdp1990_model = getdataframe(m, :rffsp, :gdp1990) # billions
129129
pop1990_model = getdataframe(m, :rffsp, :population1990) # millions
130-
ypc1990_model = gdp1990_model.gdp1990 ./ pop1990_model.population1990 .* 1e3 # per capita
130+
ypc1990_model = gdp1990_model.gdp1990 ./ pop1990_model.population1990 .* 1e3 # per capita
131131

132132
@test ypc1990_model ypc1990.value .* MimiRFFSPs.pricelevel_2011_to_2005 atol = tolerance
133133

@@ -153,9 +153,9 @@ for id in ids
153153
end
154154

155155
# Alternatively run the Monte Carlo Simulation on model `m` for sample ids 1,2, and 3
156-
mcs = MimiRFFSPs.get_mcs([1,2,3])
156+
mcs = MimiRFFSPs.get_mcs([1, 2, 3])
157157
Mimi.add_save!(mcs, (:rffsp, :id))
158158
Mimi.add_save!(mcs, (:rffsp, :co2_emissions))
159159
results = run(mcs, m, 3)
160160

161-
@test getdataframe(results, :rffsp, :id).id == [1,2,3]
161+
@test getdataframe(results, :rffsp, :id).id == [1, 2, 3]

0 commit comments

Comments
 (0)