Exemple #1
0
def test__sanity_check_input():
    test_subject = OpenCADCCutout()
    input = '[9][100:1000]'

    sanity_input = test_subject._sanity_check_input(input)

    assert isinstance(sanity_input, list), 'Should be list'
Exemple #2
0
def test__parse_input():
    test_subject = OpenCADCCutout()
    inputs = ['[9][100:1000]']

    results = test_subject._parse_input(inputs)
    pixel_cutout = results[0]

    assert pixel_cutout.get_extension() == 9, 'Wrong extension found.'
    assert pixel_cutout.get_ranges() == [(100, 1000)], 'Wrong ranges found.'

    inputs = ['[500:700][SCI,8][40:58]']
    results = test_subject._parse_input(inputs)
    pixel_cutout1 = results[0]
    pixel_cutout2 = results[1]

    assert pixel_cutout1.get_extension() == 0, 'Wrong extension found for 1.'
    assert pixel_cutout1.get_ranges() == [(500, 700)], \
        'Wrong ranges found for 1.'

    assert pixel_cutout2.get_extension() == ('SCI', 8), \
        'Wrong extension found for SCI,8.'
    assert pixel_cutout2.get_ranges() == [(40, 58)], \
        'Wrong ranges found for 1.'

    inputs = ['CIRCLE=88.0 115.0 0.5']
    results = test_subject._parse_input(inputs)

    assert results[0] == 'CIRCLE=88.0 115.0 0.5', 'Wrong WCS input.'
Exemple #3
0
def test_multiple_ext_cutouts():
    test_subject = OpenCADCCutout()
    cutout_file_name_path = test_context.random_test_file_name_path()
    cutout_regions = [
        PixelCutoutHDU([(1, 100), (1, 100)], "1"),
        PixelCutoutHDU([(1, 100), (1, 100)], "3")
    ]
    with open(cutout_file_name_path, 'wb') as output_writer, \
            open(target_file_name, 'rb') as input_reader:
        test_subject.cutout(cutout_regions, input_reader, output_writer,
                            'FITS')
    expected = fits.open(target_file_name, do_not_scale_image_data=True)
    actual = fits.open(cutout_file_name_path, do_not_scale_image_data=True)

    assert len(actual) == 3
    # test primary header unchanged
    assert len(expected[0].header) == len(actual[0].header)
    for key in expected[0].header.keys():
        assert expected[0].header[key] == actual[0].header[key]

    # check BSCALE and BZERO correct in cutout file
    assert expected[1].header['BSCALE'] == actual[1].header['BSCALE']
    assert expected[1].header['BZERO'] == actual[1].header['BZERO']
    assert expected[3].header['BSCALE'] == actual[2].header['BSCALE']
    assert expected[3].header['BZERO'] == actual[2].header['BZERO']
def test_mef_cutout():
    test_subject = OpenCADCCutout()
    target_file_name = _create_mef_file()
    cutout_file_name_path = test_context.random_test_file_name_path()
    logger.info('Testing with {}'.format(cutout_file_name_path))
    cutout_region_str = '[2][20:35,40:50][3]'

    # Write out a test file with the test result FITS data.
    with open(cutout_file_name_path, 'ab+') as output_writer, \
            open(target_file_name, 'rb') as input_reader:
        test_subject.cutout_from_string(cutout_region_str, input_reader,
                                        output_writer, 'FITS')

    with fits.open(cutout_file_name_path, mode='readonly') as result_hdu_list:
        assert len(result_hdu_list) == 3, 'Should have 3 HDUs.'

        hdu1 = result_hdu_list[1]
        hdu2 = result_hdu_list[2]
        wcs1 = WCS(header=hdu1.header)
        wcs2 = WCS(header=hdu2.header)

        np.testing.assert_array_equal(wcs1.wcs.crpix, [-19.0, -39.0],
                                      'Wrong CRPIX values.')
        np.testing.assert_array_equal(wcs1.wcs.crval, [0.0, 0.0],
                                      'Wrong CRVAL values.')
        np.testing.assert_array_equal(wcs2.wcs.crpix, [0.0, 0.0],
                                      'Wrong CRPIX values.')
        np.testing.assert_array_equal(wcs2.wcs.crval, [0.0, 0.0],
                                      'Wrong CRVAL values.')

        assert hdu1.header['NAXIS'] == 2, 'Wrong NAXIS value.'
        assert hdu2.header['NAXIS'] == 2, 'Wrong NAXIS value.'

        assert hdu1.header.get(
            'CHECKSUM') is None, 'Should not contain CHECKSUM.'
        assert hdu2.header.get(
            'CHECKSUM') is None, 'Should not contain CHECKSUM.'

        assert hdu1.header.get(
            'DATASUM') is None, 'Should not contain DATASUM.'
        assert hdu2.header.get(
            'DATASUM') is None, 'Should not contain DATASUM.'

        expected1 = np.zeros((11, 16), dtype=hdu1.data.dtype)
        expected2 = np.arange(50000, dtype=hdu2.data.dtype).reshape(100, 500)

        for i in range(11):
            start = 3918 + (i * 100)
            expected1[i] = np.arange(start, start + 16, dtype=hdu1.data.dtype)

        np.testing.assert_array_equal(hdu1.data, expected1,
                                      'Arrays 1 do not match.')
        np.testing.assert_array_equal(hdu2.data, expected2,
                                      'Arrays 2 do not match.')
