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

Dream instrument view #184

Merged
merged 36 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a983ef7
add initial tools functions
nvaytet Jun 6, 2023
0709ace
add tests
nvaytet Jun 6, 2023
595f6a4
add initial instrument view
nvaytet Jun 8, 2023
31e621a
add initial notebook
nvaytet Jun 8, 2023
cc054d3
update notebook and instrument_view
nvaytet Jun 12, 2023
f90bbc8
add pooch data registry for dream
nvaytet Jun 12, 2023
8cbbf28
update amor data registry
nvaytet Jun 12, 2023
3e8d203
update loki data registry
nvaytet Jun 12, 2023
e8a3da1
go back to old function naming get_path
nvaytet Jun 12, 2023
914ce18
fix notebook syntax
nvaytet Jun 12, 2023
c0a1716
generalize instrument view
nvaytet Jun 28, 2023
fb97651
Merge branch 'main' into dream_diagnostics
nvaytet Aug 21, 2023
0be2770
improve instrument view
nvaytet Sep 5, 2023
7929cf4
we know which dimensions to flatten
nvaytet Sep 13, 2023
12307fa
remove diagnostics
nvaytet Sep 13, 2023
86d23aa
flake8
nvaytet Sep 13, 2023
117d62b
formatting
nvaytet Sep 14, 2023
2e4672f
update to new mechanism
nvaytet Nov 14, 2023
72aea28
add notebook to docs
nvaytet Nov 14, 2023
52523dc
unused imports
nvaytet Nov 14, 2023
e190692
cleanup
nvaytet Nov 14, 2023
c13823a
Merge branch 'main' into dream_diagnostics
nvaytet Nov 14, 2023
c0828bc
need plopp23.10
nvaytet Nov 14, 2023
a08857c
debug prints
nvaytet Nov 14, 2023
a7954bf
do not use the same instance of Unzip
nvaytet Nov 16, 2023
7b5fc25
use index slicing instead of range slicing for much better performance
nvaytet Nov 17, 2023
e63d39b
update notebook
nvaytet Nov 17, 2023
695b712
fix load_geant4 to work with other file
nvaytet Nov 20, 2023
88e876a
fix bug in geant4 loader with missing sumo dimension in end caps
nvaytet Nov 21, 2023
de11d39
use geant4 loader in notebook
nvaytet Nov 21, 2023
9a8b218
add sentence about using other dims for slider
nvaytet Nov 21, 2023
7797731
typehints and docstrings
nvaytet Nov 21, 2023
cd8876a
fix unit tests
nvaytet Nov 21, 2023
794b8e0
update notebook
nvaytet Nov 21, 2023
57065fa
Apply automatic formatting
nvaytet Nov 21, 2023
e3c9784
black notebook
nvaytet Nov 21, 2023
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
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ This involves a "filtering" process, since scope and contribution guidelines are
:caption: Instruments

instruments/amor/amor
instruments/dream/dream
instruments/loki/loki
instruments/external/external

Expand Down
254 changes: 254 additions & 0 deletions docs/instruments/dream/dream-instrument-view.ipynb
Copy link
Collaborator

@celinedurniak celinedurniak Nov 17, 2023

Choose a reason for hiding this comment

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

