Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scenario.clone() returns the default scenario #136

Open
behnam-zakeri opened this issue May 10, 2019 · 7 comments
Open

scenario.clone() returns the default scenario #136

behnam-zakeri opened this issue May 10, 2019 · 7 comments

Comments

@behnam-zakeri
Copy link
Contributor

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

@zikolach
Copy link
Contributor

@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)

@gidden
Copy link
Member

gidden commented May 10, 2019

cc @danielhuppmann @khaeru FYI

@khaeru
Copy link
Member

khaeru commented May 10, 2019

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

@behnam-zakeri
Copy link
Contributor Author

behnam-zakeri commented May 10, 2019

@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.

@danielhuppmann
Copy link
Member

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

@behnam-zakeri
Copy link
Contributor Author

behnam-zakeri commented May 10, 2019

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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants