1+ import asdf
12import numpy as np
23import pytest
34
910)
1011from wfi_reference_pipeline .reference_types .readnoise .readnoise import ReadNoise
1112from wfi_reference_pipeline .resources .make_test_meta import MakeTestMeta
13+ from wfi_reference_pipeline .utilities .simulate_reads import simulate_dark_reads
1214
15+ # Set smaller test data size
16+ TEST_DETECTOR_PIXEL_COUNT = 32
1317
14- @pytest .fixture
18+
19+ @pytest .fixture (scope = "module" )
20+ def simulated_reads_filelist (tmp_path_factory , ref_type_data_factory ):
21+
22+ data_path = tmp_path_factory .mktemp ("data" )
23+
24+ file_list = []
25+
26+ for i in range (1 , 4 ):
27+ cube_data = ref_type_data_factory (i )
28+
29+ curr_path = data_path / f"data_num_{ i } .asdf"
30+ curr_path .parent .mkdir (parents = True , exist_ok = True )
31+
32+ tree = {
33+ "roman" : {
34+ "data" : cube_data
35+ }
36+ }
37+
38+ af = asdf .AsdfFile (tree )
39+
40+ af .write_to (curr_path )
41+
42+ file_list .append (str (curr_path ))
43+
44+ # Return the file list
45+ return file_list
46+
47+
48+ @pytest .fixture (scope = "module" )
1549def valid_meta_data ():
1650 """Fixture for generating valid meta_data for ReadNoise class."""
1751 test_meta = MakeTestMeta (ref_type = REF_TYPE_READNOISE )
@@ -24,27 +58,39 @@ def valid_ref_type_data_array():
2458 return np .random .random ((DETECTOR_PIXEL_X_COUNT , DETECTOR_PIXEL_Y_COUNT )) # Simulate a valid read noise image
2559
2660
27- @pytest .fixture
28- def valid_ref_type_data_cube ():
29- """Fixture for generating valid ref_type_data cube (read noise cube)."""
30- return np .random .random ((3 , DETECTOR_PIXEL_X_COUNT , DETECTOR_PIXEL_Y_COUNT )) # Simulate a valid read noise image
61+ # NOTE: This factory makes a new read each call
62+ # We can introduce a _cache dictionary to store in the future if generating becomes too expensive
63+ # I'm currently not concerned as long as we use the TEST_DETECTOR_PIXEL_COUNT to keep data small
64+ @pytest .fixture (scope = "session" )
65+ def ref_type_data_factory ():
66+ """Factory fixture for generating valid ref_type_data cubes with N reads."""
67+ def _make_cube (num_reads ):
68+ cube_data , _ = simulate_dark_reads (num_reads , ni = TEST_DETECTOR_PIXEL_COUNT )
69+ return cube_data
70+
71+ return _make_cube
3172
3273
3374@pytest .fixture
3475def readnoise_object_with_data_array (valid_meta_data , valid_ref_type_data_array ):
3576 """Fixture for initializing a ReadNoise object with a valid data array."""
3677 readnoise_object_with_data_array = ReadNoise (meta_data = valid_meta_data ,
3778 ref_type_data = valid_ref_type_data_array )
38- yield readnoise_object_with_data_array
79+ return readnoise_object_with_data_array
3980
4081
4182@pytest .fixture
42- def readnoise_object_with_data_cube (valid_meta_data , valid_ref_type_data_cube ):
83+ def readnoise_object_with_data_cube (valid_meta_data , ref_type_data_factory ):
4384 """Fixture for initializing a ReadNoise object with a valid data cube."""
85+ ref_type_data = ref_type_data_factory (3 )
4486 readnoise_object_with_data_cube = ReadNoise (meta_data = valid_meta_data ,
45- ref_type_data = valid_ref_type_data_cube )
46- yield readnoise_object_with_data_cube
87+ ref_type_data = ref_type_data )
88+ return readnoise_object_with_data_cube
4789
90+ @pytest .fixture
91+ def readnoise_object_with_file_list (valid_meta_data , simulated_reads_filelist ):
92+ readnoise_object_with_file_list_obj = ReadNoise (meta_data = valid_meta_data , file_list = simulated_reads_filelist )
93+ return readnoise_object_with_file_list_obj
4894
4995class TestReadNoise :
5096
@@ -137,3 +183,65 @@ def test_readnoise_outfile_default(self, readnoise_object_with_data_array):
137183 """
138184 assert readnoise_object_with_data_array .outfile == "roman_readnoise.asdf"
139185
186+
187+ def test_make_readnoise_image_sets_correct_shape_pass (readnoise_object_with_file_list ):
188+
189+ readnoise_object_with_file_list .make_readnoise_image ()
190+
191+ assert readnoise_object_with_file_list .readnoise_image is not None
192+ assert readnoise_object_with_file_list .readnoise_image .shape == (TEST_DETECTOR_PIXEL_COUNT , TEST_DETECTOR_PIXEL_COUNT )
193+
194+
195+
196+ def test_select_data_cube_sets_correct_num_reads_pass (readnoise_object_with_file_list ):
197+
198+ # Check datacube doesn't exist
199+ with pytest .raises (AttributeError ):
200+ _ = readnoise_object_with_file_list .data_cube
201+
202+ readnoise_object_with_file_list ._select_data_cube_from_file_list ()
203+
204+ assert readnoise_object_with_file_list .data_cube is not None
205+ assert readnoise_object_with_file_list .data_cube .num_reads == 3
206+
207+ def test_make_rate_image_updates_dimensions_pass (readnoise_object_with_data_cube ):
208+
209+ readnoise_object_with_data_cube .make_rate_image_from_data_cube ()
210+
211+ assert readnoise_object_with_data_cube .data_cube .rate_image .shape == (TEST_DETECTOR_PIXEL_COUNT , TEST_DETECTOR_PIXEL_COUNT )
212+ assert readnoise_object_with_data_cube .data_cube .intercept_image .shape == (TEST_DETECTOR_PIXEL_COUNT , TEST_DETECTOR_PIXEL_COUNT )
213+
214+ def test_comp_ramp_res_var_output_matches_pixel_count_pass (readnoise_object_with_data_cube , ref_type_data_factory , mocker ):
215+
216+ # Mock a datacube with the necessary parts: ramp_model
217+ mock_readnoise_datacube = mocker .Mock ()
218+
219+ mock_readnoise_datacube .num_i_pixels = TEST_DETECTOR_PIXEL_COUNT
220+ mock_readnoise_datacube .num_j_pixels = TEST_DETECTOR_PIXEL_COUNT
221+ mock_readnoise_datacube .ramp_model = ref_type_data_factory (3 )
222+ mock_readnoise_datacube .data = ref_type_data_factory (3 )
223+
224+ # Add datacube to the readnoise
225+ mocker .patch .object (readnoise_object_with_data_cube , 'data_cube' , mock_readnoise_datacube )
226+
227+ result = readnoise_object_with_data_cube .comp_ramp_res_var ()
228+
229+ assert result .shape == (TEST_DETECTOR_PIXEL_COUNT , TEST_DETECTOR_PIXEL_COUNT )
230+
231+ def est_comp_cds_noise_output_matches_pixel_count_pass (readnoise_object_with_data_cube , ref_type_data_factory , mocker ):
232+
233+ # Mock a datacube with the necessary parts: ramp_model
234+ mock_readnoise_datacube = mocker .Mock ()
235+
236+ mock_readnoise_datacube .num_i_pixels = TEST_DETECTOR_PIXEL_COUNT
237+ mock_readnoise_datacube .num_j_pixels = TEST_DETECTOR_PIXEL_COUNT
238+ mock_readnoise_datacube .ramp_model = ref_type_data_factory (3 )
239+ mock_readnoise_datacube .data = ref_type_data_factory (3 )
240+ mock_readnoise_datacube .num_reads = 3
241+
242+ # Add datacube to the readnoise
243+ mocker .patch .object (readnoise_object_with_data_cube , 'data_cube' , mock_readnoise_datacube )
244+
245+ result = readnoise_object_with_data_cube .comp_cds_noise ()
246+
247+ assert result .shape == (TEST_DETECTOR_PIXEL_COUNT , TEST_DETECTOR_PIXEL_COUNT )
0 commit comments