Skip to content

Commit

Permalink
hack: semi-automated config magicgui
Browse files Browse the repository at this point in the history
  • Loading branch information
ziw-liu committed Sep 29, 2023
1 parent b515d86 commit 954d12c
Showing 1 changed file with 76 additions and 6 deletions.
82 changes: 76 additions & 6 deletions recOrder/plugin/_widget.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
from __future__ import annotations

Check warning on line 1 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L1

Added line #L1 was not covered by tests

import os
from inspect import isclass
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Union, Literal

Check warning on line 6 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L3-L6

Added lines #L3 - L6 were not covered by tests

import pydantic
from magicgui import magicgui, widgets
from qtpy.QtCore import Qt
from qtpy.QtWidgets import (

Check warning on line 11 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L8-L11

Added lines #L8 - L11 were not covered by tests
QFileDialog,
QFormLayout,
QFrame,
QLabel,
QGridLayout,
QLabel,
QLineEdit,
QPushButton,
QVBoxLayout,
QWidget,
QSlider,
)
from superqt import QCollapsible, QLabeledSlider

Check warning on line 22 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L22

Added line #L22 was not covered by tests

from superqt import QLabeledSlider, QCollapsible
from recOrder.cli.settings import ReconstructionSettings

Check warning on line 24 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L24

Added line #L24 was not covered by tests

if TYPE_CHECKING:
from napari import Viewer

Check warning on line 27 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L26-L27

Added lines #L26 - L27 were not covered by tests
Expand All @@ -39,6 +42,70 @@ def __init__(self, name: str):
self.setLayout(layout)

Check warning on line 42 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L36-L42

Added lines #L36 - L42 were not covered by tests


class ReconstructionSettingsWidget(QWidget):
def __init__(self):
super().__init__()

Check warning on line 47 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L45-L47

Added lines #L45 - L47 were not covered by tests


def calibrate_lc(

Check warning on line 50 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L50

Added line #L50 was not covered by tests
calibration_metadata: str, states: Literal["4-states", "5-states"]
):
pass

Check warning on line 53 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L53

Added line #L53 was not covered by tests


def _filter_annoation(field_type: type):
annotation = str
if field_type.__name__ in pydantic.types.__all__:
if "Float" in str(field_type):
annotation = float
elif "Int" in str(field_type):
annotation = int
return annotation

Check warning on line 63 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L56-L63

Added lines #L56 - L63 were not covered by tests


def _get_config_field(field: pydantic.fields.ModelField):
try:
widget = widgets.create_widget(

Check warning on line 68 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L66-L68

Added lines #L66 - L68 were not covered by tests
value=field.default, annotation=field.type_, name=field.name
)
except Exception:
widget = widgets.create_widget(

Check warning on line 72 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L71-L72

Added lines #L71 - L72 were not covered by tests
value=field.default,
annotation=_filter_annoation(field.type_),
name=field.name,
)
return widget

Check warning on line 77 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L77

Added line #L77 was not covered by tests


def _is_pydantic_model_type(type_: type):
if isclass(type_):
if issubclass(type_, pydantic.BaseModel):
return True
return False

Check warning on line 84 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L80-L84

Added lines #L80 - L84 were not covered by tests


def _get_config_container(

Check warning on line 87 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L87

Added line #L87 was not covered by tests
model: Union[pydantic.BaseModel, pydantic.fields.ModelField],
scrollable: bool,
label: str = None,
):
"""Recursively create nested magic GUI widgets for a pydantic model."""
if _is_pydantic_model_type(model):
widget = widgets.Container(scrollable=scrollable)
if label is not None:
label = widgets.Label(value=label)
widget.append(label)
for field in model.__fields__.values():
widget.append(_get_config_container(field, scrollable=False))
elif _is_pydantic_model_type(model.type_):
widget = _get_config_container(

Check warning on line 101 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L93-L101

Added lines #L93 - L101 were not covered by tests
model.type_, scrollable=False, label=model.name.replace("_", " ")
)
else:
widget = _get_config_field(model)
return widget

Check warning on line 106 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L105-L106

Added lines #L105 - L106 were not covered by tests


class MainWidget(QWidget):
def __init__(self, napari_viewer: Viewer):
super().__init__()
Expand Down Expand Up @@ -73,7 +140,7 @@ def _add_calibration_layout(self) -> None:
)

def _launch_calibration_window(self) -> None:
pass
magicgui(calibrate_lc).show()

Check warning on line 143 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L142-L143

Added lines #L142 - L143 were not covered by tests

def _add_input_layout(self) -> None:
self._input_path_le = QLineEdit()
Expand Down Expand Up @@ -108,7 +175,10 @@ def _add_reconstruct_layout(self) -> None:
self._main_layout.addWidget(reconstruct_btn)

Check warning on line 175 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L173-L175

Added lines #L173 - L175 were not covered by tests

def _launch_config_window(self) -> None:
pass
container = _get_config_container(

Check warning on line 178 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L177-L178

Added lines #L177 - L178 were not covered by tests
ReconstructionSettings, scrollable=True
)
container.show()

Check warning on line 181 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L181

Added line #L181 was not covered by tests

def _reconstruct(self) -> None:
pass

Check warning on line 184 in recOrder/plugin/_widget.py

View check run for this annotation

Codecov / codecov/patch

recOrder/plugin/_widget.py#L183-L184

Added lines #L183 - L184 were not covered by tests
Expand Down

0 comments on commit 954d12c

Please sign in to comment.