Beispiel #1
0
 def _next(self):
     uchannels = self._unique_channel_names(self.channels)
     new = TimeSeriesDict()
     span = 0
     epoch = 0
     att = 0
     self.logger.debug('Waiting for next NDS2 packet...')
     while span < self.interval:
         try:
             buffers = next(self.iterator)
         except RuntimeError as e:
             self.logger.error('RuntimeError caught: %s' % str(e))
             if att < self.attempts:
                 att += 1
                 wait_time = att / 3 + 1
                 self.logger.warning(
                     'Attempting to reconnect to the nds server... %d/%d' %
                     (att, self.attempts))
                 self.logger.warning('Next attempt in minimum %d seconds' %
                                     wait_time)
                 self.restart()
                 sleep(wait_time - tconvert('now') % wait_time)
                 continue
             else:
                 self.logger.critical(
                     'Maximum number of attempts reached, exiting')
                 break
         att = 0
         for buff, c in zip(buffers, uchannels):
             ts = TimeSeries.from_nds2_buffer(buff)
             try:
                 new.append({c: ts}, gap=self.gap, pad=self.pad)
             except ValueError as e:
                 if 'discontiguous' in str(e):
                     e.message = (
                         'NDS connection dropped data between %d and '
                         '%d, restarting building the buffer from %d ') \
                         % (epoch, ts.span[0], ts.span[0])
                     self.logger.warning(str(e))
                     new = TimeSeriesDict()
                     new[c] = ts.copy()
                 elif ('starts before' in str(e)) or \
                         ('overlapping' in str(e)):
                     e.message = (
                         'Overlap between old data and new data in the '
                         'nds buffer, only the new data will be kept.')
                     self.logger.warning(str(e))
                     new = TimeSeriesDict()
                     new[c] = ts.copy()
                 else:
                     raise
             span = abs(new[c].span)
             epoch = new[c].span[-1]
             self.logger.debug('%ds data for %s received' %
                               (abs(ts.span), str(c)))
     out = type(new)()
     for chan in self.channels:
         out[chan] = new[self._channel_basename(chan)].copy()
     return out
Beispiel #2
0
    def channeling_read(out_channels: List[str], **kwargs) -> TimeSeriesDict:
        out = TimeSeriesDict()

        for channel in out_channels:
            for prefix in search_dirs:
                for in_channel in in_channels:
                    try:
                        # lock the target file
                        h5file, _ = path2h5file(get_path(
                            f'{in_channel} {generation_start}',
                            'hdf5',
                            prefix=prefix),
                                                mode='r')
                        # read off the dataset
                        out[channel] = TimeSeries.read(h5file, channel,
                                                       **kwargs)
                    except (FileNotFoundError, KeyError, OSError):
                        # file not found / hdf5 can't open file (OSError), channel not in file (KeyError)
                        continue
                    break
                else:
                    continue
                break
            else:
                # tried all search dirs but didn't find it. Attempt to download.
                raise FileNotFoundError(f'CANNOT FIND {channel}!!')
                # out[channel] = TimeSeries.get(channel, **kwargs) # slow.
        return out
