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

Add validation to TUI. #492

Merged
merged 18 commits into from
Apr 3, 2025
Merged
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
6 changes: 6 additions & 0 deletions datashuttle/datashuttle_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,12 @@ def validate_project(
any folder not prefixed with sub-, ses- or a valid datatype will
raise a validation issue.
"""
if include_central and strict_mode:
raise ValueError(
"`strict_mode` is currently only available for `include_central=False`. "
"Please raise a GitHub issue if you would like to use this feature."
)

utils.print_message_to_user(
f"Logs of the validation will be stored in: "
f"{self.cfg.make_and_get_logging_path()}\n\nValidation results:"
Expand Down
10 changes: 10 additions & 0 deletions datashuttle/datashuttle_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def quick_validate_project(
project_path: str | Path,
top_level_folder: Optional[TopLevelFolder] = "rawdata",
display_mode: DisplayMode = "warn",
strict_mode: bool = False,

Choose a reason for hiding this comment

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

To maintain uniformity across all projects, should we consider setting strict_mode=True as the default? This would ensure that only NeuroBlueprint-compliant folders exist, preventing inconsistencies in datasets. Users who need flexibility could still override this by explicitly setting strict_mode=False when calling the function. This is what I think

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @Transyltooniaa this is a good point. I'm not 100% sure on this but, due to how datashuttle is likely to be used in neuroscience projects, I think it makes sense to assume there will be non-datashuttle folders within the project folder. This is not ideal (it would be nice if all folders / data were in the datatype folders) but unfortunately this is the reality. As such, it would be good to avoid confusing new users with errors related to extra folders they have in their project. Then, they can opt-in to the stricter mode once they understand the details.

name_templates: Optional[Dict] = None,
) -> List[str]:
"""
Expand All @@ -48,6 +49,14 @@ def quick_validate_project(
The validation issues are displayed as ``"error"`` (raise error)
``"warn"`` (show warning) or ``"print"``.

strict_mode: bool
If `True`, only allow NeuroBlueprint-formatted folders to exist in
the project. By default, non-NeuroBlueprint folders (e.g. a folder
called 'my_stuff' in the 'rawdata') are allowed, and only folders
starting with sub- or ses- prefix are checked. In `Strict Mode`,
any folder not prefixed with sub-, ses- or a valid datatype will
raise a validation issue.

name_templates : Dict
A dictionary of templates for subject and session name
to validate against. See ``DataShuttle.set_name_templates()``
Expand Down Expand Up @@ -83,6 +92,7 @@ def quick_validate_project(
include_central=False,
display_mode=display_mode,
name_templates=name_templates,
strict_mode=strict_mode,
)

return error_messages
Expand Down
12 changes: 12 additions & 0 deletions datashuttle/tui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
project_manager,
project_selector,
settings,
validate_at_path,
)
from datashuttle.tui.tooltips import get_tooltip


class TuiApp(App, inherit_bindings=False): # type: ignore
Expand Down Expand Up @@ -55,13 +57,19 @@ def compose(self) -> ComposeResult:
id="mainwindow_existing_project_button",
),
Button("Make New Project", id="mainwindow_new_project_button"),
Button(
"Validate Project at Path",
id="mainwindow_validate_from_project_path",
),
Button("Settings", id="mainwindow_settings_button"),
Button("Get Help", id="mainwindow_get_help_button"),
id="mainwindow_contents_container",
)

def on_mount(self) -> None:
self.set_dark_mode(self.load_global_settings()["dark_mode"])
id = "#mainwindow_validate_from_project_path"
self.query_one(id).tooltip = get_tooltip(id)

def set_dark_mode(self, dark_mode: bool) -> None:
self.theme = "textual-dark" if dark_mode else "textual-light"
Expand Down Expand Up @@ -89,9 +97,13 @@ def on_button_pressed(self, event: Button.Pressed) -> None:
self,
)
)

elif event.button.id == "mainwindow_get_help_button":
self.push_screen(get_help.GetHelpScreen())

elif event.button.id == "mainwindow_validate_from_project_path":
self.push_screen(validate_at_path.ValidateScreen(self))

