Exemple #1
0
 def test_property_type_ndstype(self):
     new = Channel('')
     self.assertIsNone(new.ndstype)
     new.type = 'm-trend'
     self.assertEqual(new.ndstype, 16)
     self.assertEqual(new.type, 'm-trend')
     new.ndstype = 's-trend'
     self.assertEqual(new.ndstype, 8)
     self.assertEqual(new.type, 's-trend')
Exemple #2
0
 def test_property_type_ndstype(self):
     new = Channel('')
     self.assertIsNone(new.ndstype)
     new.type = 'm-trend'
     self.assertEqual(new.ndstype, 16)
     self.assertEqual(new.type, 'm-trend')
     new.ndstype = 's-trend'
     self.assertEqual(new.ndstype, 8)
     self.assertEqual(new.type, 's-trend')
Exemple #3
0
 def test_create(self):
     new = Channel('L1:LSC-DARM_ERR', sample_rate=16384, unit='m')
     self.assertTrue(str(new) == 'L1:LSC-DARM_ERR')
     self.assertTrue(new.ifo == 'L1')
     self.assertTrue(new.system == 'LSC')
     self.assertTrue(new.subsystem == 'DARM')
     self.assertTrue(new.signal == 'ERR')
     self.assertTrue(new.sample_rate == units.Quantity(16384, 'Hz'))
     self.assertTrue(new.unit == units.meter)
     self.assertTrue(new.texname == r'L1:LSC-DARM\_ERR')
     new2 = Channel(new)
     self.assertEqual(new.sample_rate, new2.sample_rate)
     self.assertEqual(new.unit, new2.unit)
     self.assertEqual(new.texname, new2.texname)
Exemple #4
0
    def test_find_frame_type(self):
        channel = Channel('L1:TEST-CHANNEL')
        assert data.find_frame_type(channel) == 'L1_R'

        channel = Channel('C1:TEST-CHANNEL')
        assert data.find_frame_type(channel) == 'R'

        channel = Channel('H1:TEST-CHANNEL.rms,s-trend')
        assert data.find_frame_type(channel) == 'H1_T'

        channel = Channel('H1:TEST-CHANNEL.rms,m-trend')
        assert data.find_frame_type(channel) == 'H1_M'

        channel = Channel('H1:TEST-CHANNEL.rms,online')
        assert data.find_frame_type(channel) == 'H1_lldetchar'
Exemple #5
0
 def test_query(self):
     try:
         new = Channel.query(self.channel)
     except URLError as e:
         msg = str(e)
         if ('timed out' in msg.lower() or
             'connection reset' in msg.lower()):
             self.skipTest(msg)
         raise
     except ValueError as e:
         if 'No JSON object could be decoded' in str(e):
             self.skipTest(str(e))
         raise
     except Exception as e:
         try:
             import kerberos
         except ImportError:
             self.skipTest('Channel.query() requires kerberos '
                                     'to be installed')
         else:
             if isinstance(e, kerberos.GSSError):
                 self.skipTest(str(e))
             else:
                 raise
     self.assertTrue(str(new) == self.channel)
     self.assertTrue(new.ifo == self.channel.split(':', 1)[0])
     self.assertTrue(new.sample_rate == units.Quantity(32768, 'Hz'))
     self.assertTrue(new.texname == self.channel.replace('_', r'\_'))
Exemple #6
0
    def test_frequency_range(self):
        new = self.TEST_CLASS('test', frequency_range=(1, 40))
        assert isinstance(new.frequency_range, units.Quantity)
        utils.assert_quantity_equal(new.frequency_range, (1, 40) * units.Hz)

        with pytest.raises(TypeError):
            Channel('', frequency_range=1)
Exemple #7
0
 def test_query(self):
     try:
         new = Channel.query(self.channel)
     except URLError as e:
         msg = str(e)
         if ('timed out' in msg.lower() or
             'connection reset' in msg.lower()):
             self.skipTest(msg)
         raise
     except ValueError as e:
         if 'No JSON object could be decoded' in str(e):
             self.skipTest(str(e))
         raise
     except Exception as e:
         try:
             import kerberos
         except ImportError:
             self.skipTest('Channel.query() requires kerberos '
                                     'to be installed')
         else:
             if isinstance(e, kerberos.GSSError):
                 self.skipTest(str(e))
             else:
                 raise
     self.assertTrue(str(new) == self.channel)
     self.assertTrue(new.ifo == self.channel.split(':', 1)[0])
     self.assertTrue(new.sample_rate == units.Quantity(32768, 'Hz'))
     self.assertTrue(new.texname == self.channel.replace('_', r'\_'))
