Ejemplo n.º 1
0
def test_refining_on_noisy_data():
    nx, ny = 401, 403
    read_noise = 10.0
    test_data = np.zeros((ny, nx))
    x2d, y2d = np.meshgrid(np.arange(nx), np.arange(ny))

    trace_centers = []
    y_0s = [100, 200, 300]
    for i in range(3):
        trace_centers.append(5e-4 * (np.arange(nx) - nx / 2.)**2 + y_0s[i])
        test_data += gaussian(y2d, trace_centers[i], 3, a=10000.0)

    test_data += np.random.poisson(test_data)
    test_data += np.random.normal(0.0, read_noise, size=test_data.shape)
    uncertainty = np.sqrt(test_data + read_noise**2.0)
    test_image = NRESObservationFrame([
        CCDData(data=test_data,
                uncertainty=uncertainty,
                meta={'OBJECTS': 'tung&tung&none'})
    ], 'foo.fits')
    input_context = context.Context({'TRACE_HALF_HEIGHT': 5})
    stage = TraceInitializer(input_context)
    output_image = stage.do_stage(test_image)

    for trace_center in trace_centers:
        # Make sure that the center +- 4 pixels is in the trace image
        assert all(output_image.traces[np.abs(y2d - trace_center) <= 4])
Ejemplo n.º 2
0
def test_background_subtraction_with_traces(seed):
    nx, ny = 405, 403
    x = np.arange(nx)
    y = np.arange(ny)
    X, Y = np.meshgrid(x, y)
    noise_sigma = 1.0
    input_background = 30 * np.exp(-(X - nx / 2.0)**2 / 300**2 -
                                   (Y - ny / 2.0 - 50.0)**2 / 200**2)
    test_data = input_background + np.random.normal(
        0.0, noise_sigma, size=input_background.shape)
    test_image = NRESObservationFrame([
        CCDData(data=test_data,
                uncertainty=np.ones((ny, nx)) * noise_sigma,
                meta={'OBJECTS': 'tung&tung&none'})
    ], 'foo.fits')

    test_image.traces = np.zeros((ny, nx))

    for i in range(1, 8):
        test_image.traces[40 * i:40 * i + 10] = i

    input_context = context.Context({})
    stage = BackgroundSubtractor(input_context)
    output_image = stage.do_stage(test_image)
    # Make sure our background estimation is good. This is roughly 4 counts which is not bad
    # If we fully model the image, we can do better than this, but that becomes computationally prohibitive on a 4kx4k
    # image
    np.testing.assert_allclose(output_image.background,
                               input_background,
                               atol=5.0)
Ejemplo n.º 3
0
def test_refine_traces_offset_centroid():
    nx, ny = 401, 403
    x2d, y2d = np.meshgrid(np.arange(nx), np.arange(ny))
    test_data, trace_centers, input_traces = make_simple_traces(nx, ny)
    read_noise = 10.0

    # Filter out the numerical noise
    test_data[test_data < 1e-15] = 0.0
    test_data += np.random.poisson(test_data)
    test_data += np.random.normal(0.0, read_noise, size=test_data.shape)
    uncertainty = np.sqrt(test_data + read_noise**2.0)
    test_image = NRESObservationFrame([
        CCDData(data=test_data,
                uncertainty=uncertainty,
                meta={'OBJECTS': 'tung&tung&none'})
    ], 'foo.fits')
    test_image.traces = input_traces
    input_context = context.Context({'TRACE_HALF_HEIGHT': 5})

    stage = TraceRefiner(input_context)
    output_image = stage.do_stage(test_image)

    for trace_center in trace_centers:
        # Make sure that the center +- 4 pixels is in the trace image
        assert all(output_image.traces[np.abs(y2d - trace_center + 1) <= 4])
