dartbrains-tools 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,25 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ *.egg-info/
8
+ *.egg
9
+ .eggs/
10
+ build/
11
+ dist/
12
+ wheels/
13
+ .venv/
14
+ .pytest_cache/
15
+ .ruff_cache/
16
+ .mypy_cache/
17
+
18
+ # uv
19
+ .python-version
20
+
21
+ # OS / editors
22
+ .DS_Store
23
+ .idea/
24
+ .vscode/
25
+ *.swp
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Luke Chang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,91 @@
1
+ Metadata-Version: 2.4
2
+ Name: dartbrains-tools
3
+ Version: 0.1.0
4
+ Summary: Helper library and interactive anywidgets for the DartBrains fMRI course
5
+ Project-URL: Homepage, https://dartbrains.org
6
+ Project-URL: Source, https://github.com/ljchang/dartbrains-tools
7
+ Project-URL: Issues, https://github.com/ljchang/dartbrains-tools/issues
8
+ Author-email: Luke Chang <luke.j.chang@dartmouth.edu>
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: anywidget,education,fmri,marimo,neuroimaging
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Education
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Topic :: Education
20
+ Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
21
+ Requires-Python: >=3.11
22
+ Requires-Dist: anywidget>=0.9.21
23
+ Requires-Dist: huggingface-hub>=1.0
24
+ Requires-Dist: numpy>=2.0
25
+ Requires-Dist: pandas
26
+ Requires-Dist: plotly>=6.0
27
+ Requires-Dist: scipy>=1.13
28
+ Requires-Dist: traitlets
29
+ Provides-Extra: notebook
30
+ Requires-Dist: marimo>=0.21; extra == 'notebook'
31
+ Description-Content-Type: text/markdown
32
+
33
+ # dartbrains-tools
34
+
35
+ Helper library and interactive anywidgets for the [DartBrains](https://dartbrains.org)
36
+ fMRI course. Extracted from the [book repo](https://github.com/ljchang/dartbrains)
37
+ so the widgets and helpers can be installed standalone — including in
38
+ [molab](https://molab.marimo.io) and pyodide/WASM marimo notebooks.
39
+
40
+ ## Install
41
+
42
+ ```bash
43
+ pip install dartbrains-tools
44
+
45
+ # Optional: include marimo for notebook_utils.youtube()
46
+ pip install "dartbrains-tools[notebook]"
47
+ ```
48
+
49
+ ## Modules
50
+
51
+ - `dartbrains_tools.data` — load the Pinel Localizer dataset from the Hugging Face Hub.
52
+ - `dartbrains_tools.mr_simulations` — Bloch equation solvers, signal generators,
53
+ HRF, and Plotly visualization helpers.
54
+ - `dartbrains_tools.mr_widgets` — 10 anywidgets for interactive MR physics teaching
55
+ (`PrecessionWidget`, `SpinEnsembleWidget`, `KSpaceWidget`, `ConvolutionWidget`,
56
+ `EncodingWidget`, `CompassWidget`, `NetMagnetizationWidget`, `TransformCubeWidget`,
57
+ `CostFunctionWidget`, `SmoothingWidget`).
58
+ - `dartbrains_tools.notebook_utils` — small marimo helpers (`youtube`).
59
+
60
+ ## Quick start
61
+
62
+ ```python
63
+ from dartbrains_tools.mr_widgets import PrecessionWidget
64
+
65
+ w = PrecessionWidget(b0=3.0, flip_angle=90.0)
66
+ w # Interactive 3D Three.js animation in any anywidget host.
67
+ ```
68
+
69
+ ```python
70
+ from dartbrains_tools.data import get_subjects, get_file, load_events
71
+
72
+ subjects = get_subjects()
73
+ bold = get_file("S01", "bold")
74
+ events = load_events("S01")
75
+ ```
76
+
77
+ ## Development
78
+
79
+ ```bash
80
+ git clone https://github.com/ljchang/dartbrains-tools
81
+ cd dartbrains-tools
82
+ uv sync
83
+ uv run pytest
84
+ uv build
85
+ ```
86
+
87
+ ## License
88
+
89
+ MIT. The parent course materials at [dartbrains](https://github.com/ljchang/dartbrains)
90
+ remain CC-BY-SA-4.0; this companion library is permissive so it can be reused
91
+ in any downstream project.
@@ -0,0 +1,59 @@
1
+ # dartbrains-tools
2
+
3
+ Helper library and interactive anywidgets for the [DartBrains](https://dartbrains.org)
4
+ fMRI course. Extracted from the [book repo](https://github.com/ljchang/dartbrains)
5
+ so the widgets and helpers can be installed standalone — including in
6
+ [molab](https://molab.marimo.io) and pyodide/WASM marimo notebooks.
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ pip install dartbrains-tools
12
+
13
+ # Optional: include marimo for notebook_utils.youtube()
14
+ pip install "dartbrains-tools[notebook]"
15
+ ```
16
+
17
+ ## Modules
18
+
19
+ - `dartbrains_tools.data` — load the Pinel Localizer dataset from the Hugging Face Hub.
20
+ - `dartbrains_tools.mr_simulations` — Bloch equation solvers, signal generators,
21
+ HRF, and Plotly visualization helpers.
22
+ - `dartbrains_tools.mr_widgets` — 10 anywidgets for interactive MR physics teaching
23
+ (`PrecessionWidget`, `SpinEnsembleWidget`, `KSpaceWidget`, `ConvolutionWidget`,
24
+ `EncodingWidget`, `CompassWidget`, `NetMagnetizationWidget`, `TransformCubeWidget`,
25
+ `CostFunctionWidget`, `SmoothingWidget`).
26
+ - `dartbrains_tools.notebook_utils` — small marimo helpers (`youtube`).
27
+
28
+ ## Quick start
29
+
30
+ ```python
31
+ from dartbrains_tools.mr_widgets import PrecessionWidget
32
+
33
+ w = PrecessionWidget(b0=3.0, flip_angle=90.0)
34
+ w # Interactive 3D Three.js animation in any anywidget host.
35
+ ```
36
+
37
+ ```python
38
+ from dartbrains_tools.data import get_subjects, get_file, load_events
39
+
40
+ subjects = get_subjects()
41
+ bold = get_file("S01", "bold")
42
+ events = load_events("S01")
43
+ ```
44
+
45
+ ## Development
46
+
47
+ ```bash
48
+ git clone https://github.com/ljchang/dartbrains-tools
49
+ cd dartbrains-tools
50
+ uv sync
51
+ uv run pytest
52
+ uv build
53
+ ```
54
+
55
+ ## License
56
+
57
+ MIT. The parent course materials at [dartbrains](https://github.com/ljchang/dartbrains)
58
+ remain CC-BY-SA-4.0; this companion library is permissive so it can be reused
59
+ in any downstream project.
@@ -0,0 +1,69 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "dartbrains-tools"
7
+ version = "0.1.0"
8
+ description = "Helper library and interactive anywidgets for the DartBrains fMRI course"
9
+ readme = "README.md"
10
+ requires-python = ">=3.11"
11
+ license = "MIT"
12
+ authors = [
13
+ { name = "Luke Chang", email = "luke.j.chang@dartmouth.edu" },
14
+ ]
15
+ keywords = ["neuroimaging", "fmri", "anywidget", "education", "marimo"]
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Education",
19
+ "Intended Audience :: Science/Research",
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3.11",
22
+ "Programming Language :: Python :: 3.12",
23
+ "Programming Language :: Python :: 3.13",
24
+ "Topic :: Education",
25
+ "Topic :: Scientific/Engineering :: Medical Science Apps.",
26
+ ]
27
+ dependencies = [
28
+ "anywidget>=0.9.21",
29
+ "traitlets",
30
+ "numpy>=2.0",
31
+ "scipy>=1.13",
32
+ "plotly>=6.0",
33
+ "pandas",
34
+ "huggingface-hub>=1.0",
35
+ ]
36
+
37
+ [project.optional-dependencies]
38
+ notebook = ["marimo>=0.21"]
39
+
40
+ [project.urls]
41
+ Homepage = "https://dartbrains.org"
42
+ Source = "https://github.com/ljchang/dartbrains-tools"
43
+ Issues = "https://github.com/ljchang/dartbrains-tools/issues"
44
+
45
+ [tool.hatch.build.targets.wheel]
46
+ packages = ["src/dartbrains_tools"]
47
+
48
+ [tool.hatch.build.targets.sdist]
49
+ include = [
50
+ "src/dartbrains_tools",
51
+ "tests",
52
+ "README.md",
53
+ "LICENSE",
54
+ "pyproject.toml",
55
+ ]
56
+
57
+ [dependency-groups]
58
+ dev = [
59
+ "pytest>=8",
60
+ "ruff",
61
+ ]
62
+
63
+ [tool.ruff]
64
+ line-length = 100
65
+ target-version = "py311"
66
+
67
+ [tool.ruff.lint]
68
+ select = ["E", "F", "I", "W", "UP"]
69
+ ignore = ["E501"]
@@ -0,0 +1,20 @@
1
+ """DartBrains helper library: data loaders, MR physics simulations, anywidgets."""
2
+
3
+ __version__ = "0.1.0"
4
+
5
+ from . import data, mr_simulations, mr_widgets
6
+
7
+ __all__ = [
8
+ "__version__",
9
+ "data",
10
+ "mr_simulations",
11
+ "mr_widgets",
12
+ "notebook_utils", # lazy: requires the [notebook] extra (marimo)
13
+ ]
14
+
15
+
16
+ def __getattr__(name):
17
+ if name == "notebook_utils":
18
+ from . import notebook_utils as _nu
19
+ return _nu
20
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
@@ -0,0 +1,107 @@
1
+ """
2
+ DartBrains Localizer Dataset Access
3
+ ====================================
4
+
5
+ Helper functions to download and access the Pinel Localizer dataset
6
+ from HuggingFace Hub (dartbrains/localizer).
7
+
8
+ Files are downloaded on first access and cached locally by huggingface_hub.
9
+ """
10
+
11
+ import pandas as pd
12
+ from huggingface_hub import hf_hub_download
13
+
14
+ REPO_ID = "dartbrains/localizer"
15
+
16
+ SUBJECTS = [f"S{i:02d}" for i in range(1, 21)]
17
+
18
+ TR = 2.4 # seconds, from task-localizer_bold.json
19
+
20
+ CONDITIONS = [
21
+ "audio_computation",
22
+ "audio_left_hand",
23
+ "audio_right_hand",
24
+ "audio_sentence",
25
+ "horizontal_checkerboard",
26
+ "vertical_checkerboard",
27
+ "video_computation",
28
+ "video_left_hand",
29
+ "video_right_hand",
30
+ "video_sentence",
31
+ ]
32
+
33
+
34
+ def _download(filename: str) -> str:
35
+ """Download a file from the dartbrains/localizer dataset. Returns local cached path."""
36
+ return hf_hub_download(repo_id=REPO_ID, filename=filename, repo_type="dataset")
37
+
38
+
39
+ def get_subjects() -> list[str]:
40
+ """Return list of subject IDs (S01-S20)."""
41
+ return list(SUBJECTS)
42
+
43
+
44
+ def get_tr() -> float:
45
+ """Return the repetition time in seconds."""
46
+ return TR
47
+
48
+
49
+ def get_file(subject: str, scope: str, suffix: str, extension: str = ".nii.gz") -> str:
50
+ """Download and return the local path to a dataset file.
51
+
52
+ Args:
53
+ subject: Subject ID, e.g. "S01"
54
+ scope: One of "raw", "derivatives", or "betas"
55
+ suffix: BIDS suffix -- "bold", "T1w", "events", "confounds", "mask",
56
+ or a condition name for betas (e.g. "audio_computation"),
57
+ or "all" for the stacked betas file
58
+ extension: File extension including dot, e.g. ".nii.gz", ".tsv"
59
+
60
+ Returns:
61
+ Local filesystem path to the cached file.
62
+ """
63
+ s = subject
64
+ sub = f"sub-{s}"
65
+
66
+ if scope == "betas":
67
+ if suffix == "all":
68
+ filename = f"derivatives/betas/{s}_betas{extension}"
69
+ else:
70
+ filename = f"derivatives/betas/{s}_beta_{suffix}{extension}"
71
+
72
+ elif scope == "raw":
73
+ if suffix == "events":
74
+ filename = f"{sub}/func/{sub}_task-localizer_events.tsv"
75
+ elif suffix == "bold":
76
+ # No raw bold on HF -- use preprocessed
77
+ filename = f"derivatives/fmriprep/{sub}/func/{sub}_task-localizer_space-MNI152NLin2009cAsym_desc-preproc_bold{extension}"
78
+ else:
79
+ raise ValueError(f"Unknown raw suffix: {suffix}")
80
+
81
+ elif scope == "derivatives":
82
+ if suffix == "bold":
83
+ filename = f"derivatives/fmriprep/{sub}/func/{sub}_task-localizer_space-MNI152NLin2009cAsym_desc-preproc_bold{extension}"
84
+ elif suffix == "T1w":
85
+ filename = f"derivatives/fmriprep/{sub}/anat/{sub}_space-MNI152NLin2009cAsym_desc-preproc_T1w{extension}"
86
+ elif suffix == "confounds":
87
+ filename = f"derivatives/fmriprep/{sub}/func/{sub}_task-localizer_desc-confounds_regressors.tsv"
88
+ elif suffix == "mask":
89
+ filename = f"derivatives/fmriprep/{sub}/func/{sub}_task-localizer_space-MNI152NLin2009cAsym_desc-brain_mask{extension}"
90
+ else:
91
+ raise ValueError(f"Unknown derivatives suffix: {suffix}")
92
+ else:
93
+ raise ValueError(f"Unknown scope: {scope}. Use 'raw', 'derivatives', or 'betas'.")
94
+
95
+ return _download(filename)
96
+
97
+
98
+ def load_events(subject: str) -> pd.DataFrame:
99
+ """Download and load the events TSV for a subject as a DataFrame."""
100
+ path = get_file(subject, scope="raw", suffix="events", extension=".tsv")
101
+ return pd.read_csv(path, sep="\t")
102
+
103
+
104
+ def load_confounds(subject: str) -> pd.DataFrame:
105
+ """Download and load the fmriprep confounds TSV for a subject."""
106
+ path = get_file(subject, scope="derivatives", suffix="confounds")
107
+ return pd.read_csv(path, sep="\t")