Exemple #8
0
def split_combination(channelstring):
    """Split a math-combination of channels
    """
    channel = Channel(channelstring)
    if channel.ifo == 'G1':
        return channel.ndsname.split(' ')
    else:
        return re_channel.findall(channel.ndsname)
Exemple #9
0
 def test_query_nds2(self):
     try:
         import nds2
     except ImportError as e:
         self.skipTest(str(e))
     new = Channel.query_nds2(self.channel, host=NDSHOST,
                              type=nds2.channel.CHANNEL_TYPE_RAW)
     self.assertTrue(str(new) == self.channel)
     self.assertTrue(new.ifo == self.channel.split(':', 1)[0])
     self.assertTrue(new.sample_rate == units.Quantity(32768, 'Hz'))
     self.assertTrue(new.type == 'raw')
     self.assertTrue(new.texname == self.channel.replace('_', r'\_'))
Exemple #10
0
def test_update_missing_channel_params():
    # define empty channel
    chan = channels.get_channel('X1:TEST:1')
    assert chan.unit is None

    # update using kwargs
    channels.update_missing_channel_params('X1:TEST:1', unit='meter')
    assert chan.unit == units.meter
    chan.unit = None

    # update from another channel
    c2 = Channel('X1:TEST:1', unit='V')
    channels.update_missing_channel_params(c2)
    assert chan.unit == units.volt
Exemple #11
0
 def test_parse_channel_name(self):
     self.assertRaises(ValueError, Channel.parse_channel_name, 'blah')
     valid = {
         'ifo': 'X1',
         'system': 'TEST',
         'subsystem': 'CHANNEL',
         'signal': 'NAME_PARSING',
         'trend': 'rms',
         'type': 'm-trend',
     }
     out = Channel.parse_channel_name(
         'X1:TEST-CHANNEL_NAME_PARSING.rms,m-trend')
     self.assertDictEqual(out, valid)
     c = Channel('')
     for attr in valid:
         self.assertIsNone(getattr(c, attr))
     c = Channel('X1:TEST-CHANNEL_NAME_PARSING.rms,m-trend')
     for attr in valid:
         self.assertEqual(valid[attr], getattr(c, attr))
     self.assertEqual(c.name, 'X1:TEST-CHANNEL_NAME_PARSING.rms')
     self.assertEqual(c.ndsname,
                      'X1:TEST-CHANNEL_NAME_PARSING.rms,m-trend')
     # test parsing GEO channels
     out = Channel.parse_channel_name("G1:PSL_SL_PWR-AMPL-OUTLP-av")
     self.assertDictEqual(
         out, {'ifo': 'G1', 'system': 'PSL', 'subsystem': 'SL',
               'signal': 'PWR-AMPL-OUTLP', 'trend': 'av', 'type': None})
     # test virgo channels
     out = Channel.parse_channel_name("V1:h_16384Hz")
     self.assertDictEqual(
         out, {'ifo': 'V1', 'system':'h', 'subsystem':'16384Hz',
               'signal': None, 'trend': None, 'type': None})
     out = Channel.parse_channel_name("V1:Sa_PR_f0_zL_500Hz")
     self.assertDictEqual(
         out, {'ifo': 'V1', 'system':'Sa', 'subsystem':'PR',
               'signal': 'f0_zL_500Hz', 'trend': None, 'type': None})
Exemple #12
0
 def test_nds2_conversion(self):
     try:
         import nds2
     except ImportError as e:
         self.skipTest(str(e))
     else:
         try:
             conn = nds2.connection(NDSHOST)
         except Exception as f:
             self.skipTest(str(f))
         else:
             nds2channel = conn.find_channels(self.channel)[0]
             new = Channel.from_nds2(nds2channel)
             self.assertTrue(str(new) == self.channel)
             self.assertTrue(new.ifo == self.channel.split(':', 1)[0])
             self.assertTrue(new.sample_rate == units.Quantity(32768, 'Hz'))
Exemple #13
0
 def test_nds2_conversion(self):
     try:
         import nds2
     except ImportError as e:
         self.skipTest(str(e))
     else:
         try:
             conn = nds2.connection(NDSHOST)
         except Exception as f:
             self.skipTest(str(f))
         else:
             nds2channel = conn.find_channels(self.channel)[0]
             new = Channel.from_nds2(nds2channel)
             self.assertTrue(str(new) == self.channel)
             self.assertTrue(new.ifo == self.channel.split(':', 1)[0])
             self.assertTrue(new.sample_rate == units.Quantity(32768, 'Hz'))