Exemple #5
0
def test_integration_test(cutout_region_string, target_file_name,
                          expected_cutout_file_path, use_fits_diff,
                          test_dir_name, wcs_naxis_val, use_extension_names):
    test_subject = OpenCADCCutout()
    result_cutout_file_path = random_test_file_name_path(
        dir_name=test_dir_name)

    logger.info('Testing output to {}'.format(result_cutout_file_path))

    # Write out a test file with the test result FITS data.
    with open(result_cutout_file_path, 'ab+') as test_file_handle, \
            open(target_file_name, 'rb') as input_file_handle:
        test_subject.cutout_from_string(cutout_region_string,
                                        input_file_handle, test_file_handle,
                                        'FITS')

    with fits.open(expected_cutout_file_path, mode='readonly',
                   do_not_scale_image_data=True) \
            as expected_hdu_list, \
            fits.open(result_cutout_file_path, mode='readonly',
                      do_not_scale_image_data=True) \
            as result_hdu_list:
        if use_fits_diff:
            fits_diff = fits.FITSDiff(expected_hdu_list, result_hdu_list)
            np.testing.assert_array_equal((), fits_diff.diff_hdu_count,
                                          'HDU count diff should be empty.')

        if use_extension_names:
            result_hdu_list.sort(key=_extname_sort_func)
            expected_hdu_list.sort(key=_extname_sort_func)

        for extension, result_hdu in enumerate(result_hdu_list):
            logger.debug('\nChecking extension {}\n'.format(extension))
            expected_hdu = expected_hdu_list[extension]

            expected_wcs = WCS(header=expected_hdu.header, naxis=wcs_naxis_val)
            result_wcs = WCS(header=result_hdu.header, naxis=wcs_naxis_val)

            np.testing.assert_array_equal(expected_wcs.wcs.crpix,
                                          result_wcs.wcs.crpix,
                                          'Wrong CRPIX values.')
            np.testing.assert_array_equal(expected_wcs.wcs.crval,
                                          result_wcs.wcs.crval,
                                          'Wrong CRVAL values.')
            np.testing.assert_array_equal(expected_wcs.wcs.naxis,
                                          result_wcs.wcs.naxis,
                                          'Wrong NAXIS values.')
            assert expected_hdu.header.get(
                'CHECKSUM') is None, 'Should not contain CHECKSUM.'
            assert expected_hdu.header.get(
                'DATASUM') is None, 'Should not contain DATASUM.'
            np.testing.assert_array_equal(np.squeeze(expected_hdu.data),
                                          result_hdu.data,
                                          'Arrays do not match.')
