コード例 #1
0
    def test_set_and_get_attribute(self) -> None:

        pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

        pipeline.set_attribute('images', 'PIXSCALE', 0.1, static=True)
        pipeline.set_attribute('images',
                               'PARANG',
                               np.arange(1., 11., 1.),
                               static=False)

        attribute = pipeline.get_attribute('images', 'PIXSCALE', static=True)
        assert attribute == pytest.approx(0.1, rel=self.limit, abs=0.)

        attribute = pipeline.get_attribute('images', 'PARANG', static=False)
        assert attribute == pytest.approx(np.arange(1., 11., 1.),
                                          rel=self.limit,
                                          abs=0.)

        pipeline.set_attribute('images',
                               'PARANG',
                               np.arange(10., 21., 1.),
                               static=False)

        attribute = pipeline.get_attribute('images', 'PARANG', static=False)
        assert attribute == pytest.approx(np.arange(10., 21., 1.),
                                          rel=self.limit,
                                          abs=0.)
コード例 #2
0
    def test_set_and_get_attribute(self):
        pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

        pipeline.set_attribute('images', 'PIXSCALE', 0.1, static=True)
        pipeline.set_attribute('images',
                               'PARANG',
                               np.arange(1., 11., 1.),
                               static=False)

        attribute = pipeline.get_attribute('images', 'PIXSCALE', static=True)
        assert np.allclose(attribute, 0.1, rtol=limit, atol=0.)

        attribute = pipeline.get_attribute('images', 'PARANG', static=False)
        assert np.allclose(attribute,
                           np.arange(1., 11., 1.),
                           rtol=limit,
                           atol=0.)

        pipeline.set_attribute('images',
                               'PARANG',
                               np.arange(10., 21., 1.),
                               static=False)

        attribute = pipeline.get_attribute('images', 'PARANG', static=False)
        assert np.allclose(attribute,
                           np.arange(10., 21., 1.),
                           rtol=limit,
                           atol=0.)
コード例 #3
0
    def test_create_none_config(self) -> None:

        file_obj = open(self.test_dir + 'PynPoint_config.ini', 'w')
        file_obj.write('[header]\n\n')
        file_obj.write('INSTRUMENT: None\n')
        file_obj.write('NFRAMES: None\n')
        file_obj.write('EXP_NO: None\n')
        file_obj.write('NDIT: None\n')
        file_obj.write('PARANG_START: ESO ADA POSANG\n')
        file_obj.write('PARANG_END: None\n')
        file_obj.write('DITHER_X: None\n')
        file_obj.write('DITHER_Y: None\n')
        file_obj.write('DIT: None\n')
        file_obj.write('LATITUDE: None\n')
        file_obj.write('LONGITUDE: None\n')
        file_obj.write('PUPIL: None\n')
        file_obj.write('DATE: None\n')
        file_obj.write('RA: None\n')
        file_obj.write('DEC: None\n\n')
        file_obj.write('[settings]\n\n')
        file_obj.write('PIXSCALE: None\n')
        file_obj.write('MEMORY: None\n')
        file_obj.write('CPU: None\n')
        file_obj.close()

        pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

        attribute = pipeline.get_attribute('config', 'MEMORY', static=True)
        assert attribute == 0

        attribute = pipeline.get_attribute('config', 'CPU', static=True)
        assert attribute == 0

        attribute = pipeline.get_attribute('config', 'PIXSCALE', static=True)
        assert attribute == pytest.approx(0., rel=self.limit, abs=0.)

        attribute = pipeline.get_attribute('config', 'INSTRUMENT', static=True)
        assert attribute == 'None'

        create_config(self.test_dir + 'PynPoint_config.ini')
コード例 #4
0
    def test_create_none_config(self):
        file_obj = open(self.test_dir + "PynPoint_config.ini", 'w')
        file_obj.write('[header]\n\n')
        file_obj.write('INSTRUMENT: None\n')
        file_obj.write('NFRAMES: None\n')
        file_obj.write('EXP_NO: None\n')
        file_obj.write('NDIT: None\n')
        file_obj.write('PARANG_START: ESO ADA POSANG\n')
        file_obj.write('PARANG_END: None\n')
        file_obj.write('DITHER_X: None\n')
        file_obj.write('DITHER_Y: None\n')
        file_obj.write('DIT: None\n')
        file_obj.write('LATITUDE: None\n')
        file_obj.write('LONGITUDE: None\n')
        file_obj.write('PUPIL: None\n')
        file_obj.write('DATE: None\n')
        file_obj.write('RA: None\n')
        file_obj.write('DEC: None\n\n')
        file_obj.write('[settings]\n\n')
        file_obj.write('PIXSCALE: None\n')
        file_obj.write('MEMORY: None\n')
        file_obj.write('CPU: None\n')
        file_obj.close()

        pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

        attribute = pipeline.get_attribute("config", "MEMORY", static=True)
        assert attribute == 0

        attribute = pipeline.get_attribute("config", "CPU", static=True)
        assert attribute == 0

        attribute = pipeline.get_attribute("config", "PIXSCALE", static=True)
        assert np.allclose(attribute, 0.0, rtol=limit, atol=0.)

        attribute = pipeline.get_attribute("config", "INSTRUMENT", static=True)
        assert attribute == "None"

        create_config(self.test_dir + "PynPoint_config.ini")
コード例 #5
0
    def test_create_pipeline_existing_database(self):
        np.random.seed(1)
        images = np.random.normal(loc=0, scale=2e-4, size=(10, 100, 100))

        with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'w') as hdf_file:
            dset = hdf_file.create_dataset('images', data=images)
            dset.attrs['PIXSCALE'] = 0.01

        pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)
        data = pipeline.get_data('images')

        assert np.allclose(data[0, 0, 0], 0.00032486907273264834, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 1.0506056979365338e-06, rtol=limit, atol=0.)
        assert pipeline.get_attribute('images', 'PIXSCALE') == 0.01

        os.remove(self.test_dir+'PynPoint_database.hdf5')
コード例 #6
0
ファイル: test_pypeline.py プロジェクト: nenasedk/PynPoint
    def test_create_pipeline_existing_database(self) -> None:

        np.random.seed(1)
        images = np.random.normal(loc=0, scale=2e-4, size=(5, 11, 11))

        with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'w') as hdf_file:
            dset = hdf_file.create_dataset('images', data=images)
            dset.attrs['PIXSCALE'] = 0.01

        pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

        data = pipeline.get_data('images')
        assert np.mean(data) == pytest.approx(1.1824138000882435e-05, rel=self.limit, abs=0.)
        assert data.shape == (5, 11, 11)

        assert pipeline.get_attribute('images', 'PIXSCALE') == 0.01

        os.remove(self.test_dir+'PynPoint_database.hdf5')
コード例 #7
0
    def test_create_pipeline_existing_database(self):
        np.random.seed(1)
        images = np.random.normal(loc=0, scale=2e-4, size=(10, 100, 100))

        h5f = h5py.File(self.test_dir + "PynPoint_database.hdf5", "w")
        dset = h5f.create_dataset("images", data=images)
        dset.attrs['PIXSCALE'] = 0.01
        h5f.close()

        pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)
        data = pipeline.get_data("images")

        assert np.allclose(data[0, 0, 0],
                           0.00032486907273264834,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           1.0506056979365338e-06,
                           rtol=limit,
                           atol=0.)
        assert pipeline.get_attribute("images", "PIXSCALE") == 0.01

        os.remove(self.test_dir + "PynPoint_database.hdf5")
