From c6199779bcc17813337e68caec88c6e73dfb26ac Mon Sep 17 00:00:00 2001 From: Maxim Gonchar <maxim.mg.gonchar@gmail.com> Date: Tue, 21 Mar 2023 17:16:40 +0300 Subject: [PATCH] Squashed 'subtrees/dagflow/' changes from 63525278..e8a89804 e8a89804 feat: load_variables, configure sub path 29003583 chore: Parameters, properly treat kwargs a2af3511 feat: bundle load_variable `state` option a39073df feat: Parameter/GaussianParameter, `__slots__`, `free`/`constrained`/`fixed`/`variable`/`correlated` properties d7974b2a feat: load_variables, enable loading the files 7646352f feat: more schema modules e874df2c feat: graphviz add mid-nodes to handle single output to multiple inputs connection 80f2c3d0 update conftest.py REVERT: 63525278 feat: bundle load_variable `state` option REVERT: b6dc9a47 feat: Parameter/GaussianParameter, `__slots__`, `free`/`constrained`/`fixed`/`variable`/`correlated` properties REVERT: 289b18f2 feat: load_variables, enable loading the files REVERT: 4b237e30 feat: more schema modules REVERT: 249aa720 feat: graphviz add mid-nodes to handle single output to multiple inputs connection git-subtree-dir: subtrees/dagflow git-subtree-split: e8a898040fa580d5e91697783aa8f98a587d3979 --- conftest.py | 25 +++++++++-------------- dagflow/bundles/load_variables.py | 20 +++++++++++++----- dagflow/variable.py | 15 ++++++++------ test/variables/test_load_variables.py | 29 ++++++++++++++++++++++++--- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/conftest.py b/conftest.py index 1e7b36a..cb4f3e0 100644 --- a/conftest.py +++ b/conftest.py @@ -1,7 +1,7 @@ -from os import chdir, getcwd, mkdir +from os import chdir, getcwd, mkdir, listdir from os.path import isdir -from pytest import fixture, skip +from pytest import fixture def pytest_sessionstart(session): @@ -11,19 +11,14 @@ def pytest_sessionstart(session): Automatic change path to the `dag-flow/test` and create `test/output` dir """ - path = getcwd() - lastdir = path.split("/")[-1] - if lastdir == "dag-flow": # rootdir - chdir("./test") - elif lastdir in ( - "dagflow", - "example", - "doc", - "docs", - "source", - "sources", - ): # childdir - chdir("../test") + while(path := getcwd()): + if (lastdir := path.split("/")[-1]) == "test": + break + elif ".git" in listdir(path): + chdir("./test") + break + else: + chdir("..") if not isdir("output"): mkdir("output") diff --git a/dagflow/bundles/load_variables.py b/dagflow/bundles/load_variables.py index e6556d7..fb75bf8 100644 --- a/dagflow/bundles/load_variables.py +++ b/dagflow/bundles/load_variables.py @@ -66,7 +66,8 @@ IsVarsCfgDict = Schema({ 'variables': IsValuesDict, 'labels': IsLabelsDict, 'format': IsFormat, - 'state': Or('fixed', 'variable', error='Invalid parameters state: {}') + 'state': Or('fixed', 'variable', error='Invalid parameters state: {}'), + Optional('path', default=''): str }, # error = 'Invalid parameters configuration: {}' ) @@ -142,21 +143,30 @@ def load_variables(acfg): cfg = IsProperVarsCfg.validate(acfg) cfg = DictWrapper(cfg) + path = cfg['path'] + if path: + path = path.split('.') + else: + path = () + + state = cfg['state'] + ret = DictWrapper({'constants': {}, 'free': {}, 'constrained': {}}, sep='.') for key, varcfg in iterate_varcfgs(cfg): skey = '.'.join(key) label = varcfg['label'] label['key'] = skey label.setdefault('text', skey) + varcfg.setdefault(state, True) par = Parameters.from_numbers(**varcfg) if par.is_constrained: - target = ret['constrained'] + target = ('constrained',) + path elif par.is_fixed: - target = ret['constants'] + target = ('constants',) + path else: - target = ret['free'] + target = ('free',) + path - target[key] = par + ret[target+key] = par return ret diff --git a/dagflow/variable.py b/dagflow/variable.py index 0bae37a..a28f494 100644 --- a/dagflow/variable.py +++ b/dagflow/variable.py @@ -53,24 +53,27 @@ class Parameters(object): @staticmethod def from_numbers(*, dtype: DTypeLike='d', **kwargs) -> 'Parameters': - sigma = kwargs['sigma'] + sigma = kwargs.pop('sigma') if sigma is not None: - return GaussianParameters.from_numbers(dtype=dtype, **kwargs) + return GaussianParameters.from_numbers(dtype=dtype, sigma=sigma, **kwargs) - label: Dict[str, str] = kwargs.get('label') + del kwargs['central'] + + label: Dict[str, str] = kwargs.pop('label', None) if label is None: label = {'text': 'parameter'} else: label = dict(label) name: str = label.setdefault('name', 'parameter') - value = kwargs['value'] + value = kwargs.pop('value') return Parameters( Array( name, array((value,), dtype=dtype), label = label, - mode='store_weak' - ) + mode='store_weak', + ), + **kwargs ) class GaussianParameters(Parameters): diff --git a/test/variables/test_load_variables.py b/test/variables/test_load_variables.py index 73c491e..60b7d46 100644 --- a/test/variables/test_load_variables.py +++ b/test/variables/test_load_variables.py @@ -21,6 +21,25 @@ cfg1 = { 'var2': 'simple label 2', }, } +cfg1a = { + 'variables': { + 'var1': 1.0, + 'var2': 1.0, + 'sub1': { + 'var3': 2.0 + } + }, + 'format': 'value', + 'state': 'fixed', + 'labels': { + 'var1': { + 'text': 'text label 1', + 'latex': r'\LaTeX label 1', + 'name': 'v1-1' + }, + 'var2': 'simple label 2', + }, + } cfg2 = { 'variables': { @@ -100,10 +119,14 @@ cfg5 = { 'state': 'variable', } +from pprint import pprint def test_load_variables_v01(): - cfgs = (cfg1, cfg2, cfg3, cfg4, cfg5) + cfgs = (cfg1, cfg1a, cfg2, cfg3, cfg4, cfg5) with Graph(close=True) as g: - for cfg in cfgs: - load_variables(cfg) + for i, cfg in enumerate(cfgs): + vars = load_variables(cfg) + print(cfg['state']) + print(i, end=' ') + pprint(vars.object) savegraph(g, 'output/test_load_variables.pdf', show='all') -- GitLab