Exemple #14
0
 def test_from_nds2_buffer(self):
     nds_buffer = mocks.nds2_buffer('X1:TEST', self.data, 1000000000,
                                    self.data.shape[0], 'm')
     a = self.TEST_CLASS.from_nds2_buffer(nds_buffer)
     assert isinstance(a, self.TEST_CLASS)
     utils.assert_array_equal(a.value, self.data)
     assert a.unit == units.m
     assert a.t0 == 1000000000 * units.s
     assert a.dt == units.s / self.data.shape[0]
     assert a.name == 'X1:TEST'
     assert a.channel == Channel('X1:TEST',
                                 sample_rate=self.data.shape[0],
                                 unit='m',
                                 type='raw',
                                 dtype='float32')
     b = self.TEST_CLASS.from_nds2_buffer(nds_buffer, sample_rate=128)
     assert b.dt == 1 / 128. * units.s
Exemple #15
0
 def test_parse_channel_name(self):
     self.assertRaises(ValueError, Channel.parse_channel_name, 'blah')
     valid = {
         'ifo': 'X1',
         'system': 'TEST',
         'subsystem': 'CHANNEL',
         'signal': 'NAME_PARSING',
         'trend': 'rms',
         'type': 'm-trend',
     }
     out = Channel.parse_channel_name(
         'X1:TEST-CHANNEL_NAME_PARSING.rms,m-trend')
     self.assertDictEqual(out, valid)
     c = Channel('')
     for attr in valid:
         self.assertIsNone(getattr(c, attr))
     c = Channel('X1:TEST-CHANNEL_NAME_PARSING.rms,m-trend')
     for attr in valid:
         self.assertEqual(valid[attr], getattr(c, attr))
     self.assertEqual(c.name, 'X1:TEST-CHANNEL_NAME_PARSING.rms')
     self.assertEqual(c.ndsname, 'X1:TEST-CHANNEL_NAME_PARSING.rms,m-trend')
     # test parsing GEO channels
     out = Channel.parse_channel_name("G1:PSL_SL_PWR-AMPL-OUTLP-av")
     self.assertDictEqual(
         out, {
             'ifo': 'G1',
             'system': 'PSL',
             'subsystem': 'SL',
             'signal': 'PWR-AMPL-OUTLP',
             'trend': 'av',
             'type': None
         })
     # test virgo channels
     out = Channel.parse_channel_name("V1:h_16384Hz")
     self.assertDictEqual(
         out, {
             'ifo': 'V1',
             'system': 'h',
             'subsystem': '16384Hz',
             'signal': None,
             'trend': None,
             'type': None
         })
     out = Channel.parse_channel_name("V1:Sa_PR_f0_zL_500Hz")
     self.assertDictEqual(
         out, {
             'ifo': 'V1',
             'system': 'Sa',
             'subsystem': 'PR',
             'signal': 'f0_zL_500Hz',
             'trend': None,
             'type': None
         })
Exemple #16
0
    def test_read_frame_file(self):
        start_time = 0
        end_time = 10
        channel = "H1:GDS-CALIB_STRAIN"
        N = 100
        times = np.linspace(start_time, end_time, N)
        data = np.random.normal(0, 1, N)
        ts = TimeSeries(data=data, times=times, t0=0)
        ts.channel = Channel(channel)
        ts.name = channel
        filename = os.path.join(self.outdir, "test.gwf")
        ts.write(filename, format="gwf")

        # Check reading without time limits
        strain = gwutils.read_frame_file(
            filename, start_time=None, end_time=None, channel=channel
        )
        self.assertEqual(strain.channel.name, channel)
        self.assertTrue(np.all(strain.value == data[:-1]))

        # Check reading with time limits
        start_cut = 2
        end_cut = 8
        strain = gwutils.read_frame_file(
            filename, start_time=start_cut, end_time=end_cut, channel=channel
        )
        idxs = (times > start_cut) & (times < end_cut)
        # Dropping the last element - for some reason gwpy drops the last element when reading in data
        self.assertTrue(np.all(strain.value == data[idxs][:-1]))

        # Check reading with unknown channels
        strain = gwutils.read_frame_file(filename, start_time=None, end_time=None)
        self.assertTrue(np.all(strain.value == data[:-1]))

        # Check reading with incorrect channel
        strain = gwutils.read_frame_file(
            filename, start_time=None, end_time=None, channel="WRONG"
        )
        self.assertTrue(np.all(strain.value == data[:-1]))

        ts = TimeSeries(data=data, times=times, t0=0)
        ts.name = "NOT-A-KNOWN-CHANNEL"
        ts.write(filename, format="gwf")
        strain = gwutils.read_frame_file(filename, start_time=None, end_time=None)
        self.assertEqual(strain, None)
Exemple #17
0
def _match(channel):
    channel = Channel(channel)
    name = str(channel)
    type_ = channel.type
    found = globalv.CHANNELS.sieve(name=name, type=type_, exact_match=True)

    # if match, return now
    if found:
        return found

    # if no matches, try again without matching type
    found = globalv.CHANNELS.sieve(name=name, exact_match=True)
    if len(found) == 1:
        # found single match that is less specific, so we make it more
        # specific. If someone else wants another type for the sme channel
        # this is fine, and it will create another channel.
        found[0].type = type_
    return found
