EnergyEconomyDecoupling / PFUPipeline

Primary, Final, and Useful energy and exergy for 1960-2019
https://energyeconomydecoupling.github.io/PFUPipeline/
Other
3 stars 0 forks source link

Error when attempting to generate GHA etas template #5

Closed ZekeMarshall closed 4 years ago

ZekeMarshall commented 4 years ago

Hi Matt,

I'm getting the following error when i attempt to run generate_eta_fu_template("GHA")

 Error: Problem with `mutate()` input `Eu.product`.
x factor level [8] is duplicated
i Input `Eu.product` is `factor(Eu.product, levels = eu_product_sort_order)`.

Are you recieving the same error?

If so is it due to the fact that the Eu.product's on rows 161 and 162 are the same?

Thanks,

Zeke

MatthewHeun commented 4 years ago

In issue #6, you mentioned that you solved the issue with GHA. If that's right, can you close #5?

ZekeMarshall commented 4 years ago

Hi Matt, i successfully generated the GHA allocations template, but I cannot generate the GHA FU etas template. Strange!

ZekeMarshall commented 4 years ago

I've just changed the name of the Eu.product in row 162 from Light to LightTV and attempted to generate the etas template again, still receiving the same error.

MatthewHeun commented 4 years ago

I see the same error ("factor level [8] is duplicated"). Let me look into it. I'll start with the GHA problem (#5). Later, I'll work on the CHN issue (#6).

MatthewHeun commented 4 years ago

Looks like this error occurs in the IEATools::eta_fu_template(). The specific section of the code is

    # Now compile the order of Eu.products for this data frame.
    eu_product_sort_order <- c(md, ke, light, heat_prods_sorted, leftover_eu_prods)
    # Sort the Maxima data frame to get the order we want.
    row_order <- Maxima %>% 
      dplyr::filter(!!as.name(quantity) == e_dot_machine_perc) %>% 
      dplyr::mutate(
        !!as.name(eu_product) := factor(!!as.name(eu_product), levels = eu_product_sort_order)
      ) %>% 
      dplyr::arrange(!!!meta_cols, !!as.name(eu_product), dplyr::desc(!!as.name(maximum_values))) %>% 
      dplyr::mutate(
        !!as.name(.row_order) := paste(!!!meta_cols, !!as.name(machine), !!as.name(eu_product), sep = "+")
      ) %>% 
      magrittr::extract2(.row_order)

Maybe there are duplicates in eu_product_sort_order. I'm not sure why that would be. Still investigating!

MatthewHeun commented 4 years ago

Well, it doesn't look like there are duplicates in eu_product_sort_order. I added this line to eta_fu_template():

print(eu_product_sort_order)

and received this output:

[1] "MD"        "KE"        "Light"     "HTH.600.C" "MTH.200.C" "MTH.100.C" "LTH.-10.C" "KE"   

I note that the 8th level is "KE". Still digging.

MatthewHeun commented 4 years ago

Duh! Staring me right in the face. "KE" is present twice in eu_product_sort_order, once at eu_product_sort_order[2] and again at eu_product_sort_order[8]. Need to figure out why that is happening.

MatthewHeun commented 4 years ago

The key was to change a preceding line:

    leftover_eu_prods <- sort(setdiff(eu_prods, c(md, light, heat_prods)))

to

    leftover_eu_prods <- sort(setdiff(eu_prods, c(md, ke, light, heat_prods)))

Now generate_eta_fu_template("GHA") works for me.

However, the fix is in the IEATools package. So you'll need to install an updated version of IEATools after I publish it. Hang on a few minutes until it is ready. I'll post here when things are ready to go.

MatthewHeun commented 4 years ago

I have published the new version of IEATools which solves this issue. Get it with install_github("MatthewHeun/IEATools").

ZekeMarshall commented 4 years ago

Ah I see! I've just seen that you've pushed the latest version of IEATools, i'll update it and give generating the GHA etas template a go now. I assume that if there are any additional Eu.products will we have to add them to the above string every time?

MatthewHeun commented 4 years ago

No. I want the order to be mech drive, kinetic energy, light, heat, and then anything else that the analyst adds (including typos!) at the end. That part of the code figures out what leftovers go at the end, and I (erroneously) was not removing kinetic energy from the leftovers when I had already included it earlier, thereby causing duplicated KE Eu products.

Of course, this problem didn't appear for countries that lacked KE. "GHA" is the first country that we have analyzed that includes KE, which triggered the issue. Now that I have fixed it, there should be no problems going forward.

ZekeMarshall commented 4 years ago

That makes sense, I've updated IEATools and have now successfully generated the GHA FU etas template. Thanks!