예제 #1
0
    def test_write_csv_data_std(self):
        """
        Test that data can be correctly written (and re-read) from a CSV file
        with the standard deviations also output.
        """

        times = np.linspace(1000000000.0, 1000086340.0, 1440)
        data = np.random.normal(0.0, 1e-25, size=(1440, 2))
        stds = 1e-25 * np.ones_like(times)
        data = np.column_stack((data, stds))

        het = HeterodynedData(data, times=times)

        datafile = "testdata.csv"
        het.write(datafile)

        # read in data
        hetnew = HeterodynedData.read(datafile)

        assert np.array_equal(het.data, hetnew.data)
        assert np.array_equal(het.times, hetnew.times)
        assert np.array_equal(het.stds, hetnew.stds)

        # check things that the read-in data should not contain
        assert hetnew.detector is None
        assert hetnew.par is None

        os.remove(datafile)  # clean up file
예제 #2
0
    def test_write_text_data(self):
        """
        Test that data can be correctly written (and re-read) from a text file.
        """

        times = np.linspace(1000000000.0, 1000086340.0, 1440)
        data = np.random.normal(0.0, 1e-25, size=(1440, 2))

        het = HeterodynedData(data, times=times)

        for suffix in ["txt", "txt.gz"]:
            datafile = "testdata.{}".format(suffix)
            het.write(datafile)

            # read in data
            hetnew = HeterodynedData.read(datafile)

            assert np.array_equal(het.data, hetnew.data)
            assert np.array_equal(het.times, hetnew.times)

            # check things that the read-in data should not contain
            assert hetnew.detector is None
            assert hetnew.par is None

            os.remove(datafile)  # clean up file
예제 #3
0
    def test_write_hdf_data(self):
        """
        Test that data can be correctly written (and re-read) from a HDF5 file.
        """

        times = np.linspace(1000000000.0, 1000086340.0, 1440)
        data = np.random.normal(0.0, 1e-25, size=(1440, 2))
        det = "H1"

        parcontent = """\
PSRJ     J0123+3456
RAJ      01:23:45.6789
DECJ     34:56:54.321
F0       567.89
F1       -1.2e-12
PEPOCH   56789
H0       9.87e-26
COSIOTA  0.3
PSI      1.1
PHI0     2.4
"""

        parfile = "J0123+3456.par"

        # add content to the par file
        with open(parfile, "w") as fp:
            fp.write(parcontent)

        het = HeterodynedData(data, times=times, detector=det, par=parfile)

        for suffix in ["hdf5", "hdf", "h5"]:
            datafile = "testdata.{}".format(suffix)
            het.write(datafile, overwrite=True)

            # read in data
            hetnew = HeterodynedData.read(datafile)

            assert np.array_equal(het.data, hetnew.data)
            assert np.array_equal(het.times, hetnew.times)

            # check that detector and par file are read in correctly
            assert hetnew.detector == det
            for key in het.par.as_dict():
                if isinstance(hetnew.par[key], str):
                    assert hetnew.par[key] == het.par[key]
                else:
                    assert np.allclose(hetnew.par[key], het.par[key])

            # check version information is stored
            assert het.cwinpy_version == hetnew.cwinpy_version
            assert het.cwinpy_version == cwinpy.__version__

            os.remove(datafile)  # clean up file

        os.remove(parfile)
예제 #4
0
    def setup_class(cls):
        """
        Create directory for simulations.
        """

        # set the base directory
        cls.basedir = os.path.join(
            os.path.split(os.path.realpath(__file__))[0], "simulation")
        os.makedirs(cls.basedir, exist_ok=True)

        # create pulsar parameter files for testing
        cls.pardir = os.path.join(
            os.path.split(os.path.realpath(__file__))[0], "test_pardir")
        os.makedirs(cls.pardir, exist_ok=True)

        cls.names = ["J0000+0000", "J0100+0000"]
        cls.ras = [0.0, (1 / 24) * 2 * np.pi]
        cls.decs = [0.0, 0.0]
        cls.dists = [1.0, None]
        cls.pardict = {}
        for name, ra, dec, dist in zip(cls.names, cls.ras, cls.decs,
                                       cls.dists):
            par = PulsarParameters()
            par["PSRJ"] = name
            par["F"] = [100.0]  # set frequency to 100 Hz
            par["RAJ"] = ra
            par["DECJ"] = dec
            if dist is not None:
                par["DIST"] = (dist * u.kpc).to("m").value
            with open(os.path.join(cls.pardir, "{}.par".format(name)),
                      "w") as fp:
                fp.write(str(par))
            cls.pardict[name] = os.path.join(cls.pardir, "{}.par".format(name))

        # create heterodyned data for testing
        cls.hetdir = os.path.join(
            os.path.split(os.path.realpath(__file__))[0], "test_hetdir")
        os.makedirs(cls.hetdir, exist_ok=True)

        cls.hetfiles = {}
        for det, name in zip(["H1", "L1"], ["J0000+0000", "J0100+0000"]):
            het = HeterodynedData(
                times=np.linspace(1000000000.0, 1000086340.0, 1440),
                fakeasd=det,
                par=os.path.join(cls.pardir, "{}.par".format(name)),
            )
            cls.hetfiles[det] = os.path.join(cls.hetdir, "{}.hdf".format(name))
            het.write(cls.hetfiles[det], overwrite=True)