Exemple #18
0
 def test_find_frame_type(self):
     channel = Channel('L1:TEST-CHANNEL')
     self.assertEqual(data.find_frame_type(channel), 'L1_R')
     channel = Channel('C1:TEST-CHANNEL')
     self.assertEqual(data.find_frame_type(channel), 'R')
     channel = Channel('H1:TEST-CHANNEL.rms,s-trend')
     self.assertEqual(data.find_frame_type(channel), 'H1_T')
     channel = Channel('H1:TEST-CHANNEL.rms,m-trend')
     self.assertEqual(data.find_frame_type(channel), 'H1_M')
     channel = Channel('H1:TEST-CHANNEL.rms,reduced')
     self.assertEqual(data.find_frame_type(channel), 'H1_LDAS_C02_L2')
     channel = Channel('H1:TEST-CHANNEL.rms,online')
     self.assertEqual(data.find_frame_type(channel), 'H1_lldetchar')
Exemple #19
0
 def test_query_nds2(self):
     try:
         import nds2
     except ImportError as e:
         self.skipTest(str(e))
     try:
         from gwpy.io import kerberos
         kerberos.which('kinit')
     except ValueError as e:
         self.skipTest(str(e))
     try:
         new = Channel.query_nds2(self.channel, host=NDSHOST,
                                  type=nds2.channel.CHANNEL_TYPE_RAW)
     except (RuntimeError, IOError) as e:
         self.skipTest(str(e))
     self.assertTrue(str(new) == self.channel)
     self.assertTrue(new.ifo == self.channel.split(':', 1)[0])
     self.assertTrue(new.sample_rate == units.Quantity(32768, 'Hz'))
     self.assertTrue(new.type == 'raw')
     self.assertTrue(new.texname == self.channel.replace('_', r'\_'))
Exemple #20
0
 def test_query_nds2(self):
     try:
         import nds2
     except ImportError as e:
         self.skipTest(str(e))
     try:
         from gwpy.io import kerberos
         kerberos.which('kinit')
     except ValueError as e:
         self.skipTest(str(e))
     try:
         new = Channel.query_nds2(self.channel, host=NDSHOST,
                                  type=nds2.channel.CHANNEL_TYPE_RAW)
     except IOError as e:
         self.skipTest(str(e))
     self.assertTrue(str(new) == self.channel)
     self.assertTrue(new.ifo == self.channel.split(':', 1)[0])
     self.assertTrue(new.sample_rate == units.Quantity(32768, 'Hz'))
     self.assertTrue(new.type == 'raw')
     self.assertTrue(new.texname == self.channel.replace('_', r'\_'))
Exemple #21
0
    def test_query_nds2(self):
        try:
            import nds2
        except ImportError as e:
            self.skipTest(str(e))
        try:
            from gwpy.io import kerberos

            kerberos.which("kinit")
        except ValueError as e:
            self.skipTest(str(e))
        try:
            new = Channel.query_nds2(self.channel, host=NDSHOST, type=nds2.channel.CHANNEL_TYPE_RAW)
        except IOError as e:
            self.skipTest(str(e))
        self.assertTrue(str(new) == self.channel)
        self.assertTrue(new.ifo == self.channel.split(":", 1)[0])
        self.assertTrue(new.sample_rate == units.Quantity(32768, "Hz"))
        self.assertTrue(new.type == "raw")
        self.assertTrue(new.texname == self.channel.replace("_", r"\_"))
Exemple #22
0
 def test_parse_channel_name(self):
     self.assertRaises(ValueError, Channel.parse_channel_name, 'blah')
     valid = {
         'ifo': 'X1',
         'system': 'TEST',
         'subsystem': 'CHANNEL',
         'signal': 'NAME_PARSING',
         'trend': 'rms',
         'type': 'm-trend',
     }
     out = Channel.parse_channel_name(
         'X1:TEST-CHANNEL_NAME_PARSING.rms,m-trend')
     self.assertDictEqual(out, valid)
     c = Channel('')
     for attr in valid:
         self.assertIsNone(getattr(c, attr))
     c = Channel('X1:TEST-CHANNEL_NAME_PARSING.rms,m-trend')
     for attr in valid:
         self.assertEqual(valid[attr], getattr(c, attr))
     self.assertEqual(c.name, 'X1:TEST-CHANNEL_NAME_PARSING.rms')
     self.assertEqual(c.ndsname,
                      'X1:TEST-CHANNEL_NAME_PARSING.rms,m-trend')