Ejemplo n.º 4
0
    def test_unit_weights_extraction(self):
        image = two_order_image()
        image.weights = np.ones_like(image.data)
        expected_extracted_flux = np.max(image.data) * 3
        expected_extracted_wavelength = np.max(image.wavelengths)
        expected_extracted_uncertainty = np.sqrt(3) * np.max(image.data)
        input_context = context.Context({})

        stage = WeightedExtract(input_context)
        output_image = stage.do_stage(image)
        spectrum = output_image.spectrum

        assert np.allclose(spectrum[0, 0]['flux'], expected_extracted_flux)
        assert np.allclose(spectrum[0, 0]['wavelength'],
                           expected_extracted_wavelength)
        assert np.allclose(spectrum[0, 0]['uncertainty'],
                           expected_extracted_uncertainty)
        assert np.allclose(spectrum[0, 0]['id'], 1)

        assert np.allclose(spectrum[1, 1]['flux'][1:-1],
                           expected_extracted_flux)
        assert np.allclose(spectrum[1, 1]['wavelength'][1:-1],
                           expected_extracted_wavelength)
        assert len(spectrum[1, 1]['flux']) == image.traces.shape[1] - 2
        assert np.allclose(spectrum[1, 1]['uncertainty'][1:-1],
                           expected_extracted_uncertainty)
        assert len(spectrum[1, 1]['uncertainty']) == image.traces.shape[1] - 2
        assert np.allclose(spectrum[1, 1]['id'], 2)
Ejemplo n.º 5
0
    def test_extract_in_poisson_regime(self):
        trace_width, number_traces = 20, 10
        seed = 1408235915
        image = five_hundred_square_image(50000,
                                          number_traces,
                                          trace_width,
                                          seed=seed)
        expected_extracted_wavelength = np.max(image.wavelengths)

        image2 = five_hundred_square_image(50000,
                                           number_traces,
                                           trace_width,
                                           seed=seed)
        image2.profile = np.ones_like(image.data)
        input_context = context.Context({})

        stage = GetOptimalExtractionWeights(input_context)
        image = stage.do_stage(image)
        image2.weights = np.ones_like(image2.data)
        stage2 = WeightedExtract(input_context)
        optimal_image = stage2.do_stage(image)
        box_image = stage2.do_stage(image2)
        for i in range(1, number_traces + 1):
            assert np.allclose(optimal_image.spectrum[i, i]['flux'],
                               box_image.spectrum[i, i]['flux'],
                               rtol=0.05)
            assert np.allclose(optimal_image.spectrum[i, i]['wavelength'],
                               expected_extracted_wavelength)
            assert np.allclose(optimal_image.spectrum[i, i]['uncertainty'],
                               box_image.spectrum[i, i]['uncertainty'],
                               rtol=0.05)
Ejemplo n.º 6
0
    def test_extract_in_readnoise_regime(self):
        trace_width, number_traces = 20, 10
        seed = 192074123
        image = five_hundred_square_image(100,
                                          number_traces,
                                          trace_width,
                                          read_noise=100,
                                          seed=seed)
        image2 = five_hundred_square_image(100,
                                           number_traces,
                                           trace_width,
                                           read_noise=100,
                                           seed=seed)
        image2.profile = np.ones_like(image.data)
        input_context = context.Context({})

        stage = GetOptimalExtractionWeights(input_context)
        image = stage.do_stage(image)
        image2.weights = np.ones_like(image2.data)
        stage2 = WeightedExtract(input_context)
        optimal_image = stage2.do_stage(image)
        box_image = stage2.do_stage(image2)
        for i in range(1, number_traces + 1):
            optimal_median_sn = np.median(
                optimal_image.spectrum[i, i]['flux'] /
                optimal_image.spectrum[i, i]['uncertainty'])
            box_median_sn = np.median(box_image.spectrum[i, i]['flux'] /
                                      box_image.spectrum[i, i]['uncertainty'])
            assert optimal_median_sn > 1.45 * box_median_sn
Ejemplo n.º 7
0
def test_profile_fit_with_noise_with_blaze():
    nx, ny = 401, 403
    input_profile, trace_centers, input_traces = make_simple_traces(nx,
                                                                    ny,
                                                                    blaze=True)
    read_noise = 10.0

    # Filter out the numerical noise
    input_profile[input_profile < 1e-15] = 0.0
    input_profile += np.random.poisson(input_profile)
    input_profile += np.random.normal(0.0,
                                      read_noise,
                                      size=input_profile.shape)
    uncertainty = np.sqrt(input_profile + read_noise**2.0)
    image = NRESObservationFrame([
        CCDData(data=input_profile.copy(),
                uncertainty=uncertainty,
                meta={'OBJECTS': 'tung&tung&none'})
    ], 'foo.fits')
    image.traces = input_traces
    input_context = context.Context({})

    stage = ProfileFitter(input_context)
    image = stage.do_stage(image)

    for i in range(1, np.max(image.traces) + 1):
        this_trace = get_trace_region(image.traces == i)
        scale_factor = np.max(input_profile[this_trace]) / np.max(
            image.profile[this_trace] * image.blaze[i - 1]['blaze'])
        np.testing.assert_allclose(input_profile[this_trace],
                                   image.profile[this_trace] *
                                   image.blaze[i - 1]['blaze'] * scale_factor,
                                   rtol=1.5e-2,
                                   atol=1.0)