Exemple #6
0
def test_hst_mef_cutout_missing_one():
    # Should result in a 3-HDU MEF.  Extension 2 is an ERR one with no data.
    cutout_region_string = \
        '[SCI,10][80:220,100:150][2][10:16,70:90][106][8:32,88:112][126]'
    test_subject = OpenCADCCutout()
    result_cutout_file_path = random_test_file_name_path()

    logger.info('Testing output to {}'.format(result_cutout_file_path))

    # Write out a test file with the test result FITS data.
    with open(result_cutout_file_path, 'ab+') as test_file_handle, \
            open(target_file_name, 'rb') as input_file_handle:
        test_subject.cutout_from_string(cutout_region_string,
                                        input_file_handle, test_file_handle,
                                        'FITS')

    with fits.open(expected_cutout_file_path, mode='readonly') \
            as expected_hdu_list, \
            fits.open(result_cutout_file_path, mode='readonly') \
            as result_hdu_list:
        # Index 0's value is missing from the result, so remove it here.
        expected_hdu_list.pop(0)
        fits_diff = fits.FITSDiff(expected_hdu_list, result_hdu_list)
        np.testing.assert_array_equal((), fits_diff.diff_hdu_count,
                                      'HDU count diff should be empty.')

        for extension in [('SCI', 10), ('SCI', 22), ('SCI', 26)]:
            expected_hdu = expected_hdu_list[expected_hdu_list.index_of(
                extension)]
            result_hdu = result_hdu_list[result_hdu_list.index_of(extension)]
            expected_wcs = WCS(header=expected_hdu.header, fix=False)
            result_wcs = WCS(header=result_hdu.header, fix=False)

            np.testing.assert_array_equal(expected_wcs.wcs.crpix,
                                          result_wcs.wcs.crpix,
                                          'Wrong CRPIX values.')
            np.testing.assert_array_equal(expected_wcs.wcs.crval,
                                          result_wcs.wcs.crval,
                                          'Wrong CRVAL values.')
            assert expected_hdu.header.get('NAXIS1') == result_hdu.header.get(
                'NAXIS1'), 'Wrong NAXIS1 values.'
            assert expected_hdu.header.get('NAXIS2') == result_hdu.header.get(
                'NAXIS2'), 'Wrong NAXIS2 values.'
            assert expected_hdu.header.get(
                'CHECKSUM') is None, 'Should not contain CHECKSUM.'
            assert expected_hdu.header.get(
                'DATASUM') is None, 'Should not contain DATASUM.'
            np.testing.assert_array_equal(
                expected_hdu.data, result_hdu.data,
                'Arrays do not match for extension {}.'.format(extension))
Exemple #7
0
def test__sanity_check_input():
    test_subject = OpenCADCCutout()
    input = '[9][100:1000]'

    sanity_input = test_subject._sanity_check_input(input)
    assert isinstance(sanity_input, list), 'Should be list'

    with pytest.raises(ValueError) as ve:
        test_subject._sanity_check_input(('bad', 'tuple'))
        assert ('{}'.format(ve) ==
                'Input is expected to be a string or list but was \
(u\'bad\', u\'tuple\')'                       ) or ('{}'.format(ve) ==
                             'Input is expected to be a string or list but was \
(\'bad\', \'tuple\')'                     ), \
            'Wrong error message.'
Exemple #8
0
def test_mef_cutout_no_overlap():
    test_subject = OpenCADCCutout()
    target_file_name = _create_mef_file()
    cutout_file_name_path = test_context.random_test_file_name_path()
    logger.info('Testing with {}'.format(cutout_file_name_path))
    cutout_region_str = '[1][300:800,810:1000]'

    try:
        # Write out a test file with the test result FITS data.
        with open(cutout_file_name_path, 'ab+') as output_writer, \
                open(target_file_name, 'rb') as input_reader:
            test_subject.cutout_from_string(cutout_region_str, input_reader,
                                            output_writer, 'FITS')
    except NoContentError as err:
        assert str(err) == 'No content (arrays do not overlap).', \
            'Wrong message.'
def test_simple_cutout():
    test_subject = OpenCADCCutout()
    cutout_file_name_path = test_context.random_test_file_name_path()
    logger.info('Testing with {}'.format(cutout_file_name_path))
    cutout_regions = [PixelCutoutHDU([(300, 800), (810, 1000)])]

    # Write out a test file with the test result FITS data.
    with open(cutout_file_name_path, 'ab+') as output_writer, \
            open(target_file_name, 'rb') as input_reader:
        test_subject.cutout(cutout_regions, input_reader, output_writer,
                            'FITS')

    with fits.open(expected_cutout_file_name, mode='readonly') \
            as expected_hdu_list, \
            fits.open(cutout_file_name_path, mode='readonly') \
            as result_hdu_list:
        fits_diff = fits.FITSDiff(expected_hdu_list, result_hdu_list)
        np.testing.assert_array_equal(
            (), fits_diff.diff_hdu_count, 'HDU count diff should be empty.')

        for extension, result_hdu in enumerate(result_hdu_list):
            expected_hdu = expected_hdu_list[extension]
            expected_wcs = WCS(header=expected_hdu.header)
            result_wcs = WCS(header=result_hdu.header)

            np.testing.assert_array_equal(
                expected_wcs.wcs.crpix, result_wcs.wcs.crpix,
                'Wrong CRPIX values.')
            np.testing.assert_array_equal(
                expected_wcs.wcs.crval, result_wcs.wcs.crval,
                'Wrong CRVAL values.')
            assert expected_hdu.header['NAXIS1'] \
                == result_hdu.header['NAXIS1'], 'Wrong NAXIS1 values.'
            assert expected_hdu.header['NAXIS2'] \
                == result_hdu.header['NAXIS2'], 'Wrong NAXIS2 values.'
            assert expected_hdu.header.get(
                'CHECKSUM') is None, 'Should not contain CHECKSUM.'
            assert expected_hdu.header.get(
                'DATASUM') is None, 'Should not contain DATASUM.'
            np.testing.assert_array_equal(
                np.squeeze(expected_hdu.data), result_hdu.data,
                'Arrays do not match.')
