diff --git a/docs/index.md b/docs/index.md index f621463..b374a8f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -10,7 +10,7 @@ hidden: --- -examples/index +user-guide/index api-reference/index developer/index about/index diff --git a/docs/examples/index.md b/docs/user-guide/index.md similarity index 83% rename from docs/examples/index.md rename to docs/user-guide/index.md index 9c3f900..ad2cca2 100644 --- a/docs/examples/index.md +++ b/docs/user-guide/index.md @@ -1,4 +1,4 @@ -# Examples +# User Guide ```{toctree} --- diff --git a/docs/examples/scaling_workflow.ipynb b/docs/user-guide/scaling_workflow.ipynb similarity index 100% rename from docs/examples/scaling_workflow.ipynb rename to docs/user-guide/scaling_workflow.ipynb diff --git a/docs/examples/workflow.ipynb b/docs/user-guide/workflow.ipynb similarity index 61% rename from docs/examples/workflow.ipynb rename to docs/user-guide/workflow.ipynb index 3edd44a..7a2f954 100644 --- a/docs/examples/workflow.ipynb +++ b/docs/user-guide/workflow.ipynb @@ -23,21 +23,20 @@ "from ess.nmx.data import small_mcstas_3_sample\n", "\n", "from ess.nmx.types import *\n", - "from ess.nmx.reduction import NMXData, NMXReducedData, merge_panels\n", + "from ess.nmx.reduction import NMXReducedData, merge_panels\n", "from ess.nmx.nexus import export_as_nexus\n", "\n", - "wf = McStasWorkflow()\n", - "# Replace with the path to your own file\n", - "wf[FilePath] = small_mcstas_3_sample()\n", - "wf[MaximumProbability] = 10000\n", - "wf[TimeBinSteps] = 50" + "base_wf = McStasWorkflow() # Instantiate the base workflow\n", + "base_wf[FilePath] = small_mcstas_3_sample() # Replace with the path to your own file\n", + "base_wf[MaximumProbability] = 10_000\n", + "base_wf[TimeBinSteps] = 50" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "To see what the workflow can produce, display it:" + "### All Types and Providers of the Workflow" ] }, { @@ -46,13 +45,31 @@ "metadata": {}, "outputs": [], "source": [ - "wf" + "base_wf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ + "### Graph of the Workflow." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "base_wf.visualize(NMXReducedData, graph_attr={\"rankdir\": \"LR\"})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Detector Index Mapping\n", + "\n", "We want to reduce all three panels, so we map the relevant part of the workflow over a list of the three panels:" ] }, @@ -64,18 +81,15 @@ "source": [ "# DetectorIndex selects what detector panels to include in the run\n", "# in this case we select all three panels.\n", - "wf[NMXReducedData] = (\n", - " wf[NMXReducedData]\n", - " .map({DetectorIndex: sc.arange('panel', 3, unit=None)})\n", - " .reduce(index=\"panel\", func=merge_panels)\n", - ")" + "detector_panel_ids = {DetectorIndex: sc.arange('panel', 3, unit=None)}\n", + "pipeline = base_wf.map(detector_panel_ids)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Build Workflow" + "### Visualize Mapped Graph" ] }, { @@ -84,14 +98,20 @@ "metadata": {}, "outputs": [], "source": [ - "wf.visualize(NMXReducedData, graph_attr={\"rankdir\": \"TD\"}, compact=True)" + "import sciline as sl\n", + "\n", + "pipeline.visualize(\n", + " sl.get_mapped_node_names(pipeline, NMXReducedData),\n", + " graph_attr={\"rankdir\": \"LR\"},\n", + " compact=True,\n", + ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Compute Desired Types" + "### Reduce Mapped Results" ] }, { @@ -100,12 +120,18 @@ "metadata": {}, "outputs": [], "source": [ - "from cyclebane.graph import NodeName, IndexValues\n", + "from typing import NewType\n", "\n", - "# Event data grouped by pixel id for each of the selected detectors\n", - "targets = [NodeName(NMXData, IndexValues((\"panel\",), (i,))) for i in range(3)]\n", - "dg = merge_panels(*wf.compute(targets).values())\n", - "dg" + "MergedNMXReducedData = NewType('MergedNMXReducedData', sc.DataGroup)\n", + "graph = pipeline.reduce(func=merge_panels, name=MergedNMXReducedData)\n", + "graph.get(MergedNMXReducedData).visualize(compact=True, graph_attr={\"rankdir\": \"LR\"})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Compute Desired Types" ] }, { @@ -114,8 +140,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Data from all selected detectors binned by panel, pixel and timeslice\n", - "binned_dg = wf.compute(NMXReducedData)\n", + "binned_dg = graph.compute(MergedNMXReducedData)\n", "binned_dg" ] }, @@ -139,6 +164,27 @@ "export_as_nexus(binned_dg, \"test.nxs\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Merge Intermediate Mapped Nodes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from ess.nmx.reduction import NMXData\n", + "\n", + "dg = merge_panels(\n", + " *pipeline.compute(sl.get_mapped_node_names(pipeline, NMXData)).values()\n", + ") # We need ``NMXData`` to use instrument view\n", + "dg" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -162,17 +208,17 @@ "source": [ "import scippneutron as scn\n", "\n", - "da = dg[\"weights\"]\n", - "da.coords[\"position\"] = dg[\"position\"]\n", + "da = binned_dg['counts'].sum('t')\n", + "da.coords[\"position\"] = binned_dg[\"position\"]\n", "# Plot one out of 100 pixels to reduce size of docs output\n", - "view = scn.instrument_view(da[\"id\", ::100].hist(), pixel_size=0.0075)\n", + "view = scn.instrument_view(da[\"id\", ::100], pixel_size=0.0075)\n", "view" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "nmx-dev-310", "language": "python", "name": "python3" }, @@ -186,7 +232,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.13" } }, "nbformat": 4,