Ejemplo n.º 8
0
    def test_optimal_weights_zero_on_zero_profile(self):
        image = two_order_image()
        image.profile = np.zeros_like(image.traces, dtype=float)
        input_context = context.Context({})

        stage = GetOptimalExtractionWeights(input_context)
        output_image = stage.do_stage(image)
        assert np.allclose(output_image.weights, 0)
Ejemplo n.º 9
0
 def test_do_stage_with_existing_wavelengths(self, mock_refine_wavelengths):
     # test that feature wavelengths are populated from the old solutions.
     image = self.generate_image()
     image.wavelengths = np.random.random(size=image.traces.shape)
     image.features['xcentroid'], image.features['ycentroid'] = np.array([0, 1, 2, 0]), np.array([2, 0, 1, 0])
     expected_wavelengths = image.wavelengths[image.features['ycentroid'], image.features['xcentroid']]
     image = WavelengthCalibrate(context.Context({})).do_stage(image)
     assert np.allclose(image.features['wavelength'], expected_wavelengths)
Ejemplo n.º 10
0
class TestAssessWavelengthSolution:
    test_image = TestWavelengthCalibrate().generate_image()
    input_context = context.Context({})
    test_qc_results = {}

    def test_do_stage_does_not_crash(self):
        image = AssessWavelengthSolution(self.input_context).do_stage(
            self.test_image)
        assert image is not None

    @mock.patch('banzai_nres.qc.qc_wavelength.qc.save_qc_results')
    def test_do_stage_posts_to_elastic_search(self, fake_post):
        # define a test function to mock the saving of the quality control metrics

        def save_qc_results_locally(runtime_context, qc_results, image):
            self.test_qc_results = qc_results
            return None

        fake_post.side_effect = save_qc_results_locally
        # run the stage
        AssessWavelengthSolution(self.input_context).do_stage(self.test_image)
        assert len(self.test_qc_results) == 5
        assert np.isfinite(self.test_qc_results['RVPRECSN'])

    @mock.patch('banzai_nres.qc.qc_wavelength.qc.save_qc_results')
    def test_do_stage_savesqc_toheader(self, fake_post):
        # for now we just test that one of the results (the most important one) was saved to the header.
        image = AssessWavelengthSolution(self.input_context).do_stage(
            self.test_image)
        assert np.isfinite(image.meta['RVPRECSN']
                           [0])  # check the calculated wavelength precision
        assert len(
            image.meta['RVPRECSN'][1]) > 0  # description string is not empty

    def test_velocity_precision(self):
        np.random.seed(87213483)
        # make a mock line list
        nlines, expected_precision = 1000, 10 * units.m / units.s
        lab_lines = np.linspace(4000, 5000, nlines)
        # We assume the precision will go like sqrt(n)
        scatter_per_line = expected_precision * np.sqrt(nlines)
        # Our precision is in velocity space so delta lambda = lambda * delta v / c
        features = np.random.normal(lab_lines,
                                    scale=lab_lines * scatter_per_line /
                                    constants.c)
        velocity_precision = get_velocity_precision(features, lab_lines,
                                                    nlines)
        # For a 1000 lines we expect sigma to be ~3% so we double that here in the tests.
        assert np.isclose(velocity_precision, expected_precision, rtol=6.e-2)

    def test_line_matching(self):
        nlines, wavelength_scatter = 100, 0.1
        mock_lines = np.linspace(4000, 5000, nlines)
        line_list = np.random.permutation(mock_lines)
        features = mock_lines + np.random.randn(nlines) * wavelength_scatter
        lab_lines = find_nearest(features, np.sort(line_list))
        sigma_delta_lambda = robust_standard_deviation(features - lab_lines)
        assert np.isclose(sigma_delta_lambda, wavelength_scatter, rtol=5.e-2)
