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

Shared param test #180

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/ngen_cal/src/ngen/cal/ngen.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ def _params_as_df(params: Mapping[str, Parameters], name: str = None):
df['model'] = k
df.rename(columns={'name':'param'}, inplace=True)
dfs.append(df)
return pd.concat(dfs)
return pd.concat(dfs).set_index('param')
else:
p = params.get(name, [])
df = pd.DataFrame([s.__dict__ for s in p])
df['model'] = name
df.rename(columns={'name':'param'}, inplace=True)
return df
return df.set_index('param')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh. It is actually possible for this dataframe to be empty if called from _map_params_to_realization and there is a module in the realization which doesn't have any params from the config to map to it, in this case, the set_index here will fail

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think this breaks search, since param is no longer a column when subset to update_config

search.py", line 168, in dds_set


def _map_params_to_realization(params: Mapping[str, Parameters], realization: Realization):
# don't even think about calibration multiple formulations at once just yet..
Expand Down
24 changes: 23 additions & 1 deletion python/ngen_cal/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import pytest
from typing import Generator, List
from typing import Generator, List, Mapping
from pathlib import Path
from copy import deepcopy
import json
Expand All @@ -11,6 +13,7 @@
from ngen.cal.calibration_cathment import CalibrationCatchment
from ngen.cal.model import EvaluationOptions
from ngen.cal.agent import Agent
from ngen.cal.parameter import Parameter
from hypy import Nexus

from .utils import *
Expand Down Expand Up @@ -219,3 +222,22 @@ def explicit_catchments(nexus, fabric, workdir) -> Generator[ List[ CalibrationC
cat = CalibrationCatchment(workdir, id, nexus, start, end, fabric, 'Q_Out', eval_options, data)
catchments.append(cat)
yield catchments

@pytest.fixture
def multi_model_shared_params() -> Mapping[str, list[Parameter]]:
p1 = Parameter(name='a', min=0, max=1, init=0)
p2 = Parameter(name='d', min=2, max=3, init=0)
p3 = Parameter(name='c', min=0, max=1, init=0)
p4 = Parameter(name='a', min=0, max=1, init=0)
params = {'A':[p1], 'B':[p2, p4], 'C':[p3, p4]}

return params

@pytest.fixture
def multi_model_shared_params2() -> Mapping[str, list[Parameter]]:
p1 = Parameter(name='a', min=0, max=1, init=0)
p2 = Parameter(name='a', min=0, max=1, init=0)
p3 = Parameter(name='c', min=0, max=1, init=0)
params = {'A':[p1, p3], 'B':[p2]}

return params
42 changes: 42 additions & 0 deletions python/ngen_cal/tests/test_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from __future__ import annotations

from ngen.cal.ngen import _params_as_df
import pandas as pd

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Mapping
from ngen.cal.parameter import Parameter

def test_multi_params(multi_model_shared_params: Mapping[str, list[Parameter]]):
# This is essentially the path the params go through from
# creation in model.py to update in search.py
params = _params_as_df(multi_model_shared_params)
params = pd.DataFrame(params).rename(columns={'init':'0'})
# create new iteration from old
params['1'] = params['0']
#update the parameters by index
idx1 = 'a'
idx2 = 'c'
params.loc[idx1, '1'] = 0.5
pa = params[ params['model'] == 'A' ].loc[idx1]
pb = params[ params['model'] == 'B' ].loc[idx1]
pc = params[ params['model'] == 'C' ].loc[idx2]

assert pa.drop('model').equals( pb.drop('model') )
# ensure unique params/alias are not modifed by selection
assert pa.loc['1'] != pc.loc['1']

def test_multi_params2(multi_model_shared_params2: Mapping[str, list[Parameter]]):
# This is essentially the path the params go through from
# creation in model.py to update in search.py
params = _params_as_df(multi_model_shared_params2)
params = pd.DataFrame(params).rename(columns={'init':'0'})
# create new iteration from old
params['1'] = params['0']
#update the parameters by index
params.loc['a', '1'] = 0.5
pa = params[ params['model'] == 'A' ].drop('model', axis=1).loc['a']
pb = params[ params['model'] == 'B' ].drop('model', axis=1).loc['a']
assert pa.equals( pb )
Loading