The instrument view works correctly. But instead of displaying the SUMOs, I think that for consistency, the 4 tick boxes for the SUMOs should be replaced by 2 tick boxes for the endcap backward and forward detectors.
Note that for the endcap detectors, SUMOs are numbered starting from 3.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right now, I am just using the keys in the data group that is supplied as input, to keep it flexible.
If you want to replace the sumos with end-caps, it's probably at the data loading that we should do that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, using dream.load_geant4_csv should make this go away.

Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4852fe41-3f9a-4cdb-8aba-ff7c7f198e6c",
"metadata": {},
"source": [
"# DREAM instrument view\n",
"\n",
"This notebook is a simple example on how to use the instrument view for the DREAM instrument.\n",
"\n",
"- The DREAM-specific instrument view is capable of slicing the data with a slider widget along a dimension (e.g. `tof`) by using the `dim` argument.\n",
"- There are also checkboxes to hide/show the different elements that make up the DREAM detectors."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f416595-83b4-44d1-b506-9ba73bf0786e",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import scipp as sc\n",
"from ess import dream"
]
},
{
"cell_type": "markdown",
"id": "228a23dc-17f2-4273-a4c9-3e3276db8c54",
"metadata": {},
"source": [
"## Load the data\n",
"\n",
"We load a data set from a Geant4 simulation (stored as a `.csv` file).\n",
"In each detector bank, the data is organised by `wire`, `strip`, `module`, `segment`, and `counter`.\n",
"The end caps also have an additional `sumo` dimension."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ef8f35b-815f-436a-80e5-cbe4b3172b58",
"metadata": {},
"outputs": [],
"source": [
"dg = dream.io.load_geant4_csv(\n",
" dream.data.get_path('data_dream_HF_mil_closed_alldets_1e9.csv.zip')\n",
")\n",
"dg = dg['instrument'] # Extract the instrument data\n",
"\n",
"# Construct the pixel positions from event positions\n",
"for da in dg.values():\n",
" da.coords['position'] = da.bins.coords['position'].bins.mean()\n",
"\n",
"dg"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8325797-5651-43c1-b601-3db6d4348758",
"metadata": {
"editable": true,
"nbsphinx": "hidden",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"# Only plot half of the pixels to reduce html docs size\n",
"dg = dg['counter', 0]"
]
},
{
"cell_type": "markdown",
"id": "49595486-ab4e-4662-86f7-d301aedcf974",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"## Full instrument view\n",
"\n",
"We first histogram the data along the time-of-flight (`tof`) dimension,\n",
"making sure the same bins are used for all elements:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e85a8364-e0a1-4c10-8cae-c873f297e651",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"tof_edges = sc.linspace('tof', 1.0e7, 1.0e8, 51, unit='ns', dtype=int)\n",
"data = dg.hist(tof=tof_edges)"
]
},
{
"cell_type": "markdown",
"id": "d08ca911-b1a4-4f17-ba1e-355971531ffe",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"We now use the `instrument_view` function to show the 3D view of the instrument pixels,\n",
"specifying that we wish to have a slider along the `tof` dimension:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "43f9ffbc-6bf5-4407-b3ad-5d1626efc43d",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"full_view = dream.instrument_view(data, dim='tof')\n",
Copy link
Member

Choose a reason for hiding this comment

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

Is the intention here that one could pass dim='wavelength' after transforming the coords? Might be worth mentioning here, since it may not be obvious to readers.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a sentence.

"full_view"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a7030d56-a375-47b5-898c-28fd06c2f361",
"metadata": {
"editable": true,
"nbsphinx": "hidden",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"full_view[2].controls['tof']['slider'].value = 35"
]
},
{
"cell_type": "markdown",
"id": "c0b29ebf-21ff-4385-bf8b-0e4fa14dfaf9",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Note that it is possible to use any dimension for the slider instead of `tof`, such as `wavelength` (if present in the data).\n",
"\n",
"## Displaying individual detector elements\n",
"\n",
"It is also possible to view a single detector element, selecting e.g. `mantle` from the original data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b417011e-0d12-4287-91d5-c1fb6ecc7cac",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"mantle_view = dream.instrument_view(dg['mantle'].hist(wavelength=50), dim='wavelength')\n",
"mantle_view"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "299ec404-fb18-4533-ad96-e23bf8ba24d6",
"metadata": {
"editable": true,
"nbsphinx": "hidden",
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"mantle_view[1].controls['wavelength']['slider'].value = 43"
]
},
{
"cell_type": "markdown",
"id": "1df1aa56-5251-4555-a4f4-283145747198",
"metadata": {},
"source": [
"The instrument view is designed to be flexible in terms of what it accepts as input.\n",
"This means that you can easily inspect, for example, a single module by using the usual slicing notation for data arrays.\n",
"\n",
"Below, we display the first module in the backward end-cap detector:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c86e491-3564-498d-9232-39485f5b95d7",
"metadata": {},
"outputs": [],
"source": [
"dream.instrument_view(dg['endcap_backward']['module', 0].hist(tof=1))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
7 changes: 7 additions & 0 deletions docs/instruments/dream/dream.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DREAM
=====

.. toctree::
:maxdepth: 4

dream-instrument-view
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies:
- pandas=2.0.3
- pandoc=3.1.3
- pip=23.2.1
- plopp=23.09.0
- plopp=23.10.1
- pooch=1.7.0
- pytest=7.4.0
- python=3.8.*
Expand Down
2 changes: 1 addition & 1 deletion src/ess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
except importlib.metadata.PackageNotFoundError:
__version__ = "0.0.0"

from . import logging
from . import data, logging
from .logging import get_logger
37 changes: 10 additions & 27 deletions src/ess/amor/data.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
_version = '1'

__all__ = ['get_path']

from ..data import Registry

def _make_pooch():
import pooch
_registry = Registry(
instrument='amor',
files={
"reference.nxs": "md5:56d493c8051e1c5c86fb7a95f8ec643b",
"sample.nxs": "md5:4e07ccc87b5c6549e190bc372c298e83",
},
version='1',
)

return pooch.create(
path=pooch.os_cache('ess/amor'),
env='ESS_AMOR_DATA_DIR',
base_url='https://public.esss.dk/groups/scipp/ess/amor/{version}/',
version=_version,
registry={
"reference.nxs": "md5:56d493c8051e1c5c86fb7a95f8ec643b",
"sample.nxs": "md5:4e07ccc87b5c6549e190bc372c298e83",
},
Comment on lines +5 to -19
Copy link
Member

Choose a reason for hiding this comment

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

This code has been moved already, the refactor will get lost (but that is ok, I guess).

)


_pooch = _make_pooch()


def get_path(name: str) -> str:
"""
Return the path to a data file bundled with scippneutron.

This function only works with example data and cannot handle
paths to custom files.
"""
return _pooch.fetch(name)
get_path = _registry.get_path
35 changes: 35 additions & 0 deletions src/ess/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)

from typing import Dict

__all__ = ['Registry']


class Registry:
def __init__(self, instrument: str, files: Dict[str, str], version: str):
import pooch

self._registry = pooch.create(
path=pooch.os_cache(f'ess/{instrument}'),
env=f'ESS_{instrument.upper()}_DATA_DIR',
base_url=f'https://public.esss.dk/groups/scipp/ess/{instrument}/'
+ '{version}/',
version=version,
registry=files,
)

def get_path(self, name: str, unzip: bool = False) -> str:
"""
Get the path to a file in the registry.

Parameters
----------
name:
Name of the file to get the path for.
unzip:
If `True`, unzip the file before returning the path.
"""
import pooch

return self._registry.fetch(name, processor=pooch.Unzip() if unzip else None)
3 changes: 2 additions & 1 deletion src/ess/dream/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)

from . import data
from .instrument_view import instrument_view
from .io import load_geant4_csv

__all__ = ['data', 'load_geant4_csv']
__all__ = ['data', 'instrument_view', 'load_geant4_csv']
Loading