Ejemplo n.º 11
0
 def test_do_stage_on_empty_features(self):
     input_context = context.Context({})
     image = NRESObservationFrame([CCDData(data=self.data, uncertainty=self.err,
                                   meta={'OBJECTS': 'tung&tung&none'})], 'foo.fits')
     image.traces = np.ones_like(self.data, dtype=int)
     image.blaze = {'blaze': np.ones_like(self.data, dtype=int)}
     stage = IdentifyFeatures(input_context)
     stage.fwhm, stage.nsigma = self.sigma, 1.5
     image = stage.do_stage(image)
     assert len(image.features) == 0
Ejemplo n.º 12
0
 def test_do_stage_no_blaze(self):
     input_context = context.Context({})
     ccd_data = CCDData(data=self.data, uncertainty=self.err, meta={'OBJECTS': 'tung&tung&none'})
     image = NRESObservationFrame([ccd_data], 'foo.fits')
     image.traces = np.ones_like(self.data, dtype=int)
     stage = IdentifyFeatures(input_context)
     stage.fwhm, stage.nsigma = self.sigma, 0.5
     image = stage.do_stage(image)
     image.features.sort('pixel')
     assert np.allclose(image.features['pixel'], self.xcoords, atol=0.001)
     assert np.allclose(image.features['ycentroid'], self.ycoords, atol=0.001)
     assert np.allclose(image.features['id'], 1)
Ejemplo n.º 13
0
    def test_optimal_weights_on_box_profile(self):
        profile = np.zeros((5, 5))
        variance = np.ones_like(profile, dtype=float)
        mask = np.zeros_like(profile, dtype=float)
        profile[1:4, :], variance[1:4, :] = 1., 1.
        input_context = context.Context({})

        stage = GetOptimalExtractionWeights(input_context)
        weights = stage.weights(profile, variance, mask)
        # check that the weights in the order are 1/width of the order:
        assert np.allclose(weights[np.isclose(profile, 1)], 1. / 3.)
        # check that the weights of the area with zero profile are zero:
        assert np.allclose(weights[np.isclose(profile, 0)], 0)
Ejemplo n.º 14
0
def test_blind_solve():
    trace_centers, test_data, x2d, y2d = make_realistic_trace_image()
    test_image = NRESObservationFrame([
        CCDData(data=test_data,
                uncertainty=1e-5 * np.ones_like(test_data),
                meta={'OBJECTS': 'tung&tung&none'})
    ], 'foo.fits')
    input_context = context.Context({'TRACE_HALF_HEIGHT': 5})
    stage = TraceInitializer(input_context)
    output_image = stage.do_stage(test_image)
    for trace_center in trace_centers:
        # Make sure that the center +- 4 pixels is in the trace image
        assert all(output_image.traces[np.abs(y2d - trace_center) <= 4])
Ejemplo n.º 15
0
def test_blind_solve_with_edge_clipping_traces():
    trace_centers, test_data, x2d, y2d = make_realistic_trace_image(
        nx=401, ny=403, y0_centers=[1, 200, 400])
    test_image = NRESObservationFrame([
        CCDData(data=test_data,
                uncertainty=1e-5 * np.ones_like(test_data),
                meta={'OBJECTS': 'tung&tung&none'})
    ], 'foo.fits')
    input_context = context.Context({'TRACE_HALF_HEIGHT': 5})
    stage = TraceInitializer(input_context)
    output_image = stage.do_stage(test_image)
    assert np.count_nonzero(list(set(output_image.traces[:, 200]))) == 1
    # test that the only valid trace is centered correctly
    assert all(output_image.traces[np.abs(y2d - trace_centers[1]) <= 4])
Ejemplo n.º 16
0
    def test_do_stage(self):
        blaze_factor = 0.5
        input_context = context.Context({})
        ccd_data = CCDData(data=self.data, uncertainty=self.err, meta={'OBJECTS': 'tung&tung&none'})
        image = NRESObservationFrame([ccd_data], 'foo.fits')
        image.traces = np.ones_like(self.data, dtype=int)
        image.blaze = {'blaze': blaze_factor * np.ones((1, self.data.shape[1]), dtype=int)}

        stage = IdentifyFeatures(input_context)
        stage.fwhm, stage.nsigma = self.sigma, 0.5
        image = stage.do_stage(image)
        image.features.sort('pixel')
        assert np.allclose(image.features['corrected_flux'], image.features['flux'] / blaze_factor, rtol=1E-4)
        assert np.allclose(image.features['pixel'], self.xcoords, atol=0.001)
        assert np.allclose(image.features['ycentroid'], self.ycoords, atol=0.001)
        assert np.allclose(image.features['id'], 1)
