ixmp icon indicating copy to clipboard operation
ixmp copied to clipboard

scenario.clone() returns the default scenario

Open behnam-zakeri opened this issue 5 years ago • 7 comments

I observe an unexpected behavior when I clone an ixmp Scenario class. For example, I load a Scenario that has a version number of 42 and clone this via: scenario.clone() Then, when I check the version of the cloned scenario (via scenario.version), it is 20, while I expect that it would be 43. But when I look at the database scenario list (via mp.scenario_list()), I see that the cloned scenario has got a version of 43. More importantly, the cloned scenario is not actually cloned from scenario version 42! It is indeed my scenario version 20 loaded. When I look further to the database, I see that this scenario version 20 is the default scenario. So, in short, it seems when I clone scenario version 42, it will be saved as 43 in the database, but scenario.clone() method returns my default scenario instead (and not scenario version 43). I hope I'm not missing something out. ixmp.version: 0.1.3.post.dev21

behnam-zakeri avatar May 10 '19 09:05 behnam-zakeri

@behnam2015 can you help adding a test for this case or extending one or more of existing? (see test_integration.py and test_core.py for details)

zikolach avatar May 10 '19 10:05 zikolach

cc @danielhuppmann @khaeru FYI

gidden avatar May 10 '19 10:05 gidden

cc @danielhuppmann @khaeru FYI

Thanks. This is pursuant to @behnam2015 explaining to me why I had to do this.

May also be related to #112

khaeru avatar May 10 '19 10:05 khaeru

@zikolach, when I only load ixmp things are okey. But when I load a message_ix Scenario this behavoir happens, i.e., scen = message_ix.Scenario(mp, model, scenario) I wrote the test below to show this. And I realized that when I do scenario.clone() for a model that doesn't have a first model year, I get an error: image Then, when I add the first model year and I clone, the situation that I explained happens: the default version is loaded instead of the new version. It seems there is a problem in the interaction between ixmp and message_ix on my machine.

behnam-zakeri avatar May 10 '19 12:05 behnam-zakeri

thanks for identifying this issue @behnam2015. I'm currently reworking the clone feature for MESSAGE-scheme scenarios and will take a look.

danielhuppmann avatar May 10 '19 12:05 danielhuppmann

from message_ix import Scenario
import ixmp
test_mp = ixmp.Platform()
def model_setup(scen):
    scen.add_set('node', 'node')
    scen.add_set('commodity', 'comm')
    scen.add_set('level', 'level')
    scen.add_set('year', 2010)
    scen.add_set('technology', 'tec')
    scen.add_set('mode', 'mode')
    output_specs = ['node', 'comm', 'level', 'year', 'year']
    scen.add_par('demand', ['node', 'comm', 'level', 2010, 'year'], 1, 'GWa')
    tec_specs = ['node', 'tec', 2010, 2010, 'mode']
    scen.add_par('output', tec_specs + output_specs, 1, 'GWa')
def add_first_year(scen):
    scen.remove_solution()
    scen.check_out()
    scen.add_set('cat_year', ['firstmodelyear', 2010])
    scen.commit('')

Check if clone works as expected

def do_clone(scen):
    try:
        scen_new = scen.clone()
    except:
        print('Clone did not work!')
        scen_new = []
    return scen_new

Testing version number after cloning

def test_clone(test_mp):
    scen = Scenario(test_mp, 'test_clone', 'test', version='new')
    model_setup(scen)
    scen.commit('initialize test model')
    scen.solve(case='test_clone')
    scen.set_as_default()
    ver_old = scen.version

Cloning without first model year

    scen_new = do_clone(scen)
    assert scen_new

Cloning after adding first model year

    add_first_year(scen)
    scen_new = do_clone(scen)
    ver_new = scen_new.version

Testing the version number

    assert ver_new == ver_old + 1

behnam-zakeri avatar May 10 '19 12:05 behnam-zakeri

@danielhuppmann, thanks Daniel! I used the test above for this.

behnam-zakeri avatar May 10 '19 12:05 behnam-zakeri