コード例 #1
0
def test_timeseries_annotations(client, timeseries):
    assert timeseries.exists
    print("layers = ", timeseries.layers)

    # Create Layer
    layer1 = TimeSeriesAnnotationLayer(name="test_layer",
                                       time_series_id=timeseries.id,
                                       description="test_description")
    a = layer1.as_dict()
    assert a["name"] == "test_layer"
    assert a["description"] == "test_description"

    # Add Layer
    timeseries.add_layer(layer1)
    assert layer1.exists

    # Get Layer
    layer1b = timeseries.get_layer("test_layer")
    assert layer1b.exists
    assert layer1b.name == "test_layer"
    assert layer1.id == layer1b.id
    assert layer1._api.timeseries is not None

    # Add another layer
    layer2 = timeseries.add_layer("test_layer2", "test_description2")
    assert layer2.exists

    layer2copy = timeseries.add_layer("test_layer2")
    assert layer2copy.id == layer2.id

    # Get Layer
    layer2b = timeseries.get_layer("test_layer2")
    assert layer2b.exists
    assert layer2b.name == "test_layer2"
    assert layer2.id == layer2b.id
    assert layer2._api.timeseries is not None

    layers = timeseries.layers
    assert len(layers) == 2
    assert set([layers[0].name,
                layers[1].name]) == set(["test_layer", "test_layer2"])

    # Create channels
    ch = TimeSeriesChannel(name="test_channel",
                           rate=256,
                           unit="uV",
                           start=1,
                           end=60 * 1e6)

    # Create annotation over one channel
    # create
    timeseries.add_channels(ch)
    assert ch.exists
    assert ch.id in [x.id for x in timeseries.channels]
    annot = TimeSeriesAnnotation(
        label="test_label",
        channel_ids=timeseries.channels[0].id,
        start=timeseries.channels[0].start,
        end=timeseries.channels[0].start + 1 * 1e6,
    )
    # Add Annotation
    layer1.add_annotations(annot)
    assert annot.exists

    # get annotations
    annotb = layer1.annotations()
    assert annotb[0].label == annot.label

    annotc = client._api.timeseries.get_annotation(timeseries, layer1, annot)
    assert annotc.label == annot.label

    # Create annotation over multiple channels
    ch2 = TimeSeriesChannel(name="test_channel",
                            rate=256,
                            unit="uV",
                            start=1,
                            end=60 * 1e6)

    timeseries.add_channels(ch2)
    channels = timeseries.channels
    ch_ids = [x.id for x in channels]
    assert ch2.exists
    assert ch2.id in ch_ids
    assert ch.id in ch_ids
    for ch in channels:
        assert ch.rate == 256
        assert ch.exists

    # add annotation over two channels
    channel_ids = [
        timeseries.channels[x].id for x in range(len(timeseries.channels))
    ]

    annot2 = layer1.insert_annotation(
        annotation="test_label2",
        channel_ids=channel_ids,
        start=timeseries.channels[0].start + 1 * 1e6,
        end=timeseries.channels[0].start + 2 * 1e6,
    )

    assert annot2.exists

    annot_gen = layer1.iter_annotations(1)
    annot = next(annot_gen)
    assert annot[0].label == "test_label"
    next_annot = next(annot_gen)
    assert next_annot[0].label == "test_label2"

    ### TEST ANNOTATION COUNTS
    layer1_expected_counts = [
        {
            "start": 0,
            "end": 250000,
            "value": 1.0
        },
        {
            "start": 250000,
            "end": 500000,
            "value": 1.0
        },
        {
            "start": 500000,
            "end": 750000,
            "value": 1.0
        },
        {
            "start": 750000,
            "end": 1000000,
            "value": 1.0
        },
        {
            "start": 1000000,
            "end": 1250000,
            "value": 2.0
        },
        {
            "start": 1250000,
            "end": 1500000,
            "value": 1.0
        },
        {
            "start": 1500000,
            "end": 1750000,
            "value": 1.0
        },
        {
            "start": 1750000,
            "end": 2000000,
            "value": 1.0
        },
        {
            "start": 2000000,
            "end": 2000001,
            "value": 1.0
        },
    ]

    def _sort_counts(counts):
        return sorted(counts, key=lambda c: c["start"])

    assert (_sort_counts(
        timeseries.annotation_counts(
            start=timeseries.channels[0].start * 1e6,
            end=timeseries.channels[0].start + 2 * 1e6,
            layers=[layer1],
            period="0.25s",
        )[str(layer1.id)]) == layer1_expected_counts)
    assert (_sort_counts(
        layer1.annotation_counts(
            start=timeseries.channels[0].start * 1e6,
            end=timeseries.channels[0].start + 2 * 1e6,
            period="0.25s",
        )[str(layer1.id)]) == layer1_expected_counts)

    ### TEST DELETION

    annot3 = TimeSeriesAnnotation(
        label="test_label3",
        channel_ids=channel_ids,
        start=timeseries.channels[0].start + 1 * 1e6,
        end=timeseries.channels[0].start + 2 * 1e6,
    )
    layer1.add_annotations([annot2, annot3])
    annot3 = timeseries.add_annotations(layer=layer1, annotations=annot3)
    assert annot3.exists
    annot3.delete()
    assert not annot3.exists

    annot4 = timeseries.insert_annotation(
        layer=layer1,
        annotation="test_label3",
        start=timeseries.channels[0].start + 1 * 1e6,
        end=timeseries.channels[0].start + 2 * 1e6,
    )
    assert annot4.exists
    annot4.delete()
    assert not annot4.exists

    annot5 = timeseries.insert_annotation(
        layer="test_layer4",
        annotation="test_label3",
        start=timeseries.channels[0].start + 1 * 1e6,
        end=timeseries.channels[0].start + 2 * 1e6,
    )
    assert annot5.exists
    annot5.delete()
    assert not annot5.exists

    layer1.add_annotations([annot2, annot3])
    assert annot2.exists
    assert annot3.exists

    # test datetime input
    annot4 = timeseries.insert_annotation(
        layer="test_layer4",
        annotation="test_label3",
        start=datetime.datetime.utcfromtimestamp(
            (timeseries.channels[0].start + 1 * 1e6) / 1e6),
        end=datetime.datetime.utcfromtimestamp(timeseries.channels[0].start +
                                               2 * 1e6),
    )
    assert annot4.exists
    annot4.delete()
    assert not annot4.exists

    layer = timeseries.get_layer("test_layer4")
    assert layer.exists
    layer.delete()
    assert not layer.exists

    # delete annotations
    annot[0].delete()
    assert not annot[0].exists

    assert timeseries.exists
    timeseries.delete_layer(layer1)
    assert not layer1.exists

    assert layer2.exists
    layer2.delete()
    assert not layer2.exists