예제 #5
0
# create some fake heterodyned data
detector = "H1"  # the detector to use
asd = 1e-24  # noise amplitude spectral density
times = np.linspace(1000000000.0, 1000086340.0, 1440)  # times
het = HeterodynedData(
    times=times,
    par=parfile,
    injpar=parfile,
    inject=True,
    fakeasd=asd,
    detector=detector,
)

# output the data
hetfile = os.path.join(outdir, "{}_data.txt".format(label))
het.write(hetfile)

# create priors
phi0range = [0.0, np.pi]
psirange = [0.0, np.pi / 2.0]
cosiotarange = [-1.0, 1.0]
h0range = [0.0, 1e-23]

# set prior for lalapps_pulsar_parameter_estimation_nested
priorfile = os.path.join(outdir, "{}_prior.txt".format(label))
priorcontent = """H0 uniform {} {}
PHI0 uniform {} {}
PSI uniform {} {}
COSIOTA uniform {} {}
"""
with open(priorfile, "w") as fp:
예제 #6
0
    def test_merge_data(self):
        """
        Test merging multiple data sets during reading.
        """

        # create three sets of data
        times1 = np.linspace(1000000000.0, 1000086340.0, 1440)
        data1 = np.random.normal(0.0, 1e-25, size=(len(times1), 2))
        stds = 1e-25 * np.ones_like(times1)
        data1 = np.column_stack((data1, stds))

        times2 = np.linspace(999913600.0, 999999940.0, 1440)
        data2 = np.random.normal(0.0, 1e-25, size=(len(times2), 2))
        stds = 1e-25 * np.ones_like(times2)
        data2 = np.column_stack((data2, stds))

        # don't add standard deviations to third data set for now
        times3 = np.linspace(1000186400.0, 1000359140.0, 2880)
        data3 = np.random.normal(0.0, 1e-25, size=(len(times3), 2))

        parcontent1 = """\
PSRJ     J0123+3456
RAJ      01:23:45.6789
DECJ     34:56:54.321
F0       567.89
F1       -1.2e-12
PEPOCH   56789
H0       9.87e-26
COSIOTA  0.3
PSI      1.1
PHI0     2.4
"""

        parfile1 = "J0123+3456.par"

        parcontent2 = """\
PSRJ     J0123+3457
RAJ      01:23:45.6789
DECJ     34:56:54.321
F0       567.89
F1       -1.2e-12
PEPOCH   56789
H0       9.87e-26
COSIOTA  0.3
PSI      1.1
PHI0     2.4
"""

        parfile2 = "J0123+3457.par"

        # add content to the par file
        for parfile, parcontent in zip([parfile1, parfile2],
                                       [parcontent1, parcontent2]):
            with open(parfile, "w") as fp:
                fp.write(parcontent)

        # test for overlapping times
        datafiles = []
        datalist = [data1, data2, data1]
        timeslist = [times1, times2, times1]

        N = len(datalist)

        for i in range(N):
            datafile = "testdata_H1_{}.hdf5".format(i)
            datafiles.append(datafile)

            # write out data
            het = HeterodynedData(datalist[i],
                                  times=timeslist[i],
                                  detector="H1",
                                  par=parfile1)
            het.write(datafile, overwrite=True)

        # read in data
        with pytest.raises(ValueError) as e:
            _ = HeterodynedData.read(datafiles)
        assert "Cannot merge overlapping data" in str(e)

        datalist = [data1, data2, data3]
        timeslist = [times1, times2, times3]

        # test for inconsistent detectors when merging
        for i, det in enumerate(["H1", "H1", "L1"]):
            # write out data
            het = HeterodynedData(datalist[i],
                                  times=timeslist[i],
                                  detector=det,
                                  par=parfile1)
            het.write(datafiles[i], overwrite=True)

        # read in data
        with pytest.raises(ValueError) as e:
            _ = HeterodynedData.read(datafiles)
        assert "Incompatible detectors" in str(e)

        # test for inconsistent pulsars
        for i in range(N):
            # write out data
            het = HeterodynedData(
                datalist[i],
                times=timeslist[i],
                detector="H1",
                par=(parfile1 if i == 0 else parfile2),
            )
            het.write(datafiles[i], overwrite=True)

        # read in data
        with pytest.raises(ValueError) as e:
            _ = HeterodynedData.read(datafiles)
        assert "Incompatible pulsars" in str(e)

        # test for inconsistent frequency scale factors
        for i in range(N):
            # write out data
            het = HeterodynedData(
                datalist[i],
                times=timeslist[i],
                detector="H1",
                par=parfile1,
                freqfactor=(2 if i == 0 else 1),
            )
            het.write(datafiles[i], overwrite=True)

        # read in data
        with pytest.raises(ValueError) as e:
            _ = HeterodynedData.read(datafiles)
        assert "Incompatible frequency factors" in str(e)

        # check for inconsistencies in whether variances were set or not
        for i in range(N):
            # write out data
            het = HeterodynedData(
                datalist[i],
                times=timeslist[i],
                detector="H1",
                par=parfile1,
                freqfactor=2,
            )
            het.write(datafiles[i], overwrite=True)

        # read in data
        with pytest.raises(ValueError) as e:
            _ = HeterodynedData.read(datafiles)
        assert "Incompatible setting of variances" in str(e)

        # make data sets have compatible variances settings
        stds = 1e-25 * np.ones_like(times3)
        data3 = np.column_stack((data3, stds))
        datalist[-1] = data3

        # check for inconsistent injection of a signal
        for i in range(N):
            # write out data
            het = HeterodynedData(
                datalist[i],
                times=timeslist[i],
                detector="H1",
                par=parfile1,
                freqfactor=2,
                inject=(True if i < (N - 1) else False),
            )
            het.write(datafiles[i], overwrite=True)

        # read in data
        with pytest.raises(ValueError) as e:
            _ = HeterodynedData.read(datafiles)
        assert "Incompatible injection times" in str(e)

        # create consistent files for merging and check the output
        hets = []
        for i in range(N):
            # write out data
            het = HeterodynedData(
                datalist[i],
                times=timeslist[i],
                detector="H1",
                par=parfile1,
                freqfactor=2,
                inject=True,
            )

            # add dummy heterodyne_arguments for testing
            het.heterodyne_arguments = {"dummy": "argument"}

            het.write(datafiles[i], overwrite=True)
            hets.append(het)  # store for comparisons

        # read in data
        newhet = HeterodynedData.read(datafiles)

        # test times are correct and sorted
        times = np.concatenate((times2, times1, times3))  # correct time order
        assert len(newhet) == len(times)
        assert np.array_equal(times, newhet.times.value)
        assert newhet.dt.value == np.min(np.diff(times))

        # test data is correct
        assert np.array_equal(
            newhet.data, np.concatenate([hets[i].data for i in [1, 0, 2]]))

        # test injection data
        assert newhet.injtimes.shape == (N, 2)
        assert np.allclose(
            newhet.injection_data,
            np.concatenate([hets[i].injection_data for i in [1, 0, 2]]),
        )

        # test heterodyne arguments
        assert len(newhet.heterodyne_arguments) == N
        assert all([
            hetargs == {
                "dummy": "argument"
            } for hetargs in newhet.heterodyne_arguments
        ])

        # remove par files
        for parfile in [parfile1, parfile2]:
            os.remove(parfile)

        # remove data files
        for datafile in datafiles:
            os.remove(datafile)