コード例 #8
0
class TestFrameSelection:
    def setup_class(self) -> None:

        self.limit = 1e-10
        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(self.test_dir + 'images')
        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self) -> None:

        remove_test_data(self.test_dir, folders=['images'])

    def test_read_data(self) -> None:

        module = FitsReadingModule(name_in='read',
                                   image_tag='read',
                                   input_dir=self.test_dir + 'images',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('read')

        data = self.pipeline.get_data('read')
        assert np.sum(data) == pytest.approx(105.54278879805277,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

        attr = self.pipeline.get_attribute('read', 'NDIT', static=False)
        assert np.sum(attr) == pytest.approx(10, rel=self.limit, abs=0.)
        assert attr.shape == (2, )

        attr = self.pipeline.get_attribute('read', 'NFRAMES', static=False)
        assert np.sum(attr) == pytest.approx(10, rel=self.limit, abs=0.)
        assert attr.shape == (2, )

        self.pipeline.set_attribute('read', 'NDIT', [4, 4], static=False)

    def test_remove_last_frame(self) -> None:

        module = RemoveLastFrameModule(name_in='last',
                                       image_in_tag='read',
                                       image_out_tag='last')

        self.pipeline.add_module(module)
        self.pipeline.run_module('last')

        data = self.pipeline.get_data('last')
        assert np.sum(data) == pytest.approx(84.68885503527224,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (8, 11, 11)

        self.pipeline.set_attribute('last',
                                    'PARANG',
                                    np.arange(8.),
                                    static=False)
        self.pipeline.set_attribute('last',
                                    'STAR_POSITION',
                                    np.full((8, 2), 5.),
                                    static=False)

        attr = self.pipeline.get_attribute('last', 'PARANG', static=False)
        assert np.sum(attr) == pytest.approx(28., rel=self.limit, abs=0.)
        assert attr.shape == (8, )

        attr = self.pipeline.get_attribute('last',
                                           'STAR_POSITION',
                                           static=False)
        assert np.sum(attr) == pytest.approx(80., rel=self.limit, abs=0.)
        assert attr.shape == (8, 2)

    def test_remove_start_frame(self) -> None:

        module = RemoveStartFramesModule(frames=1,
                                         name_in='start',
                                         image_in_tag='last',
                                         image_out_tag='start')

        self.pipeline.add_module(module)
        self.pipeline.run_module('start')

        data = self.pipeline.get_data('start')
        assert np.sum(data) == pytest.approx(64.44307047549808,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (6, 11, 11)

        attr = self.pipeline.get_attribute('start', 'PARANG', static=False)
        assert np.sum(attr) == pytest.approx(24., rel=self.limit, abs=0.)
        assert attr.shape == (6, )

        attr = self.pipeline.get_attribute('start',
                                           'STAR_POSITION',
                                           static=False)
        assert np.sum(attr) == pytest.approx(60., rel=self.limit, abs=0.)
        assert attr.shape == (6, 2)

    def test_remove_frames(self) -> None:

        module = RemoveFramesModule(name_in='remove',
                                    image_in_tag='start',
                                    selected_out_tag='selected',
                                    removed_out_tag='removed',
                                    frames=[2, 5])

        self.pipeline.add_module(module)
        self.pipeline.run_module('remove')

        data = self.pipeline.get_data('selected')
        assert np.sum(data) == pytest.approx(43.68337741822863,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (4, 11, 11)

        data = self.pipeline.get_data('removed')
        assert np.sum(data) == pytest.approx(20.759693057269445,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (2, 11, 11)

        attr = self.pipeline.get_attribute('selected', 'PARANG', static=False)
        assert np.sum(attr) == pytest.approx(14., rel=self.limit, abs=0.)
        assert attr.shape == (4, )

        attr = self.pipeline.get_attribute('selected',
                                           'STAR_POSITION',
                                           static=False)
        assert np.sum(attr) == pytest.approx(40., rel=self.limit, abs=0.)
        assert attr.shape == (4, 2)

        attr = self.pipeline.get_attribute('removed', 'PARANG', static=False)
        assert np.sum(attr) == pytest.approx(10., rel=self.limit, abs=0.)
        assert attr.shape == (2, )

        attr = self.pipeline.get_attribute('removed',
                                           'STAR_POSITION',
                                           static=False)
        assert np.sum(attr) == pytest.approx(20., rel=self.limit, abs=0.)
        assert attr.shape == (2, 2)

    def test_frame_selection(self) -> None:

        module = FrameSelectionModule(name_in='select1',
                                      image_in_tag='start',
                                      selected_out_tag='selected1',
                                      removed_out_tag='removed1',
                                      index_out_tag='index1',
                                      method='median',
                                      threshold=2.,
                                      fwhm=0.1,
                                      aperture=('circular', 0.1),
                                      position=(None, None, 0.2))

        self.pipeline.add_module(module)
        self.pipeline.run_module('select1')

        data = self.pipeline.get_data('selected1')
        assert np.sum(data) == pytest.approx(54.58514780071149,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (5, 11, 11)

        data = self.pipeline.get_data('removed1')
        assert np.sum(data) == pytest.approx(9.857922674786586,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (1, 11, 11)

        data = self.pipeline.get_data('index1')
        assert np.sum(data) == pytest.approx(5, rel=self.limit, abs=0.)
        assert data.shape == (1, )

        attr = self.pipeline.get_attribute('selected1', 'PARANG', static=False)
        assert np.sum(attr) == pytest.approx(17., rel=self.limit, abs=0.)
        assert attr.shape == (5, )

        attr = self.pipeline.get_attribute('selected1',
                                           'STAR_POSITION',
                                           static=False)
        assert np.sum(attr) == pytest.approx(50, rel=self.limit, abs=0.)
        assert attr.shape == (5, 2)

        attr = self.pipeline.get_attribute('removed1', 'PARANG', static=False)
        assert np.sum(attr) == pytest.approx(7., rel=self.limit, abs=0.)
        assert attr.shape == (1, )

        attr = self.pipeline.get_attribute('removed1',
                                           'STAR_POSITION',
                                           static=False)
        assert np.sum(attr) == pytest.approx(10, rel=self.limit, abs=0.)
        assert attr.shape == (1, 2)

        module = FrameSelectionModule(name_in='select2',
                                      image_in_tag='start',
                                      selected_out_tag='selected2',
                                      removed_out_tag='removed2',
                                      index_out_tag='index2',
                                      method='max',
                                      threshold=1.,
                                      fwhm=0.1,
                                      aperture=('annulus', 0.05, 0.1),
                                      position=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('select2')

        data = self.pipeline.get_data('selected2')
        assert np.sum(data) == pytest.approx(21.42652724866543,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (2, 11, 11)

        data = self.pipeline.get_data('removed2')
        assert np.sum(data) == pytest.approx(43.016543226832646,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (4, 11, 11)

        data = self.pipeline.get_data('index2')
        assert np.sum(data) == pytest.approx(10, rel=self.limit, abs=0.)
        assert data.shape == (4, )

        attr = self.pipeline.get_attribute('selected2', 'PARANG', static=False)
        assert np.sum(attr) == pytest.approx(8., rel=self.limit, abs=0.)
        assert attr.shape == (2, )

        attr = self.pipeline.get_attribute('selected2',
                                           'STAR_POSITION',
                                           static=False)
        assert np.sum(attr) == pytest.approx(20, rel=self.limit, abs=0.)
        assert attr.shape == (2, 2)

        attr = self.pipeline.get_attribute('removed2', 'PARANG', static=False)
        assert np.sum(attr) == pytest.approx(16., rel=self.limit, abs=0.)
        assert attr.shape == (4, )

        attr = self.pipeline.get_attribute('removed2',
                                           'STAR_POSITION',
                                           static=False)
        assert np.sum(attr) == pytest.approx(40, rel=self.limit, abs=0.)
        assert attr.shape == (4, 2)

        module = FrameSelectionModule(name_in='select3',
                                      image_in_tag='start',
                                      selected_out_tag='selected3',
                                      removed_out_tag='removed3',
                                      index_out_tag='index3',
                                      method='range',
                                      threshold=(10., 10.7),
                                      fwhm=0.1,
                                      aperture=('circular', 0.1),
                                      position=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('select3')

        data = self.pipeline.get_data('selected3')
        assert np.sum(data) == pytest.approx(22.2568501695632,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (2, 11, 11)

        data = self.pipeline.get_data('removed3')
        assert np.sum(data) == pytest.approx(42.18622030593487,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (4, 11, 11)

        data = self.pipeline.get_data('index3')
        assert np.sum(data) == pytest.approx(12, rel=self.limit, abs=0.)
        assert data.shape == (4, )

    def test_image_statistics_full(self) -> None:

        module = ImageStatisticsModule(name_in='stat1',
                                       image_in_tag='read',
                                       stat_out_tag='stat1',
                                       position=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('stat1')

        data = self.pipeline.get_data('stat1')
        assert np.sum(data) == pytest.approx(115.68591492205017,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 6)

    def test_image_statistics_posiiton(self) -> None:

        module = ImageStatisticsModule(name_in='stat2',
                                       image_in_tag='read',
                                       stat_out_tag='stat2',
                                       position=(5, 5, 0.1))

        self.pipeline.add_module(module)
        self.pipeline.run_module('stat2')

        data = self.pipeline.get_data('stat2')
        assert np.sum(data) == pytest.approx(118.7138708968444,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 6)

    def test_frame_similarity_mse(self) -> None:

        module = FrameSimilarityModule(name_in='simi1',
                                       image_tag='read',
                                       method='MSE',
                                       mask_radius=(0., 0.2))

        self.pipeline.add_module(module)
        self.pipeline.run_module('simi1')

        attr = self.pipeline.get_attribute('read', 'MSE', static=False)
        assert np.min(attr) > 0.
        assert np.sum(attr) == pytest.approx(0.11739141370277852,
                                             rel=self.limit,
                                             abs=0.)
        assert attr.shape == (10, )

    def test_frame_similarity_pcc(self) -> None:

        module = FrameSimilarityModule(name_in='simi2',
                                       image_tag='read',
                                       method='PCC',
                                       mask_radius=(0., 0.2))

        self.pipeline.add_module(module)
        self.pipeline.run_module('simi2')

        attr = self.pipeline.get_attribute('read', 'PCC', static=False)
        assert np.min(attr) > 0.
        assert np.sum(attr) == pytest.approx(9.134820985662829,
                                             rel=self.limit,
                                             abs=0.)
        assert attr.shape == (10, )

    def test_frame_similarity_ssim(self) -> None:

        module = FrameSimilarityModule(name_in='simi3',
                                       image_tag='read',
                                       method='SSIM',
                                       mask_radius=(0., 0.2),
                                       temporal_median='constant')

        self.pipeline.add_module(module)
        self.pipeline.run_module('simi3')

        attr = self.pipeline.get_attribute('read', 'SSIM', static=False)
        assert np.min(attr) > 0.
        assert np.sum(attr) == pytest.approx(9.096830542868524,
                                             rel=self.limit,
                                             abs=0.)
        assert attr.shape == (10, )

    def test_select_by_attribute(self) -> None:

        self.pipeline.set_attribute('read',
                                    'INDEX',
                                    np.arange(44),
                                    static=False)

        module = SelectByAttributeModule(name_in='frame_removal_1',
                                         image_in_tag='read',
                                         attribute_tag='SSIM',
                                         number_frames=6,
                                         order='descending',
                                         selected_out_tag='select_sim',
                                         removed_out_tag='remove_sim')

        self.pipeline.add_module(module)
        self.pipeline.run_module('frame_removal_1')

        attr = self.pipeline.get_attribute('select_sim', 'INDEX', static=False)
        assert np.sum(attr) == pytest.approx(946, rel=self.limit, abs=0.)
        assert attr.shape == (44, )

        attr = self.pipeline.get_attribute('select_sim', 'SSIM', static=False)
        assert np.sum(attr) == pytest.approx(5.556889532446573,
                                             rel=self.limit,
                                             abs=0.)
        assert attr.shape == (6, )

        attr = self.pipeline.get_attribute('remove_sim', 'SSIM', static=False)
        assert np.sum(attr) == pytest.approx(3.539941010421951,
                                             rel=self.limit,
                                             abs=0.)
        assert attr.shape == (4, )

    def test_residual_selection(self) -> None:

        module = ResidualSelectionModule(name_in='residual_select',
                                         image_in_tag='start',
                                         selected_out_tag='res_selected',
                                         removed_out_tag='res_removed',
                                         percentage=80.,
                                         annulus_radii=(0.1, 0.2))

        self.pipeline.add_module(module)
        self.pipeline.run_module('residual_select')

        data = self.pipeline.get_data('res_selected')
        assert np.sum(data) == pytest.approx(41.77295229983322,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (4, 11, 11)

        data = self.pipeline.get_data('res_removed')
        assert np.sum(data) == pytest.approx(22.670118175664847,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (2, 11, 11)
コード例 #9
0
class TestBackground:
    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + '/'

        create_fake(path=self.test_dir + 'dither',
                    ndit=[20, 20, 20, 20],
                    nframes=[20, 20, 20, 20],
                    exp_no=[1, 2, 3, 4],
                    npix=(100, 100),
                    fwhm=3.,
                    x0=[25, 75, 75, 25],
                    y0=[75, 75, 25, 25],
                    angles=[[0., 25.], [25., 50.], [50., 75.], [75., 100.]],
                    sep=None,
                    contrast=None)

        create_fake(path=self.test_dir + 'star',
                    ndit=[10, 10, 10, 10],
                    nframes=[10, 10, 10, 10],
                    exp_no=[1, 3, 5, 7],
                    npix=(100, 100),
                    fwhm=3.,
                    x0=[50, 50, 50, 50],
                    y0=[50, 50, 50, 50],
                    angles=[[0., 25.], [25., 50.], [50., 75.], [75., 100.]],
                    sep=None,
                    contrast=None)

        create_fake(path=self.test_dir + 'sky',
                    ndit=[5, 5, 5, 5],
                    nframes=[5, 5, 5, 5],
                    exp_no=[2, 4, 6, 8],
                    npix=(100, 100),
                    fwhm=None,
                    x0=[50, 50, 50, 50],
                    y0=[50, 50, 50, 50],
                    angles=[[0., 25.], [25., 50.], [50., 75.], [75., 100.]],
                    sep=None,
                    contrast=None)

        create_fake(path=self.test_dir + 'line',
                    ndit=[4, 4, 4, 4],
                    nframes=[4, 4, 4, 4],
                    exp_no=[1, 3, 5, 7],
                    npix=(20, 20),
                    fwhm=3.,
                    x0=[10, 10, 10, 10],
                    y0=[10, 10, 10, 10],
                    angles=[[0., 25.], [25., 50.], [50., 75.], [75., 100.]],
                    sep=None,
                    contrast=None)

        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir,
                         folders=['dither', 'star', 'sky', 'line'])

    def test_read_data(self):

        read = FitsReadingModule(name_in='read1',
                                 image_tag='dither',
                                 input_dir=self.test_dir + 'dither')

        self.pipeline.add_module(read)

        read = FitsReadingModule(name_in='read2',
                                 image_tag='star',
                                 input_dir=self.test_dir + 'star')

        self.pipeline.add_module(read)

        read = FitsReadingModule(name_in='read3',
                                 image_tag='sky',
                                 input_dir=self.test_dir + 'sky')

        self.pipeline.add_module(read)

        read = FitsReadingModule(name_in='read4',
                                 image_tag='line',
                                 input_dir=self.test_dir + 'line')

        self.pipeline.add_module(read)

        self.pipeline.run_module('read1')
        self.pipeline.run_module('read2')
        self.pipeline.run_module('read3')
        self.pipeline.run_module('read4')

        data = self.pipeline.get_data('dither')
        assert np.allclose(data[0, 74, 24],
                           0.05304008435511765,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010033896953157959,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (80, 100, 100)

        data = self.pipeline.get_data('star')
        assert np.allclose(data[0, 50, 50],
                           0.09798413502193704,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738066,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

        data = self.pipeline.get_data('sky')
        assert np.allclose(data[0, 50, 50],
                           -7.613171257478652e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           8.937360237872607e-07,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (20, 100, 100)

        data = self.pipeline.get_data('line')
        assert np.allclose(data[0, 10, 10],
                           0.09799496683489618,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.002502384977510189,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (16, 20, 20)

    def test_simple_background(self):

        simple = SimpleBackgroundSubtractionModule(shift=20,
                                                   name_in='simple',
                                                   image_in_tag='dither',
                                                   image_out_tag='simple')

        self.pipeline.add_module(simple)
        self.pipeline.run_module('simple')

        data = self.pipeline.get_data('simple')
        assert np.allclose(data[0, 74, 74],
                           -0.05288064325101517,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           2.7755575615628916e-22,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (80, 100, 100)

    def test_mean_background_shift(self):

        mean = MeanBackgroundSubtractionModule(shift=20,
                                               cubes=1,
                                               name_in='mean2',
                                               image_in_tag='dither',
                                               image_out_tag='mean2')

        self.pipeline.add_module(mean)
        self.pipeline.run_module('mean2')

        data = self.pipeline.get_data('mean2')
        assert np.allclose(data[0, 74, 24],
                           0.0530465391626132,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           1.3970872216676808e-07,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (80, 100, 100)

    def test_mean_background_nframes(self):

        mean = MeanBackgroundSubtractionModule(shift=None,
                                               cubes=1,
                                               name_in='mean1',
                                               image_in_tag='dither',
                                               image_out_tag='mean1')

        self.pipeline.add_module(mean)
        self.pipeline.run_module('mean1')

        data = self.pipeline.get_data('mean1')
        assert np.allclose(data[0, 74, 24],
                           0.0530465391626132,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           1.3970872216676808e-07,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (80, 100, 100)

    def test_dithering_attributes(self):

        pca_dither = DitheringBackgroundModule(name_in='pca_dither1',
                                               image_in_tag='dither',
                                               image_out_tag='pca_dither1',
                                               center=None,
                                               cubes=None,
                                               size=0.8,
                                               gaussian=0.1,
                                               subframe=0.5,
                                               pca_number=5,
                                               mask_star=0.1,
                                               crop=True,
                                               prepare=True,
                                               pca_background=True,
                                               combine='pca')

        self.pipeline.add_module(pca_dither)
        self.pipeline.run_module('pca_dither1')

        data = self.pipeline.get_data('dither_dither_crop1')
        assert np.allclose(data[0, 14, 14],
                           0.05304008435511765,
                           rtol=1e-6,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.0002606205855710527,
                           rtol=1e-6,
                           atol=0.)
        assert data.shape == (80, 31, 31)

        data = self.pipeline.get_data('dither_dither_star1')
        assert np.allclose(data[0, 14, 14],
                           0.05304008435511765,
                           rtol=1e-6,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.0010414302265833978,
                           rtol=1e-6,
                           atol=0.)
        assert data.shape == (20, 31, 31)

        data = self.pipeline.get_data('dither_dither_mean1')
        assert np.allclose(data[0, 14, 14],
                           0.0530465391626132,
                           rtol=1e-6,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.0010426228104479674,
                           rtol=1e-6,
                           atol=0.)
        assert data.shape == (20, 31, 31)

        data = self.pipeline.get_data('dither_dither_background1')
        assert np.allclose(data[0, 14, 14],
                           -0.00010629310882411674,
                           rtol=1e-6,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           3.5070523360436835e-07,
                           rtol=1e-6,
                           atol=0.)
        assert data.shape == (60, 31, 31)

        data = self.pipeline.get_data('dither_dither_pca_fit1')
        assert np.allclose(data[0, 14, 14],
                           1.5196412298279846e-05,
                           rtol=1e-5,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           1.9779802529804516e-07,
                           rtol=1e-4,
                           atol=0.)
        assert data.shape == (20, 31, 31)

        data = self.pipeline.get_data('dither_dither_pca_res1')
        assert np.allclose(data[0, 14, 14],
                           0.05302488794281937,
                           rtol=1e-6,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.0010412324285580998,
                           rtol=1e-6,
                           atol=0.)
        assert data.shape == (20, 31, 31)

        data = self.pipeline.get_data('dither_dither_pca_mask1')
        assert np.allclose(data[0, 14, 14], 0., rtol=1e-6, atol=0.)
        assert np.allclose(np.mean(data),
                           0.9531737773152965,
                           rtol=1e-6,
                           atol=0.)
        assert data.shape == (20, 31, 31)

        data = self.pipeline.get_data('pca_dither1')
        assert np.allclose(data[0, 14, 14],
                           0.05302488794281937,
                           rtol=1e-6,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.001040627977720779,
                           rtol=1e-6,
                           atol=0.)
        assert data.shape == (80, 31, 31)

        data = self.pipeline.get_attribute('dither_dither_pca_res1',
                                           'STAR_POSITION',
                                           static=False)
        assert np.allclose(data[0, 0], [15., 15.], rtol=1e-6, atol=0.)
        assert np.allclose(np.mean(data), 15., rtol=1e-6, atol=0.)
        assert data.shape == (20, 2)

    def test_dithering_center(self):

        pca_dither = DitheringBackgroundModule(name_in='pca_dither2',
                                               image_in_tag='dither',
                                               image_out_tag='pca_dither2',
                                               center=((25., 75.), (75., 75.),
                                                       (75., 25.), (25., 25.)),
                                               cubes=1,
                                               size=0.8,
                                               gaussian=0.1,
                                               subframe=None,
                                               pca_number=5,
                                               mask_star=0.1,
                                               bad_pixel=None,
                                               crop=True,
                                               prepare=True,
                                               pca_background=True,
                                               combine='pca')

        self.pipeline.add_module(pca_dither)
        self.pipeline.run_module('pca_dither2')

        data = self.pipeline.get_data('pca_dither2')
        assert np.allclose(data[0, 14, 14],
                           0.05302488794328089,
                           rtol=1e-6,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.0010406279782666378,
                           rtol=1e-3,
                           atol=0.)
        assert data.shape == (80, 31, 31)

    def test_nodding_background(self):

        mean = StackCubesModule(name_in='mean',
                                image_in_tag='sky',
                                image_out_tag='mean',
                                combine='mean')

        self.pipeline.add_module(mean)
        self.pipeline.run_module('mean')

        data = self.pipeline.get_data('mean')
        assert np.allclose(data[0, 50, 50],
                           1.270877476321969e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           8.937360237872607e-07,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 100, 100)

        attribute = self.pipeline.get_attribute('mean', 'INDEX', static=False)
        assert np.allclose(np.mean(attribute), 1.5, rtol=limit, atol=0.)
        assert attribute.shape == (4, )

        attribute = self.pipeline.get_attribute('mean',
                                                'NFRAMES',
                                                static=False)
        assert np.allclose(np.mean(attribute), 1, rtol=limit, atol=0.)
        assert attribute.shape == (4, )

        nodding = NoddingBackgroundModule(name_in='nodding',
                                          sky_in_tag='mean',
                                          science_in_tag='star',
                                          image_out_tag='nodding',
                                          mode='both')

        self.pipeline.add_module(nodding)
        self.pipeline.run_module('nodding')

        data = self.pipeline.get_data('nodding')
        assert np.allclose(data[0, 50, 50],
                           0.09797142624717381,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           9.945087327935862e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

    def test_line_background_mean(self):

        module = LineSubtractionModule(name_in='line1',
                                       image_in_tag='line',
                                       image_out_tag='line_mean',
                                       combine='mean',
                                       mask=0.1)

        self.pipeline.add_module(module)
        self.pipeline.run_module('line1')

        data = self.pipeline.get_data('line_mean')
        assert np.allclose(data[0, 10, 10],
                           0.09792388324443534,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.0024245904637616735,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (16, 20, 20)

    def test_line_background_median(self):

        module = LineSubtractionModule(name_in='line2',
                                       image_in_tag='line',
                                       image_out_tag='line_median',
                                       combine='median',
                                       mask=0.1)

        self.pipeline.add_module(module)
        self.pipeline.run_module('line2')

        data = self.pipeline.get_data('line_median')
        assert np.allclose(data[0, 10, 10],
                           0.09782789699611127,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.0024723022374338196,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (16, 20, 20)
コード例 #10
0
class TestLimits:

    def setup_class(self) -> None:

        self.limit = 1e-10
        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(self.test_dir+'self.limits', npix=21, pos_star=10.)
        create_config(self.test_dir+'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self) -> None:

        remove_test_data(path=self.test_dir,
                         folders=['self.limits'],
                         files=['model.AMES-Cond-2000.M-0.0.NaCo.Vega'])

    def test_read_data(self) -> None:

        module = FitsReadingModule(name_in='read',
                                   image_tag='read',
                                   input_dir=self.test_dir+'self.limits')

        self.pipeline.add_module(module)
        self.pipeline.run_module('read')

        data = self.pipeline.get_data('read')
        assert np.sum(data) == pytest.approx(108.43655133957289, rel=self.limit, abs=0.)
        assert data.shape == (10, 21, 21)

    def test_angle_interpolation(self) -> None:

        module = AngleInterpolationModule(name_in='angle',
                                          data_tag='read')

        self.pipeline.add_module(module)
        self.pipeline.run_module('angle')

        attr = self.pipeline.get_attribute('read', 'PARANG', static=False)
        assert np.sum(attr) == pytest.approx(900., rel=self.limit, abs=0.)
        assert attr.shape == (10, )

    def test_contrast_curve(self) -> None:

        proc = ['single', 'multi']

        for item in proc:

            if item == 'multi':
                with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'a') as hdf_file:
                    hdf_file['config'].attrs['CPU'] = 4

            module = ContrastCurveModule(name_in='contrast_'+item,
                                         image_in_tag='read',
                                         psf_in_tag='read',
                                         contrast_out_tag='limits_'+item,
                                         separation=(0.2, 0.3, 0.2),
                                         angle=(0., 360., 180.),
                                         threshold=('sigma', 5.),
                                         psf_scaling=1.,
                                         aperture=0.05,
                                         pca_number=2,
                                         cent_size=None,
                                         edge_size=1.,
                                         extra_rot=0.)

            self.pipeline.add_module(module)
            self.pipeline.run_module('contrast_'+item)

            data = self.pipeline.get_data('limits_'+item)
            assert data[0, 0] == pytest.approx(0.2, rel=self.limit, abs=0.)
            assert data[0, 1] == pytest.approx(2.5223717329932676, rel=self.limit, abs=0.)
            assert data[0, 2] == pytest.approx(0.0006250749411563979, rel=self.limit, abs=0.)
            assert data[0, 3] == pytest.approx(0.00026866680137822624, rel=self.limit, abs=0.)
            assert data.shape == (1, 4)

    def test_contrast_curve_fpf(self) -> None:

        with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 1

        module = ContrastCurveModule(name_in='contrast_fpf',
                                     image_in_tag='read',
                                     psf_in_tag='read',
                                     contrast_out_tag='limits_fpf',
                                     separation=(0.2, 0.3, 0.2),
                                     angle=(0., 360., 180.),
                                     threshold=('fpf', 1e-6),
                                     psf_scaling=1.,
                                     aperture=0.05,
                                     pca_number=2,
                                     cent_size=None,
                                     edge_size=1.,
                                     extra_rot=0.)

        self.pipeline.add_module(module)
        self.pipeline.run_module('contrast_fpf')

        data = self.pipeline.get_data('limits_fpf')
        assert data[0, 0] == pytest.approx(0.2, rel=self.limit, abs=0.)
        assert data[0, 1] == pytest.approx(1.797063014325614, rel=self.limit, abs=0.)
        assert data[0, 2] == pytest.approx(0.0006250749411564145, rel=self.limit, abs=0.)
        assert data[0, 3] == pytest.approx(1e-06, rel=self.limit, abs=0.)
        assert data.shape == (1, 4)

    def test_mass_limits(self) -> None:

        separation = np.linspace(0.1, 1.0, 10)
        contrast = -2.5*np.log10(1e-4/separation)
        variance = 0.1*contrast

        limits = np.zeros((10, 4))
        limits[:, 0] = separation
        limits[:, 1] = contrast
        limits[:, 2] = variance

        with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'a') as hdf_file:
            hdf_file['contrast_limits'] = limits

        url = 'https://home.strw.leidenuniv.nl/~stolker/pynpoint/' \
              'model.AMES-Cond-2000.M-0.0.NaCo.Vega'

        filename = self.test_dir + 'model.AMES-Cond-2000.M-0.0.NaCo.Vega'

        urlretrieve(url, filename)

        module = MassLimitsModule(model_file=filename,
                                  star_prop={'magnitude': 10., 'distance': 100., 'age': 20.},
                                  name_in='mass',
                                  contrast_in_tag='contrast_limits',
                                  mass_out_tag='mass_limits',
                                  instr_filter='L\'')

        self.pipeline.add_module(module)
        self.pipeline.run_module('mass')

        data = self.pipeline.get_data('mass_limits')
        assert np.mean(data[:, 0]) == pytest.approx(0.55, rel=self.limit, abs=0.)
        assert np.mean(data[:, 1]) == pytest.approx(0.001891690765603738, rel=self.limit, abs=0.)
        assert np.mean(data[:, 2]) == pytest.approx(0.000964309686441908, rel=self.limit, abs=0.)
        assert np.mean(data[:, 3]) == pytest.approx(-0.000696402843279597, rel=self.limit, abs=0.)
        assert data.shape == (10, 4)
コード例 #11
0
class TestAttributeReading:
    def setup_class(self) -> None:

        self.test_dir = os.path.dirname(__file__) + '/'

        create_random(self.test_dir, ndit=10, parang=None)
        create_config(self.test_dir + 'PynPoint_config.ini')

        np.savetxt(self.test_dir + 'parang.dat', np.arange(1., 11., 1.))
        np.savetxt(self.test_dir + 'new.dat', np.arange(10., 21., 1.))
        np.savetxt(self.test_dir + 'attribute.dat',
                   np.arange(1, 11, 1),
                   fmt='%i')
        np.savetxt(self.test_dir + 'wavelength.dat', np.arange(1, 11, 1))

        data2d = np.random.normal(loc=0, scale=2e-4, size=(10, 10))
        np.savetxt(self.test_dir + 'data_2d.dat', data2d)

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self) -> None:

        remove_test_data(self.test_dir,
                         files=[
                             'parang.dat', 'new.dat', 'attribute.dat',
                             'data_2d.dat', 'wavelength.dat'
                         ])

    def test_input_data(self) -> None:

        data = self.pipeline.get_data('images')
        assert np.allclose(data[0, 75, 25],
                           6.921353838812206e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           1.0506056979365338e-06,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (10, 100, 100)

    def test_parang_reading(self) -> None:

        module = ParangReadingModule(file_name='parang.dat',
                                     name_in='parang1',
                                     input_dir=None,
                                     data_tag='images',
                                     overwrite=False)

        self.pipeline.add_module(module)
        self.pipeline.run_module('parang1')

        data = self.pipeline.get_data('header_images/PARANG')
        assert data.dtype == 'float64'
        assert np.allclose(data, np.arange(1., 11., 1.), rtol=limit, atol=0.)
        assert data.shape == (10, )

    def test_parang_reading_same(self) -> None:

        module = ParangReadingModule(file_name='parang.dat',
                                     name_in='parang2',
                                     input_dir=None,
                                     data_tag='images',
                                     overwrite=True)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('parang2')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'The PARANG attribute is already present and ' \
                                             'contains the same values as are present in ' \
                                             'parang.dat.'

    def test_parang_reading_present(self) -> None:

        module = ParangReadingModule(file_name='new.dat',
                                     name_in='parang3',
                                     input_dir=None,
                                     data_tag='images',
                                     overwrite=False)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('parang3')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'The PARANG attribute is already present. Set the ' \
                                             '\'overwrite\' parameter to True in order to ' \
                                             'overwrite the values with new.dat.'

    def test_parang_reading_overwrite(self) -> None:

        module = ParangReadingModule(file_name='new.dat',
                                     name_in='parang4',
                                     input_dir=None,
                                     data_tag='images',
                                     overwrite=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('parang4')

    def test_parang_reading_2d(self) -> None:

        module = ParangReadingModule(file_name='data_2d.dat',
                                     name_in='parang6',
                                     input_dir=None,
                                     data_tag='images',
                                     overwrite=False)

        self.pipeline.add_module(module)

        with pytest.raises(ValueError) as error:
            self.pipeline.run_module('parang6')

        assert str(error.value) == 'The input file data_2d.dat should contain a 1D data set with ' \
                                   'the parallactic angles.'

    def test_attribute_reading(self) -> None:

        module = AttributeReadingModule(file_name='attribute.dat',
                                        attribute='EXP_NO',
                                        name_in='attribute1',
                                        input_dir=None,
                                        data_tag='images',
                                        overwrite=False)

        self.pipeline.add_module(module)
        self.pipeline.run_module('attribute1')

        data = self.pipeline.get_data('header_images/EXP_NO')
        assert data.dtype == 'int64'
        assert np.allclose(data, np.arange(1, 11, 1), rtol=limit, atol=0.)
        assert data.shape == (10, )

    def test_attribute_reading_present(self) -> None:

        module = AttributeReadingModule(file_name='parang.dat',
                                        attribute='PARANG',
                                        name_in='attribute3',
                                        input_dir=None,
                                        data_tag='images',
                                        overwrite=False)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('attribute3')

        assert warning[0].message.args[0] == 'The attribute \'PARANG\' is already present. Set ' \
                                             'the \'overwrite\' parameter to True in order to ' \
                                             'overwrite the values with parang.dat.'

    def test_attribute_reading_invalid(self) -> None:

        module = AttributeReadingModule(file_name='attribute.dat',
                                        attribute='test',
                                        name_in='attribute4',
                                        input_dir=None,
                                        data_tag='images',
                                        overwrite=False)

        self.pipeline.add_module(module)

        with pytest.raises(ValueError) as error:
            self.pipeline.run_module('attribute4')

        assert str(error.value) == '\'test\' is not a valid attribute.'

    def test_attribute_reading_2d(self) -> None:

        module = AttributeReadingModule(file_name='data_2d.dat',
                                        attribute='DITHER_X',
                                        name_in='attribute5',
                                        input_dir=None,
                                        data_tag='images',
                                        overwrite=False)

        self.pipeline.add_module(module)

        with pytest.raises(ValueError) as error:
            self.pipeline.run_module('attribute5')

        assert str(error.value) == 'The input file data_2d.dat should contain a 1D list with ' \
                                   'attributes.'

    def test_attribute_reading_same(self) -> None:

        module = AttributeReadingModule(file_name='attribute.dat',
                                        attribute='EXP_NO',
                                        name_in='attribute6',
                                        input_dir=None,
                                        data_tag='images',
                                        overwrite=True)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('attribute6')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'The \'EXP_NO\' attribute is already present and ' \
                                             'contains the same values as are present in ' \
                                             'attribute.dat.'

    def test_attribute_reading_overwrite(self) -> None:

        module = AttributeReadingModule(file_name='parang.dat',
                                        attribute='PARANG',
                                        name_in='attribute7',
                                        input_dir=None,
                                        data_tag='images',
                                        overwrite=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('attribute7')

        attribute = self.pipeline.get_attribute('images',
                                                'PARANG',
                                                static=False)
        assert np.allclose(attribute,
                           np.arange(1., 11., 1.),
                           rtol=limit,
                           atol=0.)

    def test_wavelength_reading(self) -> None:

        module = WavelengthReadingModule(file_name='wavelength.dat',
                                         name_in='wavelength1',
                                         input_dir=None,
                                         data_tag='images',
                                         overwrite=False)

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelength1')

        data = self.pipeline.get_data('header_images/WAVELENGTH')
        assert data.dtype == 'float64'
        assert np.allclose(data, np.arange(1., 11., 1.), rtol=limit, atol=0.)
        assert data.shape == (10, )

    def test_wavelength_reading_same(self) -> None:

        module = WavelengthReadingModule(file_name='wavelength.dat',
                                         name_in='wavelength2',
                                         input_dir=None,
                                         data_tag='images',
                                         overwrite=True)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('wavelength2')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'The WAVELENGTH attribute is already present and ' \
                                             'contains the same values as are present in ' \
                                             'wavelength.dat.'

    def test_wavelength_reading_present(self) -> None:

        module = WavelengthReadingModule(file_name='new.dat',
                                         name_in='wavelength3',
                                         input_dir=None,
                                         data_tag='images',
                                         overwrite=False)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('wavelength3')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'The WAVELENGTH attribute is already present. Set ' \
                                             'the \'overwrite\' parameter to True in order to ' \
                                             'overwrite the values with new.dat.'

    def test_wavelength_reading_overwrite(self) -> None:

        module = WavelengthReadingModule(file_name='new.dat',
                                         name_in='wavelength4',
                                         input_dir=None,
                                         data_tag='images',
                                         overwrite=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelength4')

    def test_wavelength_reading_2d(self) -> None:

        module = WavelengthReadingModule(file_name='data_2d.dat',
                                         name_in='wavelength6',
                                         input_dir=None,
                                         data_tag='images',
                                         overwrite=False)

        self.pipeline.add_module(module)

        with pytest.raises(ValueError) as error:
            self.pipeline.run_module('wavelength6')

        assert str(error.value) == 'The input file data_2d.dat should contain a 1D data set with ' \
                                   'the wavelengths.'
コード例 #12
0
class TestFrameSelection:
    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(path=self.test_dir + 'images', ndit=10, nframes=11)
        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir, folders=['images'])

    def test_read_data(self):

        module = FitsReadingModule(name_in='read',
                                   image_tag='read',
                                   input_dir=self.test_dir + 'images',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('read')

        data = self.pipeline.get_data('read')
        assert np.allclose(data[0, 50, 50],
                           0.09798413502193704,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.0001002167910262529,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (44, 100, 100)

    def test_remove_last_frame(self):

        module = RemoveLastFrameModule(name_in='last',
                                       image_in_tag='read',
                                       image_out_tag='last')

        self.pipeline.add_module(module)
        self.pipeline.run_module('last')

        data = self.pipeline.get_data('last')
        assert np.allclose(data[0, 50, 50],
                           0.09798413502193704,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010020258903646778,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

        self.pipeline.set_attribute('last',
                                    'PARANG',
                                    np.arange(0., 40., 1.),
                                    static=False)

        star = np.zeros((40, 2))
        star[:, 0] = np.arange(40., 80., 1.)
        star[:, 1] = np.arange(40., 80., 1.)

        self.pipeline.set_attribute('last',
                                    'STAR_POSITION',
                                    star,
                                    static=False)

        attribute = self.pipeline.get_attribute('last', 'PARANG', static=False)
        assert np.allclose(np.mean(attribute), 19.5, rtol=limit, atol=0.)
        assert attribute.shape == (40, )

        attribute = self.pipeline.get_attribute('last',
                                                'STAR_POSITION',
                                                static=False)
        assert np.allclose(np.mean(attribute), 59.5, rtol=limit, atol=0.)
        assert attribute.shape == (40, 2)

    def test_remove_start_frame(self):

        module = RemoveStartFramesModule(frames=2,
                                         name_in='start',
                                         image_in_tag='last',
                                         image_out_tag='start')

        self.pipeline.add_module(module)
        self.pipeline.run_module('start')

        data = self.pipeline.get_data('start')
        assert np.allclose(data[0, 50, 50],
                           0.09797376304048713,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010011298467340513,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (32, 100, 100)

        attribute = self.pipeline.get_attribute('start',
                                                'PARANG',
                                                static=False)
        assert np.allclose(np.mean(attribute), 20.5, rtol=limit, atol=0.)
        assert attribute.shape == (32, )

        attribute = self.pipeline.get_attribute('start',
                                                'STAR_POSITION',
                                                static=False)
        assert np.allclose(np.mean(attribute), 60.5, rtol=limit, atol=0.)
        assert attribute.shape == (32, 2)

    def test_remove_frames(self):

        module = RemoveFramesModule(name_in='remove',
                                    image_in_tag='start',
                                    selected_out_tag='selected',
                                    removed_out_tag='removed',
                                    frames=[5, 8, 13, 25, 31])

        self.pipeline.add_module(module)
        self.pipeline.run_module('remove')

        data = self.pipeline.get_data('selected')
        assert np.allclose(data[0, 50, 50],
                           0.09797376304048713,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           9.984682304434105e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (27, 100, 100)

        data = self.pipeline.get_data('removed')
        assert np.allclose(data[0, 50, 50],
                           0.09818692015286978,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010155025747035087,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (5, 100, 100)

        attribute = self.pipeline.get_attribute('selected',
                                                'PARANG',
                                                static=False)
        assert np.allclose(np.mean(attribute),
                           20.296296296296298,
                           rtol=limit,
                           atol=0.)
        assert attribute.shape == (27, )

        attribute = self.pipeline.get_attribute('selected',
                                                'STAR_POSITION',
                                                static=False)
        assert np.allclose(np.mean(attribute),
                           60.2962962962963,
                           rtol=limit,
                           atol=0.)
        assert attribute.shape == (27, 2)

        attribute = self.pipeline.get_attribute('removed',
                                                'PARANG',
                                                static=False)
        assert np.allclose(np.mean(attribute), 21.6, rtol=limit, atol=0.)
        assert attribute.shape == (5, )

        attribute = self.pipeline.get_attribute('removed',
                                                'STAR_POSITION',
                                                static=False)
        assert np.allclose(np.mean(attribute), 61.6, rtol=limit, atol=0.)
        assert attribute.shape == (5, 2)

    def test_frame_selection(self):

        module = FrameSelectionModule(name_in='select1',
                                      image_in_tag='start',
                                      selected_out_tag='selected1',
                                      removed_out_tag='removed1',
                                      index_out_tag='index1',
                                      method='median',
                                      threshold=1.,
                                      fwhm=0.1,
                                      aperture=('circular', 0.2),
                                      position=(None, None, 0.5))

        self.pipeline.add_module(module)
        self.pipeline.run_module('select1')

        data = self.pipeline.get_data('selected1')
        assert np.allclose(data[0, 50, 50],
                           0.09791350617182591,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           9.980792188317311e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (22, 100, 100)

        data = self.pipeline.get_data('removed1')
        assert np.allclose(data[0, 50, 50],
                           0.09797376304048713,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010078412281191547,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (10, 100, 100)

        data = self.pipeline.get_data('index1')
        assert data[-1] == 28
        assert np.sum(data) == 115
        assert data.shape == (10, )

        attribute = self.pipeline.get_attribute('selected1',
                                                'PARANG',
                                                static=False)
        assert np.allclose(np.mean(attribute),
                           22.681818181818183,
                           rtol=limit,
                           atol=0.)
        assert attribute.shape == (22, )

        attribute = self.pipeline.get_attribute('selected1',
                                                'STAR_POSITION',
                                                static=False)
        assert np.allclose(np.mean(attribute), 50.0, rtol=limit, atol=0.)
        assert attribute.shape == (22, 2)

        attribute = self.pipeline.get_attribute('removed1',
                                                'PARANG',
                                                static=False)
        assert np.allclose(np.mean(attribute), 15.7, rtol=limit, atol=0.)
        assert attribute.shape == (10, )

        attribute = self.pipeline.get_attribute('removed1',
                                                'STAR_POSITION',
                                                static=False)
        assert np.allclose(np.mean(attribute), 50.0, rtol=limit, atol=0.)
        assert attribute.shape == (10, 2)

        module = FrameSelectionModule(name_in='select2',
                                      image_in_tag='start',
                                      selected_out_tag='selected2',
                                      removed_out_tag='removed2',
                                      index_out_tag='index2',
                                      method='max',
                                      threshold=3.,
                                      fwhm=0.1,
                                      aperture=('annulus', 0.1, 0.2),
                                      position=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('select2')

        data = self.pipeline.get_data('selected2')
        assert np.allclose(data[0, 50, 50],
                           0.09797376304048713,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010037996502199598,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (20, 100, 100)

        data = self.pipeline.get_data('removed2')
        assert np.allclose(data[0, 50, 50],
                           0.097912284606689,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           9.966801742575358e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (12, 100, 100)

        data = self.pipeline.get_data('index2')
        assert data[-1] == 30
        assert np.sum(data) == 230
        assert data.shape == (12, )

        attribute = self.pipeline.get_attribute('selected2',
                                                'PARANG',
                                                static=False)
        assert np.allclose(np.mean(attribute), 17.8, rtol=limit, atol=0.)
        assert attribute.shape == (20, )

        attribute = self.pipeline.get_attribute('selected2',
                                                'STAR_POSITION',
                                                static=False)
        assert np.allclose(np.mean(attribute), 50.0, rtol=limit, atol=0.)
        assert attribute.shape == (20, 2)

        attribute = self.pipeline.get_attribute('removed2',
                                                'PARANG',
                                                static=False)
        assert np.allclose(np.mean(attribute), 25.0, rtol=limit, atol=0.)
        assert attribute.shape == (12, )

        attribute = self.pipeline.get_attribute('removed2',
                                                'STAR_POSITION',
                                                static=False)
        assert np.allclose(np.mean(attribute), 50.0, rtol=limit, atol=0.)
        assert attribute.shape == (12, 2)

    def test_image_statistics_full(self):

        module = ImageStatisticsModule(name_in='stat1',
                                       image_in_tag='read',
                                       stat_out_tag='stat1',
                                       position=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('stat1')

        data = self.pipeline.get_data('stat1')
        assert np.allclose(data[0, 0],
                           -0.0007312880198509591,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.sum(data),
                           48.479917666979716,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (44, 6)

    def test_image_statistics_posiiton(self):

        module = ImageStatisticsModule(name_in='stat2',
                                       image_in_tag='read',
                                       stat_out_tag='stat2',
                                       position=(70, 20, 0.5))

        self.pipeline.add_module(module)
        self.pipeline.run_module('stat2')

        data = self.pipeline.get_data('stat2')
        assert np.allclose(data[0, 0],
                           -0.0006306714900382097,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.sum(data),
                           -0.05448258074038106,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (44, 6)

    def test_frame_similarity_mse(self):

        module = FrameSimilarityModule(name_in='simi1',
                                       image_tag='read',
                                       method='MSE',
                                       mask_radius=(0., 1.))

        self.pipeline.add_module(module)
        self.pipeline.run_module('simi1')

        similarity = self.pipeline.get_attribute('read', 'MSE', static=False)

        assert len(similarity) == self.pipeline.get_shape('read')[0]
        assert np.min(similarity) > 0
        assert similarity[4] != similarity[8]
        assert np.allclose(np.sum(similarity),
                           1.7938335695664495e-06,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(similarity[0],
                           4.103008589430469e-08,
                           rtol=limit,
                           atol=0.)

    def test_frame_similarity_pcc(self):

        module = FrameSimilarityModule(name_in='simi2',
                                       image_tag='read',
                                       method='PCC',
                                       mask_radius=(0., 1.))

        self.pipeline.add_module(module)
        self.pipeline.run_module('simi2')

        similarity = self.pipeline.get_attribute('read', 'PCC', static=False)

        assert len(similarity) == self.pipeline.get_shape('read')[0]
        assert np.min(similarity) > 0
        assert np.max(similarity) < 1
        assert similarity[4] != similarity[8]
        assert np.allclose(np.sum(similarity),
                           43.974652830856314,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(similarity[0],
                           0.9994193494590345,
                           rtol=limit,
                           atol=0.)

    def test_frame_similarity_ssim(self):

        module = FrameSimilarityModule(name_in='simi3',
                                       image_tag='read',
                                       method='SSIM',
                                       mask_radius=(0., 1.),
                                       temporal_median='constant')

        self.pipeline.add_module(module)
        self.pipeline.run_module('simi3')

        similarity = self.pipeline.get_attribute('read', 'SSIM', static=False)

        assert len(similarity) == self.pipeline.get_shape('read')[0]
        assert np.min(similarity) > 0
        assert np.max(similarity) < 1
        assert similarity[4] != similarity[8]
        assert np.allclose(np.sum(similarity),
                           43.999059977871184,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(similarity[0],
                           0.9999793908738922,
                           rtol=limit,
                           atol=0.)

    def test_select_by_attribute(self):

        total_length = self.pipeline.get_shape('read')[0]
        self.pipeline.set_attribute('read',
                                    'INDEX',
                                    range(total_length),
                                    static=False)
        attribute_tag = 'SSIM'
        module = SelectByAttributeModule(name_in='frame_removal_1',
                                         image_in_tag='read',
                                         attribute_tag=attribute_tag,
                                         number_frames=6,
                                         order='descending',
                                         selected_out_tag='select_sim',
                                         removed_out_tag='remove_sim')

        self.pipeline.add_module(module)
        self.pipeline.run_module('frame_removal_1')

        index = self.pipeline.get_attribute('select_sim',
                                            'INDEX',
                                            static=False)
        similarity = self.pipeline.get_attribute('select_sim',
                                                 attribute_tag,
                                                 static=False)
        sim_removed = self.pipeline.get_attribute('remove_sim',
                                                  attribute_tag,
                                                  static=False)

        # check attribute length
        assert self.pipeline.get_shape('select_sim')[0] == 6
        assert len(similarity) == 6
        assert len(similarity) == len(index)
        assert len(similarity) + len(sim_removed) == total_length

        # check sorted
        assert all(similarity[i] >= similarity[i + 1]
                   for i in range(len(similarity) - 1))

        # check that the selected attributes are in the correct tags
        assert np.min(similarity) > np.max(sim_removed)
コード例 #13
0
class TestStackingAndSubsampling(object):

    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + "/"

        create_star_data(path=self.test_dir+"data")
        create_star_data(path=self.test_dir+"extra")

        create_config(self.test_dir+"PynPoint_config.ini")

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir, folders=["data", "extra"])

    def test_read_data(self):

        read = FitsReadingModule(name_in="read1",
                                 image_tag="images",
                                 input_dir=self.test_dir+"data",
                                 overwrite=True,
                                 check=True)

        self.pipeline.add_module(read)
        self.pipeline.run_module("read1")

        data = self.pipeline.get_data("images")
        assert np.allclose(data[0, 50, 50], 0.09798413502193704, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010029494781738066, rtol=limit, atol=0.)
        assert data.shape == (40, 100, 100)

        read = FitsReadingModule(name_in="read2",
                                 image_tag="extra",
                                 input_dir=self.test_dir+"extra",
                                 overwrite=True,
                                 check=True)

        self.pipeline.add_module(read)
        self.pipeline.run_module("read2")

        extra = self.pipeline.get_data("extra")
        assert np.allclose(data, extra, rtol=limit, atol=0.)

    def test_stack_and_subset(self):

        self.pipeline.set_attribute("images", "PARANG", np.arange(1., 41., 1.), static=False)

        stack = StackAndSubsetModule(name_in="stack",
                                     image_in_tag="images",
                                     image_out_tag="stack",
                                     random=10,
                                     stacking=2)

        self.pipeline.add_module(stack)
        self.pipeline.run_module("stack")

        data = self.pipeline.get_data("stack")
        assert np.allclose(data[0, 50, 50], 0.09816320034649725, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 9.983545774937238e-05, rtol=limit, atol=0.)
        assert data.shape == (10, 100, 100)

        data = self.pipeline.get_data("header_stack/INDEX")
        index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        assert np.allclose(data, index, rtol=limit, atol=0.)
        assert data.shape == (10, )

        data = self.pipeline.get_data("header_stack/PARANG")
        parang = [1.5, 15.5, 19.5, 23.5, 25.5, 29.5, 31.5, 35.5, 37.5, 39.5]
        assert np.allclose(data, parang, rtol=limit, atol=0.)
        assert data.shape == (10, )

    def test_mean_cube(self):

        mean = MeanCubeModule(name_in="mean",
                              image_in_tag="images",
                              image_out_tag="mean")

        self.pipeline.add_module(mean)
        self.pipeline.run_module("mean")

        data = self.pipeline.get_data("mean")
        assert np.allclose(data[0, 50, 50], 0.09805840100024205, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010029494781738069, rtol=limit, atol=0.)
        assert data.shape == (4, 100, 100)

        attribute = self.pipeline.get_attribute("mean", "INDEX", static=False)
        assert np.allclose(np.mean(attribute), 1.5, rtol=limit, atol=0.)
        assert attribute.shape == (4, )

        attribute = self.pipeline.get_attribute("mean", "NFRAMES", static=False)
        assert np.allclose(np.mean(attribute), 1, rtol=limit, atol=0.)
        assert attribute.shape == (4, )

    def test_derotate_and_stack(self):

        derotate = DerotateAndStackModule(name_in="derotate1",
                                          image_in_tag="images",
                                          image_out_tag="derotate1",
                                          derotate=True,
                                          stack="mean",
                                          extra_rot=10.)

        self.pipeline.add_module(derotate)
        self.pipeline.run_module("derotate1")

        data = self.pipeline.get_data("derotate1")
        assert np.allclose(data[50, 50], 0.09689679769268554, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010021671152246617, rtol=limit, atol=0.)
        assert data.shape == (100, 100)

        derotate = DerotateAndStackModule(name_in="derotate2",
                                          image_in_tag="images",
                                          image_out_tag="derotate2",
                                          derotate=False,
                                          stack="median",
                                          extra_rot=0.)

        self.pipeline.add_module(derotate)
        self.pipeline.run_module("derotate2")

        data = self.pipeline.get_data("derotate2")
        assert np.allclose(data[50, 50], 0.09809001768003645, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010033064394962, rtol=limit, atol=0.)
        assert data.shape == (100, 100)

    def test_combine_tags(self):

        combine = CombineTagsModule(image_in_tags=("images", "extra"),
                                    check_attr=True,
                                    index_init=False,
                                    name_in="combine1",
                                    image_out_tag="combine1")


        self.pipeline.add_module(combine)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module("combine1")

        assert len(warning) == 1
        assert warning[0].message.args[0] == "The non-static keyword FILES is already used but " \
                                             "with different values. It is advisable to only " \
                                             "combine tags that descend from the same data set."

        data = self.pipeline.get_data("combine1")
        assert np.allclose(data[0, 50, 50], 0.09798413502193704, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010029494781738068, rtol=limit, atol=0.)
        assert data.shape == (80, 100, 100)

        data = self.pipeline.get_data("header_combine1/INDEX")
        assert data[40] == 0
        assert data.shape == (80, )

        combine = CombineTagsModule(image_in_tags=("images", "extra"),
                                    check_attr=False,
                                    index_init=True,
                                    name_in="combine2",
                                    image_out_tag="combine2")


        self.pipeline.add_module(combine)
        self.pipeline.run_module("combine2")

        data = self.pipeline.get_data("combine1")
        extra = self.pipeline.get_data("combine2")
        assert np.allclose(data, extra, rtol=limit, atol=0.)

        data = self.pipeline.get_data("header_combine2/INDEX")
        assert data[40] == 40
        assert data.shape == (80, )
コード例 #14
0
class TestCentering:

    def setup_class(self) -> None:

        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(path=self.test_dir+'dither',
                         npix_x=100,
                         npix_y=100,
                         x0=[25, 75, 75, 25],
                         y0=[75, 75, 25, 25],
                         parang_start=[0., 25., 50., 75.],
                         parang_end=[25., 50., 75., 100.],
                         exp_no=[1, 2, 3, 4])

        create_star_data(path=self.test_dir+'star_odd',
                         npix_x=101,
                         npix_y=101,
                         x0=[50],
                         y0=[50],
                         parang_start=[0.],
                         parang_end=[25.],
                         exp_no=[1],
                         noise=False)

        create_star_data(path=self.test_dir+'star_even',
                         npix_x=100,
                         npix_y=100,
                         x0=[49.5],
                         y0=[49.5],
                         parang_start=[0.],
                         parang_end=[25.],
                         exp_no=[1],
                         noise=False)

        create_waffle_data(path=self.test_dir+'waffle_odd',
                           npix=101,
                           x_spot=[20., 20., 80., 80.],
                           y_spot=[20., 80., 80., 20.])

        create_waffle_data(path=self.test_dir+'waffle_even',
                           npix=100,
                           x_spot=[20., 20., 79., 79.],
                           y_spot=[20., 79., 79., 20.])

        create_config(self.test_dir+'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self) -> None:

        remove_test_data(path=self.test_dir,
                         folders=['dither', 'star_odd', 'star_even', 'waffle_odd', 'waffle_even'])

    def test_read_data(self) -> None:

        module = FitsReadingModule(name_in='read1',
                                   image_tag='dither',
                                   input_dir=self.test_dir+'dither',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)

        module = FitsReadingModule(name_in='read2',
                                   image_tag='waffle_odd',
                                   input_dir=self.test_dir+'waffle_odd',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)

        module = FitsReadingModule(name_in='read3',
                                   image_tag='waffle_even',
                                   input_dir=self.test_dir+'waffle_even',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)

        module = FitsReadingModule(name_in='read4',
                                   image_tag='star_odd',
                                   input_dir=self.test_dir+'star_odd',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)

        read = FitsReadingModule(name_in='read5',
                                 image_tag='star_even',
                                 input_dir=self.test_dir+'star_even',
                                 overwrite=True,
                                 check=True)

        self.pipeline.add_module(read)

        self.pipeline.run_module('read1')
        self.pipeline.run_module('read2')
        self.pipeline.run_module('read3')
        self.pipeline.run_module('read4')
        self.pipeline.run_module('read5')

        data = self.pipeline.get_data('dither')
        assert np.allclose(data[0, 75, 25], 0.09812948027289994, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010029494781738066, rtol=limit, atol=0.)
        assert data.shape == (40, 100, 100)

        data = self.pipeline.get_data('waffle_odd')
        assert np.allclose(data[0, 20, 20], 0.09806026673451182, rtol=1e-4, atol=0.)
        assert np.allclose(data[0, 20, 80], 0.09806026673451182, rtol=1e-4, atol=0.)
        assert np.allclose(data[0, 80, 20], 0.09806026673451182, rtol=1e-4, atol=0.)
        assert np.allclose(data[0, 80, 80], 0.09806026673451182, rtol=1e-4, atol=0.)
        assert np.allclose(np.mean(data), 0.0003921184197627874, rtol=1e-4, atol=0.)
        assert data.shape == (1, 101, 101)

        data = self.pipeline.get_data('waffle_even')
        assert np.allclose(data[0, 20, 20], 0.09806026673451182, rtol=1e-4, atol=0.)
        assert np.allclose(data[0, 20, 79], 0.09806026673451182, rtol=1e-4, atol=0.)
        assert np.allclose(data[0, 79, 20], 0.09806026673451182, rtol=1e-4, atol=0.)
        assert np.allclose(data[0, 79, 79], 0.09806026673451182, rtol=1e-4, atol=0.)
        assert np.allclose(np.mean(data), 0.00040000000000001953, rtol=1e-4, atol=0.)
        assert data.shape == (1, 100, 100)

        data = self.pipeline.get_data('star_odd')
        assert np.allclose(data[0, 50, 50], 0.09806026673451182, rtol=1e-4, atol=0.)
        assert np.allclose(np.mean(data), 9.80296049406969e-05, rtol=1e-4, atol=0.)
        assert data.shape == (10, 101, 101)

        data = self.pipeline.get_data('star_even')
        assert np.allclose(data[0, 49, 49], 0.08406157361512759, rtol=1e-4, atol=0.)
        assert np.allclose(data[0, 49, 50], 0.08406157361512759, rtol=1e-4, atol=0.)
        assert np.allclose(data[0, 50, 49], 0.08406157361512759, rtol=1e-4, atol=0.)
        assert np.allclose(data[0, 50, 50], 0.08406157361512759, rtol=1e-4, atol=0.)
        assert np.allclose(np.mean(data), 9.99999999999951e-05, rtol=1e-4, atol=0.)
        assert data.shape == (10, 100, 100)

    def test_star_extract(self) -> None:

        module = StarExtractionModule(name_in='extract1',
                                      image_in_tag='dither',
                                      image_out_tag='extract1',
                                      index_out_tag='index',
                                      image_size=1.0,
                                      fwhm_star=0.1,
                                      position=None)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('extract1')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'The new dataset that is stored under the tag name ' \
                                             '\'index\' is empty.'

        data = self.pipeline.get_data('extract1')

        assert np.allclose(data[0, 19, 19], 0.09812948027289994, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0006578482216906739, rtol=limit, atol=0.)
        assert data.shape == (40, 39, 39)

        attr = self.pipeline.get_attribute('extract1', 'STAR_POSITION', static=False)
        assert attr[10, 0] == attr[10, 1] == 75

    def test_star_align(self) -> None:

        module = StarAlignmentModule(name_in='align1',
                                     image_in_tag='extract1',
                                     ref_image_in_tag=None,
                                     image_out_tag='align1',
                                     accuracy=10,
                                     resize=2.,
                                     num_references=10,
                                     subframe=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('align1')

        data = self.pipeline.get_data('align1')
        assert np.allclose(data[0, 39, 39], 0.023556628129942768, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00016446205542266847, rtol=limit, atol=0.)
        assert data.shape == (40, 78, 78)

    def test_star_align_subframe(self) -> None:

        module = StarAlignmentModule(name_in='align2',
                                     image_in_tag='extract1',
                                     ref_image_in_tag=None,
                                     image_out_tag='align2',
                                     accuracy=10,
                                     resize=None,
                                     num_references=10,
                                     subframe=0.5)

        self.pipeline.add_module(module)
        self.pipeline.run_module('align2')

        data = self.pipeline.get_data('align2')
        assert np.allclose(data[0, 19, 19], 0.09812948027289994, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0006578482216906739, rtol=limit, atol=0.)
        assert data.shape == (40, 39, 39)

    def test_star_align_ref(self) -> None:

        module = StarAlignmentModule(name_in='align3',
                                     image_in_tag='extract1',
                                     ref_image_in_tag='align2',
                                     image_out_tag='align3',
                                     accuracy=10,
                                     resize=None,
                                     num_references=10,
                                     subframe=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('align3')

        data = self.pipeline.get_data('align3')
        assert np.allclose(data[0, 19, 19], 0.09812948027289994, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0006578482216906739, rtol=limit, atol=0.)
        assert data.shape == (40, 39, 39)

    def test_star_align_number_ref(self) -> None:

        module = StarAlignmentModule(name_in='align4',
                                     image_in_tag='extract1',
                                     ref_image_in_tag='align2',
                                     image_out_tag='align4',
                                     accuracy=10,
                                     resize=None,
                                     num_references=50,
                                     subframe=None)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('align4')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'Number of available images (40) is smaller than ' \
                                             'num_references (50). Using all available images ' \
                                             'instead.'

        data = self.pipeline.get_data('align4')
        assert np.allclose(data[0, 19, 19], 0.09812948027289994, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0006578482216906739, rtol=limit, atol=0.)
        assert data.shape == (40, 39, 39)

    def test_shift_images_spline(self) -> None:

        module = ShiftImagesModule(shift_xy=(6., 4.),
                                   interpolation='spline',
                                   name_in='shift1',
                                   image_in_tag='align1',
                                   image_out_tag='shift')

        self.pipeline.add_module(module)
        self.pipeline.run_module('shift1')

        data = self.pipeline.get_data('shift')
        assert np.allclose(data[0, 43, 45], 0.023556628129942764, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00016430682224782259, rtol=limit, atol=0.)
        assert data.shape == (40, 78, 78)

    def test_shift_images_fft(self) -> None:

        module = ShiftImagesModule(shift_xy=(6., 4.),
                                   interpolation='fft',
                                   name_in='shift2',
                                   image_in_tag='align1',
                                   image_out_tag='shift_fft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('shift2')

        data = self.pipeline.get_data('shift_fft')
        assert np.allclose(data[0, 43, 45], 0.023556628129942764, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00016446205542266847, rtol=limit, atol=0.)
        assert data.shape == (40, 78, 78)

    def test_waffle_center_odd(self) -> None:

        module = WaffleCenteringModule(size=2.,
                                       center=(50, 50),
                                       name_in='waffle_odd',
                                       image_in_tag='star_odd',
                                       center_in_tag='waffle_odd',
                                       image_out_tag='center_odd',
                                       radius=42.5,
                                       pattern='x',
                                       sigma=0.135)

        self.pipeline.add_module(module)
        self.pipeline.run_module('waffle_odd')

        data = self.pipeline.get_data('star_odd')
        assert np.allclose(data[0, 50, 50], 0.09806026673451182, rtol=limit, atol=0.)

        data = self.pipeline.get_data('center_odd')
        assert np.allclose(data[0, 37, 37], 0.0980602667345118, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00017777777777778643, rtol=limit, atol=0.)
        assert data.shape == (10, 75, 75)

        attribute = self.pipeline.get_attribute('center_odd', 'History: WaffleCenteringModule')
        assert attribute == '[x, y] = [50.0, 50.0]'

    def test_waffle_center_even(self) -> None:

        module = WaffleCenteringModule(size=2.,
                                       center=(50, 50),
                                       name_in='waffle_even',
                                       image_in_tag='star_even',
                                       center_in_tag='waffle_even',
                                       image_out_tag='center_even',
                                       radius=42.5,
                                       pattern='x',
                                       sigma=0.135)

        self.pipeline.add_module(module)
        self.pipeline.run_module('waffle_even')

        data = self.pipeline.get_data('star_even')
        assert np.allclose(data[0, 49, 49], 0.08406157361512759, rtol=limit, atol=0.)
        assert np.allclose(data[0, 49, 50], 0.08406157361512759, rtol=limit, atol=0.)
        assert np.allclose(data[0, 50, 49], 0.08406157361512759, rtol=limit, atol=0.)
        assert np.allclose(data[0, 50, 50], 0.08406157361512759, rtol=limit, atol=0.)

        data = self.pipeline.get_data('center_even')
        assert np.allclose(data[0, 37, 37], 0.09778822940550569, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00017777777777778643, rtol=limit, atol=0.)
        assert data.shape == (10, 75, 75)

        attribute = self.pipeline.get_attribute('center_even', 'History: WaffleCenteringModule')
        assert attribute == '[x, y] = [49.5, 49.5]'

    def test_fit_center_full(self) -> None:

        module = FitCenterModule(name_in='fit1',
                                 image_in_tag='shift',
                                 fit_out_tag='fit_full',
                                 mask_out_tag='mask',
                                 method='full',
                                 radius=0.05,
                                 sign='positive',
                                 model='gaussian',
                                 guess=(6., 4., 3., 3., 0.01, 0., 0.))

        self.pipeline.add_module(module)
        self.pipeline.run_module('fit1')

        data = self.pipeline.get_data('fit_full')
        assert np.allclose(np.mean(data[:, 0]), 5.999068486622676, rtol=1e-3, atol=0.)
        assert np.allclose(np.mean(data[:, 2]), 4.000055166165185, rtol=1e-3, atol=0.)
        assert np.allclose(np.mean(data[:, 4]), 0.08106141046470318, rtol=1e-3, atol=0.)
        assert np.allclose(np.mean(data[:, 6]), 0.0810026137349896, rtol=1e-3, atol=0.)
        assert np.allclose(np.mean(data[:, 8]), 0.024462594420743763, rtol=1e-3, atol=0.)
        assert np.allclose(np.mean(data[:, 12]), 3.0281141786814477e-05, rtol=1e-3, atol=0.)
        assert data.shape == (40, 14)

        data = self.pipeline.get_data('mask')
        assert np.allclose(data[0, 43, 45], 0.023556628129942764, rtol=limit, atol=0.)
        assert np.allclose(data[0, 43, 55], 0.0, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010827527282995305, rtol=limit, atol=0.)
        assert data.shape == (40, 78, 78)

    def test_fit_center_mean(self) -> None:

        module = FitCenterModule(name_in='fit2',
                                 image_in_tag='shift',
                                 fit_out_tag='fit_mean',
                                 mask_out_tag=None,
                                 method='mean',
                                 radius=0.05,
                                 sign='positive',
                                 model='moffat',
                                 guess=(6., 4., 3., 3., 0.01, 0., 0., 1.))

        self.pipeline.add_module(module)
        self.pipeline.run_module('fit2')

        data = self.pipeline.get_data('fit_mean')
        assert np.allclose(np.mean(data[:, 0]), 5.999072568941366, rtol=1e-3, atol=0.)
        assert np.allclose(np.mean(data[:, 2]), 4.000051869708742, rtol=1e-3, atol=0.)
        assert np.allclose(np.mean(data[:, 4]), 0.08384036587023312, rtol=1e-3, atol=0.)
        assert np.allclose(np.mean(data[:, 6]), 0.08379313488754872, rtol=1e-3, atol=0.)
        assert np.allclose(np.mean(data[:, 8]), 0.025631328037795074, rtol=1e-3, atol=0.)
        assert np.allclose(np.mean(data[:, 12]), -0.0011275279023032867, rtol=1e-3, atol=0.)
        assert data.shape == (40, 16)

    def test_shift_images_tag(self) -> None:

        module = ShiftImagesModule(shift_xy='fit_full',
                                   interpolation='spline',
                                   name_in='shift3',
                                   image_in_tag='shift',
                                   image_out_tag='shift_tag_1')

        self.pipeline.add_module(module)
        self.pipeline.run_module('shift3')

        data = self.pipeline.get_data('shift_tag_1')
        assert np.allclose(data[0, 39, 39], 0.023563080974545528, rtol=1e-6, atol=0.)
        assert np.allclose(np.mean(data), 0.0001643062943690491, rtol=1e-6, atol=0.)
        assert data.shape == (40, 78, 78)

    def test_shift_images_tag_mean(self) -> None:

        module = ShiftImagesModule(shift_xy='fit_mean',
                                   interpolation='spline',
                                   name_in='shift4',
                                   image_in_tag='shift',
                                   image_out_tag='shift_tag_2')

        self.pipeline.add_module(module)
        self.pipeline.run_module('shift4')
        data = self.pipeline.get_data('shift_tag_2')
        assert np.allclose(data[0, 20, 31], 5.348337712000518e-05, rtol=1e-6, atol=0.)
        assert np.allclose(np.mean(data), 0.00016430318227546225, rtol=1e-6, atol=0.)
        assert data.shape == (40, 78, 78)
コード例 #15
0
class TestDetectionLimits(object):
    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + "/"

        create_star_data(path=self.test_dir + "limits")
        create_config(self.test_dir + "PynPoint_config.ini")

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir, folders=["limits"])

    def test_read_data(self):

        read = FitsReadingModule(name_in="read",
                                 image_tag="read",
                                 input_dir=self.test_dir + "limits")

        self.pipeline.add_module(read)
        self.pipeline.run_module("read")

        data = self.pipeline.get_data("read")
        assert np.allclose(data[0, 10, 10],
                           0.00012958496246258364,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738066,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

    def test_angle_interpolation(self):

        angle = AngleInterpolationModule(name_in="angle", data_tag="read")

        self.pipeline.add_module(angle)
        self.pipeline.run_module("angle")

        data = self.pipeline.get_attribute("read", "PARANG", static=False)
        assert data[5] == 2.7777777777777777
        assert np.allclose(np.mean(data), 10.0, rtol=limit, atol=0.)
        assert data.shape == (40, )

    def test_contrast_curve(self):

        proc = ["single", "multi"]

        for item in proc:

            if item == "multi":
                database = h5py.File(self.test_dir + 'PynPoint_database.hdf5',
                                     'a')
                database['config'].attrs['CPU'] = 4

            contrast = ContrastCurveModule(name_in="contrast_" + item,
                                           image_in_tag="read",
                                           psf_in_tag="read",
                                           contrast_out_tag="limits_" + item,
                                           separation=(0.5, 0.6, 0.1),
                                           angle=(0., 360., 180.),
                                           threshold=("sigma", 5.),
                                           psf_scaling=1.,
                                           aperture=0.1,
                                           pca_number=15,
                                           cent_size=None,
                                           edge_size=None,
                                           extra_rot=0.)

            self.pipeline.add_module(contrast)
            self.pipeline.run_module("contrast_" + item)

            data = self.pipeline.get_data("limits_" + item)
            assert np.allclose(data[0, 0], 5.00000000e-01, rtol=limit, atol=0.)
            assert np.allclose(data[0, 1],
                               2.3624384190310397,
                               rtol=limit,
                               atol=0.)
            assert np.allclose(data[0, 2],
                               0.05234065236317515,
                               rtol=limit,
                               atol=0.)
            assert np.allclose(data[0, 3],
                               0.00012147700290954244,
                               rtol=limit,
                               atol=0.)
            assert data.shape == (1, 4)
コード例 #16
0
class TestPypeline(object):
    def setup_class(self):
        self.test_dir = os.path.dirname(__file__) + "/"

        np.random.seed(1)

        image_3d = np.random.normal(loc=0, scale=2e-4, size=(4, 10, 10))
        image_2d = np.random.normal(loc=0, scale=2e-4, size=(10, 10))
        science = np.random.normal(loc=0, scale=2e-4, size=(4, 10, 10))
        dark = np.random.normal(loc=0, scale=2e-4, size=(4, 10, 10))

        h5f = h5py.File(self.test_dir + "PynPoint_database.hdf5", "w")
        h5f.create_dataset("image_3d", data=image_3d)
        h5f.create_dataset("image_2d", data=image_2d)
        h5f.create_dataset("science", data=science)
        h5f.create_dataset("dark", data=dark)
        h5f.close()

        create_star_data(path=self.test_dir + "images")
        create_config(self.test_dir + "PynPoint_config.ini")

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):
        remove_test_data(self.test_dir, folders=["images"])

    def test_output_port_name(self):
        read = FitsReadingModule(name_in="read",
                                 input_dir=self.test_dir + "images",
                                 image_tag="images")
        read.add_output_port("test")

        with pytest.warns(UserWarning) as warning:
            read.add_output_port("test")

        assert len(warning) == 1
        assert warning[0].message.args[
            0] == "Tag 'test' of ReadingModule 'read' is already used."

        process = BadPixelSigmaFilterModule(name_in="badpixel",
                                            image_in_tag="images")
        process.add_output_port("test")

        with pytest.warns(UserWarning) as warning:
            process.add_output_port("test")

        assert len(warning) == 1
        assert warning[0].message.args[0] == "Tag 'test' of ProcessingModule 'badpixel' is " \
                                             "already used."

        self.pipeline.m_data_storage.close_connection()

        process._m_data_base = self.test_dir + "database.hdf5"
        process.add_output_port("new")

    def test_apply_function_to_images_3d(self):
        self.pipeline.set_attribute("config", "MEMORY", 1, static=True)

        remove = RemoveLinesModule(lines=(1, 0, 0, 0),
                                   name_in="remove1",
                                   image_in_tag="image_3d",
                                   image_out_tag="remove_3d")

        self.pipeline.add_module(remove)
        self.pipeline.run_module("remove1")

        data = self.pipeline.get_data("image_3d")
        assert np.allclose(np.mean(data),
                           1.0141852764605783e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 10, 10)

        data = self.pipeline.get_data("remove_3d")
        assert np.allclose(np.mean(data),
                           1.1477029889801025e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 10, 9)

    def test_apply_function_to_images_2d(self):
        remove = RemoveLinesModule(lines=(1, 0, 0, 0),
                                   name_in="remove2",
                                   image_in_tag="image_2d",
                                   image_out_tag="remove_2d")

        self.pipeline.add_module(remove)
        self.pipeline.run_module("remove2")

        data = self.pipeline.get_data("image_2d")
        assert np.allclose(np.mean(data),
                           1.2869483197883442e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (10, 10)

        data = self.pipeline.get_data("remove_2d")
        assert np.allclose(np.mean(data),
                           1.3957075246029751e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (10, 9)

    def test_apply_function_to_images_same_port(self):
        dark = DarkCalibrationModule(name_in="dark1",
                                     image_in_tag="science",
                                     dark_in_tag="dark",
                                     image_out_tag="science")

        self.pipeline.add_module(dark)
        self.pipeline.run_module("dark1")

        data = self.pipeline.get_data("science")
        assert np.allclose(np.mean(data),
                           -3.190113568690675e-06,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 10, 10)

        self.pipeline.set_attribute("config", "MEMORY", 0, static=True)

        dark = DarkCalibrationModule(name_in="dark2",
                                     image_in_tag="science",
                                     dark_in_tag="dark",
                                     image_out_tag="science")

        self.pipeline.add_module(dark)
        self.pipeline.run_module("dark2")

        data = self.pipeline.get_data("science")
        assert np.allclose(np.mean(data),
                           -1.026073475228737e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 10, 10)

        remove = RemoveLinesModule(lines=(1, 0, 0, 0),
                                   name_in="remove3",
                                   image_in_tag="remove_3d",
                                   image_out_tag="remove_3d")

        self.pipeline.add_module(remove)

        with pytest.raises(ValueError) as error:
            self.pipeline.run_module("remove3")

        assert str(error.value) == "Input and output port have the same tag while the input " \
                                   "function is changing the image shape. This is only " \
                                   "possible with MEMORY=None."

    def test_apply_function_to_images_memory_none(self):
        remove = RemoveLinesModule(lines=(1, 0, 0, 0),
                                   name_in="remove4",
                                   image_in_tag="image_3d",
                                   image_out_tag="remove_3d_none")

        self.pipeline.add_module(remove)
        self.pipeline.run_module("remove4")

        data = self.pipeline.get_data("remove_3d_none")
        assert np.allclose(np.mean(data),
                           1.1477029889801025e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 10, 9)

    def test_apply_function_to_images_3d_args(self):
        self.pipeline.set_attribute("config", "MEMORY", 1, static=True)
        self.pipeline.set_attribute("image_3d", "PIXSCALE", 0.1, static=True)

        scale = ScaleImagesModule(scaling=(1.2, 1.2, 10.),
                                  pixscale=True,
                                  name_in="scale1",
                                  image_in_tag="image_3d",
                                  image_out_tag="scale_3d")

        self.pipeline.add_module(scale)
        self.pipeline.run_module("scale1")

        data = self.pipeline.get_data("scale_3d")
        assert np.allclose(np.mean(data),
                           7.042953308754017e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 12, 12)

        attribute = self.pipeline.get_attribute("scale_3d",
                                                "PIXSCALE",
                                                static=True)
        assert np.allclose(attribute, 0.08333333333333334, rtol=limit, atol=0.)

    def test_apply_function_to_images_2d_args(self):
        self.pipeline.set_attribute("image_2d", "PIXSCALE", 0.1, static=True)

        scale = ScaleImagesModule(scaling=(1.2, 1.2, 10.),
                                  pixscale=True,
                                  name_in="scale2",
                                  image_in_tag="image_2d",
                                  image_out_tag="scale_2d")

        self.pipeline.add_module(scale)
        self.pipeline.run_module("scale2")

        data = self.pipeline.get_data("scale_2d")
        assert np.allclose(np.mean(data),
                           8.937141109641279e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (12, 12)

        attribute = self.pipeline.get_attribute("scale_2d",
                                                "PIXSCALE",
                                                static=True)
        assert np.allclose(attribute, 0.08333333333333334, rtol=limit, atol=0.)
コード例 #17
0
class TestTimeDenoising:
    def setup_class(self) -> None:

        self.limit = 1e-10
        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(self.test_dir + 'images')
        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self) -> None:

        remove_test_data(self.test_dir, folders=['images'])

    def test_read_data(self) -> None:

        module = FitsReadingModule(name_in='read',
                                   image_tag='images',
                                   input_dir=self.test_dir + 'images',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('read')

        data = self.pipeline.get_data('images')
        assert np.sum(data) == pytest.approx(105.54278879805277,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

    def test_wavelet_denoising_cwt_dog(self) -> None:

        cwt_config = CwtWaveletConfiguration(wavelet='dog',
                                             wavelet_order=2,
                                             keep_mean=False,
                                             resolution=0.5)

        assert cwt_config.m_wavelet == 'dog'
        assert cwt_config.m_wavelet_order == 2
        assert not cwt_config.m_keep_mean
        assert cwt_config.m_resolution == 0.5

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_cwt_dog',
                                            image_in_tag='images',
                                            image_out_tag='wavelet_cwt_dog',
                                            padding='zero',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_cwt_dog')

        data = self.pipeline.get_data('wavelet_cwt_dog')
        assert np.sum(data) == pytest.approx(105.1035789572968,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

        with h5py.File(self.test_dir + 'PynPoint_database.hdf5',
                       'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 4

        self.pipeline.run_module('wavelet_cwt_dog')

        data_multi = self.pipeline.get_data('wavelet_cwt_dog')
        assert data == pytest.approx(data_multi, rel=self.limit, abs=0.)
        assert data.shape == data_multi.shape

    def test_wavelet_denoising_cwt_morlet(self) -> None:

        with h5py.File(self.test_dir + 'PynPoint_database.hdf5',
                       'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 1

        cwt_config = CwtWaveletConfiguration(wavelet='morlet',
                                             wavelet_order=5,
                                             keep_mean=False,
                                             resolution=0.5)

        assert cwt_config.m_wavelet == 'morlet'
        assert cwt_config.m_wavelet_order == 5
        assert not cwt_config.m_keep_mean
        assert cwt_config.m_resolution == 0.5

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_cwt_morlet',
                                            image_in_tag='images',
                                            image_out_tag='wavelet_cwt_morlet',
                                            padding='mirror',
                                            median_filter=False,
                                            threshold_function='hard')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_cwt_morlet')

        data = self.pipeline.get_data('wavelet_cwt_morlet')
        assert np.sum(data) == pytest.approx(104.86262840716438,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

        data = self.pipeline.get_attribute('wavelet_cwt_morlet',
                                           'NFRAMES',
                                           static=False)
        assert data[0] == data[1] == 5

    def test_wavelet_denoising_dwt(self) -> None:

        dwt_config = DwtWaveletConfiguration(wavelet='db8')

        assert dwt_config.m_wavelet == 'db8'

        module = WaveletTimeDenoisingModule(wavelet_configuration=dwt_config,
                                            name_in='wavelet_dwt',
                                            image_in_tag='images',
                                            image_out_tag='wavelet_dwt',
                                            padding='zero',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_dwt')

        data = self.pipeline.get_data('wavelet_dwt')
        assert np.sum(data) == pytest.approx(105.54278879805277,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

    def test_time_normalization(self) -> None:

        module = TimeNormalizationModule(name_in='timenorm',
                                         image_in_tag='images',
                                         image_out_tag='timenorm')

        self.pipeline.add_module(module)
        self.pipeline.run_module('timenorm')

        data = self.pipeline.get_data('timenorm')
        assert np.sum(data) == pytest.approx(56.443663773873,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

    def test_wavelet_denoising_even_size(self) -> None:

        module = AddLinesModule(name_in='add',
                                image_in_tag='images',
                                image_out_tag='images_even',
                                lines=(1, 0, 1, 0))

        self.pipeline.add_module(module)
        self.pipeline.run_module('add')

        data = self.pipeline.get_data('images_even')
        assert np.sum(data) == pytest.approx(105.54278879805275,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 12, 12)

        cwt_config = CwtWaveletConfiguration(wavelet='dog',
                                             wavelet_order=2,
                                             keep_mean=False,
                                             resolution=0.5)

        assert cwt_config.m_wavelet == 'dog'
        assert cwt_config.m_wavelet_order == 2
        assert not cwt_config.m_keep_mean
        assert cwt_config.m_resolution == 0.5

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_even_1',
                                            image_in_tag='images_even',
                                            image_out_tag='wavelet_even_1',
                                            padding='zero',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_even_1')

        data = self.pipeline.get_data('wavelet_even_1')
        assert np.sum(data) == pytest.approx(105.1035789572968,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 12, 12)

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_even_2',
                                            image_in_tag='images_even',
                                            image_out_tag='wavelet_even_2',
                                            padding='mirror',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_even_2')

        data = self.pipeline.get_data('wavelet_even_2')
        assert np.sum(data) == pytest.approx(105.06809820408587,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 12, 12)

        data = self.pipeline.get_attribute('images', 'NFRAMES', static=False)
        assert data == pytest.approx([5, 5], rel=self.limit, abs=0.)

        data = self.pipeline.get_attribute('wavelet_even_1',
                                           'NFRAMES',
                                           static=False)
        assert data == pytest.approx([5, 5], rel=self.limit, abs=0.)

        data = self.pipeline.get_attribute('wavelet_even_2',
                                           'NFRAMES',
                                           static=False)
        assert data == pytest.approx([5, 5], rel=self.limit, abs=0.)
コード例 #18
0
class TestPsfPreparation:
    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(path=self.test_dir + 'prep')
        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir, folders=['prep'])

    def test_read_data(self):

        module = FitsReadingModule(name_in='read',
                                   image_tag='read',
                                   input_dir=self.test_dir + 'prep')

        self.pipeline.add_module(module)
        self.pipeline.run_module('read')

        data = self.pipeline.get_data('read')
        assert np.allclose(data[0, 25, 25],
                           2.0926464668090656e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738066,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

    def test_angle_interpolation(self):

        module = AngleInterpolationModule(name_in='angle1', data_tag='read')

        self.pipeline.add_module(module)
        self.pipeline.run_module('angle1')

        data = self.pipeline.get_data('header_read/PARANG')
        assert np.allclose(data[0], 0., rtol=limit, atol=0.)
        assert np.allclose(data[15], 7.777777777777778, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 10.0, rtol=limit, atol=0.)
        assert data.shape == (40, )

    def test_angle_calculation(self):

        self.pipeline.set_attribute('read', 'LATITUDE', -25.)
        self.pipeline.set_attribute('read', 'LONGITUDE', -70.)
        self.pipeline.set_attribute('read', 'DIT', 1.)

        self.pipeline.set_attribute('read',
                                    'RA', (90., 90., 90., 90.),
                                    static=False)
        self.pipeline.set_attribute('read',
                                    'DEC', (-51., -51., -51., -51.),
                                    static=False)
        self.pipeline.set_attribute('read',
                                    'PUPIL', (90., 90., 90., 90.),
                                    static=False)

        date = ('2012-12-01T07:09:00.0000', '2012-12-01T07:09:01.0000',
                '2012-12-01T07:09:02.0000', '2012-12-01T07:09:03.0000')

        self.pipeline.set_attribute('read', 'DATE', date, static=False)

        module = AngleCalculationModule(instrument='NACO',
                                        name_in='angle2',
                                        data_tag='read')

        self.pipeline.add_module(module)
        self.pipeline.run_module('angle2')

        data = self.pipeline.get_data('header_read/PARANG')
        assert np.allclose(data[0], -55.04109770947442, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data),
                           -54.99858360618869,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, )

        self.pipeline.set_attribute('read',
                                    'RA', (60000.0, 60000.0, 60000.0, 60000.0),
                                    static=False)

        self.pipeline.set_attribute('read',
                                    'DEC',
                                    (-510000., -510000., -510000., -510000.),
                                    static=False)

        module = AngleCalculationModule(instrument='SPHERE/IRDIS',
                                        name_in='angle3',
                                        data_tag='read')

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('angle3')

        assert warning[0].message.args[0] == 'For SPHERE data it is recommended to use the ' \
                                             'header keyword \'ESO INS4 DROT2 RA\' to specify ' \
                                             'the object\'s right ascension. The input will be ' \
                                             'parsed accordingly. Using the regular \'RA\' '\
                                             'keyword will lead to wrong parallactic angles.' \

        assert warning[1].message.args[0] == 'For SPHERE data it is recommended to use the ' \
                                             'header keyword \'ESO INS4 DROT2 DEC\' to specify ' \
                                             'the object\'s declination. The input will be ' \
                                             'parsed accordingly. Using the regular \'DEC\' '\
                                             'keyword will lead to wrong parallactic angles.' \

        data = self.pipeline.get_data('header_read/PARANG')
        assert np.allclose(data[0], 170.39102715170227, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data),
                           170.46341123194824,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, )

        module = AngleCalculationModule(instrument='SPHERE/IFS',
                                        name_in='angle4',
                                        data_tag='read')

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('angle4')

        assert warning[0].message.args[0] == 'AngleCalculationModule has not been tested for ' \
                                             'SPHERE/IFS data.'

        assert warning[1].message.args[0] == 'For SPHERE data it is recommended to use the ' \
                                             'header keyword \'ESO INS4 DROT2 RA\' to specify ' \
                                             'the object\'s right ascension. The input will be ' \
                                             'parsed accordingly. Using the regular \'RA\' '\
                                             'keyword will lead to wrong parallactic angles.' \

        assert warning[2].message.args[0] == 'For SPHERE data it is recommended to use the ' \
                                             'header keyword \'ESO INS4 DROT2 DEC\' to specify ' \
                                             'the object\'s declination. The input will be ' \
                                             'parsed accordingly. Using the regular \'DEC\' '\
                                             'keyword will lead to wrong parallactic angles.' \

        data = self.pipeline.get_data('header_read/PARANG')
        assert np.allclose(data[0], -89.12897284829768, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data),
                           -89.02755918786514,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, )

    def test_angle_interpolation_mismatch(self):

        self.pipeline.set_attribute('read', 'NDIT', [9, 9, 9, 9], static=False)

        module = AngleInterpolationModule(name_in='angle5', data_tag='read')

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('angle5')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'There is a mismatch between the NDIT and NFRAMES ' \
                                             'values. The parallactic angles are calculated ' \
                                             'with a linear interpolation by using NFRAMES ' \
                                             'steps. A frame selection should be applied ' \
                                             'after the parallactic angles are calculated.'

        data = self.pipeline.get_data('header_read/PARANG')
        assert np.allclose(data[0], 0., rtol=limit, atol=0.)
        assert np.allclose(data[15], 7.777777777777778, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 10.0, rtol=limit, atol=0.)
        assert data.shape == (40, )

    def test_psf_preparation_norm_mask(self):

        module = PSFpreparationModule(name_in='prep1',
                                      image_in_tag='read',
                                      image_out_tag='prep1',
                                      mask_out_tag='mask1',
                                      norm=True,
                                      cent_size=0.1,
                                      edge_size=1.0)

        self.pipeline.add_module(module)
        self.pipeline.run_module('prep1')

        data = self.pipeline.get_data('prep1')
        assert np.allclose(data[0, 0, 0], 0., rtol=limit, atol=0.)
        assert np.allclose(data[0, 99, 99], 0., rtol=limit, atol=0.)
        assert np.allclose(np.mean(data),
                           0.0001690382058762809,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

        data = self.pipeline.get_data('mask1')
        assert np.allclose(data[0, 0], 0., rtol=limit, atol=0.)
        assert np.allclose(data[99, 99], 0., rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.4268, rtol=limit, atol=0.)
        assert data.shape == (100, 100)

    def test_psf_preparation_none(self):

        module = PSFpreparationModule(name_in='prep2',
                                      image_in_tag='read',
                                      image_out_tag='prep2',
                                      mask_out_tag='mask2',
                                      norm=False,
                                      cent_size=None,
                                      edge_size=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('prep2')

        data = self.pipeline.get_data('prep2')
        assert np.allclose(data[0, 0, 0],
                           0.00032486907273264834,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(data[0, 25, 25],
                           2.0926464668090656e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(data[0, 99, 99],
                           -0.000287573978535779,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738066,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

    def test_psf_preparation_no_mask_out(self):

        module = PSFpreparationModule(name_in='prep3',
                                      image_in_tag='read',
                                      image_out_tag='prep3',
                                      mask_out_tag=None,
                                      norm=False,
                                      cent_size=None,
                                      edge_size=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('prep3')

        data = self.pipeline.get_data('prep3')
        assert np.allclose(data[0, 0, 0],
                           0.00032486907273264834,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(data[0, 25, 25],
                           2.0926464668090656e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(data[0, 99, 99],
                           -0.000287573978535779,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738066,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

    def test_sdi_preparation(self):

        module = SDIpreparationModule(name_in='sdi',
                                      wavelength=(0.65, 0.6),
                                      width=(0.1, 0.5),
                                      image_in_tag='read',
                                      image_out_tag='sdi')

        self.pipeline.add_module(module)
        self.pipeline.run_module('sdi')

        data = self.pipeline.get_data('sdi')
        assert np.allclose(data[0, 25, 25],
                           -2.6648118007008814e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           2.0042892634995876e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

        attribute = self.pipeline.get_attribute(
            'sdi', 'History: SDIpreparationModule')
        assert attribute == '(line, continuum) = (0.65, 0.6)'
コード例 #19
0
class TestCentering:
    def setup_class(self) -> None:

        self.limit = 1e-10
        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(self.test_dir + 'star')
        create_waffle_data(self.test_dir + 'waffle')
        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self) -> None:

        remove_test_data(path=self.test_dir, folders=['star', 'waffle'])

    def test_read_data(self) -> None:

        module = FitsReadingModule(name_in='read1',
                                   image_tag='star',
                                   input_dir=self.test_dir + 'star',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)

        module = FitsReadingModule(name_in='read2',
                                   image_tag='waffle',
                                   input_dir=self.test_dir + 'waffle',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)

        self.pipeline.run_module('read1')
        self.pipeline.run_module('read2')

        data = self.pipeline.get_data('star')
        assert np.sum(data) == pytest.approx(105.54278879805277,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

        data = self.pipeline.get_data('waffle')
        assert np.sum(data) == pytest.approx(4.000000000000196,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (1, 101, 101)

    def test_star_extract(self) -> None:

        module = StarExtractionModule(name_in='extract1',
                                      image_in_tag='star',
                                      image_out_tag='extract1',
                                      index_out_tag='index',
                                      image_size=0.2,
                                      fwhm_star=0.1,
                                      position=None)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('extract1')

        assert len(warning) == 3

        assert warning[0].message.args[0] == 'Can not store the attribute \'INSTRUMENT\' ' \
                                             'because the dataset \'index\' does not exist.'

        data = self.pipeline.get_data('extract1')
        assert np.sum(data) == pytest.approx(104.93318507061295,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 9, 9)

    def test_star_align(self) -> None:

        module = StarAlignmentModule(name_in='align1',
                                     image_in_tag='extract1',
                                     ref_image_in_tag=None,
                                     image_out_tag='align1',
                                     accuracy=10,
                                     resize=2.,
                                     num_references=10,
                                     subframe=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('align1')

        data = self.pipeline.get_data('align1')
        assert np.sum(data) == pytest.approx(104.70747423205349,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 18, 18)

    def test_star_align_subframe(self) -> None:

        module = StarAlignmentModule(name_in='align2',
                                     image_in_tag='extract1',
                                     ref_image_in_tag=None,
                                     image_out_tag='align2',
                                     accuracy=10,
                                     resize=None,
                                     num_references=10,
                                     subframe=0.1)

        self.pipeline.add_module(module)
        self.pipeline.run_module('align2')

        data = self.pipeline.get_data('align2')
        assert np.sum(data) == pytest.approx(104.39031104541652,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 9, 9)

    def test_star_align_ref(self) -> None:

        module = StarAlignmentModule(name_in='align3',
                                     image_in_tag='extract1',
                                     ref_image_in_tag='align2',
                                     image_out_tag='align3',
                                     accuracy=10,
                                     resize=None,
                                     num_references=10,
                                     subframe=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('align3')

        data = self.pipeline.get_data('align3')
        assert np.sum(data) == pytest.approx(104.46997194330757,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 9, 9)

    def test_star_align_number_ref(self) -> None:

        module = StarAlignmentModule(name_in='align4',
                                     image_in_tag='extract1',
                                     ref_image_in_tag='align2',
                                     image_out_tag='align4',
                                     accuracy=10,
                                     resize=None,
                                     num_references=20,
                                     subframe=None)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('align4')

        assert len(warning) == 1

        assert warning[0].message.args[0] == 'Number of available images (10) is smaller than ' \
                                             'num_references (20). Using all available images ' \
                                             'instead.'

        data = self.pipeline.get_data('align4')
        assert np.sum(data) == pytest.approx(104.46997194330757,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 9, 9)

    def test_shift_images_spline(self) -> None:

        module = ShiftImagesModule(shift_xy=(1., 2.),
                                   interpolation='spline',
                                   name_in='shift1',
                                   image_in_tag='align1',
                                   image_out_tag='shift')

        self.pipeline.add_module(module)
        self.pipeline.run_module('shift1')

        data = self.pipeline.get_data('shift')
        assert np.sum(data) == pytest.approx(104.20425101355242,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 18, 18)

    def test_shift_images_fft(self) -> None:

        module = ShiftImagesModule(shift_xy=(1., 2.),
                                   interpolation='fft',
                                   name_in='shift2',
                                   image_in_tag='align1',
                                   image_out_tag='shift_fft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('shift2')

        data = self.pipeline.get_data('shift_fft')
        assert np.sum(data) == pytest.approx(104.70747423205349,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 18, 18)

    def test_waffle_center_odd(self) -> None:

        module = AddLinesModule(name_in='add',
                                image_in_tag='star',
                                image_out_tag='star_add',
                                lines=(45, 45, 45, 45))

        self.pipeline.add_module(module)
        self.pipeline.run_module('add')

        data = self.pipeline.get_data('star_add')
        assert np.sum(data) == pytest.approx(105.54278879805278,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 101, 101)

        module = WaffleCenteringModule(size=0.2,
                                       center=(50, 50),
                                       name_in='waffle',
                                       image_in_tag='star_add',
                                       center_in_tag='waffle',
                                       image_out_tag='center',
                                       radius=35.,
                                       pattern='x',
                                       sigma=0.05)

        self.pipeline.add_module(module)

        with pytest.warns(DeprecationWarning) as warning:
            self.pipeline.run_module('waffle')

        assert len(warning) == 1

        assert warning[0].message.args[0] == 'The \'pattern\' parameter will be deprecated in a ' \
                                             'future release. Please Use the \'angle\' ' \
                                             'parameter instead and set it to 45.0 degrees.'

        data = self.pipeline.get_data('center')
        assert np.sum(data) == pytest.approx(104.93318507061295,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 9, 9)

        attr = self.pipeline.get_attribute('center',
                                           'History: WaffleCenteringModule')
        assert attr == '[x, y] = [50.0, 50.0]'

    def test_waffle_center_even(self) -> None:

        module = AddLinesModule(name_in='add1',
                                image_in_tag='star_add',
                                image_out_tag='star_even',
                                lines=(0, 1, 0, 1))

        self.pipeline.add_module(module)
        self.pipeline.run_module('add1')

        data = self.pipeline.get_data('star_even')
        assert np.sum(data) == pytest.approx(105.54278879805275,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 102, 102)

        module = AddLinesModule(name_in='add2',
                                image_in_tag='waffle',
                                image_out_tag='waffle_even',
                                lines=(0, 1, 0, 1))

        self.pipeline.add_module(module)
        self.pipeline.run_module('add2')

        data = self.pipeline.get_data('waffle_even')
        assert np.sum(data) == pytest.approx(4.000000000000195,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (1, 102, 102)

        module = ShiftImagesModule(shift_xy=(0.5, 0.5),
                                   interpolation='spline',
                                   name_in='shift3',
                                   image_in_tag='star_even',
                                   image_out_tag='star_shift')

        self.pipeline.add_module(module)
        self.pipeline.run_module('shift3')

        data = self.pipeline.get_data('star_shift')
        assert np.sum(data) == pytest.approx(105.54278879805274,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 102, 102)

        module = ShiftImagesModule(shift_xy=(0.5, 0.5),
                                   interpolation='spline',
                                   name_in='shift4',
                                   image_in_tag='waffle_even',
                                   image_out_tag='waffle_shift')

        self.pipeline.add_module(module)
        self.pipeline.run_module('shift4')

        data = self.pipeline.get_data('waffle_shift')
        assert np.sum(data) == pytest.approx(4.000000000000194,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (1, 102, 102)

        module = WaffleCenteringModule(size=0.2,
                                       center=(50, 50),
                                       name_in='waffle_even',
                                       image_in_tag='star_shift',
                                       center_in_tag='waffle_shift',
                                       image_out_tag='center_even',
                                       radius=35.,
                                       pattern='x',
                                       sigma=0.05)

        self.pipeline.add_module(module)

        with pytest.warns(DeprecationWarning) as warning:
            self.pipeline.run_module('waffle_even')

        assert len(warning) == 1

        assert warning[0].message.args[0] == 'The \'pattern\' parameter will be deprecated in a ' \
                                             'future release. Please Use the \'angle\' ' \
                                             'parameter instead and set it to 45.0 degrees.'

        data = self.pipeline.get_data('center_even')
        assert np.sum(data) == pytest.approx(105.22695036281449,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 9, 9)

        attr = self.pipeline.get_attribute('center_even',
                                           'History: WaffleCenteringModule')
        assert attr == '[x, y] = [50.5, 50.5]'

    def test_fit_center_full(self) -> None:

        module = FitCenterModule(name_in='fit1',
                                 image_in_tag='shift',
                                 fit_out_tag='fit_full',
                                 mask_out_tag='mask',
                                 method='full',
                                 mask_radii=(None, 0.1),
                                 sign='positive',
                                 model='gaussian',
                                 guess=(1., 2., 3., 3., 0.01, 0., 0.))

        self.pipeline.add_module(module)
        self.pipeline.run_module('fit1')

        data = self.pipeline.get_data('fit_full')
        assert np.mean(data[:, 0]) == pytest.approx(0.94, rel=0., abs=0.01)
        assert np.mean(data[:, 2]) == pytest.approx(2.07, rel=0., abs=0.01)
        assert np.mean(data[:, 4]) == pytest.approx(0.08, rel=0., abs=0.01)
        assert np.mean(data[:, 6]) == pytest.approx(0.08, rel=0., abs=0.01)
        assert np.mean(data[:, 8]) == pytest.approx(0.24, rel=0., abs=0.01)
        assert data.shape == (10, 14)

        data = self.pipeline.get_data('mask')
        assert np.sum(data) == pytest.approx(103.45599730750453,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 18, 18)

    def test_fit_center_mean(self) -> None:

        module = FitCenterModule(name_in='fit2',
                                 image_in_tag='shift',
                                 fit_out_tag='fit_mean',
                                 mask_out_tag=None,
                                 method='mean',
                                 mask_radii=(None, 0.1),
                                 sign='positive',
                                 model='moffat',
                                 guess=(1., 2., 3., 3., 0.01, 0., 0., 1.))

        self.pipeline.add_module(module)
        self.pipeline.run_module('fit2')

        data = self.pipeline.get_data('fit_mean')
        assert np.mean(data[:, 0]) == pytest.approx(0.94, rel=0., abs=0.01)
        assert np.mean(data[:, 2]) == pytest.approx(2.06, rel=0., abs=0.01)
        assert np.mean(data[:, 4]) == pytest.approx(0.08, rel=0., abs=0.01)
        assert np.mean(data[:, 6]) == pytest.approx(0.08, rel=0., abs=0.01)
        assert np.mean(data[:, 8]) == pytest.approx(0.24, rel=0., abs=0.01)
        assert data.shape == (10, 16)

    def test_shift_images_tag(self) -> None:

        module = ShiftImagesModule(shift_xy='fit_full',
                                   interpolation='spline',
                                   name_in='shift5',
                                   image_in_tag='shift',
                                   image_out_tag='shift_tag_1')

        self.pipeline.add_module(module)
        self.pipeline.run_module('shift5')

        data = self.pipeline.get_data('shift_tag_1')
        assert np.sum(data) == pytest.approx(103.76504482668594,
                                             rel=1e-6,
                                             abs=0.)
        assert data.shape == (10, 18, 18)

    def test_shift_images_tag_mean(self) -> None:

        module = ShiftImagesModule(shift_xy='fit_mean',
                                   interpolation='spline',
                                   name_in='shift6',
                                   image_in_tag='shift',
                                   image_out_tag='shift_tag_2')

        self.pipeline.add_module(module)
        self.pipeline.run_module('shift6')

        data = self.pipeline.get_data('shift_tag_2')
        assert np.sum(data) == pytest.approx(103.42285579230325,
                                             rel=1e-6,
                                             abs=0.)
        assert data.shape == (10, 18, 18)
コード例 #20
0
class TestFrameSelection(object):

    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + "/"

        create_star_data(path=self.test_dir+"images", ndit=10, nframes=11)
        create_config(self.test_dir+"PynPoint_config.ini")

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir, folders=["images"])

    def test_read_data(self):

        read = FitsReadingModule(name_in="read",
                                 image_tag="read",
                                 input_dir=self.test_dir+"images",
                                 overwrite=True,
                                 check=True)

        self.pipeline.add_module(read)
        self.pipeline.run_module("read")

        data = self.pipeline.get_data("read")
        assert np.allclose(data[0, 50, 50], 0.09798413502193704, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0001002167910262529, rtol=limit, atol=0.)
        assert data.shape == (44, 100, 100)

    def test_remove_last_frame(self):

        last = RemoveLastFrameModule(name_in="last",
                                     image_in_tag="read",
                                     image_out_tag="last")

        self.pipeline.add_module(last)
        self.pipeline.run_module("last")

        data = self.pipeline.get_data("last")
        assert np.allclose(data[0, 50, 50], 0.09798413502193704, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010020258903646778, rtol=limit, atol=0.)
        assert data.shape == (40, 100, 100)

        self.pipeline.set_attribute("last", "PARANG", np.arange(0., 40., 1.), static=False)

        star = np.zeros((40, 2))
        star[:, 0] = np.arange(40., 80., 1.)
        star[:, 1] = np.arange(40., 80., 1.)

        self.pipeline.set_attribute("last", "STAR_POSITION", star, static=False)

        attribute = self.pipeline.get_attribute("last", "PARANG", static=False)
        assert np.allclose(np.mean(attribute), 19.5, rtol=limit, atol=0.)
        assert attribute.shape == (40, )

        attribute = self.pipeline.get_attribute("last", "STAR_POSITION", static=False)
        assert np.allclose(np.mean(attribute), 59.5, rtol=limit, atol=0.)
        assert attribute.shape == (40, 2)

    def test_remove_start_frame(self):

        start = RemoveStartFramesModule(frames=2,
                                        name_in="start",
                                        image_in_tag="last",
                                        image_out_tag="start")

        self.pipeline.add_module(start)
        self.pipeline.run_module("start")

        data = self.pipeline.get_data("start")
        assert np.allclose(data[0, 50, 50], 0.09797376304048713, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010011298467340513, rtol=limit, atol=0.)
        assert data.shape == (32, 100, 100)

        attribute = self.pipeline.get_attribute("start", "PARANG", static=False)
        assert np.allclose(np.mean(attribute), 20.5, rtol=limit, atol=0.)
        assert attribute.shape == (32, )

        attribute = self.pipeline.get_attribute("start", "STAR_POSITION", static=False)
        assert np.allclose(np.mean(attribute), 60.5, rtol=limit, atol=0.)
        assert attribute.shape == (32, 2)

    def test_remove_frames(self):

        remove = RemoveFramesModule(frames=(5, 8, 13, 25, 31),
                                    name_in="remove",
                                    image_in_tag="start",
                                    selected_out_tag="selected",
                                    removed_out_tag="removed")

        self.pipeline.add_module(remove)
        self.pipeline.run_module("remove")

        data = self.pipeline.get_data("selected")
        assert np.allclose(data[0, 50, 50], 0.09797376304048713, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 9.984682304434105e-05, rtol=limit, atol=0.)
        assert data.shape == (27, 100, 100)

        data = self.pipeline.get_data("removed")
        assert np.allclose(data[0, 50, 50], 0.09818692015286978, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010155025747035087, rtol=limit, atol=0.)
        assert data.shape == (5, 100, 100)

        attribute = self.pipeline.get_attribute("selected", "PARANG", static=False)
        assert np.allclose(np.mean(attribute), 20.296296296296298, rtol=limit, atol=0.)
        assert attribute.shape == (27, )

        attribute = self.pipeline.get_attribute("selected", "STAR_POSITION", static=False)
        assert np.allclose(np.mean(attribute), 60.2962962962963, rtol=limit, atol=0.)
        assert attribute.shape == (27, 2)

        attribute = self.pipeline.get_attribute("removed", "PARANG", static=False)
        assert np.allclose(np.mean(attribute), 21.6, rtol=limit, atol=0.)
        assert attribute.shape == (5, )

        attribute = self.pipeline.get_attribute("removed", "STAR_POSITION", static=False)
        assert np.allclose(np.mean(attribute), 61.6, rtol=limit, atol=0.)
        assert attribute.shape == (5, 2)

    def test_frame_selection(self):

        select = FrameSelectionModule(name_in="select1",
                                      image_in_tag="start",
                                      selected_out_tag="selected1",
                                      removed_out_tag="removed1",
                                      index_out_tag="index1",
                                      method="median",
                                      threshold=1.,
                                      fwhm=0.1,
                                      aperture=("circular", 0.2),
                                      position=(None, None, 0.5))

        self.pipeline.add_module(select)
        self.pipeline.run_module("select1")

        data = self.pipeline.get_data("selected1")
        assert np.allclose(data[0, 50, 50], 0.09791350617182591, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 9.980792188317311e-05, rtol=limit, atol=0.)
        assert data.shape == (22, 100, 100)

        data = self.pipeline.get_data("removed1")
        assert np.allclose(data[0, 50, 50], 0.09797376304048713, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010078412281191547, rtol=limit, atol=0.)
        assert data.shape == (10, 100, 100)

        data = self.pipeline.get_data("index1")
        assert data[-1] == 28
        assert np.sum(data) == 115
        assert data.shape == (10, )

        attribute = self.pipeline.get_attribute("selected1", "PARANG", static=False)
        assert np.allclose(np.mean(attribute), 22.681818181818183, rtol=limit, atol=0.)
        assert attribute.shape == (22, )

        attribute = self.pipeline.get_attribute("selected1", "STAR_POSITION", static=False)
        assert np.allclose(np.mean(attribute), 50.0, rtol=limit, atol=0.)
        assert attribute.shape == (22, 2)

        attribute = self.pipeline.get_attribute("removed1", "PARANG", static=False)
        assert np.allclose(np.mean(attribute), 15.7, rtol=limit, atol=0.)
        assert attribute.shape == (10, )

        attribute = self.pipeline.get_attribute("removed1", "STAR_POSITION", static=False)
        assert np.allclose(np.mean(attribute), 50.0, rtol=limit, atol=0.)
        assert attribute.shape == (10, 2)

        select = FrameSelectionModule(name_in="select2",
                                      image_in_tag="start",
                                      selected_out_tag="selected2",
                                      removed_out_tag="removed2",
                                      index_out_tag="index2",
                                      method="max",
                                      threshold=3.,
                                      fwhm=0.1,
                                      aperture=("annulus", 0.1, 0.2),
                                      position=None)

        self.pipeline.add_module(select)
        self.pipeline.run_module("select2")

        data = self.pipeline.get_data("selected2")
        assert np.allclose(data[0, 50, 50], 0.09797376304048713, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010037996502199598, rtol=limit, atol=0.)
        assert data.shape == (20, 100, 100)

        data = self.pipeline.get_data("removed2")
        assert np.allclose(data[0, 50, 50], 0.097912284606689, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 9.966801742575358e-05, rtol=limit, atol=0.)
        assert data.shape == (12, 100, 100)

        data = self.pipeline.get_data("index2")
        assert data[-1] == 30
        assert np.sum(data) == 230
        assert data.shape == (12, )

        attribute = self.pipeline.get_attribute("selected2", "PARANG", static=False)
        assert np.allclose(np.mean(attribute), 17.8, rtol=limit, atol=0.)
        assert attribute.shape == (20, )

        attribute = self.pipeline.get_attribute("selected2", "STAR_POSITION", static=False)
        assert np.allclose(np.mean(attribute), 50.0, rtol=limit, atol=0.)
        assert attribute.shape == (20, 2)

        attribute = self.pipeline.get_attribute("removed2", "PARANG", static=False)
        assert np.allclose(np.mean(attribute), 25.0, rtol=limit, atol=0.)
        assert attribute.shape == (12, )

        attribute = self.pipeline.get_attribute("removed2", "STAR_POSITION", static=False)
        assert np.allclose(np.mean(attribute), 50.0, rtol=limit, atol=0.)
        assert attribute.shape == (12, 2)
コード例 #21
0
class TestResizing:
    def setup_class(self) -> None:

        self.limit = 1e-10
        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(self.test_dir + 'resize')
        create_ifs_data(self.test_dir + 'resize_ifs')
        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self) -> None:

        remove_test_data(self.test_dir, folders=['resize', 'resize_ifs'])

    def test_read_data(self) -> None:

        module = FitsReadingModule(name_in='read',
                                   image_tag='read',
                                   input_dir=self.test_dir + 'resize',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('read')

        data = self.pipeline.get_data('read')
        assert np.sum(data) == pytest.approx(105.54278879805277,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

        module = FitsReadingModule(name_in='read_ifs',
                                   image_tag='read_ifs',
                                   input_dir=self.test_dir + 'resize_ifs',
                                   overwrite=True,
                                   check=True,
                                   ifs_data=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('read_ifs')

        data = self.pipeline.get_data('read_ifs')
        assert np.sum(data) == pytest.approx(749.8396528807369,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (3, 10, 21, 21)

    def test_crop_images(self) -> None:

        module = CropImagesModule(size=0.2,
                                  center=None,
                                  name_in='crop1',
                                  image_in_tag='read',
                                  image_out_tag='crop1')

        self.pipeline.add_module(module)
        self.pipeline.run_module('crop1')

        module = CropImagesModule(size=0.2,
                                  center=(4, 4),
                                  name_in='crop2',
                                  image_in_tag='read',
                                  image_out_tag='crop2')

        self.pipeline.add_module(module)
        self.pipeline.run_module('crop2')

        module = CropImagesModule(size=0.2,
                                  center=(4, 4),
                                  name_in='crop_ifs',
                                  image_in_tag='read_ifs',
                                  image_out_tag='crop_ifs')

        self.pipeline.add_module(module)
        self.pipeline.run_module('crop_ifs')

        data = self.pipeline.get_data('crop1')
        assert np.sum(data) == pytest.approx(104.93318507061295,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 9, 9)

        data = self.pipeline.get_data('crop2')
        assert np.sum(data) == pytest.approx(105.64863165433025,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 9, 9)

        data = self.pipeline.get_data('crop_ifs')
        assert np.sum(data) == pytest.approx(15.870936600122521,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (3, 10, 9, 9)

    def test_scale_images(self) -> None:

        module = ScaleImagesModule(name_in='scale1',
                                   image_in_tag='read',
                                   image_out_tag='scale1',
                                   scaling=(2., 2., None),
                                   pixscale=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('scale1')

        module = ScaleImagesModule(name_in='scale2',
                                   image_in_tag='read',
                                   image_out_tag='scale2',
                                   scaling=(None, None, 2.),
                                   pixscale=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('scale2')

        data = self.pipeline.get_data('scale1')
        assert np.sum(data) == pytest.approx(105.54278879805277,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 22, 22)

        data = self.pipeline.get_data('scale2')
        assert np.sum(data) == pytest.approx(211.08557759610554,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

        attr = self.pipeline.get_attribute('read', 'PIXSCALE', static=True)
        assert attr == pytest.approx(0.027, rel=self.limit, abs=0.)

        attr = self.pipeline.get_attribute('scale1', 'PIXSCALE', static=True)
        assert attr == pytest.approx(0.0135, rel=self.limit, abs=0.)

        attr = self.pipeline.get_attribute('scale2', 'PIXSCALE', static=True)
        assert attr == pytest.approx(0.027, rel=self.limit, abs=0.)

    def test_add_lines(self) -> None:

        module = AddLinesModule(lines=(2, 5, 0, 3),
                                name_in='add',
                                image_in_tag='read',
                                image_out_tag='add')

        self.pipeline.add_module(module)
        self.pipeline.run_module('add')

        data = self.pipeline.get_data('add')
        assert np.sum(data) == pytest.approx(105.54278879805275,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 14, 18)

    def test_remove_lines(self) -> None:

        module = RemoveLinesModule(lines=(2, 5, 0, 3),
                                   name_in='remove',
                                   image_in_tag='read',
                                   image_out_tag='remove')

        self.pipeline.add_module(module)
        self.pipeline.run_module('remove')

        data = self.pipeline.get_data('remove')
        assert np.sum(data) == pytest.approx(67.49726677462391,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 8, 4)

        with h5py.File(self.test_dir + 'PynPoint_database.hdf5',
                       'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 4

        self.pipeline.run_module('remove')

        data_multi = self.pipeline.get_data('remove')
        assert data == pytest.approx(data_multi, rel=self.limit, abs=0.)
        assert data.shape == data_multi.shape
コード例 #22
0
class TestHdf5WritingModule:

    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + '/'

        create_random(self.test_dir)
        create_config(self.test_dir+'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir, files=['test.hdf5'])

    def test_hdf5_writing(self):

        write = Hdf5WritingModule(file_name='test.hdf5',
                                  name_in='write1',
                                  output_dir=None,
                                  tag_dictionary={'images':'data1'},
                                  keep_attributes=True,
                                  overwrite=True)

        self.pipeline.add_module(write)
        self.pipeline.run_module('write1')

    def test_no_data_tag(self):

        write = Hdf5WritingModule(file_name='test.hdf5',
                                  name_in='write2',
                                  output_dir=None,
                                  tag_dictionary={'empty':'empty'},
                                  keep_attributes=True,
                                  overwrite=False)

        self.pipeline.add_module(write)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('write2')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'No data under the tag which is linked by the ' \
                                             'InputPort.'

    def test_overwrite_false(self):

        write = Hdf5WritingModule(file_name='test.hdf5',
                                  name_in='write3',
                                  output_dir=None,
                                  tag_dictionary={'images':'data2'},
                                  keep_attributes=True,
                                  overwrite=False)

        self.pipeline.add_module(write)
        self.pipeline.run_module('write3')

    def test_dictionary_none(self):

        write = Hdf5WritingModule(file_name='test.hdf5',
                                  name_in='write4',
                                  output_dir=None,
                                  tag_dictionary=None,
                                  keep_attributes=True,
                                  overwrite=False)

        self.pipeline.add_module(write)
        self.pipeline.run_module('write4')

    def test_hdf5_reading(self):

        read = Hdf5ReadingModule(name_in='read',
                                 input_filename='test.hdf5',
                                 input_dir=self.test_dir,
                                 tag_dictionary={'data1':'data1', 'data2':'data2'})

        self.pipeline.add_module(read)
        self.pipeline.run_module('read')

        data1 = self.pipeline.get_data('data1')
        data2 = self.pipeline.get_data('data2')
        data3 = self.pipeline.get_data('images')
        assert np.allclose(data1, data2, rtol=limit, atol=0.)
        assert np.allclose(data2, data3, rtol=limit, atol=0.)

        attribute1 = self.pipeline.get_attribute('images', 'PARANG', static=False)
        attribute2 = self.pipeline.get_attribute('data1', 'PARANG', static=False)
        attribute3 = self.pipeline.get_attribute('data2', 'PARANG', static=False)
        assert np.allclose(attribute1, attribute2, rtol=limit, atol=0.)
        assert np.allclose(attribute2, attribute3, rtol=limit, atol=0.)
コード例 #23
0
ファイル: test_limits.py プロジェクト: pahuber/PynPoint
class TestLimits:

    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(path=self.test_dir+'limits')
        create_config(self.test_dir+'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(path=self.test_dir,
                         folders=['limits'],
                         files=['model.AMES-Cond-2000.M-0.0.NaCo.Vega'])

    def test_read_data(self):

        module = FitsReadingModule(name_in='read',
                                   image_tag='read',
                                   input_dir=self.test_dir+'limits')

        self.pipeline.add_module(module)
        self.pipeline.run_module('read')

        data = self.pipeline.get_data('read')
        assert np.allclose(data[0, 10, 10], 0.00012958496246258364, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.00010029494781738066, rtol=limit, atol=0.)
        assert data.shape == (40, 100, 100)

    def test_angle_interpolation(self):

        module = AngleInterpolationModule(name_in='angle',
                                          data_tag='read')

        self.pipeline.add_module(module)
        self.pipeline.run_module('angle')

        data = self.pipeline.get_attribute('read', 'PARANG', static=False)
        assert data[5] == 2.7777777777777777
        assert np.allclose(np.mean(data), 10.0, rtol=limit, atol=0.)
        assert data.shape == (40, )

    def test_contrast_curve(self):

        proc = ['single', 'multi']

        for item in proc:

            if item == 'multi':
                with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'a') as hdf_file:
                    hdf_file['config'].attrs['CPU'] = 4

            module = ContrastCurveModule(name_in='contrast_'+item,
                                         image_in_tag='read',
                                         psf_in_tag='read',
                                         contrast_out_tag='limits_'+item,
                                         separation=(0.5, 0.6, 0.1),
                                         angle=(0., 360., 180.),
                                         threshold=('sigma', 5.),
                                         psf_scaling=1.,
                                         aperture=0.1,
                                         pca_number=15,
                                         cent_size=None,
                                         edge_size=None,
                                         extra_rot=0.)

            self.pipeline.add_module(module)
            self.pipeline.run_module('contrast_'+item)

            data = self.pipeline.get_data('limits_'+item)
            assert np.allclose(data[0, 0], 5.00000000e-01, rtol=limit, atol=0.)
            assert np.allclose(data[0, 1], 1.2034060712266164, rtol=limit, atol=0.)
            assert np.allclose(data[0, 2], 0.2594381985736874, rtol=limit, atol=0.)
            assert np.allclose(data[0, 3], 0.00012147700290954244, rtol=limit, atol=0.)
            assert data.shape == (1, 4)

    def test_contrast_curve_fpf(self):

        with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 1

        module = ContrastCurveModule(name_in='contrast_fpf',
                                     image_in_tag='read',
                                     psf_in_tag='read',
                                     contrast_out_tag='limits_fpf',
                                     separation=(0.5, 0.6, 0.1),
                                     angle=(0., 360., 180.),
                                     threshold=('fpf', 1e-6),
                                     psf_scaling=1.,
                                     aperture=0.1,
                                     pca_number=15,
                                     cent_size=None,
                                     edge_size=None,
                                     extra_rot=0.)

        self.pipeline.add_module(module)
        self.pipeline.run_module('contrast_fpf')

        data = self.pipeline.get_data('limits_fpf')
        assert np.allclose(data[0, 0], 5.00000000e-01, rtol=limit, atol=0.)
        assert np.allclose(data[0, 1], 0.6820548720545829, rtol=limit, atol=0.)
        assert np.allclose(data[0, 2], 0.2594381985736874, rtol=limit, atol=0.)
        assert np.allclose(data[0, 3], 1e-6, rtol=limit, atol=0.)
        assert data.shape == (1, 4)

    def test_mass_limits(self):

        separation = np.linspace(0.1, 1.0, 10)
        contrast = -2.5*np.log10(1e-4/separation)
        variance = 0.1*contrast

        limits = np.zeros((10, 4))
        limits[:, 0] = separation
        limits[:, 1] = contrast
        limits[:, 2] = variance

        with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'a') as hdf_file:
            hdf_file['contrast_limits'] = limits

        url = 'https://phoenix.ens-lyon.fr/Grids/AMES-Cond/ISOCHRONES/' \
              'model.AMES-Cond-2000.M-0.0.NaCo.Vega'

        filename = self.test_dir + 'model.AMES-Cond-2000.M-0.0.NaCo.Vega'

        urlretrieve(url, filename)

        module = MassLimitsModule(model_file=filename,
                                  star_prop={'magnitude': 10., 'distance': 100., 'age': 20.},
                                  name_in='mass',
                                  contrast_in_tag='contrast_limits',
                                  mass_out_tag='mass_limits',
                                  instr_filter='L\'')

        self.pipeline.add_module(module)
        self.pipeline.run_module('mass')

        data = self.pipeline.get_data('mass_limits')
        assert np.allclose(np.mean(data[:, 0]), 0.55, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data[:, 1]), 0.001891690765603738, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data[:, 2]), 0.000964309686441908, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data[:, 3]), -0.000696402843279597, rtol=limit, atol=0.)
        assert data.shape == (10, 4)
コード例 #24
0
class TestPypeline:
    def setup_class(self):
        self.test_dir = os.path.dirname(__file__) + '/'

        np.random.seed(1)

        image_3d = np.random.normal(loc=0, scale=2e-4, size=(4, 10, 10))
        image_2d = np.random.normal(loc=0, scale=2e-4, size=(1, 10, 10))
        science = np.random.normal(loc=0, scale=2e-4, size=(4, 10, 10))
        dark = np.random.normal(loc=0, scale=2e-4, size=(4, 10, 10))

        with h5py.File(self.test_dir + 'PynPoint_database.hdf5',
                       'w') as hdf_file:
            hdf_file.create_dataset('image_3d', data=image_3d)
            hdf_file.create_dataset('image_2d', data=image_2d)
            hdf_file.create_dataset('science', data=science)
            hdf_file.create_dataset('dark', data=dark)

        create_star_data(path=self.test_dir + 'images')
        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):
        remove_test_data(self.test_dir, folders=['images'])

    def test_output_port_name(self):
        read = FitsReadingModule(name_in='read',
                                 input_dir=self.test_dir + 'images',
                                 image_tag='images')
        read.add_output_port('test')

        with pytest.warns(UserWarning) as warning:
            read.add_output_port('test')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'Tag \'test\' of ReadingModule \'read\' is already ' \
                                             'used.'

        process = BadPixelSigmaFilterModule(name_in='badpixel',
                                            image_in_tag='images',
                                            image_out_tag='im_out')

        process.add_output_port('test')

        with pytest.warns(UserWarning) as warning:
            process.add_output_port('test')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'Tag \'test\' of ProcessingModule \'badpixel\' is ' \
                                             'already used.'

        self.pipeline.m_data_storage.close_connection()

        process._m_data_base = self.test_dir + 'database.hdf5'
        process.add_output_port('new')

    def test_apply_function_to_images_3d(self):
        self.pipeline.set_attribute('config', 'MEMORY', 1, static=True)

        remove = RemoveLinesModule(lines=(1, 0, 0, 0),
                                   name_in='remove1',
                                   image_in_tag='image_3d',
                                   image_out_tag='remove_3d')

        self.pipeline.add_module(remove)
        self.pipeline.run_module('remove1')

        data = self.pipeline.get_data('image_3d')
        assert np.allclose(np.mean(data),
                           1.0141852764605783e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 10, 10)

        data = self.pipeline.get_data('remove_3d')
        assert np.allclose(np.mean(data),
                           1.1477029889801025e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 10, 9)

    def test_apply_function_to_images_2d(self):
        remove = RemoveLinesModule(lines=(1, 0, 0, 0),
                                   name_in='remove2',
                                   image_in_tag='image_2d',
                                   image_out_tag='remove_2d')

        self.pipeline.add_module(remove)
        self.pipeline.run_module('remove2')

        data = self.pipeline.get_data('image_2d')
        assert np.allclose(np.mean(data),
                           1.2869483197883442e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (1, 10, 10)

        data = self.pipeline.get_data('remove_2d')
        assert np.allclose(np.mean(data),
                           1.3957075246029751e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (1, 10, 9)

    def test_apply_function_to_images_same_port(self):
        dark = DarkCalibrationModule(name_in='dark1',
                                     image_in_tag='science',
                                     dark_in_tag='dark',
                                     image_out_tag='science')

        self.pipeline.add_module(dark)
        self.pipeline.run_module('dark1')

        data = self.pipeline.get_data('science')
        assert np.allclose(np.mean(data),
                           -3.190113568690675e-06,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 10, 10)

        self.pipeline.set_attribute('config', 'MEMORY', 0, static=True)

        dark = DarkCalibrationModule(name_in='dark2',
                                     image_in_tag='science',
                                     dark_in_tag='dark',
                                     image_out_tag='science')

        self.pipeline.add_module(dark)
        self.pipeline.run_module('dark2')

        data = self.pipeline.get_data('science')
        assert np.allclose(np.mean(data),
                           -1.026073475228737e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 10, 10)

        remove = RemoveLinesModule(lines=(1, 0, 0, 0),
                                   name_in='remove3',
                                   image_in_tag='remove_3d',
                                   image_out_tag='remove_3d')

        self.pipeline.add_module(remove)

        with pytest.raises(ValueError) as error:
            self.pipeline.run_module('remove3')

        assert str(error.value) == 'Input and output port have the same tag while the input ' \
                                   'function is changing the image shape. This is only ' \
                                   'possible with MEMORY=None.'

    def test_apply_function_to_images_memory_none(self):
        remove = RemoveLinesModule(lines=(1, 0, 0, 0),
                                   name_in='remove4',
                                   image_in_tag='image_3d',
                                   image_out_tag='remove_3d_none')

        self.pipeline.add_module(remove)
        self.pipeline.run_module('remove4')

        data = self.pipeline.get_data('remove_3d_none')
        assert np.allclose(np.mean(data),
                           1.1477029889801025e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 10, 9)

    def test_apply_function_to_images_3d_args(self):
        self.pipeline.set_attribute('config', 'MEMORY', 1, static=True)
        self.pipeline.set_attribute('image_3d', 'PIXSCALE', 0.1, static=True)

        scale = ScaleImagesModule(scaling=(1.2, 1.2, 10.),
                                  pixscale=True,
                                  name_in='scale1',
                                  image_in_tag='image_3d',
                                  image_out_tag='scale_3d')

        self.pipeline.add_module(scale)
        self.pipeline.run_module('scale1')

        data = self.pipeline.get_data('scale_3d')
        assert np.allclose(np.mean(data),
                           7.042953308754017e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 12, 12)

        attribute = self.pipeline.get_attribute('scale_3d',
                                                'PIXSCALE',
                                                static=True)
        assert np.allclose(attribute, 0.08333333333333334, rtol=limit, atol=0.)

    def test_apply_function_to_images_2d_args(self):
        self.pipeline.set_attribute('image_2d', 'PIXSCALE', 0.1, static=True)

        scale = ScaleImagesModule(scaling=(1.2, 1.2, 10.),
                                  pixscale=True,
                                  name_in='scale2',
                                  image_in_tag='image_2d',
                                  image_out_tag='scale_2d')

        self.pipeline.add_module(scale)
        self.pipeline.run_module('scale2')

        data = self.pipeline.get_data('scale_2d')
        assert np.allclose(np.mean(data),
                           8.937141109641279e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (1, 12, 12)

        attribute = self.pipeline.get_attribute('scale_2d',
                                                'PIXSCALE',
                                                static=True)
        assert np.allclose(attribute, 0.08333333333333334, rtol=limit, atol=0.)
コード例 #25
0
class TestTimeDenoising:

    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(path=self.test_dir+'images',
                         npix_x=20,
                         npix_y=20,
                         x0=[10, 10, 10, 10],
                         y0=[10, 10, 10, 10],
                         parang_start=[0., 25., 50., 75.],
                         parang_end=[25., 50., 75., 100.],
                         exp_no=[1, 2, 3, 4])

        create_config(self.test_dir+'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir, folders=['images'])

    def test_read_data(self):

        module = FitsReadingModule(name_in='read',
                                   image_tag='images',
                                   input_dir=self.test_dir+'images',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('read')

        data = self.pipeline.get_data('images')
        assert np.allclose(data[0, 10, 10], 0.09799496683489618, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0025020285041348557, rtol=limit, atol=0.)
        assert data.shape == (40, 20, 20)

    def test_wavelet_denoising_cwt_dog(self):

        cwt_config = CwtWaveletConfiguration(wavelet='dog',
                                             wavelet_order=2,
                                             keep_mean=False,
                                             resolution=0.5)

        assert cwt_config.m_wavelet == 'dog'
        assert np.allclose(cwt_config.m_wavelet_order, 2, rtol=limit, atol=0.)
        assert not cwt_config.m_keep_mean
        assert np.allclose(cwt_config.m_resolution, 0.5, rtol=limit, atol=0.)

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_cwt_dog',
                                            image_in_tag='images',
                                            image_out_tag='wavelet_cwt_dog',
                                            padding='zero',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_cwt_dog')

        data = self.pipeline.get_data('wavelet_cwt_dog')
        assert np.allclose(data[0, 10, 10], 0.09805577173716859, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.002502083112599873, rtol=limit, atol=0.)
        assert data.shape == (40, 20, 20)

        with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 4

        self.pipeline.run_module('wavelet_cwt_dog')

        data_multi = self.pipeline.get_data('wavelet_cwt_dog')
        assert np.allclose(data, data_multi, rtol=limit, atol=0.)
        assert data.shape == data_multi.shape

    def test_wavelet_denoising_cwt_morlet(self):

        with h5py.File(self.test_dir+'PynPoint_database.hdf5', 'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 1

        cwt_config = CwtWaveletConfiguration(wavelet='morlet',
                                             wavelet_order=5,
                                             keep_mean=False,
                                             resolution=0.5)

        assert cwt_config.m_wavelet == 'morlet'
        assert np.allclose(cwt_config.m_wavelet_order, 5, rtol=limit, atol=0.)
        assert not cwt_config.m_keep_mean
        assert np.allclose(cwt_config.m_resolution, 0.5, rtol=limit, atol=0.)

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_cwt_morlet',
                                            image_in_tag='images',
                                            image_out_tag='wavelet_cwt_morlet',
                                            padding='mirror',
                                            median_filter=False,
                                            threshold_function='hard')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_cwt_morlet')

        data = self.pipeline.get_data('wavelet_cwt_morlet')
        assert np.allclose(data[0, 10, 10], 0.09805577173716859, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0025019409784314286, rtol=limit, atol=0.)
        assert data.shape == (40, 20, 20)

        data = self.pipeline.get_attribute('wavelet_cwt_morlet', 'NFRAMES', static=False)
        assert np.allclose(data, [10, 10, 10, 10], rtol=limit, atol=0.)

    def test_wavelet_denoising_dwt(self):

        dwt_config = DwtWaveletConfiguration(wavelet='db8')

        assert dwt_config.m_wavelet == 'db8'

        module = WaveletTimeDenoisingModule(wavelet_configuration=dwt_config,
                                            name_in='wavelet_dwt',
                                            image_in_tag='images',
                                            image_out_tag='wavelet_dwt',
                                            padding='zero',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_dwt')

        data = self.pipeline.get_data('wavelet_dwt')
        assert np.allclose(data[0, 10, 10], 0.09650639476873678, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0024998798596330475, rtol=limit, atol=0.)
        assert data.shape == (40, 20, 20)

    def test_time_normalization(self):

        module = TimeNormalizationModule(name_in='timenorm',
                                         image_in_tag='images',
                                         image_out_tag='timenorm')

        self.pipeline.add_module(module)
        self.pipeline.run_module('timenorm')

        data = self.pipeline.get_data('timenorm')
        assert np.allclose(data[0, 10, 10], 0.09793500165714215, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0024483409033199985, rtol=limit, atol=0.)
        assert data.shape == (40, 20, 20)

    def test_wavelet_denoising_odd_size(self):

        module = AddLinesModule(name_in='add',
                                image_in_tag='images',
                                image_out_tag='images_odd',
                                lines=(1, 0, 1, 0))

        self.pipeline.add_module(module)
        self.pipeline.run_module('add')

        data = self.pipeline.get_data('images_odd')
        assert np.allclose(data[0, 10, 10], 0.05294085050174391, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.002269413609192613, rtol=limit, atol=0.)
        assert data.shape == (40, 21, 21)

        cwt_config = CwtWaveletConfiguration(wavelet='dog',
                                             wavelet_order=2,
                                             keep_mean=False,
                                             resolution=0.5)

        assert cwt_config.m_wavelet == 'dog'
        assert np.allclose(cwt_config.m_wavelet_order, 2, rtol=limit, atol=0.)
        assert not cwt_config.m_keep_mean
        assert np.allclose(cwt_config.m_resolution, 0.5, rtol=limit, atol=0.)

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_odd_1',
                                            image_in_tag='images_odd',
                                            image_out_tag='wavelet_odd_1',
                                            padding='zero',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_odd_1')

        data = self.pipeline.get_data('wavelet_odd_1')
        assert np.allclose(data[0, 10, 10], 0.0529782051386938, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0022694631406801565, rtol=limit, atol=0.)
        assert data.shape == (40, 21, 21)

        module = WaveletTimeDenoisingModule(wavelet_configuration=cwt_config,
                                            name_in='wavelet_odd_2',
                                            image_in_tag='images_odd',
                                            image_out_tag='wavelet_odd_2',
                                            padding='mirror',
                                            median_filter=True,
                                            threshold_function='soft')

        self.pipeline.add_module(module)
        self.pipeline.run_module('wavelet_odd_2')

        data = self.pipeline.get_data('wavelet_odd_2')
        assert np.allclose(data[0, 10, 10], 0.05297146283932275, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.0022694809842930034, rtol=limit, atol=0.)
        assert data.shape == (40, 21, 21)

        data = self.pipeline.get_attribute('images', 'NFRAMES', static=False)
        assert np.allclose(data, [10, 10, 10, 10], rtol=limit, atol=0.)

        data = self.pipeline.get_attribute('wavelet_odd_1', 'NFRAMES', static=False)
        assert np.allclose(data, [10, 10, 10, 10], rtol=limit, atol=0.)

        data = self.pipeline.get_attribute('wavelet_odd_2', 'NFRAMES', static=False)
        assert np.allclose(data, [10, 10, 10, 10], rtol=limit, atol=0.)
コード例 #26
0
ファイル: test_background.py プロジェクト: nenasedk/PynPoint
class TestBackground:
    def setup_class(self) -> None:

        self.limit = 1e-10
        self.test_dir = os.path.dirname(__file__) + '/'

        create_dither_data(self.test_dir + 'dither')
        create_star_data(self.test_dir + 'science')
        create_star_data(self.test_dir + 'sky')
        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self) -> None:

        remove_test_data(self.test_dir, folders=['dither', 'science', 'sky'])

    def test_read_data(self) -> None:

        module = FitsReadingModule(name_in='read1',
                                   image_tag='dither',
                                   input_dir=self.test_dir + 'dither')

        self.pipeline.add_module(module)
        self.pipeline.run_module('read1')

        data = self.pipeline.get_data('dither')
        assert np.sum(data) == pytest.approx(211.20534661914408,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (20, 21, 21)

        module = FitsReadingModule(name_in='read2',
                                   image_tag='science',
                                   input_dir=self.test_dir + 'science')

        self.pipeline.add_module(module)
        self.pipeline.run_module('read2')

        data = self.pipeline.get_data('science')
        assert np.sum(data) == pytest.approx(105.54278879805277,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

        module = FitsReadingModule(name_in='read3',
                                   image_tag='sky',
                                   input_dir=self.test_dir + 'sky')

        self.pipeline.add_module(module)
        self.pipeline.run_module('read3')

        data = self.pipeline.get_data('sky')
        assert np.sum(data) == pytest.approx(105.54278879805277,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

    def test_simple_background(self) -> None:

        module = SimpleBackgroundSubtractionModule(shift=5,
                                                   name_in='simple',
                                                   image_in_tag='dither',
                                                   image_out_tag='simple')

        self.pipeline.add_module(module)
        self.pipeline.run_module('simple')

        data = self.pipeline.get_data('simple')
        assert np.sum(data) == pytest.approx(3.552713678800501e-15,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (20, 21, 21)

    def test_mean_background_shift(self) -> None:

        module = MeanBackgroundSubtractionModule(shift=5,
                                                 cubes=1,
                                                 name_in='mean2',
                                                 image_in_tag='dither',
                                                 image_out_tag='mean2')

        self.pipeline.add_module(module)
        self.pipeline.run_module('mean2')

        data = self.pipeline.get_data('mean2')
        assert np.sum(data) == pytest.approx(2.473864361018551,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (20, 21, 21)

    def test_mean_background_nframes(self) -> None:

        module = MeanBackgroundSubtractionModule(shift=None,
                                                 cubes=1,
                                                 name_in='mean1',
                                                 image_in_tag='dither',
                                                 image_out_tag='mean1')

        self.pipeline.add_module(module)
        self.pipeline.run_module('mean1')

        data = self.pipeline.get_data('mean1')
        assert np.sum(data) == pytest.approx(2.473864361018551,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (20, 21, 21)

    def test_dithering_attributes(self) -> None:

        module = DitheringBackgroundModule(name_in='pca_dither1',
                                           image_in_tag='dither',
                                           image_out_tag='pca_dither1',
                                           center=None,
                                           cubes=None,
                                           size=0.2,
                                           gaussian=0.05,
                                           subframe=0.1,
                                           pca_number=1,
                                           mask_star=0.05,
                                           crop=True,
                                           prepare=True,
                                           pca_background=True,
                                           combine='pca')

        self.pipeline.add_module(module)
        self.pipeline.run_module('pca_dither1')

        data = self.pipeline.get_data('dither_dither_crop1')
        assert np.sum(data) == pytest.approx(54.62410860562912,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (20, 9, 9)

        data = self.pipeline.get_data('dither_dither_star1')
        assert np.sum(data) == pytest.approx(54.873885838788595,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (5, 9, 9)

        data = self.pipeline.get_data('dither_dither_mean1')
        assert np.sum(data) == pytest.approx(54.204960755115245,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (5, 9, 9)

        data = self.pipeline.get_data('dither_dither_background1')
        assert np.sum(data) == pytest.approx(-0.24977723315947564,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (15, 9, 9)

        data = self.pipeline.get_data('dither_dither_pca_fit1')
        assert np.sum(data) == pytest.approx(-0.01019999314121019,
                                             rel=1e-5,
                                             abs=0.)
        print(np.sum(data))
        assert data.shape == (5, 9, 9)

        data = self.pipeline.get_data('dither_dither_pca_res1')
        assert np.sum(data) == pytest.approx(54.884085831929795,
                                             rel=1e-6,
                                             abs=0.)
        assert data.shape == (5, 9, 9)

        data = self.pipeline.get_data('dither_dither_pca_mask1')
        assert np.sum(data) == pytest.approx(360.0, rel=self.limit, abs=0.)
        assert data.shape == (5, 9, 9)

        data = self.pipeline.get_data('pca_dither1')
        assert np.sum(data) == pytest.approx(208.774670964812,
                                             rel=1e-6,
                                             abs=0.)
        assert data.shape == (20, 9, 9)

        attr = self.pipeline.get_attribute('dither_dither_pca_res1',
                                           'STAR_POSITION',
                                           static=False)
        assert np.sum(attr) == pytest.approx(51., rel=self.limit, abs=0.)
        assert attr.shape == (5, 2)

    def test_dithering_center(self) -> None:

        module = DitheringBackgroundModule(name_in='pca_dither2',
                                           image_in_tag='dither',
                                           image_out_tag='pca_dither2',
                                           center=((5, 5), (5, 15), (15, 15),
                                                   (15, 5)),
                                           cubes=1,
                                           size=0.2,
                                           gaussian=0.05,
                                           subframe=None,
                                           pca_number=1,
                                           mask_star=0.05,
                                           crop=True,
                                           prepare=True,
                                           pca_background=True,
                                           combine='pca')

        self.pipeline.add_module(module)
        self.pipeline.run_module('pca_dither2')

        data = self.pipeline.get_data('pca_dither2')
        assert np.sum(data) == pytest.approx(209.8271898501695,
                                             rel=1e-6,
                                             abs=0.)
        assert data.shape == (20, 9, 9)

    def test_nodding_background(self) -> None:

        module = StackCubesModule(name_in='mean',
                                  image_in_tag='sky',
                                  image_out_tag='mean',
                                  combine='mean')

        self.pipeline.add_module(module)
        self.pipeline.run_module('mean')

        data = self.pipeline.get_data('mean')
        assert np.sum(data) == pytest.approx(21.108557759610548,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (2, 11, 11)

        attr = self.pipeline.get_attribute('mean', 'INDEX', static=False)
        assert np.sum(attr) == pytest.approx(1, rel=self.limit, abs=0.)
        assert attr.shape == (2, )

        attr = self.pipeline.get_attribute('mean', 'NFRAMES', static=False)
        assert np.sum(attr) == pytest.approx(2, rel=self.limit, abs=0.)
        assert attr.shape == (2, )

        module = NoddingBackgroundModule(name_in='nodding',
                                         sky_in_tag='mean',
                                         science_in_tag='science',
                                         image_out_tag='nodding',
                                         mode='both')

        self.pipeline.add_module(module)
        self.pipeline.run_module('nodding')

        data = self.pipeline.get_data('nodding')
        assert np.sum(data) == pytest.approx(1.793466459074705,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

    def test_line_background_mean(self) -> None:

        module = LineSubtractionModule(name_in='line1',
                                       image_in_tag='science',
                                       image_out_tag='science_line1',
                                       combine='mean',
                                       mask=0.1)

        self.pipeline.add_module(module)
        self.pipeline.run_module('line1')

        data = self.pipeline.get_data('science_line1')
        assert np.sum(data) == pytest.approx(104.55443019231085,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)

    def test_line_background_median(self) -> None:

        module = LineSubtractionModule(name_in='line2',
                                       image_in_tag='science',
                                       image_out_tag='science_line2',
                                       combine='median',
                                       mask=0.1)

        self.pipeline.add_module(module)
        self.pipeline.run_module('line2')

        data = self.pipeline.get_data('science_line2')
        assert np.sum(data) == pytest.approx(106.09825573198366,
                                             rel=self.limit,
                                             abs=0.)
        assert data.shape == (10, 11, 11)
コード例 #27
0
class TestHdf5WritingModule(object):

    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + "/"

        create_random(self.test_dir)
        create_config(self.test_dir+"PynPoint_config.ini")

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir, files=["test.hdf5"])

    def test_hdf5_writing(self):

        write = Hdf5WritingModule(file_name="test.hdf5",
                                  name_in="write1",
                                  output_dir=None,
                                  tag_dictionary={"images":"data1"},
                                  keep_attributes=True,
                                  overwrite=True)

        self.pipeline.add_module(write)
        self.pipeline.run_module("write1")

    def test_no_data_tag(self):

        write = Hdf5WritingModule(file_name="test.hdf5",
                                  name_in="write2",
                                  output_dir=None,
                                  tag_dictionary={"empty":"empty"},
                                  keep_attributes=True,
                                  overwrite=False)

        self.pipeline.add_module(write)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module("write2")

        assert len(warning) == 1
        assert warning[0].message.args[0] == "No data under the tag which is linked by the " \
                                             "InputPort."

    def test_overwrite_false(self):

        write = Hdf5WritingModule(file_name="test.hdf5",
                                  name_in="write3",
                                  output_dir=None,
                                  tag_dictionary={"images":"data2"},
                                  keep_attributes=True,
                                  overwrite=False)

        self.pipeline.add_module(write)
        self.pipeline.run_module("write3")

    def test_dictionary_none(self):

        write = Hdf5WritingModule(file_name="test.hdf5",
                                  name_in="write4",
                                  output_dir=None,
                                  tag_dictionary=None,
                                  keep_attributes=True,
                                  overwrite=False)

        self.pipeline.add_module(write)
        self.pipeline.run_module("write4")

    def test_hdf5_reading(self):

        read = Hdf5ReadingModule(name_in="read",
                                 input_filename="test.hdf5",
                                 input_dir=self.test_dir,
                                 tag_dictionary={"data1":"data1", "data2":"data2"})

        self.pipeline.add_module(read)
        self.pipeline.run_module("read")

        data1 = self.pipeline.get_data("data1")
        data2 = self.pipeline.get_data("data2")
        data3 = self.pipeline.get_data("images")
        assert np.allclose(data1, data2, rtol=limit, atol=0.)
        assert np.allclose(data2, data3, rtol=limit, atol=0.)

        attribute1 = self.pipeline.get_attribute("images", "PARANG", static=False)
        attribute2 = self.pipeline.get_attribute("data1", "PARANG", static=False)
        attribute3 = self.pipeline.get_attribute("data2", "PARANG", static=False)
        assert np.allclose(attribute1, attribute2, rtol=limit, atol=0.)
        assert np.allclose(attribute2, attribute3, rtol=limit, atol=0.)
コード例 #28
0
class TestPSFpreparation(object):
    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + "/"

        create_star_data(path=self.test_dir + "prep")
        create_config(self.test_dir + "PynPoint_config.ini")

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir, folders=["prep"])

    def test_read_data(self):

        read = FitsReadingModule(name_in="read",
                                 image_tag="read",
                                 input_dir=self.test_dir + "prep")

        self.pipeline.add_module(read)
        self.pipeline.run_module("read")

        data = self.pipeline.get_data("read")
        assert np.allclose(data[0, 25, 25],
                           2.0926464668090656e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738066,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

    def test_angle_interpolation(self):

        angle = AngleInterpolationModule(name_in="angle1", data_tag="read")

        self.pipeline.add_module(angle)
        self.pipeline.run_module("angle1")

        data = self.pipeline.get_data("header_read/PARANG")
        assert np.allclose(data[0], 0., rtol=limit, atol=0.)
        assert np.allclose(data[15], 7.777777777777778, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 10.0, rtol=limit, atol=0.)
        assert data.shape == (40, )

    def test_angle_calculation(self):

        self.pipeline.set_attribute("read", "LATITUDE", -25.)
        self.pipeline.set_attribute("read", "LONGITUDE", -70.)
        self.pipeline.set_attribute("read", "DIT", 1.)

        self.pipeline.set_attribute("read",
                                    "RA", (90., 90., 90., 90.),
                                    static=False)
        self.pipeline.set_attribute("read",
                                    "DEC", (-51., -51., -51., -51.),
                                    static=False)
        self.pipeline.set_attribute("read",
                                    "PUPIL", (90., 90., 90., 90.),
                                    static=False)

        date = ("2012-12-01T07:09:00.0000", "2012-12-01T07:09:01.0000",
                "2012-12-01T07:09:02.0000", "2012-12-01T07:09:03.0000")

        self.pipeline.set_attribute("read", "DATE", date, static=False)

        angle = AngleCalculationModule(instrument="NACO",
                                       name_in="angle2",
                                       data_tag="read")

        self.pipeline.add_module(angle)
        self.pipeline.run_module("angle2")

        data = self.pipeline.get_data("header_read/PARANG")
        assert np.allclose(data[0], -55.041097524594186, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data),
                           -54.99858342139904,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, )

        self.pipeline.set_attribute("read",
                                    "RA", (60000.0, 60000.0, 60000.0, 60000.0),
                                    static=False)
        self.pipeline.set_attribute("read",
                                    "DEC",
                                    (-510000., -510000., -510000., -510000.),
                                    static=False)

        angle = AngleCalculationModule(instrument="SPHERE/IRDIS",
                                       name_in="angle3",
                                       data_tag="read")

        self.pipeline.add_module(angle)
        self.pipeline.run_module("angle3")

        data = self.pipeline.get_data("header_read/PARANG")
        assert np.allclose(data[0], 170.39102733657813, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data),
                           170.46341141667205,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, )

        angle = AngleCalculationModule(instrument="SPHERE/IFS",
                                       name_in="angle4",
                                       data_tag="read")

        self.pipeline.add_module(angle)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module("angle4")

        assert len(warning) == 2
        assert warning[0].message.args[0] == "AngleCalculationModule has not been tested for " \
                                             "SPHERE/IFS data."
        assert warning[1].message.args[0] == "For SPHERE data it is recommended to use the " \
                                             "header keywords \"ESO INS4 DROT2 RA/DEC\" to " \
                                             "specify the object's position. The input will be " \
                                             "parsed accordingly. Using the regular RA/DEC "\
                                             "parameters will lead to wrong parallactic angles." \

        data = self.pipeline.get_data("header_read/PARANG")
        assert np.allclose(data[0], -89.12897266342185, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data),
                           -89.02755900320116,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, )

    def test_angle_interpolation_mismatch(self):

        self.pipeline.set_attribute("read", "NDIT", [9, 9, 9, 9], static=False)

        angle = AngleInterpolationModule(name_in="angle5", data_tag="read")

        self.pipeline.add_module(angle)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module("angle5")

        assert len(warning) == 1
        assert warning[0].message.args[0] == "There is a mismatch between the NDIT and NFRAMES " \
                                             "values. The derotation angles are calculated with " \
                                             "a linear interpolation by using NFRAMES steps. A " \
                                             "frame selection should be applied after the " \
                                             "derotation angles are calculated."

        data = self.pipeline.get_data("header_read/PARANG")
        assert np.allclose(data[0], 0., rtol=limit, atol=0.)
        assert np.allclose(data[15], 7.777777777777778, rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 10.0, rtol=limit, atol=0.)
        assert data.shape == (40, )

    def test_psf_preparation_norm_mask(self):

        prep = PSFpreparationModule(name_in="prep1",
                                    image_in_tag="read",
                                    image_out_tag="prep1",
                                    mask_out_tag="mask1",
                                    norm=True,
                                    cent_size=0.1,
                                    edge_size=1.0)

        self.pipeline.add_module(prep)
        self.pipeline.run_module("prep1")

        data = self.pipeline.get_data("prep1")
        assert np.allclose(data[0, 0, 0], 0., rtol=limit, atol=0.)
        assert np.allclose(data[0, 99, 99], 0., rtol=limit, atol=0.)
        assert np.allclose(np.mean(data),
                           0.0001690382058762809,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

        data = self.pipeline.get_data("mask1")
        assert np.allclose(data[0, 0], 0., rtol=limit, atol=0.)
        assert np.allclose(data[99, 99], 0., rtol=limit, atol=0.)
        assert np.allclose(np.mean(data), 0.4268, rtol=limit, atol=0.)
        assert data.shape == (100, 100)

    def test_psf_preparation_none(self):

        prep = PSFpreparationModule(name_in="prep2",
                                    image_in_tag="read",
                                    image_out_tag="prep2",
                                    mask_out_tag="mask2",
                                    norm=False,
                                    cent_size=None,
                                    edge_size=None)

        self.pipeline.add_module(prep)
        self.pipeline.run_module("prep2")

        data = self.pipeline.get_data("prep2")
        assert np.allclose(data[0, 0, 0],
                           0.00032486907273264834,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(data[0, 25, 25],
                           2.0926464668090656e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(data[0, 99, 99],
                           -0.000287573978535779,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738066,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

    def test_psf_preparation_no_mask_out(self):

        prep = PSFpreparationModule(name_in="prep3",
                                    image_in_tag="read",
                                    image_out_tag="prep3",
                                    mask_out_tag=None,
                                    norm=False,
                                    cent_size=None,
                                    edge_size=None)

        self.pipeline.add_module(prep)
        self.pipeline.run_module("prep3")

        data = self.pipeline.get_data("prep3")
        assert np.allclose(data[0, 0, 0],
                           0.00032486907273264834,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(data[0, 25, 25],
                           2.0926464668090656e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(data[0, 99, 99],
                           -0.000287573978535779,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738066,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

    def test_sdi_preparation(self):

        sdi = SDIpreparationModule(name_in="sdi",
                                   wavelength=(0.65, 0.6),
                                   width=(0.1, 0.5),
                                   image_in_tag="read",
                                   image_out_tag="sdi")

        self.pipeline.add_module(sdi)
        self.pipeline.run_module("sdi")

        data = self.pipeline.get_data("sdi")
        assert np.allclose(data[0, 25, 25],
                           -2.6648118007008814e-05,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           2.0042892634995876e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

        attribute = self.pipeline.get_attribute(
            "sdi", "History: SDIpreparationModule")
        assert attribute == "(line, continuum) = (0.65, 0.6)"
コード例 #29
0
ファイル: test_extract.py プロジェクト: pahuber/PynPoint
class TestExtract:
    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(path=self.test_dir + 'star',
                         npix_x=51,
                         npix_y=51,
                         x0=[10., 10., 10., 10.],
                         y0=[10., 10., 10., 10.])

        create_fake(path=self.test_dir + 'binary',
                    ndit=[20, 20, 20, 20],
                    nframes=[20, 20, 20, 20],
                    exp_no=[1, 2, 3, 4],
                    npix=(101, 101),
                    fwhm=3.,
                    x0=[50, 50, 50, 50],
                    y0=[50, 50, 50, 50],
                    angles=[[0., 25.], [25., 50.], [50., 75.], [75., 100.]],
                    sep=20.,
                    contrast=1.)

        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(path=self.test_dir, folders=['star', 'binary'])

    def test_read_data(self):

        module = FitsReadingModule(name_in='read1',
                                   image_tag='star',
                                   input_dir=self.test_dir + 'star',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('read1')

        data = self.pipeline.get_data('star')
        assert np.allclose(data[0, 10, 10],
                           0.09834884212021108,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00038538535294683216,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 51, 51)

        module = FitsReadingModule(name_in='read2',
                                   image_tag='binary',
                                   input_dir=self.test_dir + 'binary',
                                   overwrite=True,
                                   check=True)

        self.pipeline.add_module(module)
        self.pipeline.run_module('read2')

        data = self.pipeline.get_data('binary')
        assert np.allclose(data[0, 50, 50],
                           0.0986064357966972,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00019636787665654158,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (80, 101, 101)

    def test_angle_interpolation(self):

        module = AngleInterpolationModule(name_in='angle', data_tag='binary')

        self.pipeline.add_module(module)
        self.pipeline.run_module('angle')

        data = self.pipeline.get_attribute('binary', 'PARANG', static=False)
        assert data[5] == 6.578947368421053
        assert np.allclose(np.mean(data), 50.0, rtol=limit, atol=0.)
        assert data.shape == (80, )

        parang = self.pipeline.get_attribute('binary', 'PARANG', static=False)
        self.pipeline.set_attribute('binary',
                                    'PARANG',
                                    -1. * parang,
                                    static=False)

        data = self.pipeline.get_attribute('binary', 'PARANG', static=False)
        assert data[5] == -6.578947368421053
        assert np.allclose(np.mean(data), -50.0, rtol=limit, atol=0.)
        assert data.shape == (80, )

    def test_extract_position_none(self):

        module = StarExtractionModule(name_in='extract1',
                                      image_in_tag='star',
                                      image_out_tag='extract1',
                                      index_out_tag='index',
                                      image_size=0.4,
                                      fwhm_star=0.1,
                                      position=None)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('extract1')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'The new dataset that is stored under the tag name ' \
                                             '\'index\' is empty.'

        data = self.pipeline.get_data('extract1')

        assert np.allclose(data[0, 7, 7],
                           0.09834884212021108,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.004444871536643222,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 15, 15)

        attr = self.pipeline.get_attribute('extract1',
                                           'STAR_POSITION',
                                           static=False)
        assert attr[10, 0] == attr[10, 1] == 10

    def test_extract_center_none(self):

        module = StarExtractionModule(name_in='extract2',
                                      image_in_tag='star',
                                      image_out_tag='extract2',
                                      index_out_tag='index',
                                      image_size=0.4,
                                      fwhm_star=0.1,
                                      position=(None, None, 1.))

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('extract2')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'The new dataset that is stored under the tag name ' \
                                             '\'index\' is empty.'

        data = self.pipeline.get_data('extract2')

        assert np.allclose(data[0, 7, 7],
                           0.09834884212021108,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.004444871536643222,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 15, 15)

        attr = self.pipeline.get_attribute('extract2',
                                           'STAR_POSITION',
                                           static=False)
        assert attr[10, 0] == attr[10, 1] == 10

    def test_extract_position(self):

        module = StarExtractionModule(name_in='extract7',
                                      image_in_tag='star',
                                      image_out_tag='extract7',
                                      index_out_tag=None,
                                      image_size=0.4,
                                      fwhm_star=0.1,
                                      position=(10, 10, 0.1))

        self.pipeline.add_module(module)
        self.pipeline.run_module('extract7')

        data = self.pipeline.get_data('extract7')

        assert np.allclose(data[0, 7, 7],
                           0.09834884212021108,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.004444871536643222,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 15, 15)

        attr = self.pipeline.get_attribute('extract7',
                                           'STAR_POSITION',
                                           static=False)
        assert attr[10, 0] == attr[10, 1] == 10

    def test_extract_too_large(self):

        module = StarExtractionModule(name_in='extract3',
                                      image_in_tag='star',
                                      image_out_tag='extract3',
                                      index_out_tag=None,
                                      image_size=0.8,
                                      fwhm_star=0.1,
                                      position=None)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('extract3')

        assert len(warning) == 40

        for i, item in enumerate(warning):
            assert item.message.args[0] == f'Chosen image size is too large to crop the image ' \
                                           f'around the brightest pixel (image index = {i}, ' \
                                           f'pixel [x, y] = [10, 10]). Using the center of ' \
                                           f'the image instead.'

        data = self.pipeline.get_data('extract3')

        assert np.allclose(data[0, 0, 0],
                           0.09834884212021108,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.0004499242959139202,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 31, 31)

        attr = self.pipeline.get_attribute('extract3',
                                           'STAR_POSITION',
                                           static=False)
        assert attr[10, 0] == attr[10, 1] == 25

    def test_star_extract_cpu(self):

        with h5py.File(self.test_dir + 'PynPoint_database.hdf5',
                       'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 4

        module = StarExtractionModule(name_in='extract4',
                                      image_in_tag='star',
                                      image_out_tag='extract4',
                                      index_out_tag='index',
                                      image_size=0.8,
                                      fwhm_star=0.1,
                                      position=None)

        self.pipeline.add_module(module)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('extract4')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'Chosen image size is too large to crop the image ' \
                                             'around the brightest pixel. Using the center of ' \
                                             'the image instead.'

    def test_extract_binary(self):

        with h5py.File(self.test_dir + 'PynPoint_database.hdf5',
                       'a') as hdf_file:
            hdf_file['config'].attrs['CPU'] = 1

        module = ExtractBinaryModule(pos_center=(50., 50.),
                                     pos_binary=(50., 70.),
                                     name_in='extract5',
                                     image_in_tag='binary',
                                     image_out_tag='extract5',
                                     image_size=0.5,
                                     search_size=0.2,
                                     filter_size=None)

        self.pipeline.add_module(module)
        self.pipeline.run_module('extract5')

        data = self.pipeline.get_data('extract5')

        assert np.allclose(data[0, 9, 9],
                           0.09774483733119443,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.0027700881940171283,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (80, 19, 19)

    def test_extract_binary_filter(self):

        module = ExtractBinaryModule(pos_center=(50., 50.),
                                     pos_binary=(50., 70.),
                                     name_in='extract6',
                                     image_in_tag='binary',
                                     image_out_tag='extract6',
                                     image_size=0.5,
                                     search_size=0.2,
                                     filter_size=0.1)

        self.pipeline.add_module(module)
        self.pipeline.run_module('extract6')

        data = self.pipeline.get_data('extract6')

        assert np.allclose(data[0, 9, 9],
                           0.09774483733119443,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.002770040591615301,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (80, 19, 19)
コード例 #30
0
class TestStackSubset:
    def setup_class(self):

        self.test_dir = os.path.dirname(__file__) + '/'

        create_star_data(path=self.test_dir + 'data')
        create_star_data(path=self.test_dir + 'extra')

        create_config(self.test_dir + 'PynPoint_config.ini')

        self.pipeline = Pypeline(self.test_dir, self.test_dir, self.test_dir)

    def teardown_class(self):

        remove_test_data(self.test_dir, folders=['data', 'extra'])

    def test_read_data(self):

        read = FitsReadingModule(name_in='read1',
                                 image_tag='images',
                                 input_dir=self.test_dir + 'data',
                                 overwrite=True,
                                 check=True)

        self.pipeline.add_module(read)
        self.pipeline.run_module('read1')

        data = self.pipeline.get_data('images')
        assert np.allclose(data[0, 50, 50],
                           0.09798413502193704,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738066,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (40, 100, 100)

        read = FitsReadingModule(name_in='read2',
                                 image_tag='extra',
                                 input_dir=self.test_dir + 'extra',
                                 overwrite=True,
                                 check=True)

        self.pipeline.add_module(read)
        self.pipeline.run_module('read2')

        extra = self.pipeline.get_data('extra')
        assert np.allclose(data, extra, rtol=limit, atol=0.)

    def test_stack_and_subset(self):

        self.pipeline.set_attribute('images',
                                    'PARANG',
                                    np.arange(1., 41., 1.),
                                    static=False)

        stack = StackAndSubsetModule(name_in='stack',
                                     image_in_tag='images',
                                     image_out_tag='stack',
                                     random=10,
                                     stacking=2)

        self.pipeline.add_module(stack)
        self.pipeline.run_module('stack')

        data = self.pipeline.get_data('stack')
        assert np.allclose(data[0, 50, 50],
                           0.09816320034649725,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           9.983545774937238e-05,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (10, 100, 100)

        data = self.pipeline.get_data('header_stack/INDEX')
        index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        assert np.allclose(data, index, rtol=limit, atol=0.)
        assert data.shape == (10, )

        data = self.pipeline.get_data('header_stack/PARANG')
        parang = [1.5, 15.5, 19.5, 23.5, 25.5, 29.5, 31.5, 35.5, 37.5, 39.5]
        assert np.allclose(data, parang, rtol=limit, atol=0.)
        assert data.shape == (10, )

    def test_mean_cube(self):

        with pytest.warns(DeprecationWarning) as warning:
            mean = MeanCubeModule(name_in='mean',
                                  image_in_tag='images',
                                  image_out_tag='mean')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'The MeanCubeModule will be be deprecated in a ' \
                                             'future release. Please use the StackCubesModule ' \
                                             'instead.'

        self.pipeline.add_module(mean)
        self.pipeline.run_module('mean')

        data = self.pipeline.get_data('mean')
        assert np.allclose(data[0, 50, 50],
                           0.09805840100024205,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738069,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 100, 100)

        attribute = self.pipeline.get_attribute('mean', 'INDEX', static=False)
        assert np.allclose(np.mean(attribute), 1.5, rtol=limit, atol=0.)
        assert attribute.shape == (4, )

        attribute = self.pipeline.get_attribute('mean',
                                                'NFRAMES',
                                                static=False)
        assert np.allclose(np.mean(attribute), 1, rtol=limit, atol=0.)
        assert attribute.shape == (4, )

    def test_stack_cube(self):

        module = StackCubesModule(name_in='stackcube',
                                  image_in_tag='images',
                                  image_out_tag='mean',
                                  combine='mean')

        self.pipeline.add_module(module)
        self.pipeline.run_module('stackcube')

        data = self.pipeline.get_data('mean')
        assert np.allclose(data[0, 50, 50],
                           0.09805840100024205,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738069,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (4, 100, 100)

        attribute = self.pipeline.get_attribute('mean', 'INDEX', static=False)
        assert np.allclose(np.mean(attribute), 1.5, rtol=limit, atol=0.)
        assert attribute.shape == (4, )

        attribute = self.pipeline.get_attribute('mean',
                                                'NFRAMES',
                                                static=False)
        assert np.allclose(np.mean(attribute), 1, rtol=limit, atol=0.)
        assert attribute.shape == (4, )

    def test_derotate_and_stack(self):

        derotate = DerotateAndStackModule(name_in='derotate1',
                                          image_in_tag='images',
                                          image_out_tag='derotate1',
                                          derotate=True,
                                          stack='mean',
                                          extra_rot=10.)

        self.pipeline.add_module(derotate)
        self.pipeline.run_module('derotate1')

        data = self.pipeline.get_data('derotate1')
        assert np.allclose(data[0, 50, 50],
                           0.09689679769268554,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010021671152246617,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (1, 100, 100)

        derotate = DerotateAndStackModule(name_in='derotate2',
                                          image_in_tag='images',
                                          image_out_tag='derotate2',
                                          derotate=False,
                                          stack='median',
                                          extra_rot=0.)

        self.pipeline.add_module(derotate)
        self.pipeline.run_module('derotate2')

        data = self.pipeline.get_data('derotate2')
        assert np.allclose(data[0, 50, 50],
                           0.09809001768003645,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010033064394962,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (1, 100, 100)

    def test_combine_tags(self):

        combine = CombineTagsModule(image_in_tags=('images', 'extra'),
                                    check_attr=True,
                                    index_init=False,
                                    name_in='combine1',
                                    image_out_tag='combine1')

        self.pipeline.add_module(combine)

        with pytest.warns(UserWarning) as warning:
            self.pipeline.run_module('combine1')

        assert len(warning) == 1
        assert warning[0].message.args[0] == 'The non-static keyword FILES is already used but ' \
                                             'with different values. It is advisable to only ' \
                                             'combine tags that descend from the same data set.'

        data = self.pipeline.get_data('combine1')
        assert np.allclose(data[0, 50, 50],
                           0.09798413502193704,
                           rtol=limit,
                           atol=0.)
        assert np.allclose(np.mean(data),
                           0.00010029494781738068,
                           rtol=limit,
                           atol=0.)
        assert data.shape == (80, 100, 100)

        data = self.pipeline.get_data('header_combine1/INDEX')
        assert data[40] == 0
        assert data.shape == (80, )

        combine = CombineTagsModule(image_in_tags=('images', 'extra'),
                                    check_attr=False,
                                    index_init=True,
                                    name_in='combine2',
                                    image_out_tag='combine2')

        self.pipeline.add_module(combine)
        self.pipeline.run_module('combine2')

        data = self.pipeline.get_data('combine1')
        extra = self.pipeline.get_data('combine2')
        assert np.allclose(data, extra, rtol=limit, atol=0.)

        data = self.pipeline.get_data('header_combine2/INDEX')
        assert data[40] == 40
        assert data.shape == (80, )