Ejemplo n.º 17
0
class TestLineListLoader:
    stage = LineListLoader(context.Context({}))

    @mock.patch('banzai_nres.wavelength.LineListLoader.on_missing_master_calibration', return_value=None)
    def test_do_stage_aborts(self, fake_miss):
        stage = LineListLoader(context.Context({}))
        stage.LINE_LIST_FILENAME = 'some/bad/path'
        assert stage.do_stage('image') is None

    @mock.patch('numpy.genfromtxt', return_value=np.array([[1, 1]]))
    def test_do_stage(self, fake_load):
        image = type('image', (), {})
        image = self.stage.do_stage(image)
        assert np.allclose(image.line_list, [1])

    def test_apply_master_calibration(self):
        test_image = type('image', (), {})
        assert np.allclose(self.stage.apply_master_calibration(test_image, [1, 2]).line_list, [1, 2])
Ejemplo n.º 18
0
class TestArcLoader:
    stage = ArcLoader(context.Context({}))

    def test_double_on_missing_master_calibration(self):
        test_image = type('image', (), {'obstype': 'DOUBLE'})
        assert self.stage.on_missing_master_calibration(test_image).obstype == 'DOUBLE'

    @mock.patch('banzai_nres.wavelength.CalibrationUser.on_missing_master_calibration')
    def test_on_missing_master_calibration(self, mock_parent_miss):
        test_image = type('image', (), {'obstype': 'ELSE'})
        assert self.stage.on_missing_master_calibration(test_image) is None

    def test_apply_master_calibration(self):
        master_cal = type('image', (), {'wavelengths': [1, 2], 'filename': 'foo.fits', 'fibers': [0, 1]})
        test_image = type('image', (), {'wavelengths': None, 'meta': {}})
        assert np.allclose(self.stage.apply_master_calibration(test_image, master_cal).wavelengths, [1, 2])
        assert self.stage.calibration_type == 'DOUBLE'
        assert test_image.meta['L1IDARC'][0] == 'foo.fits'
Ejemplo n.º 19
0
def test_blind_solve_with_bpm():
    trace_centers, test_data, x2d, y2d = make_realistic_trace_image()
    bpm_mask = np.ones_like(test_data, dtype=bool)
    bpm_mask[
        150:
        250, :] = 0  # set the pixels around the center trace as good. Leave the other pixels masked.
    test_image = NRESObservationFrame([
        CCDData(data=test_data,
                uncertainty=1e-5 * np.ones_like(test_data),
                meta={'OBJECTS': 'tung&tung&none'},
                mask=bpm_mask)
    ], 'foo.fits')
    input_context = context.Context({'TRACE_HALF_HEIGHT': 5})
    stage = TraceInitializer(input_context)
    output_image = stage.do_stage(test_image)
    assert np.count_nonzero(list(set(output_image.traces[:, 200]))) == 1
    # test that the only valid trace is centered correctly
    assert all(output_image.traces[np.abs(y2d - trace_centers[1]) <= 4])
Ejemplo n.º 20
0
def test_profile_fit_without_noise_without_blaze():
    nx, ny = 401, 403
    input_profile, trace_centers, input_traces = make_simple_traces(
        nx, ny, blaze=False)
    # Filter out the numerical noise
    input_profile[input_profile < 1e-15] = 0.0
    uncertainty = np.sqrt(input_profile)
    image = NRESObservationFrame([
        CCDData(data=input_profile.copy(),
                uncertainty=uncertainty,
                meta={'OBJECTS': 'tung&tung&none'})
    ], 'foo.fits')
    image.traces = input_traces
    input_context = context.Context({})

    stage = ProfileFitter(input_context)
    image = stage.do_stage(image)
    scale_factor = np.max(input_profile) / np.max(image.profile)
    assert np.allclose(input_profile[input_traces != 0],
                       image.profile[input_traces != 0] * scale_factor,
                       rtol=5e-3,
                       atol=1.0)
