-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from spacetelescope/feature/delta-example-sim…
…ulator Add example optical model and example simulator
- Loading branch information
Showing
5 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pupil_mask: | ||
diameter: 1 | ||
grid_size: 1.1 | ||
|
||
detector: | ||
pixel_size: 6.5e-6 | ||
roi: 400 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from catkit2.simulator import OpticalModel, with_cached_result | ||
import hcipy | ||
import numpy as np | ||
|
||
|
||
class ExampleOpticalModel(OpticalModel): | ||
"""An example optical model for use with the example testbed. | ||
This class simulates a simple pupil mask and simple science camera. All parameters are read from | ||
the simulator configuration file, testbed_example/config/simulator.yml. | ||
""" | ||
def __init__(self, config): | ||
super().__init__() | ||
|
||
self.config = config | ||
|
||
@self.register_plane('detector', 'pupil') | ||
def detector(wf): | ||
return self.prop(wf) | ||
|
||
@self.register_plane('pupil', 'light_source') | ||
def pupil(wf): | ||
return self.pupil_mask(wf) | ||
|
||
@property | ||
def pupil_grid(self): | ||
dimensions = self.config['pupil_mask']['dimensions'] | ||
dims = np.array([dimensions, dimensions]) | ||
size = self.config['pupil_mask']['grid_size'] | ||
|
||
return hcipy.make_uniform_grid(dims, size) | ||
|
||
@property | ||
def detector_grid(self): | ||
roi = self.config['detector']['roi'] | ||
dims = np.array([roi, roi]) | ||
pixel_size = self.config['detector']['pixel_size'] | ||
|
||
return hcipy.make_uniform_grid(dims, dims * pixel_size) | ||
|
||
@property | ||
@with_cached_result | ||
def prop(self): | ||
return hcipy.FraunhoferPropagator(self.pupil_grid, self.detector_grid) | ||
|
||
@property | ||
@with_cached_result | ||
def pupil_mask(self): | ||
diameter = self.config['pupil_mask']['diameter'] | ||
mask = hcipy.circular_aperture(diameter)(self.pupil_grid) | ||
|
||
return hcipy.Apodizer(mask) |
40 changes: 40 additions & 0 deletions
40
testbed_example/services/example_simulator/example_simulator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from catkit2.simulator import Simulator | ||
from testbed_example.example_optical_model import ExampleOpticalModel | ||
import hcipy | ||
|
||
|
||
class ExampleSimulator(Simulator): | ||
"""A very simple example simulator for the example testbed. | ||
This service provides the simulator interface to the connected optical model. | ||
""" | ||
def __init__(self): | ||
super().__init__('example_simulator') | ||
|
||
def open(self): | ||
self.model = ExampleOpticalModel() | ||
wavefronts = [hcipy.Wavefront(self.model.pupil_grid.ones() * 1e6)] | ||
self.model.set_wavefronts('pre_pupil', wavefronts) | ||
|
||
self.images = self.make_data_stream('images', 'float64', self.model.focal_grid.shape, 20) | ||
|
||
def camera_readout(self, camera_name, power): | ||
image = power.shaped | ||
image = hcipy.large_poisson(image) | ||
image[image > 2**16] = 2**16 | ||
image = image.astype('float32') | ||
|
||
try: | ||
self.testbed.detector.images.update_parameters('float32', image.shape, 20) | ||
self.testbed.detector.images.submit_data(image) | ||
except Exception as e: | ||
self.log.error(str(e)) | ||
|
||
def get_camera_power(self, camera_name): | ||
wavefronts = self.model.get_wavefronts(camera_name) | ||
return sum(wf.power for wf in wavefronts) | ||
|
||
|
||
if __name__ == '__main__': | ||
service = ExampleSimulator() | ||
service.run() |