def load_project_page(self, interface: Interface) -> None:
if interface:
self.push_screen(
Expand Down
40 changes: 40 additions & 0 deletions datashuttle/tui/css/tui_menu.tcss
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,46 @@ config label and button align center */
}


/* Validate Content ----------------------------------------------------------------- */

#validate_top_container {
align: center top;
content-align: center top;
margin: 2 0 0 0;
layout: vertical;
overflow: hidden auto;
}


#validate_arguments_horizontal{
height: auto;
margin: 0 1 1 1;
}

#validate_top_level_folder_select {
width: 30%;
}

#validate_path_input {
width: 80%;
margin: 1 1 1 1;
}

#validate_select_button {
margin: 1 1 1 1;
}

#validate_path_container {
width: 100%;
height: auto;
}

#validate_richlog {
align: center top;
height: 60%;
}


/* DisplayedDatatypesScreen --------------------------------------------------------------- */

DisplayedDatatypesScreen {
Expand Down
33 changes: 33 additions & 0 deletions datashuttle/tui/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,39 @@ def validate_names(
except BaseException as e:
return False, str(e)

def validate_project(
self,
top_level_folder: list[str] | None,
include_central: bool,
strict_mode: bool,
) -> tuple[bool, list[str] | str]:
"""
Wrap the validate project function. This returns a list of validation
errors (empty if there are none).

Parameters
----------

top_level_folder
The "rawdata" or "derivatives" folder to validate. If `None`, both
will be validated.
include_central
If `True`, the central project is also validated.
strict_mode
If `True`, validation will be run in strict mode.
"""
try:
results = self.project.validate_project(
top_level_folder=top_level_folder,
display_mode="print", # unused
include_central=include_central,
strict_mode=strict_mode,
)
return True, results

except BaseException as e:
return False, str(e)

# Transfer
# ----------------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions datashuttle/tui/screens/new_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from textual.screen import Screen
from textual.widgets import Button, Header

from datashuttle.tui import configs
from datashuttle.tui.shared import configs_content


class NewProjectScreen(Screen):
Expand Down Expand Up @@ -44,7 +44,7 @@ def __init__(self, mainwindow: App) -> None:
def compose(self) -> ComposeResult:
yield Header()
yield Button("Main Menu", id="all_main_menu_buttons")
yield configs.ConfigsContent(
yield configs_content.ConfigsContent(
self, interface=None, id="new_project_configs_content"
)

Expand Down
7 changes: 6 additions & 1 deletion datashuttle/tui/screens/project_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
TabPane,
)

from datashuttle.tui.configs import ConfigsContent
from datashuttle.tui.screens import modal_dialogs
from datashuttle.tui.shared.configs_content import ConfigsContent
from datashuttle.tui.shared.validate_content import ValidateContent
from datashuttle.tui.tabs import create_folders, logging, transfer


Expand Down Expand Up @@ -73,6 +74,10 @@ def compose(self) -> ComposeResult:
self.interface,
id="tabscreen_transfer_tab",
)
with TabPane("Validate", id="tabscreen_validate_tab"):
yield ValidateContent(
self, self.interface, id="tabscreen_validate_content"
)
with TabPane("Configs", id="tabscreen_configs_tab"):
yield ConfigsContent(
self, self.interface, id="tabscreen_configs_content"
Expand Down
40 changes: 40 additions & 0 deletions datashuttle/tui/screens/validate_at_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from textual.app import ComposeResult

from datashuttle.tui.app import App

from textual.screen import Screen
from textual.widgets import Button, Header

from datashuttle.tui.shared import validate_content


class ValidateScreen(Screen):
"""
Screen to hold the validation window for
validating an existing project at a given path.
All widgets are stored in `ValidateContent`, which is
shared between here and the validation tab on the project manager.
"""

TITLE = "Validate Project"

def __init__(self, mainwindow: App) -> None:
super(ValidateScreen, self).__init__()

self.mainwindow = mainwindow

def compose(self) -> ComposeResult:
yield Header()
yield Button("Main Menu", id="all_main_menu_buttons")
yield validate_content.ValidateContent(
self, interface=None, id="validate_from_path_content"
)

def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "all_main_menu_buttons":
self.dismiss(None)
File renamed without changes.
Loading
Loading