Beispiel #3
0
 def compute(self, raw: TimeSeries) -> TimeSeriesDict:
     out = TimeSeriesDict()
     times = unique([60 * (t.value // 60) for t in raw.times])
     raw.name = raw.name + '.mean'
     out[raw.name] = TimeSeries(
         [raw.crop(t - 60, t).mean().value for t in times[1:]], times=times)
     out[raw.name].__metadata_finalize__(raw)
     return out
Beispiel #4
0
def get_signals(r, rdot, phi, dt, mass):
    """Create a `~gwpy.timeseries.TimeSeriesDict` based on orbital trajectory
    """
    rconv = G * mass / c**2
    tconv = G * mass / c**3
    dt *= tconv
    r = TimeSeries(rconv * r,
                   dt=dt.value,
                   name="radial coordinate along geodesic")
    rdot = TimeSeries(c * rdot,
                      dt=dt.value,
                      name="radial component of 4-velocity")
    phi = TimeSeries(phi,
                     dt=dt.value,
                     name="azimuthal coordinate along geodesic")
    return TimeSeriesDict({'r': r, 'rdot': rdot, 'phi': phi})
Beispiel #5
0
def test_get_data_dict_from_cache(tsdget, remove, find_data):
    # set return values
    tsdget.return_value = TimeSeriesDict({'X1:TEST-STRAIN': HOFT.crop(16, 48)})
    remove.return_value = ['X1:TEST-STRAIN']
    find_data.return_value = ['test.gwf']
    # retrieve test frame
    start = 16
    end = start + 32
    channels = ['X1:TEST-STRAIN']
    data = datafind.get_data(channels, start, end, source=True)

    # test data products
    assert isinstance(data, TimeSeriesDict)
    assert data[channels[0]].duration.value == 32
    assert data[channels[0]].span == Segment(start, end)
    nptest.assert_array_equal(data[channels[0]].value,
                              HOFT.crop(start, end).value)
Beispiel #6
0
    def update_data(self, new):
        """Update the `SpectrogramMonitor` data

        This method only applies a ratio, if configured
        """
        # check that the stored epoch is bigger then the first buffered data
        if new[self.channels[0]][0].span[0] > self.epoch:
            s = ('The available data starts at gps %d '
                 'which. is after the end of the last spectrogram (gps %d)'
                 ': a segment is missing and will be skipped!')
            self.logger.warning(s, new[self.channels[0]][0].span[0],
                                self.epoch)
            self.epoch = new[self.channels[0]][0].span[0]
        # be sure that the first cycle is syncronized with the buffer
        if not self.spectrograms.data:
            self.epoch = new[self.channels[0]][0].span[0]
        self.olepoch = self.epoch
        while int(new[self.channels[0]][0].span[-1]) >=\
                int(self.epoch + self.stride):
            # data buffer will return dict of 1-item lists, so reform to tsd
            _new = TimeSeriesDict((key, val[0].crop(self.epoch, self.epoch +
                                                    self.stride))
                                  for key, val in new.iteritems())
            self.logger.debug('Computing spectrogram from epoch %d',
                              self.epoch)
            self.spectrograms.append(
                self.spectrograms.from_timeseriesdict(_new))
            self.epoch += self.stride
        self.spectrograms.crop(self.epoch - self.duration)
        self.data = type(self.spectrograms.data)()
        for channel in self.channels:
            self.data[channel] = type(self.spectrograms.data[channel])(
                *self.spectrograms.data[channel])
            if hasattr(channel, 'ratio') and channel.ratio is not None:
                for i in range(len(self.data[channel])):
                    self.data[channel][i] = (
                        self.spectrograms.data[channel][i].ratio(
                            channel.ratio))
        self.epoch = self.data[self.channels[0]][-1].span[-1]
        return self.data
Beispiel #7
0
 def compute(self, raw: TimeSeries) -> TimeSeriesDict:
     out = TimeSeriesDict()
     if self.channel.endswith(',m-trend'):
         raw.name = raw.name.replace(',m-trend', '')
     out[raw.name] = raw
     return out
Beispiel #8
0
    def binned_event_rates(self,
                           stride,
                           column,
                           bins,
                           operator='>=',
                           start=None,
                           end=None,
                           timecolumn=None):
        """Calculate an event rate `~gwpy.timeseries.TimeSeriesDict` over
        a number of bins.

        Parameters
        ----------
        stride : `float`
            size (seconds) of each time bin

        column : `str`
            name of column by which to bin.

        bins : `list`
            a list of `tuples <tuple>` marking containing bins, or a list of
            `floats <float>` defining bin edges against which an math operation
            is performed for each event.

        operator : `str`, `callable`
            one of:

            - ``'<'``, ``'<='``, ``'>'``, ``'>='``, ``'=='``, ``'!='``,
              for a standard mathematical operation,
            - ``'in'`` to use the list of bins as containing bin edges, or
            - a callable function that takes compares an event value
              against the bin value and returns a boolean.

            .. note::

               If ``bins`` is given as a list of tuples, this argument
               is ignored.

        start : `float`, `~gwpy.time.LIGOTimeGPS`, optional
            GPS start epoch of rate `~gwpy.timeseries.TimeSeries`.

        end : `float`, `~gwpy.time.LIGOTimeGPS`, optional
            GPS end time of rate `~gwpy.timeseries.TimeSeries`.
            This value will be rounded up to the nearest sample if needed.

        timecolumn : `str`, optional, default: ``time``
            name of time-column to use when binning events

        Returns
        -------
        rates : ~gwpy.timeseries.TimeSeriesDict`
            a dict of (bin, `~gwpy.timeseries.TimeSeries`) pairs describing a
            rate of events per second (Hz) for each of the bins.
        """
        # NOTE: decorator sets timecolumn, start, end to non-None values

        from gwpy.timeseries import TimeSeriesDict

        # generate column bins
        if not bins:
            bins = [(-numpy.inf, numpy.inf)]
        if operator == 'in' and not isinstance(bins[0], tuple):
            bins = [(bin_, bins[i + 1]) for i, bin_ in enumerate(bins[:-1])]
        elif isinstance(operator, str):
            op_func = parse_operator(operator)
        else:
            op_func = operator

        coldata = self[column]

        # generate one TimeSeries per bin
        out = TimeSeriesDict()
        for bin_ in bins:
            if isinstance(bin_, tuple):
                keep = (coldata >= bin_[0]) & (coldata < bin_[1])
            else:
                keep = op_func(coldata, bin_)
            out[bin_] = self[keep].event_rate(stride,
                                              start=start,
                                              end=end,
                                              timecolumn=timecolumn)
            out[bin_].name = ' '.join((column, str(operator), str(bin_)))

        return out
Beispiel #9
0
@mock.patch('gwpy.timeseries.TimeSeries.get', return_value=HOFT)
def test_get_data_from_NDS(tsget):
    # retrieve data
    start = 0
    end = 64
    channel = 'X1:TEST-STRAIN'
    data = datafind.get_data(channel, start, end)

    # test data products
    assert isinstance(data, TimeSeries)
    nptest.assert_array_equal(data.value, HOFT.value)


@mock.patch('gwpy.timeseries.TimeSeriesDict.get',
            return_value=TimeSeriesDict({'X1:TEST-STRAIN': HOFT}))
def test_get_data_dict_from_NDS(tsdget):
    # retrieve data
    start = 33
    end = 64
    channels = ['X1:TEST-STRAIN']
    data = datafind.get_data(channels, start, end)

    # test data products
    assert isinstance(data, TimeSeriesDict)
    nptest.assert_array_equal(data['X1:TEST-STRAIN'].value, HOFT.value)


@mock.patch('gwdatafind.find_urls')
@mock.patch('gwpy.timeseries.TimeSeries.read')
def test_get_data_from_cache(tsget, find_data):
Beispiel #10
0
try:
    straindict_temp = load_gw(t0, detectorlist)
    straindict = deepcopy(straindict_temp)
except:
    st.text('Data load failed.  Try a different time and detector pair.')
    st.text('Problems can be reported to [email protected]')
    raise st.ScriptRunner.StopException

strain_load_state.text('Loading data...done!')

#-- Make a time series plot

cropstart = t0 - dt
cropend = t0 + dt

cleandict = TimeSeriesDict()
paramdict = {}
st.subheader('Whitened and Band-passed Data')
for ifo, strain in straindict.items():

    # -- Try whitened and band-passed plot
    # -- Whiten and bandpass data
    if whiten:
        white_data = strain.whiten()
        bp_data = white_data.bandpass(freqrange[0], freqrange[1])
    else:
        bp_data = strain.bandpass(freqrange[0], freqrange[1])

    # fig3 = bp_cropped.plot()
    # st.pyplot(fig3, clear_figure=True)
Beispiel #11
0
def representative_spectra(channels,
                           start,
                           stop,
                           rate,
                           label='kmeans-labels',
                           filename=DEFAULT_FILENAME,
                           prefix='.',
                           downloader=TimeSeriesDict.get,
                           cluster_numbers=None,
                           groups=None,
                           **kwargs):
    """
    Make representative spectra for each cluster based on the median psd for minutes in that cluster.
    Downloads only the raw minutes in the cluster to save.
    """
    if groups is None:
        groups = channels

    # read the labels from the save file.
    labels = TimeSeries.read(filename,
                             label,
                             start=to_gps(start),
                             end=to_gps(stop))
    logger.info(f'Read labels {start} to {stop} from {filename}')

    if cluster_numbers is None:
        clusters = list(range(max(labels.value) + 1))

        cluster_counts = list(
            len(labels.value[labels.value == c]) for c in clusters)
        largest_cluster = cluster_counts.index(max(cluster_counts))
        clusters.remove(largest_cluster)

        logger.info(
            f'Largest cluster found to be Nº{largest_cluster} ({100 * max(cluster_counts) // len(labels.value)}%). Doing {clusters}.'
        )
        cluster_counts.remove(max(cluster_counts))
    else:
        clusters = cluster_numbers
        cluster_counts = list(
            len(labels.value[labels.value == c]) for c in clusters)

    t, v, d = labels.times, labels.value, diff(labels.value)

    pairs = list(
        zip([t[0]] + list(t[:-1][d != 0]),
            list(t[1:][d != 0]) + [t[-1]]))
    values = list(v[:-1][d != 0]) + [v[-1]]
    assert len(pairs) == len(values)  # need to include start-| and |-end
    # l|r l|r l|r l|r
    # l,r l,r l,r l,r
    # l r,l r,l r,l r # zip(start + l[1:], r[:-1] + stop)

    print(pairs)
    for pair in pairs:
        print(int(pair[1].value) - int(pair[0].value))
    print(values)

    # use h5py to make a mutable object pointing to a file on disk.
    save_file, filename = path2h5file(
        get_path(f'spectra-cache {start}', 'hdf5', prefix=prefix))
    logger.debug(f'Initiated hdf5 stream to {filename}')

    logger.info(f'Patching {filename}...')
    for i, (dl_start, end) in enumerate(pairs):
        if values[i] in clusters:
            if not data_exists(channels, to_gps(end).seconds, save_file):
                logger.debug(
                    f'Downloading Nº{values[i]} from {dl_start} to {end}...')
                try:
                    dl = downloader(channels,
                                    start=to_gps(dl_start) - LIGOTimeGPS(60),
                                    end=to_gps(end) + LIGOTimeGPS(seconds=1))
                    out = TimeSeriesDict()
                    for n in dl:
                        out[n] = dl[n].resample(**better_aa_opts(dl[n], rate))
                    write_to_disk(out, to_gps(dl_start).seconds, save_file)
                except RuntimeError:  # Cannot find all relevant data on any known server
                    logger.warning(
                        f"SKIPPING Nº{values[i]} from {dl_start} to {end} !!")

    logger.info('Reading data...')
    data = TimeSeriesDict.read(save_file, channels)

    logger.info('Starting PSD generation...')

    f = data[channels[0]].crop(
        start=to_gps(data[channels[0]].times[-1]) - LIGOTimeGPS(60),
        end=to_gps(data[channels[0]].times[-1])).psd().frequencies

    d = (to_gps(labels.times[-1]).seconds - to_gps(labels.times[1]).seconds)
    for i, cluster in enumerate(clusters):
        try:
            psds = {
                channel: FrequencySeries.read(filename, f'{cluster}-{channel}')
                for channel in channels
            }
            logger.info(f'Loaded Nº{cluster}.')

        except KeyError:

            logger.info(
                f'Doing Nº{cluster} ({100 * cluster_counts[i] / len(labels.value):.2f}% of data)...'
            )
            with Progress(f'psd Nº{cluster} ({i + 1}/{len(clusters)})',
                          len(channels) * d) as progress:
                psds = {
                    channel: FrequencySeries(median(stack([
                        progress(data[channel].crop,
                                 pc * d + (to_gps(time).seconds -
                                           to_gps(labels.times[1]).seconds),
                                 start=to_gps(time) - LIGOTimeGPS(60),
                                 end=to_gps(time)).psd().value
                        for c, time in zip(labels.value, labels.times)
                        if c == cluster
                    ]),
                                                    axis=0),
                                             frequencies=f,
                                             name=f'{cluster}-{channel}')
                    for pc, channel in enumerate(channels)
                }
            for name in psds.keys():
                psds[name].write(filename, **writing_opts)

        # plotting is slow, so show a nice progress bar.
        logger.debug('Initiating plotting routine...')
        with Progress('plotting', len(groups)) as progress:

            for p, (group, lbls, title) in enumerate(groups):
                # plot the group in one figure.
                plt = Plot(*(psds[channel] for channel in group),
                           separate=False,
                           sharex=True,
                           zorder=1,
                           **kwargs)
                # plt.gca().set_xlim((30,60))
                # modify the figure as a whole.
                # plt.add_segments_bar(dq, label='')
                plt.gca().set_xscale('log')
                plt.gca().set_yscale('log')
                plt.suptitle(title)
                plt.legend(lbls)

                # save to png.
                progress(
                    plt.save, p,
                    get_path(f'{cluster}-{title}',
                             'png',
                             prefix=f'{prefix}/{cluster}'))
Beispiel #12
0
# global test objects

IND = 8
OUTLIER_IN = numpy.random.normal(loc=0, scale=1, size=1024)
OUTLIER_IN[IND] = 100
OUTLIER_TS = TimeSeries(OUTLIER_IN, sample_rate=1024, unit='Mpc',
                        name='X1:TEST_RANGE')

TARGET = numpy.array([-1, 0, 1])

SERIES = TimeSeries(TARGET, sample_rate=1, epoch=-1)
DATA = numpy.array([SERIES.value]).T

TSDICT = TimeSeriesDict({
    'full': SERIES,
    'flat': TimeSeries(numpy.ones(3), sample_rate=1, epoch=0),
    'nan': TimeSeries(numpy.full(3, numpy.nan), sample_rate=1, epoch=0),
})


# -- unit tests ---------------------------------------------------------------

def test_find_outliers():
    # find expected outliers
    outliers = core.find_outliers(OUTLIER_TS)
    assert isinstance(outliers, numpy.ndarray)
    nptest.assert_array_equal(outliers, numpy.array([IND]))


def test_remove_outliers():
    # strip off outliers
Beispiel #13
0
def binned_event_rates(self,
                       stride,
                       column,
                       bins,
                       operator='>=',
                       start=None,
                       end=None,
                       timecolumn='time'):
    """Calculate an event rate `~gwpy.timeseries.TimeSeriesDict` over
    a number of bins.

    Parameters
    ----------
    stride : `float`
        size (seconds) of each time bin
    column : `str`
        name of column by which to bin.
    bins : `list`
        a list of `tuples <tuple>` marking containing bins, or a list of
        `floats <float>` defining bin edges against which an math operation
        is performed for each event.
    operator : `str`, `callable`
        one of:

        - ``'<'``, ``'<='``, ``'>'``, ``'>='``, ``'=='``, ``'!='``,
          for a standard mathematical operation,
        - ``'in'`` to use the list of bins as containing bin edges, or
        - a callable function that takes compares an event value
          against the bin value and returns a boolean.

        .. note::

           If ``bins`` is given as a list of tuples, this argument
           is ignored.

    start : `float`, :class:`~gwpy.time.LIGOTimeGPS`, optional
        GPS start epoch of rate `~gwpy.timeseries.TimeSeries`.
    end : `float`, `~gwpy.time.LIGOTimeGPS`, optional
        GPS end time of rate `~gwpy.timeseries.TimeSeries`.
        This value will be rounded up to the nearest sample if needed.
    timecolumn : `str`, optional, default: ``time``
        name of time-column to use when binning events

    Returns
    -------
    rates : :class:`~gwpy.timeseries.TimeSeriesDict`
        a dict of (bin, `~gwpy.timeseries.TimeSeries`) pairs describing a
        rate of events per second (Hz) for each of the bins.
    """
    from gwpy.timeseries import (TimeSeries, TimeSeriesDict)
    from gwpy.plotter.table import get_column_string
    # get time data
    times = get_table_column(self, timecolumn)

    # get channel
    try:
        channel = self[0].channel
    except (IndexError, AttributeError):
        channel = None

    # generate time bins
    if not start:
        start = times.min()
    if not end:
        end = times.max()
    nsamp = int(ceil((end - start) / stride))
    timebins = numpy.arange(nsamp + 1) * stride + start
    # generate column bins
    if not bins:
        bins = [(-numpy.inf, numpy.inf)]
    if operator == 'in' and not isinstance(bins[0], tuple):
        bins2 = []
        for i, bin_ in enumerate(bins[:-1]):
            bins2.append((bin_, bins[i + 1]))
        bins = bins2
    elif isinstance(operator, (unicode, str)):
        op = OPERATORS[operator]
    else:
        op = operator
    coldata = get_table_column(self, column)
    colstr = get_column_string(column)
    # generate one TimeSeries per bin
    out = TimeSeriesDict()
    for bin_ in bins:
        if isinstance(bin_, tuple):
            bintimes = times[(coldata >= bin_[0]) & (coldata < bin_[1])]
        else:
            bintimes = times[op(coldata, bin_)]
        out[bin_] = TimeSeries(numpy.histogram(bintimes, bins=timebins)[0] /
                               float(stride),
                               epoch=start,
                               sample_rate=1 / float(stride),
                               unit='Hz',
                               name='%s $%s$ %s' % (colstr, operator, bin_),
                               channel=channel)
    return out
Beispiel #14
0
TIMES = numpy.arange(0, DURATION, 1 / SAMPLE)
PHASE = 42 * numpy.sin(2 * numpy.pi * FREQ * TIMES) / (2 * numpy.pi * FREQ)
SCATTER = TimeSeries(
    (numpy.sin(numpy.pi * TIMES / DURATION) * numpy.cos(2 * numpy.pi * PHASE)),
    sample_rate=SAMPLE,
)

HOFT = TimeSeries(
    numpy.random.normal(loc=1, scale=1.5, size=SCATTER.size),
    sample_rate=SAMPLE,
).inject(SCATTER.highpass(10))

AUX = TimeSeriesDict({
    ':'.join([IFO, chan]): TimeSeries(
        numpy.random.normal(loc=1, scale=1e-3, size=SCATTER.size),
        sample_rate=SAMPLE,
        name=':'.join([IFO, chan]),
    ).crop(4, 64)
    for chan in simple.MOTION_CHANNELS[1::]
})

PHASE = PHASE[4 * SAMPLE:-4 * SAMPLE]
AUX['{}:SUS-ITMX_R0_DAMP_L_IN1_DQ'.format(IFO)] += 1.064 * PHASE / 2

# -- cli tests ----------------------------------------------------------------


@mock.patch(
    'gwdetchar.scattering.simple._discover_data',
    return_value=(HOFT, AUX),
)
def test_main(data, caplog, tmpdir):
Beispiel #15
0
def one_injection(m1, m2, z, distance, T0, Tobs):
    print(f'Injecting m1:{m1}, m2:{m2}, z:{z}')
    time = T0 + Tobs * 365.25 * 86400 * random.random()
    ra = 2 * np.pi * random.random()
    dec = np.arcsin(-1 + 2 * random.random())
    psi = random.random() * np.pi
    phase = random.random() * 2 * np.pi
    theta_jn = np.arccos(-1 + 2 * random.random())
    injection_parameters = dict(mass_1=m1 * (1 + z),
                                mass_2=m2 * (1 + z),
                                luminosity_distance=distance,
                                theta_jn=theta_jn,
                                psi=psi,
                                phase=phase,
                                geocent_time=time,
                                ra=ra,
                                dec=dec,
                                chi_1=0,
                                chi_2=0)
    duration = 32
    sampling_frequency = 2048
    start_time = time - duration
    print(start_time)
    waveform_arguments = dict(waveform_approximant='IMRPhenomPv2',
                              reference_frequency=50,
                              minimum_frequency=40)

    # Create the waveform_generator. This is something used for injecting a signal and inference.
    waveform_generator = bilby.gw.WaveformGenerator(
        duration=duration,
        sampling_frequency=sampling_frequency,
        frequency_domain_source_model=bilby.gw.source.lal_binary_black_hole,
        waveform_arguments=waveform_arguments)

    # Set up two other detectors at Hanford and Livingston. These will use the design sensitivity PSD by default
    interferometers = bilby.gw.detector.InterferometerList(['H1', 'L1'])

    # Inject a signal into the network of detectors
    interferometers.set_strain_data_from_power_spectral_densities(
        sampling_frequency=sampling_frequency,
        duration=duration,
        start_time=start_time)
    interferometers.inject_signal(parameters=injection_parameters,
                                  waveform_generator=waveform_generator)

    for interferometer in interferometers:
        signal = interferometer.get_detector_response(
            waveform_generator.frequency_domain_strain(), injection_parameters)
        #interferometer.plot_data(signal=signal, outdir=path, label='DCO)

#Use gwpy to generate frame file for the network
    H1 = TimeSeries(interferometers[0].time_domain_strain,
                    sample_rate=sampling_frequency,
                    unit='strain',
                    channel='H1',
                    name='H1')
    L1 = TimeSeries(interferometers[1].time_domain_strain,
                    sample_rate=sampling_frequency,
                    unit='strain',
                    channel='L1',
                    name='L1')

    ifos = TimeSeriesDict()
    ifos.update(H1=H1, L1=L1)
    ifos.write('frame.gwf')

    return start_time
Beispiel #16
0
    def compute(self, raw: TimeSeries) -> TimeSeriesDict:
        debug = f'compute_blrms ({self.channel}) : '

        # resample to specified frequency.
        raw = raw.resample(**better_aa_opts(raw, self.fs))

        # compute spectogram. Set up kwargs for creation of output TimeSeries.
        F, T, Sh = spectrogram(raw.value,
                               nperseg=self.fs * self.tn,
                               noverlap=int(self.fs * self.to),
                               fs=self.fs)
        ts_kwargs = dict(t0=raw.t0, dt=T[1] - T[0], unit=raw.unit)
        logger.debug(debug + 'Computed scipy.spectrogram.')

        # identify lines by comparing the PSD to a calculated background.
        Sh_m = median(Sh, axis=1)  # get median PSD for each time.
        Nf = int(self.df /
                 F[1])  # convert the median window in number of bins.
        Sb_m = zeros_like(Sh_m)  # start with empty background vector.
        # compute a windowed median of the PSD.
        for i, f in enumerate(F):
            # select the bins in the current window.
            idx = arange(max(i - Nf, 0), min(i + Nf, F.shape[0]))
            # compute estimate of background (without lines) by computing the median in the current window.
            Sb_m[i] = median(Sh_m[idx])
        else:
            logger.debug(debug + 'Estimated PSD of background.')
        # find all lines, i.e. all bins where the PSD is larger than thr times the background.
        line_idx = where(logical_and(F > 10, Sh_m / Sb_m > self.thr))[0]
        logger.debug(debug + 'Located the line frequencies.')

        # Compute BLRMS for all bands
        out = TimeSeriesDict()
        for band_start, band_stop in self.bands:
            channel_prefix = f'{self.channel}_BLRMS_{band_start}_{band_stop}'
            # select frequency bins.
            idx = arange(int(band_start / F[1]), int(band_stop / F[1]))
            # full BLRMS, using all bins.
            out[channel_prefix] = TimeSeries(sum(Sh[idx, :], axis=0),
                                             **ts_kwargs)
            # remove the index of all lines.
            idx = setdiff1d(idx, line_idx)
            # compute BLRMS excluding lines.
            out[f'{channel_prefix}_nolines'] = TimeSeries(
                sum(Sh[idx, :], axis=0), **ts_kwargs)

            # Time-domain median smoothing and glitch removal
            for prefix in channel_prefix, f'{channel_prefix}_nolines':
                blrms = out[prefix].value
                # convert the time-domain median window size from seconds to BLRMS samples
                NT = int(self.dt / T[1])
                # empty vectors for running median and running 'median-stdev'
                blrms_m = zeros_like(blrms)
                blrms_rms = zeros_like(blrms)
                # loop over all time samples
                for i, x in enumerate(blrms):
                    # select samples in current window
                    idx = arange(max(i - NT, 0), min(i + NT, T.shape[0]))
                    # median of the BLRMS in the current window
                    blrms_m[i] = median(blrms[idx])
                    # median-based equivalent of the variance
                    blrms_rms[i] = median((blrms[idx] - blrms_m[i])**2)
                # identify all glitch times as samples when the BLRMS deviated from median more than 3 times the median-stdev
                glitch_idx = where((blrms - blrms_m) > 3 * sqrt(blrms_rms))[0]
                # remove the glitchy times
                blrms_noglitch = blrms.copy()  # first set glitchy times to NaN
                blrms_noglitch[glitch_idx] = NaN
                idx = isnan(
                    blrms_noglitch)  # then find the samples that are glitchy
                # linear interpolation using values around glitches
                blrms_noglitch[idx] = interp(T[idx], T[~idx], blrms[~idx])
                # save results to dictionary
                out[f'{prefix}_smooth'] = TimeSeries(blrms_m, **ts_kwargs)
                out[f'{prefix}_noglitch'] = TimeSeries(blrms_noglitch,
                                                       **ts_kwargs)

        # F_lines = F[line_idx]
        # lines = {'F_lines': F_lines, 'F': F, 'Smedian': Sh_m, 'Sbg': Sb_m, 'line_idx': line_idx}

        # fix channel names.
        for i in out:
            out[i].name = i

        return out
Beispiel #17
0
    numpy.sin(2 * numpy.pi * numpy.arange(END, END + 2) / 7),
    sample_rate=1,
    epoch=END,
)

START_DATA = TimeSeriesDict({
    "H1:AUX-CHANNEL_1.mean":
    START_SIGNAL,
    "H1:AUX-CHANNEL_2.mean":
    TimeSeries(
        numpy.ones(PREVIEW + 1),
        sample_rate=1,
        epoch=START - PREVIEW,
    ),
    "H1:AUX-CHANNEL_3.mean":
    TimeSeries(
        numpy.ones(PREVIEW + 1),
        sample_rate=1,
        epoch=START - PREVIEW,
    ),
    "H1:AUX-CHANNEL_4.mean":
    TimeSeries(
        numpy.ones(PREVIEW + 1),
        sample_rate=1,
        epoch=START - PREVIEW,
    ),
})

END_DATA = TimeSeriesDict({
    "H1:AUX-CHANNEL_1.mean":
    END_SIGNAL,
    "H1:AUX-CHANNEL_2.mean":
Beispiel #18
0
DATA = TimeSeries([1, 2, 3, 4, 5, 5, 5, 4, 5, 4], dx=.5, name='X1:TEST_OUTPUT')
SATURATIONS = numpy.array([2., 4.])
SEGMENTS = SegmentList([
    Segment(2., 3.5),
    Segment(4., 4.5),
])

CHANNELS = [
    'X1:TEST_LIMIT',
    'X1:TEST_LIMEN',
    'X1:TEST_SWSTAT',
]
TSDICT = TimeSeriesDict({
    'X1:TEST_LIMEN':
    TimeSeries(numpy.ones(10), dx=.5, name='X1:TEST_LIMEN'),
    'X1:TEST_OUTPUT':
    DATA,
    'X1:TEST_LIMIT':
    TimeSeries(5 * numpy.ones(10), dx=.5, name='X1:TEST_LIMIT'),
})

# -- unit tests ---------------------------------------------------------------


def test_find_saturations():
    sats = saturation.find_saturations(DATA, limit=5., segments=False)
    assert_array_equal(sats, SATURATIONS)
    segs = saturation.find_saturations(DATA,
                                       limit=5. * DATA.unit,
                                       segments=True)
    assert_segmentlist_equal(segs.active, SEGMENTS)
Beispiel #19
0
    name="K1:DCH-TEST_FLAG:1",
    active=SegmentList(),
    known=SegmentList([Segment(0, 34)]),
)

K1_DATA = TimeSeriesDict({
    "K1:GW-PRIMARY_CHANNEL": TimeSeries(
        numpy.random.normal(loc=1, scale=.5, size=4096 * GPS * 2),
        sample_rate=4096,
        epoch=0,
    ).zpk([], [0], 1).inject(SIGNAL),
    "K1:AUX-HIGH_SIGNIFICANCE": TimeSeries(
        numpy.random.normal(loc=1, scale=.5, size=4096 * GPS * 2),
        sample_rate=4096,
        epoch=0,
    ).zpk([], [0], 1).inject(SIGNAL),
    "K1:AUX-LOW_SIGNIFICANCE": TimeSeries(
        numpy.random.normal(loc=1, scale=.5, size=4096 * GPS * 2),
        sample_rate=4096,
        epoch=0,
    ),
    "K1:AUX-INVALID_DATA": TimeSeries(
        numpy.full(4096 * GPS * 2, numpy.nan),
        sample_rate=4096,
        epoch=0,
    ),
})

NETWORK_DATA = TimeSeriesDict({
    "H1:GW-PRIMARY_CHANNEL": TimeSeries(
        numpy.random.normal(loc=1, scale=.5, size=4096 * GPS * 2),
        sample_rate=4096,