Exemple #10
0
def test_multiple_cutouts_single_ext():
    test_subject = OpenCADCCutout()
    cutout_file_name_path = test_context.random_test_file_name_path()
    cutout_regions = [
        PixelCutoutHDU([(1, 100), (1, 100)], "1"),
        PixelCutoutHDU([(200, 300), (2, 300)], "1")
    ]
    with open(cutout_file_name_path, 'wb') as output_writer, \
            open(target_file_name, 'rb') as input_reader:
        test_subject.cutout(cutout_regions, input_reader, output_writer,
                            'FITS')
    expected = fits.open(target_file_name, do_not_scale_image_data=True)
    actual = fits.open(cutout_file_name_path, do_not_scale_image_data=True)

    # cutouts in the same extension => no extra primary HDU
    assert len(actual) == 2
    # test primary header changed
    assert len(expected[0].header) != len(actual[0].header)

    # check BSCALE and BZERO correct in cutout file
    assert expected[1].header['BSCALE'] == actual[0].header['BSCALE']
    assert expected[1].header['BZERO'] == actual[0].header['BZERO']
    assert expected[1].header['BSCALE'] == actual[1].header['BSCALE']
    assert expected[1].header['BZERO'] == actual[1].header['BZERO']
Exemple #11
0
def test_construct():
    test_subject = OpenCADCCutout()

    with pytest.raises(ValueError) as ve:
        test_subject.cutout([])
        assert '{}'.format(ve) == 'No Cutout regions specified.', \
            'Wrong error message.'

    with pytest.raises(ValueError) as ve:
        test_subject.cutout([PixelCutoutHDU([(8, 10)])], input_reader=None)
        assert '{}'.format(ve) == 'No input source specified.', \
            'Wrong error message.'

    with pytest.raises(ValueError) as ve:
        test_subject.cutout([PixelCutoutHDU([(8, 10)])], output_writer=None)
        assert '{}'.format(ve) == 'No output target specified.', \
            'Wrong error message.'
Exemple #12
0
def test_construct():
    test_subject = OpenCADCCutout()

    with pytest.raises(ValueError) as ve:
        test_subject.cutout([])
    assert str(ve.value) == 'No Cutout regions specified.', \
        'Wrong error message.'

    with pytest.raises(FileNotFoundError):
        test_subject.cutout([PixelCutoutHDU([(8, 10)])],
                            input_reader=open('/no/such/file'))
