iiasa / ixmp4

A data warehouse for high-powered scenario analysis in the domain of integrated assessment of climate change and energy systems modeling
https://docs.ece.iiasa.ac.at/ixmp4
MIT License
11 stars 6 forks source link

Introduce optimization variable #97

Closed glatterf42 closed 1 month ago

glatterf42 commented 4 months ago

This PR introduces the next item of the message data model: Variable. It comes complete with everything the other items already have and only minor changes to Column that are required for this to work, so without further DRY improvements, this PR size is as small as it gets (for the remaining Equation).

Some questions and notes that come up while working on this:

Working on the transport tutorial, I noticed that I had completely forgotten about adding variables = db.relationship() in the Run DB-table. I have even run migrations for these other PRs, but no error came up. I'm wondering if we even need these relationship() declarations at all.

glatterf42 commented 4 months ago

Looking at the transport tutorial, I noticed that it includes a variable like this

Variables
     z       total transportation costs in thousands of dollars ;

I.e, without constraints to indexsets. So I set out to make constrained_to_indexsets optional and in the process, stumbled upon some more questions:

glatterf42 commented 4 months ago

Context for the latest commit: typing.Never can be used to type hint empty lists. Unfortunately, it was only introduced with python 3.11. However, typing.NoReturn was introduced with 3.6.2 and these are equivalent according to the docs.

meksor commented 1 month ago

Hi I investigated the ValueError comment further: I created a test platform with ixmp4 platforms add test. Then I started the ixmp4 service in non-manged mode: IXMP4_MANAGED=false ixmp4 server start Then I executed this test script:

import ixmp4
from ixmp4.conf import settings
from ixmp4.conf.auth import SelfSignedAuth
from ixmp4.conf.manager import PlatformInfo
from ixmp4.data.backend import RestBackend

rb = RestBackend(
    PlatformInfo(name="test", dsn="http://localhost:9000/v1/test/"),
    auth=SelfSignedAuth(settings.secret_hs256),
)

mp = ixmp4.Platform(_backend=rb)

run = mp.runs.create("Model", "Scenario")
ids = mp.backend.optimization.indexsets.create(run_id=run.id, name="Indexset 1")

_ = run.optimization.variables.create(
    "Variable 0",
    constrained_to_indexsets=[ids.name],
    column_names=["Dimension 1", "Dimension 2"],
)

Which results in the following two interactive terminal outputs: image

Showing us that the ValueError, as expected, is not propagated to the client.

For now you can fix this by making a custom ixmp4 exception, but we should investigate why the test setup cannot reproduce this (i assume because the TestClient class does not prevent server side exceptions being propagated to the client) and how to fix it.

glatterf42 commented 1 month ago

Thanks for the instructions and for catching this error! It was fixed in #115 and this branch rebased/reworked to accommodate these changes :)