Exemple #23
0
 def test_query(self):
     try:
         new = Channel.query(self.channel)
     except URLError as e:
         msg = str(e)
         if "timed out" in msg.lower() or "connection reset" in msg.lower():
             self.skipTest(msg)
         raise
     except Exception as e:
         try:
             import kerberos
         except ImportError:
             self.skipTest("Channel.query() requires kerberos " "to be installed")
         else:
             if isinstance(e, kerberos.GSSError):
                 self.skipTest(str(e))
             else:
                 raise
     self.assertTrue(str(new) == self.channel)
     self.assertTrue(new.ifo == self.channel.split(":", 1)[0])
     self.assertTrue(new.sample_rate == units.Quantity(32768, "Hz"))
     self.assertTrue(new.texname == self.channel.replace("_", r"\_"))
Exemple #24
0
def _new(channel, find_parent=True):
    """Create a new `~gwpy.detector.Channel` in the globalv cache.
    """
    # convert to Channel
    if isinstance(channel, Channel):
        new = channel
    else:
        new = Channel(channel)
    name = str(channel)
    type_ = new.type

    # work out what kind of channel it is
    parts = re_channel.findall(name)

    # match single raw channel for LIGO
    if (len(parts) == 1 and new.ifo in ('H1', 'L1')
            and not re.search(r'\.[a-z]+\Z', name)):
        new.url = '%s/channel/byname/%s' % (CIS_URL, str(new))

    # match single trend
    elif len(parts) == 1:
        # set default trend type based on mode
        if type_ is None and ':DMT-' in name:  # DMT is always m-trend
            new.type = 'm-trend'
        # match parameters from 'raw' version of this channel

    # match composite channel
    else:
        new.subchannels = parts
        new._ifo = "".join(set(p.ifo for p in map(Channel, parts) if p.ifo))

    if find_parent:
        _update_dependent(new)

    # store new channel and return
    globalv.CHANNELS.append(new)
    try:
        return get_channel(new)
    except RuntimeError as e:
        if 'maximum recursion depth' in str(e):
            raise RuntimeError("Recursion error while accessing channel "
                               "information for %s" % str(channel))
        raise
Exemple #25
0
 def iterate(start, end, names):
     if not buffers:
         return []
     return [[
         b for b in buffers if Channel.from_nds2(b.channel).ndsname in names
     ]]
Exemple #26
0
 def test_property_safe(self):
     new = Channel('')
     self.assertIsNone(new.safe)
     new.safe = True
     self.assertTrue(new.safe)
from stamp_pem.coherence_segment import PEMCoherenceSegment
from gwpy.detector import Channel

# Supply the two channels to take the coherence between
ch1 = Channel('H1:GDS-CALIB_STRAIN', frametype='H1_HOFT_C00')
ch2 = Channel('H1:LSC-PRCL_IN1_DQ', frametype='H1_R')

# Supply a start and end time (10 minute interval shown here)
start_time = 1152007217
end_time = start_time + 600

# Call the function
coherence_result = PEMCoherenceSegment.coherence(ch1, ch2, st=start_time, et=end_time, stride=1)

# Create a plot
plot = coherence_result.plot()
ax = plot.gca()
ax.set_xlim(0, 600)
ax.set_ylabel(r'Coherence between two H1 channels')
# Add a plot 1/N
ax.plot([0,600], [1./coherence_result.N, 1./coherence_result.N])

# To view the plot rather than saving it, uncomment the following
# plot.show()

# To save the figure, 
plot.savefig('H1_Coherence_Example.png')

# Coarse grain the data and plot it
coherence_result.coarse_grain(deltaFy=2)
plot = coherence_result.plot()
Exemple #28
0
 def test_property_safe(self):
     new = Channel('')
     self.assertIsNone(new.safe)
     new.safe = True
     self.assertTrue(new.safe)
Exemple #29
0
 def setUp(self):
     self.channels = [Channel(n, s) for n, s in
                 zip(self.NAMES, self.SAMPLE_RATES)]