コード例 #2
0
def test_timeseries_annotations(client, timeseries):
    assert timeseries.exists
    print 'layers = ', timeseries.layers

    #Create Layer
    layer1 = TimeSeriesAnnotationLayer(name="test_layer", time_series_id = timeseries.id, description="test_description")
    a = layer1.as_dict()
    assert a['name'] == 'test_layer'
    assert a['description'] == 'test_description'

    # Add Layer
    timeseries.add_layer(layer1)
    assert layer1.exists

    # Get Layer
    layer1b = timeseries.get_layer('test_layer')
    assert layer1b.exists
    assert layer1b.name == "test_layer"
    assert layer1.id == layer1b.id
    assert layer1._api.timeseries is not None

    # Add another layer
    layer2 = timeseries.add_layer('test_layer2','test_description2')
    assert layer2.exists

    layer2copy = timeseries.add_layer('test_layer2')
    assert layer2copy.id == layer2.id

    # Get Layer
    layer2b = timeseries.get_layer('test_layer2')
    assert layer2b.exists
    assert layer2b.name == "test_layer2"
    assert layer2.id == layer2b.id
    assert layer2._api.timeseries is not None

    layers = timeseries.layers
    assert len(layers)==2
    assert layers[0].name == "test_layer"
    assert layers[1].name == 'test_layer2'

    #Create channels
    ch = TimeSeriesChannel(
        name='test_channel',
        rate=256,
        unit='uV',
        start=1,
        end=60*1e6)

    #Create annotation over one channel
    # create
    timeseries.add_channels(ch)
    assert ch.exists
    assert ch.id in [x.id for x in timeseries.channels]
    annot = TimeSeriesAnnotation(label = 'test_label', channel_ids = timeseries.channels[0].id,start = timeseries.channels[0].start,end=timeseries.channels[0].start+1*1e6)
    #Add Annotation
    layer1.add_annotations(annot)
    assert annot.exists

    # get annotations
    annotb = layer1.annotations()
    print "annotations = ", annotb
    assert annotb[0].label == annot.label

    #Create annotation over multiple channels
    ch2 = TimeSeriesChannel(
        name='test_channel',
        rate=256,
        unit='uV',
        start=1,
        end=60*1e6)

    timeseries.add_channels(ch2)
    channels = timeseries.channels
    ch_ids = [x.id for x in channels]
    assert ch2.exists
    assert ch2.id in ch_ids
    assert ch.id in ch_ids
    for ch in channels:
        assert ch.rate == 256
        assert ch.exists

    #add annotation over two channels
    channel_ids = [timeseries.channels[x].id for x in range(len(timeseries.channels))]

    annot2 = layer1.insert_annotation(annotation='test_label2', channel_ids = channel_ids,
        start =timeseries.channels[0].start+1*1e6,
        end = timeseries.channels[0].start+2*1e6)


    assert annot2.exists

    annot_gen = layer1.iter_annotations(1)
    annot = annot_gen.next()
    assert annot[0].label == 'test_label'
    next_annot= annot_gen.next()
    assert next_annot[0].label == 'test_label2'


    ### TEST DELETION

    annot3 = TimeSeriesAnnotation(label= 'test_label3', channel_ids= channel_ids,start = timeseries.channels[0].start+1*1e6,end=timeseries.channels[0].start+2*1e6)
    #layer1.add_annotations([annot2,annot3])
    annot3 = timeseries.add_annotations(layer=layer1,annotations=annot3)
    assert annot3.exists
    annot3.delete()
    assert not annot3.exists

    annot4 = timeseries.insert_annotation(layer=layer1,annotation='test_label3',start= timeseries.channels[0].start+1*1e6,end=timeseries.channels[0].start+2*1e6)
    assert annot4.exists
    annot4.delete()
    assert not annot4.exists

    annot5 = timeseries.insert_annotation(layer='test_layer4',annotation='test_label3',start=timeseries.channels[0].start+1*1e6,end=timeseries.channels[0].start+2*1e6)
    assert annot5.exists
    annot5.delete()
    assert not annot5.exists

    layer1.add_annotations([annot2,annot3])
    assert annot2.exists
    assert annot3.exists

    #test datetime input
    annot4 = timeseries.insert_annotation(layer='test_layer4',annotation='test_label3',start= datetime.datetime.utcfromtimestamp((timeseries.channels[0].start+1*1e6)/1e6),end=datetime.datetime.utcfromtimestamp(timeseries.channels[0].start+2*1e6))
    assert annot4.exists
    annot4.delete()
    assert not annot4.exists

    layer = timeseries.get_layer('test_layer4')
    assert layer.exists
    layer.delete()
    assert not layer.exists


    # delete annotations
    annot[0].delete()
    assert not annot[0].exists

    assert timeseries.exists
    timeseries.delete_layer(layer1)
    assert not layer1.exists

    assert layer2.exists
    layer2.delete()
    assert not layer2.exists
