def test_toDat(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) datfile, inffile = myTim.toDat(basename="temp_test") assert os.path.isfile(datfile) assert os.path.isfile(inffile) os.remove(datfile) os.remove(inffile)
def test_readTim(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) outfile = myTim.toFile() mynewTim = TimeSeries.readTim(filename=outfile) assert mynewTim.header.nbits == 32 assert mynewTim.header.source_name == "test" np.testing.assert_allclose(np.mean(mynewTim), 128, atol=0.1) os.remove(outfile)
def test_readDat(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) datfile, inffile = myTim.toDat(basename="temp_test") mynewTim = TimeSeries.readDat(filename=datfile) assert mynewTim.header.nbits == 32 assert mynewTim.header.source_name == "test" np.testing.assert_allclose(np.mean(mynewTim), 128, atol=0.1) os.remove(datfile) os.remove(inffile)
def getChan(self, chan, gulp=512, **kwargs): """Retrieve a single frequency channel from the data. Parameters ---------- chan : int channel to retrieve (0 is the highest frequency channel) gulp : int, optional number of samples in each read, by default 512 Returns ------- :class:`~sigpyproc.TimeSeries.TimeSeries` selected channel as a time series Raises ------ ValueError If chan is out of range (chan < 0 or chan > total channels). """ if chan >= self.header.nchans or chan < 0: raise ValueError("Selected channel out of range.") tim_ar = np.empty(self.header.nsamples, dtype="float32") for nsamps, ii, data in self.readPlan(gulp, **kwargs): lib.getChan(data, tim_ar, chan, self.header.nchans, nsamps, ii * gulp) return TimeSeries( tim_ar, self.header.newHeader({"channel": chan, "refdm": 0.0, "nchans": 1}) )
def getChan(self, chan, gulp=512): """Retrieve a single frequency channel from the data. :param chan: channel to retrieve (0 is the highest frequency channel) :type chan: int :param gulp: number of samples in each read :type gulp: int :return: selected channel as a time series :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries` """ if chan >= self.header.nchans or chan < 0: raise ValueError("Selected channel out of range.") tim_ar = np.empty(self.header.nsamples, dtype="float32") tim_ar_c = as_c(tim_ar) for nsamps, ii, data in self.readPlan(gulp): self.lib.getChan(as_c(data), tim_ar_c, C.c_int(chan), C.c_int(self.header.nchans), C.c_int(nsamps), C.c_int(ii * gulp)) return TimeSeries( tim_ar, self.header.newHeader({ "channel": chan, "refdm": 0.0, "nchans": 1 }))
def readDat(filename, inf=None): """Read a presto format .dat file. :param filename: the name of the file to read :type filename: :func:`str` :params inf: the name of the corresponding .inf file (def=None) :type inf: :func:`str` :return: an array containing the whole dat file contents :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries` .. note:: If inf=None, the function will look for a corresponding file with the same basename which has the .inf file extension. """ basename = os.path.splitext(filename)[0] if inf is None: inf = "%s.inf" % (basename) if not os.path.isfile(inf): raise IOError, "No corresponding inf file found" header = parseInfHeader(inf) f = File(filename, "r", nbits=32) data = np.fromfile(f, dtype="float32") header["basename"] = basename header["inf"] = inf header["filename"] = filename header["nsamples"] = data.size return TimeSeries(data, header)
def dedisperse(self,dm,gulp=10000): """Dedisperse the data to a time series. :param dm: dispersion measure to dedisperse to :type dm: float :param gulp: number of samples in each read :type gulp: int :return: a dedispersed time series :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries` .. note:: If gulp < maximum dispersion delay, gulp is taken to be twice the maximum dispersion delay. """ chan_delays = self.header.getDMdelays(dm) chan_delays_c = as_c(chan_delays) max_delay = int(chan_delays.max()) gulp = max(2*max_delay,gulp) tim_len = self.header.nsamples-max_delay tim_ar = np.zeros(tim_len,dtype="float32") tim_ar_c = as_c(tim_ar) for nsamps,ii,data in self.readPlan(gulp,skipback=max_delay): self.lib.dedisperse(as_c(data), tim_ar_c, chan_delays_c, C.c_int(max_delay), C.c_int(self.header.nchans), C.c_int(nsamps), C.c_int(ii*(gulp-max_delay))) return TimeSeries(tim_ar,self.header.newHeader({"nchans":1,"refdm":dm}))
def collapse(self, gulp=512, start=0, nsamps=None, **kwargs): """Sum across all frequencies for each time sample. Parameters ---------- gulp : int, optional number of samples in each read, by default 512 start : int, optional start sample, by default 0 nsamps : int, optional number of samples to read, by default all Returns ------- :class:`~sigpyproc.TimeSeries.TimeSeries` A zero-DM time series """ if nsamps is None: size = self.header.nsamples - start else: size = nsamps timar = np.zeros(size, dtype="float32") for nsamp, ii, data in self.readPlan( gulp, start=start, nsamps=nsamps, **kwargs, ): lib.getTim(data, timar, self.header.nchans, nsamp, ii * gulp) return TimeSeries(timar, self.header.newHeader({"nchans": 1, "refdm": 0.0}))
def iFFT(self): """Perform 1-D complex to real inverse FFT using FFTW3. :return: a time series :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries` """ tim_ar = np.empty(self.size - 2, dtype="float32") lib.ifft(as_c(self), as_c(tim_ar), C.c_int(self.size - 2)) return TimeSeries(tim_ar, self.header.newHeader())
def bandpass(self, gulp=512): """Sum across each time sample for all frequencies. :param gulp: number of samples in each read :type gulp: int :return: the bandpass of the data :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries` """ bpass_ar = np.zeros(self.header.nchans, dtype="float32") bpass_ar_c = as_c(bpass_ar) for nsamps, ii, data in self.readPlan(gulp): self.lib.getBpass(as_c(data), bpass_ar_c, C.c_int(self.header.nchans), C.c_int(nsamps)) return TimeSeries(bpass_ar, self.header.newHeader({"nchans": 1}))
def readTim(filename): """Read a sigproc format time series from file. :param filename: the name of the file to read :type filename: :func:`str` :return: an array containing the whole file contents :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries` """ header = parseSigprocHeader(filename) nbits = header["nbits"] hdrlen = header["hdrlen"] f = File(filename, "r", nbits=nbits) f.seek(hdrlen) data = np.fromfile(f, dtype=header["dtype"]).astype("float32") return TimeSeries(data, header)
def bandpass(self, gulp=512, **kwargs): """Average across each time sample for all frequencies. Parameters ---------- gulp : int, optional number of samples in each read, by default 512 Returns ------- :class:`~sigpyproc.TimeSeries.TimeSeries` the bandpass of the data """ bpass_ar = np.zeros(self.header.nchans, dtype="float64") num_samples = 0 for nsamps, _ii, data in self.readPlan(gulp, **kwargs): lib.getBpass(data, bpass_ar, self.header.nchans, nsamps) num_samples += nsamps bpass_ar /= num_samples return TimeSeries(bpass_ar, self.header.newHeader({"nchans": 1}))
def collapse(self,gulp=512,start=0,nsamps=None): """Sum across all frequencies for each time sample. :param gulp: number of samples in each read :type gulp: int :return: A zero-DM time series :rtype: :class:`~sigpyproc.TimeSeries.TimeSeries` """ if nsamps is None: size = self.header.nsamples-start else: size = nsamps timar = np.zeros(size,dtype="float32") timar_c = as_c(timar) for nsamps,ii,data in self.readPlan(gulp,start=start,nsamps=nsamps): self.lib.getTim(as_c(data), timar_c, C.c_int(self.header.nchans), C.c_int(nsamps), C.c_int(ii*gulp)) return TimeSeries(timar,self.header.newHeader({"nchans":1,"refdm":0.0}))
def dedisperse(self, dm, gulp=10000, **kwargs): """Dedisperse the data to a time series. Parameters ---------- dm : float dispersion measure to dedisperse to gulp : int, optional number of samples in each read, by default 10000 Returns ------- :class:`~sigpyproc.TimeSeries.TimeSeries` a dedispersed time series Notes ----- If gulp < maximum dispersion delay, gulp is taken to be twice the maximum dispersion delay. """ chan_delays = self.header.getDMdelays(dm) max_delay = int(chan_delays.max()) gulp = max(2 * max_delay, gulp) tim_len = self.header.nsamples - max_delay tim_ar = np.zeros(tim_len, dtype="float32") for nsamps, ii, data in self.readPlan(gulp, skipback=max_delay, **kwargs): lib.dedisperse( data, tim_ar, chan_delays, max_delay, self.header.nchans, nsamps, ii * (gulp - max_delay), ) return TimeSeries(tim_ar, self.header.newHeader({"nchans": 1, "refdm": dm}))
def test_runningMedian(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) median_filter = myTim.runningMedian(window=101) np.testing.assert_allclose(np.mean(median_filter), 128, atol=0.1)
def test_toFile(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) outfile = myTim.toFile() assert os.path.isfile(outfile) os.remove(outfile)
def test_timeseries(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) assert myTim.header.nbits == 32 assert myTim.header.source_name == "test" np.testing.assert_allclose(np.mean(myTim), 128, atol=0.1)
def test_downsample(self, tim_data, tim_header): myTim = TimeSeries(tim_data, Header(tim_header)) downsampled = myTim.downsample(factor=16) np.testing.assert_allclose(np.mean(downsampled), 128, atol=0.1)