Exemple #30
0
def get_channel(channel, find_trend_source=True, timeout=5):
    """Define a new :class:`~gwpy.detector.channel.Channel`

    Parameters
    ----------
    channel : `str`
        name of new channel
    find_trend_source : `bool`, optional, default: `True`
        query for raw version of trend channel (trends not in CIS)
    timeout : `float`, optional, default: `5`
        number of seconds to wait before connection times out

    Returns
    -------
    Channel : :class:`~gwpy.detector.channel.Channel`
        new channel.
    """
    chans = re_channel.findall(str(channel))
    nchans = len(chans)
    if nchans > 1 or (nchans == 1 and chans[0] != str(channel)):
        name = str(channel)
        try:
            type_ = Channel.MATCH.match(name).groupdict()['type']
        except AttributeError:
            type_ = None
        # handle special characters in channel name
        rename = name
        for cchar in ['+', '*', '^', '|']:
            rename = rename.replace(cchar, '\%s' % cchar)
        found = globalv.CHANNELS.sieve(name=rename, exact_match=True)
    elif ',' in str(channel):
        name, type_ = str(channel).rsplit(',', 1)
        found = globalv.CHANNELS.sieve(name=name, type=type_, exact_match=True)
    else:
        type_ = isinstance(channel, Channel) and channel.type or None
        name = str(channel)
        found = globalv.CHANNELS.sieve(name=name, type=type_, exact_match=True)
    if len(found) == 1:
        return found[0]
    elif len(found) > 1:
        cstrings = set([
            '%s [%s, %s]' % (c.ndsname, c.sample_rate, c.unit) for c in found
        ])
        if len(cstrings) == 1:
            return found[0]
        else:
            raise ValueError("Ambiguous channel request '%s', multiple "
                             "existing channels recovered:\n    %s" %
                             (str(channel), '\n    '.join(cstrings)))
    else:
        matches = list(Channel.MATCH.finditer(name))
        # match single raw channel
        if len(matches) == 1 and not re.search('\.[a-z]+\Z', name):
            new = Channel(str(channel))
            new.url = '%s/channel/byname/%s' % (CIS_URL, str(new))
        # match single trend
        elif len(matches) == 1:
            # set default trend type based on mode
            if type_ is None and ':DMT-' in name:  # DMT is always m-trend
                type_ = 'm-trend'
            elif type_ is None and globalv.MODE == Mode.gps:
                type_ = 's-trend'
            elif type_ is None:
                type_ = 'm-trend'
            name += ',%s' % type_
            new = Channel(name)
            if find_trend_source:
                try:
                    source = get_channel(new.name.rsplit('.')[0])
                except ValueError:
                    pass
                else:
                    new.url = source.url
                    new.unit = source.unit
                    try:
                        new.bits = source.bits
                    except AttributeError:
                        pass
                    try:
                        new.filter = source.filter
                    except AttributeError:
                        pass
                    for param in filter(
                            lambda x: x.endswith('_range') and not hasattr(
                                new, x), vars(source)):
                        setattr(new, param, getattr(source, param))
            # determine sample rate for trends
            if type_ == 'm-trend':
                new.sample_rate = 1 / 60.
            elif type_ == 's-trend':
                new.sample_rate = 1
        # match composite channel
        else:
            parts = get_channels([m.group() for m in matches])
            new = Channel(name)
            new.subchannels = parts
            new._ifo = "".join(set(p.ifo for p in parts if p.ifo))
        globalv.CHANNELS.append(new)
        try:
            return get_channel(new)
        except RuntimeError as e:
            if 'maximum recursion depth' in str(e):
                raise RuntimeError("Recursion error while accessing channel "
                                   "information for %s" % str(channel))
            else:
                raise
