`message_ix.Scenario.rename()` does not work as expected
message_ix.Scenario.rename() is supposed to rename a set element, i.e., replacing the old name with a new name in all message_ix sets and parameters. However, this is not the case, i.e., not all sets and parameters are updated with a new name correctly, and there is no error or warning thrown stating this incompleteness. One can for example run the following code and check this.
It seems the issue is that under this method in message_ix.core.py, the check for finding a set element in a parameter is based on scenario.idx_names(). This means, e.g., when renaming a "node", the code only finds parameters that have index names of "node", but missing other relevant parameters that have index names related to node but not "node" itself, e.g., "node_loc", "node_rel" etc. This can be resolved relatively easily by using scenario.idx_sets() and with a little tweak of the code, and should be done possibly soon, as this is a very useful feature.
import ixmp as ix
import message_ix
mp = ix.Platform()
# Load a scenario and clone it
scen = message_ix.Scenario(mp, "Westeros_electrified", "baseline")
test = scen.clone(scenario="test", keep_solution=False)
# Rename an element from set "node" and keeping the original member
test.rename("node", {"Westeros": "Essos"}, keep=True)
# Check if the data exists in the original and renamed format
df = test.par("output", {"node_loc": "Westeros"})
assert not df.empty
df = test.par("output", {"node_loc": "Essos"})
assert not df.empty