Ejemplo n.º 21
0
def test_refine_traces_with_previous_trace():
    nx, ny = 401, 403
    num_traces = 3
    read_noise = 10.0
    test_data = np.zeros((ny, nx))
    x2d, y2d = np.meshgrid(np.arange(nx), np.arange(ny))

    trace_half_width = 6
    trace_centers = []
    y_0s = [100, 200, 300]
    blaze_function = 1 - 1e-5 * (x2d - nx / 2.)**2
    input_traces = np.zeros((ny, nx), dtype=np.int)
    for i in range(num_traces):
        trace_centers.append(5e-4 * (np.arange(nx) - nx / 2.)**2 + y_0s[i])
        test_data += gaussian(y2d, trace_centers[i], 2,
                              a=10000.0) * blaze_function
        input_traces[np.abs(y2d -
                            trace_centers[i]) <= trace_half_width] = i + 1

    # Filter out the numerical noise
    test_data[test_data < 1e-15] = 0.0
    test_data += np.random.poisson(test_data)
    test_data += np.random.normal(0.0, read_noise, size=test_data.shape)
    uncertainty = np.sqrt(test_data + read_noise**2.0)
    test_image = NRESObservationFrame([
        CCDData(data=test_data,
                uncertainty=uncertainty,
                meta={'OBJECTS': 'tung&tung&none'})
    ], 'foo.fits')
    test_image.traces = input_traces
    input_context = context.Context({'TRACE_HALF_HEIGHT': 5})

    stage = TraceRefiner(input_context)
    output_image = stage.do_stage(test_image)

    for trace_center in trace_centers:
        # Make sure that the center +- 4 pixels is in the trace image
        assert all(output_image.traces[np.abs(y2d - trace_center) <= 4])
    assert np.isclose(num_traces, output_image.num_traces)
Ejemplo n.º 22
0
def test_if_robust_to_wavelength_region_mismatch_between_trace_region():
    """
    This creates a simulated image where the trace region for the wavelength image
    is different than image.traces by 1 pixel (a 1 pixel shift). This kind of shift
    used to cause the wavelengths to be incorrect by
    (trace width - 1)/trace width  . This tests in particular this bug, because trace regions shift by 1 pixel nearly
    every night (even though the traces themselves do not shift by much).
    """
    image = two_order_image()
    image.weights = np.ones_like(image.data)
    expected_extracted_flux = np.max(image.data) * 3
    expected_extracted_wavelength = np.max(image.wavelengths)
    expected_extracted_uncertainty = np.sqrt(3) * np.max(image.data)
    input_context = context.Context({})
    # now we shift the wavelength image up by 1 pixel. This creates a mismatch between image.wavelengths
    # and image.traces
    image.wavelengths = np.vstack([
        image.wavelengths[1:],
        np.zeros((1, len(image.wavelengths[0])), dtype=float)
    ])
    output_image = WeightedExtract(input_context).do_stage(image)
    spectrum = output_image.spectrum

    assert np.allclose(spectrum[0, 0]['flux'], expected_extracted_flux)
    assert np.allclose(spectrum[0, 0]['wavelength'],
                       expected_extracted_wavelength)
    assert np.allclose(spectrum[0, 0]['uncertainty'],
                       expected_extracted_uncertainty)
    assert np.allclose(spectrum[0, 0]['id'], 1)

    assert np.allclose(spectrum[1, 1]['flux'][1:-1], expected_extracted_flux)
    assert np.allclose(spectrum[1, 1]['wavelength'][1:-1],
                       expected_extracted_wavelength)
    assert len(spectrum[1, 1]['flux']) == image.traces.shape[1] - 2
    assert np.allclose(spectrum[1, 1]['uncertainty'][1:-1],
                       expected_extracted_uncertainty)
    assert len(spectrum[1, 1]['uncertainty']) == image.traces.shape[1] - 2
    assert np.allclose(spectrum[1, 1]['id'], 2)
