Skip to content

Commit 055005b

Browse files
committed
feat: save WIP on tutorials
fix: swap from info to tips fix: add to index fix: add to index
1 parent 80df37e commit 055005b

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
:hidden:
55

66
user_guide
7+
tutorials
78
developer_guide
89
api/index
910

docs/tutorials.rst

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
:html_theme.sidebar_secondary.remove:
2+
3+
.. _tutorials:
4+
5+
Tutorials
6+
=========
7+
8+
**nwb2bids** comes pre-packaged with some demonstrative tools to help showcase its functionality on various types of
9+
NWB file contents. No additional requirements, knowledge, or experience are necessary to run these tutorials!
10+
11+
12+
13+
Tutorial 1 - Converting a single file
14+
-------------------------------------
15+
16+
In case you don't have any NWB files handy, or perhaps you've never worked with NWB files before, you can generate a
17+
toy example through the command line interface (CLI) as follows:
18+
19+
.. code-block:: bash
20+
21+
nwb2bids tutorial generate ephys
22+
23+
This created an NWB file with contents typical of an extracellular electrophysiology experiment in your home
24+
directory for **nwb2bids**: ``~/.nwb2bids/tutorials/ephys/nwb2bids_tutorial_ephys.nwb``.
25+
26+
.. note::
27+
28+
Don't like overwhelming your home directory with lots of small files?
29+
30+
You can control where tutorial data is generated by using the ``--output-directory`` flag (or ``-o`` for short):
31+
32+
.. code-block:: bash
33+
34+
nwb2bids tutorial generate ephys --output-directory path/to/some/directory
35+
36+
NWB files like this contain a lot of metadata about the probes and electrode structure, which will be useful later
37+
when we compare the source file to the sidecar tables found in BIDS. You can explore these file contents through
38+
`neurosift.app <neurosift.app>`_ by following this link: TODO
39+
40+
.. tip::
41+
42+
The link above actually points to a similar file published on the DANDI Archive, which should be identical to
43+
what what written on your system. You can explore and visualize your local file contents and structure
44+
in many other ways, such as the
45+
`NWB GUIDE <https://nwb-guide.readthedocs.io/en/stable/installation.html>`_,
46+
`HDFView <https://www.hdfgroup.org/download-hdfview/>`_, or the
47+
`PyNWB library <https://pynwb.readthedocs.io/en/stable/tutorials/general/plot_read_basics.html#reading-and-exploring-an-nwb-file>`_.
48+
49+
Now that we have an NWB file, we can convert it to BIDS using the following command:
50+
51+
.. code-block:: bash
52+
53+
nwb2bids convert ~/.nwb2bids/tutorials/ephys/nwb2bids_tutorial_ephys.nwb --bids-directory ~/.nwb2bids/tutorials/ephys/bids_dataset
54+
55+
Notice how we explicitly specified the output BIDS directory using the ``--bids-directory`` flag. We will cover the
56+
implicit (current working directory) approach in TODO: crossref to later tutorial.
57+
58+
59+
Tutorial 2 - Converting multiple files
60+
--------------------------------------
61+
62+
63+
64+
.. tip::
65+
66+
Lost in the CLI? Can't remember the exact phrase to type when navigating through the command groups? No worries!
67+
Simply add the ``--help`` to any command to see detailed descriptions, expected inputs, and optional flags.
68+
69+
You can also see a layout of the command structure by calling each level at a time, for example ``nwb2bids``,
70+
``nwb2bids tutorial``, and so on.
71+
72+
Try it out on ``nwb2bids tutorial generate`` to see all the types of tutorial data we can generate for you.
73+
74+
This command assumes you are located within either an empty directory or a BIDS dataset, informing you accordingly if
75+
these conditions are not met.
76+
77+
To specify a specific location other than your current working directory, use the ``--bids-directory``
78+
flag (or ``-o`` for short):
79+
80+
.. code-block:: bash
81+
82+
nwb2bids convert [path to NWB files] --bids-directory [path to BIDS directory]
83+
84+
The main input to the interface can be any combination of directories containing NWB files or individual NWB files:
85+
86+
.. code-block:: bash
87+
88+
nwb2bids convert path/to/many/nwb single.nwb another.nwb
89+
90+
91+
92+
Application Programming Interface (API)
93+
---------------------------------------
94+
95+
A more advanced usage of **nwb2bids** is through its Python library. A thorough description of all publicly exposed
96+
functions and classes can be found in the :ref:`API <api>` section of the documentation.
97+
98+
The core function is the ``convert_nwb_to_bids`` function, which can be used as follows:
99+
100+
.. code-block:: python
101+
102+
import nwb2bids
103+
104+
nwb2bids.convert_nwb_to_bids(
105+
nwb_paths=["path/to/many/nwb", "single.nwb", "another.nwb"],
106+
bids_directory="path/to/bids/directory" # Optional, defaults to current working directory
107+
)
108+
109+
Essentially, this function is direct wrapper used by the CLI and behaves in the same way.
110+
111+
One key programmatic difference however is the ability to interact with the notifications returned by the operation:
112+
113+
.. code-block:: python
114+
115+
import nwb2bids
116+
117+
notifications = nwb2bids.convert_nwb_to_bids(
118+
nwb_paths=["path/to/many/nwb", "single.nwb", "another.nwb"],
119+
bids_directory="path/to/bids/directory"
120+
)
121+
122+
for notification in notifications:
123+
print(notification)
124+
125+
More examples to follow soon...

