Skip to content

Commit

Permalink
Merge pull request #67 from scipp/importable-mantidio
Browse files Browse the repository at this point in the history
Importable mantidio
  • Loading branch information
jl-wynen committed Feb 1, 2024
2 parents 7d4847d + 9fad2f7 commit b912f2f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
14 changes: 8 additions & 6 deletions src/esssans/isis/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ def read_xml_detector_masking(
The format is as follows, where the detids are inclusive ranges of detector IDs:
<?xml version="1.0"?>
<detector-masking>
<group>
<detids>1400203-1400218,1401199,1402190-1402223</detids>
</group>
</detector-masking>
.. code-block:: xml
<?xml version="1.0"?>
<detector-masking>
<group>
<detids>1400203-1400218,1401199,1402190-1402223</detids>
</group>
</detector-masking>
Parameters
----------
Expand Down
32 changes: 25 additions & 7 deletions src/esssans/isis/mantidio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
"""
File loading functions for ISIS data using Mantid.
"""
from typing import NewType
from typing import NewType, NoReturn

import sciline
import scipp as sc
import scippneutron as scn
from mantid.api import MatrixWorkspace, Workspace
from mantid.simpleapi import CopyInstrumentParameters, Load
from scipp.constants import g

from ..types import (
Expand All @@ -21,6 +19,24 @@
)
from .io import CalibrationFilename, Filename, FilePath

try:
import mantid.api as _mantid_api
import mantid.simpleapi as _mantid_simpleapi
from mantid.api import MatrixWorkspace
except ModuleNotFoundError:
# Catch runtime usages of Mantid
class _MantidFallback:
def __getattr__(self, name: str) -> NoReturn:
raise ImportError(
'Mantid is required to use `sans.isis.mantidio` ' 'but is not installed'
) from None

_mantid_api = _MantidFallback()
_mantid_simpleapi = _MantidFallback
# Needed for type annotations
MatrixWorkspace = object


CalibrationWorkspace = NewType('CalibrationWorkspace', MatrixWorkspace)


Expand All @@ -47,7 +63,7 @@ def get_detector_ids(ws: DataWorkspace[SampleRun]) -> sc.Variable:


def load_calibration(filename: FilePath[CalibrationFilename]) -> CalibrationWorkspace:
ws = Load(Filename=str(filename), StoreInADS=False)
ws = _mantid_simpleapi.Load(Filename=str(filename), StoreInADS=False)
return CalibrationWorkspace(ws)


Expand All @@ -66,7 +82,7 @@ def from_data_workspace(
ws: DataWorkspace[RunType],
calibration: CalibrationWorkspace,
) -> LoadedFileContents[RunType]:
CopyInstrumentParameters(
_mantid_simpleapi.CopyInstrumentParameters(
InputWorkspace=calibration, OutputWorkspace=ws, StoreInADS=False
)
up = ws.getInstrument().getReferenceFrame().vecPointingUp()
Expand All @@ -78,8 +94,10 @@ def from_data_workspace(


def load_run(filename: FilePath[Filename[RunType]]) -> DataWorkspace[RunType]:
loaded = Load(Filename=str(filename), LoadMonitors=True, StoreInADS=False)
if isinstance(loaded, Workspace):
loaded = _mantid_simpleapi.Load(
Filename=str(filename), LoadMonitors=True, StoreInADS=False
)
if isinstance(loaded, _mantid_api.Workspace):
# A single workspace
data_ws = loaded
else:
Expand Down

0 comments on commit b912f2f

Please sign in to comment.