예제 #7
0
    def test_write_hdf_data_std(self):
        """
        Test that data can be correctly written (and re-read) from a HDF5 file
        with the standard deviations also output. Also, add an injection!
        """

        times = np.linspace(1000000000.0, 1000086340.0, 1440)
        data = np.random.normal(0.0, 1e-25, size=(1440, 2))
        stds = 1e-25 * np.ones_like(times)
        data = np.column_stack((data, stds))
        det = "H1"

        parcontent = """\
PSRJ     J0123+3456
RAJ      01:23:45.6789
DECJ     34:56:54.321
F0       567.89
F1       -1.2e-12
PEPOCH   56789
H0       9.87e-26
COSIOTA  0.3
PSI      1.1
PHI0     2.4
"""

        parfile = "J0123+3456.par"

        # add content to the par file
        with open(parfile, "w") as fp:
            fp.write(parcontent)

        het = HeterodynedData(data,
                              times=times,
                              detector=det,
                              par=parfile,
                              inject=True)

        for suffix in ["hdf5", "hdf", "h5"]:
            datafile = "testdata.{}".format(suffix)
            het.write(datafile, overwrite=True)

            # read in data
            hetnew = HeterodynedData.read(datafile)

            assert np.array_equal(het.data, hetnew.data)
            assert np.array_equal(het.times, hetnew.times)
            assert np.array_equal(het.stds, hetnew.stds)
            assert hetnew.injection is True
            assert np.array_equal(het.injection_data, hetnew.injection_data)

            # check that detector and par file are read in correctly
            assert hetnew.detector == det
            for key in het.par.as_dict():
                if key in hetnew.par.as_dict():
                    if isinstance(hetnew.par[key], str):
                        assert hetnew.par[key] == het.par[key]
                        assert hetnew.injpar[key] == het.injpar[key]
                    else:
                        assert np.allclose(hetnew.par[key], het.par[key])
                        assert np.allclose(hetnew.injpar[key], het.injpar[key])

            os.remove(datafile)  # clean up file

        os.remove(parfile)