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

Added ctis.instruments.Instrument class. #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions ctis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
A package for inverting imagery captured by a computed tomography imaging
spectrograph.
"""

from . import instruments

__all__ = [
"instruments",
]
10 changes: 10 additions & 0 deletions ctis/instruments/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
Models of CTIS instruments used during inversions.
"""

from ._instruments import AbstractInstrument, Instrument

__all__ = [
"AbstractInstrument",
"Instrument",
]
85 changes: 85 additions & 0 deletions ctis/instruments/_instruments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from typing import Callable
import abc
import dataclasses
import named_arrays as na

__all__ = [
"AbstractInstrument",
"Instrument",
]


ProjectionCallable = Callable[
[na.FunctionArray[na.SpectralPositionalVectorArray, na.ScalarArray]],
na.FunctionArray[na.SpectralPositionalVectorArray, na.ScalarArray],
]


@dataclasses.dataclass
class AbstractInstrument(
abc.ABC,
):
"""
An interface describing a CTIS instrument.

This consists of a forward model
(which maps the spectral radiance of a physical scene to counts on a detector)
and a deprojection model
(which maps detector counts to the spectral radiance of a physical scene).
"""

@abc.abstractmethod
def project(
self,
scene: na.FunctionArray[na.SpectralPositionalVectorArray, na.AbstractScalar],
) -> na.FunctionArray[na.SpectralPositionalVectorArray, na.AbstractScalar]:
"""
The forward model of the CTIS instrument.
Maps spectral and spatial coordinates on the field to coordinates
on the detector.

Parameters
----------
scene
The spectral radiance of each spatial/spectral point in the scene.
"""

@abc.abstractmethod
def deproject(
self,
projections: na.FunctionArray[na.SpectralPositionalVectorArray, na.AbstractScalar],
) -> ProjectionCallable:
"""
The deprojection model of the CTIS instrument.
Maps spectral and spatial coordinates on the detector to coordinates
on the field.

Parameters
----------
projections
The counts gathered by each detector in the CTIS instrument.
"""


@dataclasses.dataclass
class Instrument(
AbstractInstrument,
):
"""
A CTIS instrument where the forward and deprojection models are explicitly
provided.
"""

project: ProjectionCallable = dataclasses.MISSING
"""
The forward model of the CTIS instrument.
Maps spectral and spatial coordinates on the field to coordinates
on the detector.
"""

deproject: ProjectionCallable = dataclasses.MISSING
"""
The deprojection model of the CTIS instrument.
Maps spectral and spatial coordinates on the detector to coordinates
on the field.
"""
30 changes: 30 additions & 0 deletions ctis/instruments/_instruments_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
import abc
import ctis


class AbstractTestAbstractInstrument(
abc.ABC,
):
def test_project(self, a: ctis.instruments.AbstractInstrument):
result = a.project
assert hasattr(result, "__call__")

def test_deproject(self, a: ctis.instruments.AbstractInstrument):
result = a.deproject
assert hasattr(result, "__call__")


@pytest.mark.parametrize(
argnames="a",
argvalues=[
ctis.instruments.Instrument(
project=lambda x: x,
deproject=lambda x: x,
)
],
)
class TestInstrument(
AbstractTestAbstractInstrument,
):
pass
Loading