Exemple #31
0
def get_channel(channel, find_trend_source=True, timeout=5):
    """Define a new :class:`~gwpy.detector.channel.Channel`

    Parameters
    ----------
    channel : `str`
        name of new channel
    find_trend_source : `bool`, optional, default: `True`
        query for raw version of trend channel (trends not in CIS)
    timeout : `float`, optional, default: `5`
        number of seconds to wait before connection times out

    Returns
    -------
    Channel : :class:`~gwpy.detector.channel.Channel`
        new channel.
    """
    chans = re_channel.findall(str(channel))
    nchans = len(chans)
    if nchans > 1 or (nchans == 1 and chans[0] != str(channel)):
        name = str(channel)
        try:
            type_ = Channel.MATCH.match(name).groupdict()['type']
        except AttributeError:
            type_ = None
        # handle special characters in channel name
        rename = name
        for cchar in ['+', '*', '^', '|']:
            rename = rename.replace(cchar, '\%s' % cchar)
        found = globalv.CHANNELS.sieve(name=rename, exact_match=True)
    elif ',' in str(channel):
        name, type_ = str(channel).rsplit(',', 1)
        found = globalv.CHANNELS.sieve(name=name, type=type_, exact_match=True)
    else:
        type_ = isinstance(channel, Channel) and channel.type or None
        name = str(channel)
        found = globalv.CHANNELS.sieve(name=name, type=type_, exact_match=True)
    if len(found) == 1:
        return found[0]
    elif len(found) > 1:
        cstrings = set(['%s [%s, %s]' % (c.ndsname, c.sample_rate, c.unit)
                        for c in found])
        if len(cstrings) == 1:
            return found[0]
        else:
            raise ValueError("Ambiguous channel request '%s', multiple "
                             "existing channels recovered:\n    %s"
                         % (str(channel), '\n    '.join(cstrings)))
    else:
        matches = list(Channel.MATCH.finditer(name))
        # match single raw channel
        if len(matches) == 1 and not re.search('\.[a-z]+\Z', name):
            new = Channel(str(channel))
            new.url = '%s/channel/byname/%s' % (CIS_URL, str(new))
        # match single trend
        elif len(matches) == 1:
            # set default trend type based on mode
            if type_ is None and ':DMT-' in name:  # DMT is always m-trend
                type_ = 'm-trend'
            elif type_ is None and globalv.MODE == SUMMARY_MODE_GPS:
                type_ = 's-trend'
            elif type_ is None:
                type_ = 'm-trend'
            name += ',%s' % type_
            new = Channel(name)
            if find_trend_source:
                try:
                    source = get_channel(new.name.rsplit('.')[0])
                except ValueError:
                    pass
                else:
                    new.url = source.url
                    new.unit = source.unit
                    try:
                        new.bits = source.bits
                    except AttributeError:
                        pass
                    try:
                        new.filter = source.filter
                    except AttributeError:
                        pass
                    for param in filter(lambda x: x.endswith('_range') and
                                                  not hasattr(new, x),
                                        vars(source)):
                        setattr(new, param, getattr(source, param))
            # determine sample rate for trends
            if type_ == 'm-trend':
                new.sample_rate = 1/60.
            elif type_ == 's-trend':
                new.sample_rate = 1
        # match composite channel
        else:
            parts = get_channels([m.group() for m in matches])
            new = Channel(name)
            new.subchannels = parts
            new._ifo = "".join(set(p.ifo for p in parts if p.ifo))
        globalv.CHANNELS.append(new)
        try:
            return get_channel(new)
        except RuntimeError as e:
            if 'maximum recursion depth' in str(e):
                raise RuntimeError("Recursion error while accessing channel "
                                   "information for %s" % str(channel))
            else:
                raise
Exemple #32
0
from stamp_pem.coherence_segment import PEMCoherenceSegment
from gwpy.detector import Channel

# supplying frame types speeds things up considerably
ch1 = Channel('H1:GDS-CALIB_STRAIN', frametype='H1_HOFT_C00')
ch2 = Channel('H1:IMC-F_OUT_DQ', frametype="H1_R")
st = 1157850017
et = st + 10
stride = 1
pad = False

# run coherence
coh = PEMCoherenceSegment.coherence(ch1, ch2, st, et, stride=1)

# print out the coherence spectrum
# we use get_coh() so that we don't
# have to save an extra spectrum or hold it in memory
# unless we want to...the coh object initially just has
# PSDs and the CSD between our channels
print coh.get_coh()  # gwpy.FrequencySeries

# plot it!
plot = coh.get_coh().plot()
ax = plot.gca()
ax.set_xlabel('Frequency [Hz]')
ax.set_ylabel(r'$C(f)$')
ax.set_title('Coherence between %s and %s' %
             (ch1.name.replace('_', '\_'), ch2.name.replace('_', '\_')),
             fontsize=10)
plot.savefig('coherence_segment_example_plot.png')
Exemple #33
0
def get_timeseries_dict(channels, segments, config=ConfigParser(),
                        cache=None, query=True, nds='guess', multiprocess=True,
                        frametype=None, statevector=False, return_=True,
                        datafind_error='raise', **ioargs):
    """Retrieve the data for a set of channels
    """
    # separate channels by type
    if query:
        if frametype is not None:
            frametypes = {(None, frametype): channels}
        else:
            frametypes = dict()
            allchannels = set([
                c for group in
                    map(lambda x: re_channel.findall(Channel(x).ndsname),
                        channels)
                for c in group])
            for channel in allchannels:
                channel = get_channel(channel)
                ifo = channel.ifo
                ftype = find_frame_type(channel)
                id_ = (ifo, ftype)
                if id_ in frametypes:
                    frametypes[id_].append(channel)
                else:
                    frametypes[id_] = [channel]
        for ftype, channellist in frametypes.iteritems():
            _get_timeseries_dict(channellist, segments, config=config,
                                 cache=cache, query=query, nds=nds,
                                 multiprocess=multiprocess, frametype=ftype[1],
                                 statevector=statevector, return_=False,
                                 datafind_error=datafind_error, **ioargs)
    if not return_:
        return
    else:
        out = OrderedDict()
        for channel in channels:
            channel = Channel(channel)
            chanstrs = re_channel.findall(channel.ndsname)
            chans = map(get_channel, chanstrs)
            tsdict = _get_timeseries_dict(chans, segments, config=config,
                                          query=False, statevector=statevector,
                                          **ioargs)
            tslist = [tsdict[Channel(c).ndsname] for c in chans]
            # if only one channel, simply append
            if len(chanstrs) == 1 and len(channel.ndsname) == len(chanstrs[0]):
                out[channel.ndsname] = tslist[0]
            # if one channel and some operator, do calculation
            elif len(chanstrs) == 1:
                stub = channel.ndsname[len(chanstrs[0]):].strip(' ')
                try:
                   op = OPERATOR[stub[0]]
                except KeyError as e:
                   print(channel.ndsname, chanstrs, stub)
                   e.args = ('Cannot parse math operator %r' % stub[0],)
                   raise
                value = float(stub[1:])
                out[channel.ndsname] = type(tslist[0])(*[op(ts, value)
                                                         for ts in tslist[0]])
            # if multiple channels
            else:
                # get union of segments for all sub-channels
                datasegs = reduce(operator.and_,
                                  [tsl.segments for tsl in tslist])
                # build meta-timeseries for all interseceted segments
                meta = type(globalv.DATA[chans[0].ndsname])()
                operators = [channel.name[m.span()[1]] for m in
                             list(re_channel.finditer(channel.ndsname))[:-1]]
                for seg in datasegs:
                    ts = get_timeseries(
                        chanstrs[0], SegmentList([seg]), config=config,
                        query=False, return_=True)[0]
                    ts.name = str(channel)
                    for op, ch in zip(operators, chanstrs[1:]):
                        try:
                            op = OPERATOR[op]
                        except KeyError as e:
                            e.args = ('Cannot parse math operator %r' % op,)
                            raise
                        data = get_timeseries(ch, SegmentList([seg]),
                                              config=config, query=False,
                                              return_=True)
                        ts = op(ts, data[0])
                    meta.append(ts)
                out[channel.ndsname] = meta
        return out