Exemple #13
0
def test_astropy_scaling():
    test_subject = OpenCADCCutout()
    cutout_file_name_path = test_context.random_test_file_name_path()
    cutout_regions = [PixelCutoutHDU([], "2")]
    # Write out a test file with the test result FITS data.
    with open(cutout_file_name_path, 'wb') as output_writer, \
            open(target_file_name, 'rb') as input_reader:
        test_subject.cutout(cutout_regions, input_reader, output_writer,
                            'FITS')

    # now check that BZERO and BSCALE have not been changed
    expected = fits.open(target_file_name, do_not_scale_image_data=True)
    actual = fits.open(cutout_file_name_path, do_not_scale_image_data=True)

    # check headers and data not changed. Except ...
    # expected missing keywords from actual: PCOUNT, XTENSION and GCOUNT
    # added to actual: SIMPLE
    del expected[2].header['PCOUNT']
    del expected[2].header['XTENSION']
    del expected[2].header['GCOUNT']
    assert 'SIMPLE' in actual[0].header
    del actual[0].header['SIMPLE']

    assert len(expected[2].header) == len(actual[0].header)
    for key in expected[2].header.keys():
        assert expected[2].header[key] == actual[0].header[key]
    np.testing.assert_array_equal(expected[2].data, actual[0].data,
                                  'Arrays do not match.')

    # do a cutout
    cutout_regions = [PixelCutoutHDU([(1, 100), (1, 100)], "3")]
    # Write out a test file with the test result FITS data.
    with open(cutout_file_name_path, 'wb') as output_writer, \
            open(target_file_name, 'rb') as input_reader:
        test_subject.cutout(cutout_regions, input_reader, output_writer,
                            'FITS')

    # now check that BZERO and BSCALE have not been changed
    expected = fits.open(target_file_name, do_not_scale_image_data=True)
    actual = fits.open(cutout_file_name_path, do_not_scale_image_data=True)

    # check only expected headers changed
    # changed headers
    del expected[3].header['PCOUNT']
    del expected[3].header['XTENSION']
    del expected[3].header['GCOUNT']
    assert 'SIMPLE' in actual[0].header
    del actual[0].header['SIMPLE']
    assert len(expected[3].header) == len(actual[0].header)
    for key in expected[3].header.keys():
        if key == 'NAXIS1' or key == 'NAXIS2':
            assert actual[0].header[key] == 100
        else:
            assert expected[3].header[key] == actual[0].header[key]
def test_integration_wcs_test(cutout_region_string, target_file_name,
                              expected_cutout_file_path, use_fits_diff,
                              test_dir_name, wcs_naxis_val,
                              use_extension_names):
    test_subject = OpenCADCCutout()
    result_cutout_file_path = random_test_file_name_path(
        dir_name=test_dir_name)

    logging.info('Testing output to {}'.format(result_cutout_file_path))

    # Write out a test file with the test result FITS data.
    with open(result_cutout_file_path, 'ab+') as test_file_handle, \
            open(target_file_name, 'rb') as input_file_handle:
        test_subject.cutout_from_string(cutout_region_string,
                                        input_file_handle, test_file_handle,
                                        'FITS')

    with fits.open(expected_cutout_file_path, mode='readonly',
                   do_not_scale_image_data=True) \
            as expected_hdu_list, \
            fits.open(result_cutout_file_path, mode='readonly',
                      do_not_scale_image_data=True) \
            as result_hdu_list:
        if use_fits_diff:
            fits_diff = fits.FITSDiff(expected_hdu_list, result_hdu_list)
            np.testing.assert_array_equal((), fits_diff.diff_hdu_count,
                                          'HDU count diff should be empty.')

        if use_extension_names:
            result_hdu_list.sort(key=_extname_sort_func)
            expected_hdu_list.sort(key=_extname_sort_func)

        for extension, result_hdu in enumerate(result_hdu_list):
            logging.info('\nChecking extension {}\n'.format(extension))
            expected_hdu = expected_hdu_list[extension]

            expected_wcs = WCS(header=expected_hdu.header,
                               naxis=wcs_naxis_val,
                               fix=False)
            result_wcs = WCS(header=result_hdu.header,
                             naxis=wcs_naxis_val,
                             fix=False)

            np.testing.assert_array_almost_equal(expected_wcs.wcs.crpix,
                                                 result_wcs.wcs.crpix,
                                                 decimal=-4,
                                                 err_msg='Wrong CRPIX values.')
            np.testing.assert_array_equal(expected_wcs.wcs.crval,
                                          result_wcs.wcs.crval,
                                          'Wrong CRVAL values.')
            np.testing.assert_array_equal(expected_wcs.wcs.naxis,
                                          result_wcs.wcs.naxis,
                                          'Wrong NAXIS values.')
            assert expected_hdu.header.get(
                'CHECKSUM') is None, 'Should not contain CHECKSUM.'
            assert expected_hdu.header.get(
                'DATASUM') is None, 'Should not contain DATASUM.'

            expected_data = expected_hdu.data
            result_data = result_hdu.data

            try:
                if expected_data is not None and result_data is not None:
                    assert expected_data.shape == result_data.shape, \
                        'Shapes don\'t match.'
                    np.testing.assert_array_equal(expected_data, result_data,
                                                  'Arrays do not match.')
                else:
                    assert expected_data == result_data
            except AssertionError:
                # Check the shape if the data array doesn't match.
                np.testing.assert_array_almost_equal(
                    expected_hdu.data.shape,
                    result_hdu.data.shape,
                    decimal=-4,
                    err_msg='Arrays match closely enough.')