Skip to content

Commit 389ef46

Browse files
Allow DataForms to persist MainConfig object (#194)
* updated configs to actual pydantic v2 * updated ultrack API pydantic models * fixed docs * Allow to persist object * Allow DataForms to persist MainConfig object * moved alias mapping to config file --------- Co-authored-by: Jordao Bragantini <[email protected]>
1 parent 0476320 commit 389ef46

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

Diff for: ultrack/config/config.py

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
LOG = logging.getLogger(__name__)
1313

1414

15+
CFG_ALIAS_TO_ATTR = {
16+
"segmentation": "segmentation_config",
17+
"linking": "linking_config",
18+
"tracking": "tracking_config",
19+
"data": "data_config",
20+
}
21+
22+
1523
class LinkingConfig(BaseModel):
1624
"""
1725
Candidate cell hypotheses linking configuration

Diff for: ultrack/widgets/ultrackwidget/data_forms.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
QWidget,
1414
)
1515

16-
from ultrack import MainConfig
16+
from ultrack.config import CFG_ALIAS_TO_ATTR, MainConfig
1717
from ultrack.widgets.ultrackwidget.components.blankable_number_edit import (
1818
BlankableNumberEdit,
1919
)
@@ -114,9 +114,12 @@ def load_config(self, config: MainConfig) -> None:
114114
config : MainConfig
115115
The main configuration to load.
116116
"""
117-
self._config = config.model_dump(by_alias=True)
118-
for id_form, id_field, widget, getter, setter in self._bindings:
119-
value = self._config[id_form][id_field]
117+
self._config = config
118+
for id_form, id_field, widget, _, setter in self._bindings:
119+
# Get the actual config field name from the alias mapping
120+
field_name = CFG_ALIAS_TO_ATTR.get(id_form, id_form)
121+
# In Pydantic v2, we can still use getattr for nested access
122+
value = getattr(getattr(self._config, field_name), id_field)
120123
getattr(widget, setter)(value)
121124

122125
def _create_form(self, id_form: str, metadata: Dict[str, Any]) -> None:
@@ -372,10 +375,14 @@ def get_config(self) -> MainConfig:
372375
MainConfig
373376
The main configuration object.
374377
"""
375-
for id_form, id_field, widget, getter, setter in self._bindings:
378+
for id_form, id_field, widget, getter, _ in self._bindings:
376379
value = getattr(widget, getter)()
377-
self._config[id_form][id_field] = value
378-
return MainConfig.parse_obj(self._config)
380+
# Get the actual config field name from the alias mapping
381+
field_name = CFG_ALIAS_TO_ATTR.get(id_form, id_form)
382+
sub_config = getattr(self._config, field_name)
383+
# In Pydantic v2, we can still use setattr for direct updates
384+
setattr(sub_config, id_field, value)
385+
return self._config
379386

380387
def setup_additional_options(self, workflow_choice: WorkflowChoice) -> None:
381388
"""

0 commit comments

Comments
 (0)