Exemple #34
0
 def test_property_frequency_range(self):
     new = Channel('test', frequency_range=(1, 40))
     self.assertIsInstance(new.frequency_range, units.Quantity)
     self.assertListEqual(list(new.frequency_range.value), [1, 40])
     self.assertIs(new.frequency_range.unit, units.Hz)
     self.assertRaises(TypeError, Channel, '', frequency_range=1)
Exemple #35
0
 def test_empty_create(self):
     new = Channel('')
     self.assertTrue(str(new) == '')
     self.assertTrue(new.sample_rate is None)
     self.assertTrue(new.dtype is None)
Exemple #36
0
 def test_fmcs_parse(self):
     new = Channel('LVE-EX:X3_810BTORR.mean,m-trend')
     self.assertEqual(new.ifo, None)
     self.assertEqual(new.name, 'LVE-EX:X3_810BTORR.mean')
     self.assertEqual(new.trend, 'mean')
     self.assertEqual(new.type, 'm-trend')
Exemple #37
0
from gwpy.detector import Channel
from gwpy.segments import Segment
from gwpy.time import LIGOTimeGPS

from . import utils

warnings.filterwarnings('always', category=units.UnitsWarning)
warnings.filterwarnings('always', category=UserWarning)

__author__ = 'Duncan Macleod <*****@*****.**>'

SEED = 1
GPS_EPOCH = 12345
TIME_EPOCH = Time(12345, format='gps', scale='utc')
CHANNEL_NAME = 'G1:TEST-CHANNEL'
CHANNEL = Channel(CHANNEL_NAME)


class TestArray(object):
    """Test `gwpy.types.Array`
    """
    TEST_CLASS = Array
    DTYPE = None

    # -- setup ----------------------------------

    @classmethod
    def setup_class(cls):
        numpy.random.seed(SEED)
        cls.data = (numpy.random.random(100) * 1e5).astype(dtype=cls.DTYPE)
from stamp_pem.coherence_segment import PEMSubsystem
from stamp_pem.coherence_segment import ChannelDict
from gwpy.detector import Channel

# define our darm channel, start time, end time again
ch1 = Channel('L1:GDS-CALIB_STRAIN', frametype='L1_HOFT_C00')
st = 1157850017
et = st + 10
stride = 1

# Read in our channel list into a dict with keys that are
# the subsystems specified by the headings in the ligo-channel-list
# file. You can open this ini file to see them
chandict = ChannelDict.read('L1-O2-reduced.ini', maxchans=10)

# The keys will tell us what subsystems we can choose from
print chandict.keys()

# The subsystems are broken down into subsets of 10 channels
# automatically. This can be changed with the "maxchans" keyword.
# We'll choose a subsystem with only a few channels for right now
subsystem = 'Thermal Compensation 1'


# Now we can run subsystem coherence on one of these subsystems
# it produces as dict with keys as channel names and PEMCoherenceSegments
# as the values for those keys
subcoh = PEMSubsystem.coherence(ch1, subsystem, chandict, st, et, stride=stride)

print subcoh.keys()