Ejemplo n.º 23
0
class TestCalculateScienceFrameMetrics:
    input_context = context.Context({'PIXELS_PER_RESOLUTION_ELEMENT': 4.15})
    test_wavelengths = np.linspace(5100.0, 5200.0, 4096)
    test_flux = -0.001 * test_wavelengths**2 + 10.3 * test_wavelengths
    test_uncertainty = np.sqrt(test_flux)
    snr_order = 90
    spectrum = []
    header = fits.Header({'OBJECTS': 'test&none&none'})
    for i in range(5):
        order = snr_order + i
        row = {'wavelength': test_wavelengths, 'flux': test_flux, 'uncertainty': test_uncertainty,
               'fiber': 0, 'order': snr_order}
        spectrum.append(row)
    test_image = NRESObservationFrame([CCDData(np.zeros((1, 1)), meta=header)], 'test.fits')
    test_image.spectrum = Spectrum1D(spectrum)

    def test_do_stage_does_not_crash(self):
        image = CalculateScienceFrameMetrics(self.input_context).do_stage(self.test_image)
        assert image is not None

    def test_snr_calculation(self):
        snr, _ = get_snr(self.test_image, self.snr_order, self.input_context.PIXELS_PER_RESOLUTION_ELEMENT)
        snr_test = self.test_flux/self.test_uncertainty * np.sqrt(self.input_context.PIXELS_PER_RESOLUTION_ELEMENT)
        assert np.isclose(snr, np.max(snr_test), rtol=0.1)
Ejemplo n.º 24
0
def test_to_fits():
    data = np.ones((2, 2))
    spec = Spectrum1D({
        'fiber': [0],
        'order': [1],
        'flux': [np.arange(10)],
        'wavelength': [np.arange(10)]
    })
    image = NRESObservationFrame([
        CCDData(data=data,
                uncertainty=2 * data,
                name='SCI',
                meta={
                    'OBJECTS': 'tung&tung&none',
                    'EXTNAME': ''
                })
    ], 'foo.fits')
    image.add_or_update(ArrayData(3 * data, name='TRACES'))
    image.add_or_update(ArrayData(4 * data, name='WEIGHTS'))
    image.add_or_update(ArrayData(5 * data, name='BACKGROUND'))
    image.add_or_update(DataTable(spec.table, name='SPECTRUM'))
    hdulist = image.to_fits(
        context.Context({
            'EXTENSION_NAMES_TO_CONDENSE': ['SCI'],
            'REDUCED_DATA_EXTENSION_TYPES': {},
            'fpack': True,
            'LOSSLESS_EXTENSIONS': []
        }))
    hdu_ext_names = [
        hdulist[i].header.get('EXTNAME') for i in range(len(hdulist))
        if hdulist[i].header.get('EXTNAME') is not None
    ]
    for name in [
            'SCI', 'BPM', 'ERR', 'TRACES', 'WEIGHTS', 'BACKGROUND', 'SPECTRUM'
    ]:
        assert name in hdu_ext_names
Ejemplo n.º 25
0
 def test_rejects_on_no_wavelengths(self):
     con = context.Context({})
     image = type('image', (), {'weights': 'notnone', 'wavelengths': None})
     assert WeightedExtract(con).do_stage(image) is None
Ejemplo n.º 26
0
def test_stage_caltypes():
    assert ArcStacker(context.Context({})).calibration_type == 'DOUBLE'
    assert LineListLoader(context.Context({})).calibration_type == 'LINELIST'
Ejemplo n.º 27
0
 def test_do_stage_aborts(self, fake_miss):
     stage = LineListLoader(context.Context({}))
     stage.LINE_LIST_FILENAME = 'some/bad/path'
     assert stage.do_stage('image') is None
Ejemplo n.º 28
0
 def test_do_stage(self, mock_find_wavelengths, mock_refine_wavelengths):
     image = self.generate_image()
     image.features['id'] = np.ones_like(image.features['pixel'])  # so that only one fiber is populated.
     image = WavelengthCalibrate(context.Context({})).do_stage(image)
     assert np.allclose(image.features['wavelength'], np.arange(4))
Ejemplo n.º 29
0
 def test_rejects_on_no_profile(self):
     con = context.Context({})
     assert GetOptimalExtractionWeights(con).do_stage(
         two_order_image()) is None
Ejemplo n.º 30
0
 def test_rejects_on_no_weights(self):
     con = context.Context({})
     assert WeightedExtract(con).do_stage(two_order_image()) is None