From fe8c4452ba43935f219927948d9da4548bba35a5 Mon Sep 17 00:00:00 2001 From: lrennels Date: Wed, 16 Nov 2022 23:36:41 +0000 Subject: [PATCH] Format files using DocumentFormat --- src/components/RegionAggregatorSum.jl | 10 +- src/components/SSPs.jl | 72 ++++++------ test/test_Coupled.jl | 6 +- test/test_RegionAggregatorSum.jl | 12 +- test/test_SSPs.jl | 158 +++++++++++++++----------- 5 files changed, 147 insertions(+), 111 deletions(-) diff --git a/src/components/RegionAggregatorSum.jl b/src/components/RegionAggregatorSum.jl index 131e3a8..2251041 100644 --- a/src/components/RegionAggregatorSum.jl +++ b/src/components/RegionAggregatorSum.jl @@ -1,7 +1,7 @@ using Mimi @defcomp RegionAggregatorSum begin - + inputregions = Index() outputregions = Index() @@ -14,17 +14,17 @@ using Mimi input = Parameter(index=[time, inputregions]) output = Variable(index=[time, outputregions]) - function init(p,v,d) + function init(p, v, d) idxs = indexin(p.input_output_mapping, p.output_region_names) !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 v.input_output_mapping_int[:] = idxs end - function run_timestep(p,v,d,t) - v.output[t, :] .= 0. + function run_timestep(p, v, d, t) + v.output[t, :] .= 0.0 for i in d.inputregions - v.output[t, v.input_output_mapping_int[i]] += p.input[t,i] + v.output[t, v.input_output_mapping_int[i]] += p.input[t, i] end end end diff --git a/src/components/SSPs.jl b/src/components/SSPs.jl index c9d98bf..8064b4f 100644 --- a/src/components/SSPs.jl +++ b/src/components/SSPs.jl @@ -5,37 +5,37 @@ using Mimi, CSVFiles, DataFrames, Query, Interpolations country = Index() - SSP_source = Parameter{String}() # can be one of IIASA GDP, OECD Env-Growth, PIK GDP_32, and Benveniste - SSP = Parameter{String}() # can be one of SSP1, SSP2, SSP3, SSP4, SSP5 - emissions_source = Parameter{String}() # can be one of Leach - emissions_scenario = Parameter{String}() # can be one of SSP119, SSP126, SSP245, SSP370, SSP585 + SSP_source = Parameter{String}() # can be one of IIASA GDP, OECD Env-Growth, PIK GDP_32, and Benveniste + SSP = Parameter{String}() # can be one of SSP1, SSP2, SSP3, SSP4, SSP5 + emissions_source = Parameter{String}() # can be one of Leach + emissions_scenario = Parameter{String}() # can be one of SSP119, SSP126, SSP245, SSP370, SSP585 - country_names = Parameter{String}(index=[country]) # need the names of the countries from the dimension + country_names = Parameter{String}(index=[country]) # need the names of the countries from the dimension - population = Variable(index=[time, country], unit="million") - population_global = Variable(index=[time], unit="million") - gdp = Variable(index=[time, country], unit="billion US\$2005/yr") - gdp_global = Variable(index=[time], unit="billion US\$2005/yr") + population = Variable(index=[time, country], unit="million") + population_global = Variable(index=[time], unit="million") + gdp = Variable(index=[time, country], unit="billion US\$2005/yr") + gdp_global = Variable(index=[time], unit="billion US\$2005/yr") - co2_emissions = Variable(index=[time], unit="GtC/yr") - ch4_emissions = Variable(index=[time], unit="MtCH4/yr") - n2o_emissions = Variable(index=[time], unit="MtN/yr") - sf6_emissions = Variable(index=[time], unit="MtSF6/yr") + co2_emissions = Variable(index=[time], unit="GtC/yr") + ch4_emissions = Variable(index=[time], unit="MtCH4/yr") + n2o_emissions = Variable(index=[time], unit="MtN/yr") + sf6_emissions = Variable(index=[time], unit="MtSF6/yr") - function init(p,v,d) + function init(p, v, d) # ---------------------------------------------------------------------- # Checks ssp_model_options = ["IIASA GDP", "OECD Env-Growth", "PIK GDP_23", "Benveniste"] !(p.SSP_source in ssp_model_options) && error("Model $(p.SSP_source) provided to SSPs component SSP_source parameter not found in available list: $(ssp_model_options)") - + ssp_options = ["SSP1", "SSP2", "SSP3", "SSP4", "SSP5"] !(p.SSP in ssp_options) && error("SSP $(p.SSP) provided to SSPs component SSP parameter not found in available list: $(ssp_options)") - + emissions_source_options = ["Leach"] !(p.emissions_source in emissions_source_options) && error("Model $(p.emissions_source) provided to SSPs component emissions_source parameter not found in available list: $(emissions_source_options)") - + emissions_scenario_options = ["SSP119", "SSP126", "SSP245", "SSP370", "SSP585"] !(p.emissions_scenario in emissions_scenario_options) && error("emissions_scenario $(p.emissions_scenario) provided to SSPs component emissions_scenario parameter not found in available list: $(emissions_scenario_options)") @@ -73,21 +73,21 @@ using Mimi, CSVFiles, DataFrames, Query, Interpolations if !haskey(g_emissions_scenario_datasets, emissions_scenario_dict_key) if p.emissions_source == "Leach" - emissions_data = load(emissions_path, skiplines_begin = 6)|> - DataFrame |> - i -> rename!(i, Symbol.([:year, names(i)[2:end]...])) |> - DataFrame |> - @select(:year, :carbon_dioxide, :nitrous_oxide, :methane, :sf6) |> - DataFrame + emissions_data = load(emissions_path, skiplines_begin=6) |> + DataFrame |> + i -> rename!(i, Symbol.([:year, names(i)[2:end]...])) |> + DataFrame |> + @select(:year, :carbon_dioxide, :nitrous_oxide, :methane, :sf6) |> + DataFrame else # already perfect formatted - emissions_data = load(emissions_path)|> DataFrame + emissions_data = load(emissions_path) |> DataFrame end - g_emissions_scenario_datasets[emissions_scenario_dict_key]= emissions_data - end + g_emissions_scenario_datasets[emissions_scenario_dict_key] = emissions_data + end end - function run_timestep(p,v,d,t) + function run_timestep(p, v, d, t) ssp_dict_key = Symbol(p.SSP_source, "-", p.SSP) emissions_scenario_dict_key = Symbol(p.emissions_source, "-", p.emissions_scenario) @@ -109,27 +109,27 @@ using Mimi, CSVFiles, DataFrames, Query, Interpolations # t and only the SSP countries found in the model countries list, # already checked that all model countries are in SSP countries list subset = g_ssp_datasets[ssp_dict_key] |> - @filter(_.year == gettime(t) && _.country in p.country_names) |> - DataFrame + @filter(_.year == gettime(t) && _.country in p.country_names) |> + DataFrame # get the ordered indices of the SSP countries within the parameter # of the model countries, already checked that all model countries # are in SSP countries list order = indexin(p.country_names, subset.country) - v.population[t,:] = subset.pop[order] - v.gdp[t,:] = subset.gdp[order] - + v.population[t, :] = subset.pop[order] + v.gdp[t, :] = subset.gdp[order] + # add global data for future accessibility and quality control - v.population_global[t] = sum(v.population[t,:]) - v.gdp_global[t] = sum(v.gdp[t,:]) + v.population_global[t] = sum(v.population[t, :]) + v.gdp_global[t] = sum(v.gdp[t, :]) # ---------------------------------------------------------------------- # Emissions subset = g_emissions_scenario_datasets[emissions_scenario_dict_key] |> - @filter(_.year == gettime(t)) |> - DataFrame + @filter(_.year == gettime(t)) |> + DataFrame v.co2_emissions[t] = subset.carbon_dioxide[1] v.ch4_emissions[t] = subset.methane[1] diff --git a/test/test_Coupled.jl b/test/test_Coupled.jl index 782b1c4..753d160 100644 --- a/test/test_Coupled.jl +++ b/test/test_Coupled.jl @@ -9,7 +9,7 @@ m = Model() set_dimension!(m, :time, 1750:2300) # Handle the MimiSSPs.SSPs component -add_comp!(m, MimiSSPs.SSPs, first = 2010, last = 2300) +add_comp!(m, MimiSSPs.SSPs, first=2010, last=2300) set_dimension!(m, :country, inputregions) update_param!(m, :SSPs, :country_names, inputregions) @@ -19,7 +19,7 @@ update_param!(m, :SSPs, :emissions_source, "Leach") update_param!(m, :SSPs, :emissions_scenario, "SSP119") # Handle the MimiSSPs.RegionAggregatorSum component -add_comp!(m, MimiSSPs.RegionAggregatorSum, first = 2010, last = 2300) +add_comp!(m, MimiSSPs.RegionAggregatorSum, first=2010, last=2300) set_dimension!(m, :inputregions, inputregions) set_dimension!(m, :outputregions, outputregions) @@ -33,7 +33,7 @@ connect_param!(m, :RegionAggregatorSum, :input, :SSPs, :population) run(m) # Should also work if Aggregator runs long, using backup data -Mimi.set_first_last!(m, :RegionAggregatorSum, first = 1750) +Mimi.set_first_last!(m, :RegionAggregatorSum, first=1750) backup = zeros(551, 184) connect_param!(m, :RegionAggregatorSum, :input, :SSPs, :population, backup, ignoreunits=true) diff --git a/test/test_RegionAggregatorSum.jl b/test/test_RegionAggregatorSum.jl index 8920348..f75601a 100644 --- a/test/test_RegionAggregatorSum.jl +++ b/test/test_RegionAggregatorSum.jl @@ -21,8 +21,8 @@ run(m) expected_output = zeros(length(2000:2010), length(outputregions)) for (i, output_region) in enumerate(outputregions) - idxs = findall(i -> i == output_region, dummy_input_output[:,2]) - expected_output[:, i] = sum(data[:, idxs], dims = 2) + idxs = findall(i -> i == output_region, dummy_input_output[:, 2]) + expected_output[:, i] = sum(data[:, idxs], dims=2) end @test (m[:RegionAggregatorSum, :output]) ≈ expected_output atol = 1e-9 @@ -31,5 +31,11 @@ bad_mapping = dummy_input_output.Output_Region bad_mapping[5] = "Region-MISSING" # this does not exist in the output regions list update_param!(m, :RegionAggregatorSum, :input_output_mapping, bad_mapping) -error_msg = (try eval(run(m)) catch err err end).msg +error_msg = ( + try + eval(run(m)) + catch err + err + end +).msg @test occursin("All provided region names in the RegionAggregatorSum's input_output_mapping Parameter must exist in the output_region_names Parameter.", error_msg) diff --git a/test/test_SSPs.jl b/test/test_SSPs.jl index f5f7b90..25d0e9d 100644 --- a/test/test_SSPs.jl +++ b/test/test_SSPs.jl @@ -8,7 +8,7 @@ all_countries = load(joinpath(@__DIR__, "..", "data", "keys", "OECD Env-Growth_I m = Model() set_dimension!(m, :time, 1750:2300) set_dimension!(m, :country, all_countries.ISO3) -add_comp!(m, MimiSSPs.SSPs, first = 2010, last = 2300) +add_comp!(m, MimiSSPs.SSPs, first=2010, last=2300) update_param!(m, :SSPs, :SSP_source, "OECD Env-Growth") update_param!(m, :SSPs, :SSP, "SSP1") update_param!(m, :SSPs, :emissions_source, "Leach") @@ -20,7 +20,7 @@ run(m) m = Model() set_dimension!(m, :time, 1750:2300) set_dimension!(m, :country, all_countries.ISO3[1:10]) -add_comp!(m, MimiSSPs.SSPs, first = 2010, last = 2300) +add_comp!(m, MimiSSPs.SSPs, first=2010, last=2300) update_param!(m, :SSPs, :SSP_source, "OECD Env-Growth") update_param!(m, :SSPs, :SSP, "SSP5") update_param!(m, :SSPs, :emissions_source, "Leach") @@ -41,7 +41,13 @@ update_param!(m, :SSPs, :emissions_source, "Leach") update_param!(m, :SSPs, :emissions_scenario, "SSP119") update_param!(m, :SSPs, :country_names, all_countries.ISO3) -error_msg = (try eval(run(m)) catch err err end).msg +error_msg = ( + try + eval(run(m)) + catch err + err + end +).msg @test occursin("Cannot run SSP component in year 1750", error_msg) dummy_countries = ["Sun", "Rain", "Cloud"] @@ -56,32 +62,56 @@ update_param!(m, :SSPs, :emissions_source, "Leach") update_param!(m, :SSPs, :emissions_scenario, "SSP119") update_param!(m, :SSPs, :country_names, dummy_countries) # error because countries aren't in SSP set -error_msg = (try eval(run(m)) catch err err end).msg +error_msg = ( + try + eval(run(m)) + catch err + err + end +).msg @test occursin("All countries in countries parameter must be found in SSPs component Socioeconomic Dataframe, the following were not found:", error_msg) m = Model() set_dimension!(m, :time, 1750:2300) set_dimension!(m, :country, all_countries.ISO3) -add_comp!(m, MimiSSPs.SSPs, first = 2010, last = 2300) +add_comp!(m, MimiSSPs.SSPs, first=2010, last=2300) update_param!(m, :SSPs, :SSP_source, "NOT A MODEL") update_param!(m, :SSPs, :SSP, "SSP1") update_param!(m, :SSPs, :emissions_source, "Leach") update_param!(m, :SSPs, :emissions_scenario, "SSP119") update_param!(m, :SSPs, :country_names, all_countries.ISO3) -error_msg = (try eval(run(m)) catch err err end).msg +error_msg = ( + try + eval(run(m)) + catch err + err + end +).msg @test occursin("Model NOT A MODEL provided to SSPs component SSP_source parameter not found in available list:", error_msg) update_param!(m, :SSPs, :SSP_source, "IIASA GDP") update_param!(m, :SSPs, :SSP, "NOT A SSP") -error_msg = (try eval(run(m)) catch err err end).msg +error_msg = ( + try + eval(run(m)) + catch err + err + end +).msg @test occursin("SSP NOT A SSP provided to SSPs component SSP parameter not found in available list:", error_msg) update_param!(m, :SSPs, :SSP, "SSP1") update_param!(m, :SSPs, :emissions_scenario, "NOT A emissions_scenario") -error_msg = (try eval(run(m)) catch err err end).msg +error_msg = ( + try + eval(run(m)) + catch err + err + end +).msg @test occursin("emissions_scenario NOT A emissions_scenario provided to SSPs component emissions_scenario parameter not found in available list:", error_msg) # VALIDATION @@ -98,7 +128,7 @@ SSP = "SSP2" m = Model() set_dimension!(m, :time, 1750:2300) set_dimension!(m, :country, all_countries.ISO3) -add_comp!(m, MimiSSPs.SSPs, first = 1950, last = 2300) +add_comp!(m, MimiSSPs.SSPs, first=1950, last=2300) update_param!(m, :SSPs, :SSP_source, SSP_source) update_param!(m, :SSPs, :SSP, SSP) update_param!(m, :SSPs, :emissions_source, emissions_source) @@ -108,18 +138,18 @@ update_param!(m, :SSPs, :country_names, all_countries.ISO3) run(m) emissions_path = joinpath(@__DIR__, "..", "data", "emissions", "$(emissions_source)_$(emissions_scenario).csv") -emissions_data = load(emissions_path, skiplines_begin = 6)|> - DataFrame |> - i -> rename!(i, Symbol.([:year, names(i)[2:end]...])) |> - DataFrame |> - @select(:year, :carbon_dioxide, :nitrous_oxide, :methane, :sf6) |> - DataFrame |> - @filter(_.year in 1950:2300) |> - DataFrame - -@test m[:SSPs, :co2_emissions][findfirst(i -> i == 1950, collect(1750:2300)):end] ≈ emissions_data.carbon_dioxide atol = 1e-9 -@test m[:SSPs, :ch4_emissions][findfirst(i -> i == 1950, collect(1750:2300)):end] ≈ emissions_data.methane atol = 1e-9 -@test m[:SSPs, :sf6_emissions][findfirst(i -> i == 1950, collect(1750:2300)):end] ≈ emissions_data.sf6 atol = 1e-9 +emissions_data = load(emissions_path, skiplines_begin=6) |> + DataFrame |> + i -> rename!(i, Symbol.([:year, names(i)[2:end]...])) |> + DataFrame |> + @select(:year, :carbon_dioxide, :nitrous_oxide, :methane, :sf6) |> + DataFrame |> + @filter(_.year in 1950:2300) |> + DataFrame + +@test m[:SSPs, :co2_emissions][findfirst(i -> i == 1950, collect(1750:2300)):end] ≈ emissions_data.carbon_dioxide atol = 1e-9 +@test m[:SSPs, :ch4_emissions][findfirst(i -> i == 1950, collect(1750:2300)):end] ≈ emissions_data.methane atol = 1e-9 +@test m[:SSPs, :sf6_emissions][findfirst(i -> i == 1950, collect(1750:2300)):end] ≈ emissions_data.sf6 atol = 1e-9 @test m[:SSPs, :n2o_emissions][findfirst(i -> i == 1950, collect(1750:2300)):end] ≈ emissions_data.nitrous_oxide atol = 1e-9 socioeconomic_path = joinpath(@__DIR__, "..", "data", "socioeconomic", "$(SSP_source)_$(SSP).csv") @@ -128,25 +158,25 @@ socioeconomic_data = load(socioeconomic_path) |> DataFrame for country in all_countries.ISO3 pop_data_model = getdataframe(m, :SSPs, :population) |> - @filter(_.time in collect(1950:2300) && _.country == country) |> - DataFrame |> - @orderby(:time) |> - DataFrame + @filter(_.time in collect(1950:2300) && _.country == country) |> + DataFrame |> + @orderby(:time) |> + DataFrame gdp_data_model = getdataframe(m, :SSPs, :gdp) |> - @filter(_.time in collect(1950:2300) && _.country == country) |> - DataFrame |> - @orderby(:time) |> - DataFrame + @filter(_.time in collect(1950:2300) && _.country == country) |> + DataFrame |> + @orderby(:time) |> + DataFrame socioeconomic_data_country = socioeconomic_data |> - @filter(_.year in collect(1950:2300) && _.country == country) |> - DataFrame |> - @orderby(:year) |> - DataFrame + @filter(_.year in collect(1950:2300) && _.country == country) |> + DataFrame |> + @orderby(:year) |> + DataFrame - @test pop_data_model.population ≈ socioeconomic_data_country.pop atol = 1e-9 - @test collect(skipmissing(gdp_data_model.gdp)) ≈ collect(skipmissing(socioeconomic_data_country.gdp)) atol = 1e-9 # some missing data ex. TWN + @test pop_data_model.population ≈ socioeconomic_data_country.pop atol = 1e-9 + @test collect(skipmissing(gdp_data_model.gdp)) ≈ collect(skipmissing(socioeconomic_data_country.gdp)) atol = 1e-9 # some missing data ex. TWN end # IIASA GDP and Leach @@ -161,7 +191,7 @@ SSP = "SSP2" m = Model() set_dimension!(m, :time, 1750:2300) set_dimension!(m, :country, all_countries.ISO3) -add_comp!(m, MimiSSPs.SSPs, first = 2010, last = 2300) +add_comp!(m, MimiSSPs.SSPs, first=2010, last=2300) update_param!(m, :SSPs, :SSP_source, SSP_source) update_param!(m, :SSPs, :SSP, SSP) update_param!(m, :SSPs, :emissions_source, emissions_source) @@ -170,24 +200,24 @@ update_param!(m, :SSPs, :country_names, all_countries.ISO3) run(m) -Mimi.set_first_last!(m, :SSPs, first = 1950) +Mimi.set_first_last!(m, :SSPs, first=1950) @test_throws ErrorException run(m) # can't run starting in 1950 -Mimi.set_first_last!(m, :SSPs, first = 2010) +Mimi.set_first_last!(m, :SSPs, first=2010) run(m) emissions_path = joinpath(@__DIR__, "..", "data", "emissions", "$(emissions_source)_$(emissions_scenario).csv") -emissions_data = load(emissions_path, skiplines_begin = 6)|> - DataFrame |> - i -> rename!(i, Symbol.([:year, names(i)[2:end]...])) |> - DataFrame |> - @select(:year, :carbon_dioxide, :nitrous_oxide, :methane, :sf6) |> - DataFrame |> - @filter(_.year in 2010:2300) |> - DataFrame - -@test m[:SSPs, :co2_emissions][findfirst(i -> i == 2010, collect(1750:2300)):end] ≈ emissions_data.carbon_dioxide atol = 1e-9 -@test m[:SSPs, :ch4_emissions][findfirst(i -> i == 2010, collect(1750:2300)):end] ≈ emissions_data.methane atol = 1e-9 -@test m[:SSPs, :sf6_emissions][findfirst(i -> i == 2010, collect(1750:2300)):end] ≈ emissions_data.sf6 atol = 1e-9 +emissions_data = load(emissions_path, skiplines_begin=6) |> + DataFrame |> + i -> rename!(i, Symbol.([:year, names(i)[2:end]...])) |> + DataFrame |> + @select(:year, :carbon_dioxide, :nitrous_oxide, :methane, :sf6) |> + DataFrame |> + @filter(_.year in 2010:2300) |> + DataFrame + +@test m[:SSPs, :co2_emissions][findfirst(i -> i == 2010, collect(1750:2300)):end] ≈ emissions_data.carbon_dioxide atol = 1e-9 +@test m[:SSPs, :ch4_emissions][findfirst(i -> i == 2010, collect(1750:2300)):end] ≈ emissions_data.methane atol = 1e-9 +@test m[:SSPs, :sf6_emissions][findfirst(i -> i == 2010, collect(1750:2300)):end] ≈ emissions_data.sf6 atol = 1e-9 @test m[:SSPs, :n2o_emissions][findfirst(i -> i == 2010, collect(1750:2300)):end] ≈ emissions_data.nitrous_oxide atol = 1e-9 socioeconomic_path = joinpath(@__DIR__, "..", "data", "socioeconomic", "$(SSP_source)_$(SSP).csv") @@ -196,23 +226,23 @@ socioeconomic_data = load(socioeconomic_path) |> DataFrame for country in all_countries.ISO3 pop_data_model = getdataframe(m, :SSPs, :population) |> - @filter(_.time in collect(2010:2300) && _.country == country) |> - DataFrame |> - @orderby(:time) |> - DataFrame + @filter(_.time in collect(2010:2300) && _.country == country) |> + DataFrame |> + @orderby(:time) |> + DataFrame gdp_data_model = getdataframe(m, :SSPs, :gdp) |> - @filter(_.time in collect(2010:2300) && _.country == country) |> - DataFrame |> - @orderby(:time) |> - DataFrame + @filter(_.time in collect(2010:2300) && _.country == country) |> + DataFrame |> + @orderby(:time) |> + DataFrame socioeconomic_data_country = socioeconomic_data |> - @filter(_.year in collect(2010:2300) && _.country == country) |> - DataFrame |> - @orderby(:year) |> - DataFrame + @filter(_.year in collect(2010:2300) && _.country == country) |> + DataFrame |> + @orderby(:year) |> + DataFrame - @test pop_data_model.population ≈ socioeconomic_data_country.pop atol = 1e-9 - @test gdp_data_model.gdp ≈ socioeconomic_data_country.gdp atol = 1e-9 + @test pop_data_model.population ≈ socioeconomic_data_country.pop atol = 1e-9 + @test gdp_data_model.gdp ≈ socioeconomic_data_country.gdp atol = 1e-9 end