src/nwb2bids/_command_line_interface/_main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import rich_click
55

66
from .._core._convert_nwb_dataset import convert_nwb_dataset
7+
from ..testing import generate_ephys_tutorial
78

89

910
# nwb2bids
@@ -79,3 +80,27 @@ def _run_convert_nwb_dataset(
7980
)
8081
console_notification = rich_click.style(text=text, fg="yellow")
8182
rich_click.echo(message=console_notification)
83+
84+
85+
# nwb2bids tutorial
86+
@_nwb2bids_cli.group(name="tutorial")
87+
def _nwb2bids_tutorial_cli():
88+
pass
89+
90+
91+
# nwb2bids tutorial ephys
92+
@_nwb2bids_tutorial_cli.command(name="ephys")
93+
@rich_click.option(
94+
"--output-directory",
95+
"-o",
96+
help="Path to the folder where the tutorial files will be created (default: user home directory).",
97+
required=False,
98+
type=rich_click.Path(writable=True),
99+
default=None,
100+
)
101+
def _nwb2bids_tutorial_ephys_cli(output_directory: str | None = None) -> None:
102+
file_path = generate_ephys_tutorial(output_directory=output_directory)
103+
104+
text = f"Example single session NWB file generated successfully at {file_path}."
105+
message = rich_click.style(text=text, fg="green")
106+
rich_click.echo(message=message)

src/nwb2bids/testing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from ._assert_subdirectory_structure import assert_subdirectory_structure
22
from ._mocks._mock_neurodata_objects import mock_time_intervals, mock_epochs_table, mock_trials_table
3+
from ._mocks._mock_files import generate_ephys_tutorial
34

45
__all__ = [
56
"assert_subdirectory_structure",
7+
"generate_ephys_tutorial",
68
"mock_epochs_table",
79
"mock_time_intervals",
810
"mock_trials_table",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pathlib
2+
3+
import pydantic
4+
import pynwb
5+
import pynwb.testing.mock.ecephys
6+
import pynwb.testing.mock.file
7+
8+
9+
@pydantic.validate_call
10+
def generate_ephys_tutorial(output_directory: pydantic.DirectoryPath | None) -> pathlib.Path:
11+
if output_directory is None:
12+
# TODO: update to common home/config utility
13+
tutorial_dir = pathlib.Path.home() / ".nwb2bids" / "tutorials"
14+
tutorial_dir.mkdir(parents=True, exist_ok=True)
15+
output_directory = tutorial_dir / "ephys"
16+
output_directory.mkdir(exist_ok=True)
17+
18+
nwbfile = pynwb.testing.mock.file.mock_NWBFile(
19+
session_description="An example NWB file containing ephys neurodata types - for use in the nwb2bids tutorials."
20+
)
21+
22+
electrical_series = pynwb.testing.mock.ecephys.mock_ElectricalSeries(
23+
name="ExampleElectricalSeries",
24+
description=(
25+
"An example electrical series that represents data which could have been "
26+
"read off of the channels of an ephys probe."
27+
),
28+
nwbfile=nwbfile,
29+
)
30+
nwbfile.add_acquisition(electrical_series)
31+
32+
nwbfile_path = output_directory / "ephys.nwb"
33+
with pynwb.NWBHDF5IO(name=nwbfile_path, mode="w") as file_stream:
34+
file_stream.write(nwbfile)
35+
36+
return nwbfile_path

0 commit comments

Comments
 (0)