コード例 #3
0
def test_timeseries_annotations(client, timeseries):
    assert timeseries.exists
    print('layers = ', timeseries.layers)

    #Create Layer
    layer1 = TimeSeriesAnnotationLayer(name="test_layer",
                                       time_series_id=timeseries.id,
                                       description="test_description")
    a = layer1.as_dict()
    assert a['name'] == 'test_layer'
    assert a['description'] == 'test_description'

    # Add Layer
    timeseries.add_layer(layer1)
    assert layer1.exists

    # Get Layer
    layer1b = timeseries.get_layer('test_layer')
    assert layer1b.exists
    assert layer1b.name == "test_layer"
    assert layer1.id == layer1b.id
    assert layer1._api.timeseries is not None

    # Add another layer
    layer2 = timeseries.add_layer('test_layer2', 'test_description2')
    assert layer2.exists

    layer2copy = timeseries.add_layer('test_layer2')
    assert layer2copy.id == layer2.id

    # Get Layer
    layer2b = timeseries.get_layer('test_layer2')
    assert layer2b.exists
    assert layer2b.name == "test_layer2"
    assert layer2.id == layer2b.id
    assert layer2._api.timeseries is not None

    layers = timeseries.layers
    assert len(layers) == 2
    assert layers[0].name == "test_layer"
    assert layers[1].name == 'test_layer2'

    #Create channels
    ch = TimeSeriesChannel(name='test_channel',
                           rate=256,
                           unit='uV',
                           start=1,
                           end=60 * 1e6)

    #Create annotation over one channel
    # create
    timeseries.add_channels(ch)
    assert ch.exists
    assert ch.id in [x.id for x in timeseries.channels]
    annot = TimeSeriesAnnotation(label='test_label',
                                 channel_ids=timeseries.channels[0].id,
                                 start=timeseries.channels[0].start,
                                 end=timeseries.channels[0].start + 1 * 1e6)
    #Add Annotation
    layer1.add_annotations(annot)
    assert annot.exists

    # get annotations
    annotb = layer1.annotations()
    assert annotb[0].label == annot.label

    annotc = client._api.timeseries.get_annotation(timeseries, layer1, annot)
    assert annotc.label == annot.label

    #Create annotation over multiple channels
    ch2 = TimeSeriesChannel(name='test_channel',
                            rate=256,
                            unit='uV',
                            start=1,
                            end=60 * 1e6)

    timeseries.add_channels(ch2)
    channels = timeseries.channels
    ch_ids = [x.id for x in channels]
    assert ch2.exists
    assert ch2.id in ch_ids
    assert ch.id in ch_ids
    for ch in channels:
        assert ch.rate == 256
        assert ch.exists

    #add annotation over two channels
    channel_ids = [
        timeseries.channels[x].id for x in range(len(timeseries.channels))
    ]

    annot2 = layer1.insert_annotation(
        annotation='test_label2',
        channel_ids=channel_ids,
        start=timeseries.channels[0].start + 1 * 1e6,
        end=timeseries.channels[0].start + 2 * 1e6)

    assert annot2.exists

    annot_gen = layer1.iter_annotations(1)
    annot = next(annot_gen)
    assert annot[0].label == 'test_label'
    next_annot = next(annot_gen)
    assert next_annot[0].label == 'test_label2'

    ### TEST ANNOTATION COUNTS
    layer1_expected_counts = [{
        'start': 0,
        'end': 250000,
        'value': 1.0
    }, {
        'start': 250000,
        'end': 500000,
        'value': 1.0
    }, {
        'start': 500000,
        'end': 750000,
        'value': 1.0
    }, {
        'start': 750000,
        'end': 1000000,
        'value': 1.0
    }, {
        'start': 1000000,
        'end': 1250000,
        'value': 2.0
    }, {
        'start': 1250000,
        'end': 1500000,
        'value': 1.0
    }, {
        'start': 1500000,
        'end': 1750000,
        'value': 1.0
    }, {
        'start': 1750000,
        'end': 2000000,
        'value': 1.0
    }, {
        'start': 2000000,
        'end': 2000001,
        'value': 1.0
    }]

    def _sort_counts(counts):
        return sorted(counts, key=lambda c: c['start'])

    assert _sort_counts(
        timeseries.annotation_counts(
            start=timeseries.channels[0].start * 1e6,
            end=timeseries.channels[0].start + 2 * 1e6,
            layers=[layer1],
            period="0.25s")[str(layer1.id)]) == layer1_expected_counts
    assert _sort_counts(
        layer1.annotation_counts(start=timeseries.channels[0].start * 1e6,
                                 end=timeseries.channels[0].start + 2 * 1e6,
                                 period="0.25s")[str(
                                     layer1.id)]) == layer1_expected_counts

    ### TEST DELETION

    annot3 = TimeSeriesAnnotation(label='test_label3',
                                  channel_ids=channel_ids,
                                  start=timeseries.channels[0].start + 1 * 1e6,
                                  end=timeseries.channels[0].start + 2 * 1e6)
    layer1.add_annotations([annot2, annot3])
    annot3 = timeseries.add_annotations(layer=layer1, annotations=annot3)
    assert annot3.exists
    annot3.delete()
    assert not annot3.exists

    annot4 = timeseries.insert_annotation(
        layer=layer1,
        annotation='test_label3',
        start=timeseries.channels[0].start + 1 * 1e6,
        end=timeseries.channels[0].start + 2 * 1e6)
    assert annot4.exists
    annot4.delete()
    assert not annot4.exists

    annot5 = timeseries.insert_annotation(
        layer='test_layer4',
        annotation='test_label3',
        start=timeseries.channels[0].start + 1 * 1e6,
        end=timeseries.channels[0].start + 2 * 1e6)
    assert annot5.exists
    annot5.delete()
    assert not annot5.exists

    layer1.add_annotations([annot2, annot3])
    assert annot2.exists
    assert annot3.exists

    # test datetime input
    annot4 = timeseries.insert_annotation(
        layer='test_layer4',
        annotation='test_label3',
        start=datetime.datetime.utcfromtimestamp(
            (timeseries.channels[0].start + 1 * 1e6) / 1e6),
        end=datetime.datetime.utcfromtimestamp(timeseries.channels[0].start +
                                               2 * 1e6))
    assert annot4.exists
    annot4.delete()
    assert not annot4.exists

    layer = timeseries.get_layer('test_layer4')
    assert layer.exists
    layer.delete()
    assert not layer.exists

    # delete annotations
    annot[0].delete()
    assert not annot[0].exists

    assert timeseries.exists
    timeseries.delete_layer(layer1)
    assert not layer1.exists

    assert layer2.exists
    layer2.delete()
    assert not layer2.exists