Esempio n. 1
0
def test_create_data_definitions():
    DataDefinitions(symbol='TDM_1',
                    entries=[
                        KeyValue('time', 't [s]'),
                        KeyValue('distance', 's(t) [m]'),
                        Comment('useless comment')
                    ])
Esempio n. 2
0
        def inject_into(self, stream):
            stream.set_protocol(cl1.LINE_BASED_PROTOCOL)

            stream.inject_packets([
                Header(delimiter='whitespace'),
                ReferenceSectionHeader(),
                Title('Title'),
                Creator('Creator'),
                Created('Timestamp'),
                Place('Place'),
                DataDefinitionsHeader(),
                KeyValue('distance', 'd [m]'),
                KeyValue('time', 't [s]'),
                DataHeader(),
                Comment('custom formatter test:'),
                Comment('d    t')
            ])
            # going to stream in custom protocol
            # the agent will find and use any registered adapters
            stream.set_protocol(MyNumpyProtocol)
            stream.inject_packet(np.random.uniform(size=(10, 2)))
            stream.inject_packet(np.random.uniform(size=(5, 2)))
            # going to stream in line based protocol again
            stream.set_protocol(cl1.LINE_BASED_PROTOCOL)
            stream.inject_packet(DataRow(("2.3", "4.555")))
            stream.inject_packet(DataRow(["1.2", "4.15"]))

            stream.inject_packet(END_FMF)
def test_inject_columns(lhrstream):
    from pyfmflib.cl1 import (DataColumn, FLUSH_DATA_COLUMNS,
                              format_data_column, DataDefinitionsHeader,
                              KeyValue, DataHeader, Comment)
    # we are still in line based protocol
    lhrstream.inject_packets([
        DataDefinitionsHeader(),
        KeyValue('distance', 'd [m]'),
        KeyValue('time', 't [s]'),
        DataHeader(),
        Comment('d    t'),
    ])

    # we can also use a formatter for columns
    fmt = lambda x: '%.2f' % (x, )

    lhrstream.set_protocol(cl1.DATA_COLUMN_PROTOCOL)
    lhrstream.inject_packets([
        DataColumn(("1.2", "1.3", "1.5", "1.6")),
        format_data_column((2.2, 2.3, 2.5, 2.6), fmt),
        # FLUSH_DATA_COLUMNS is a special packet that triggers translation to
        # rows in the according adapter for all previously accumulated columns
        FLUSH_DATA_COLUMNS,
        DataColumn(("3.2", )),
        DataColumn(("5.1", )),
        FLUSH_DATA_COLUMNS
    ])
    # don't forget to reset protocol
    lhrstream.set_protocol(cl1.LINE_BASED_PROTOCOL)
Esempio n. 4
0
def test_inject_meta_section(lhrstream):
    lhrstream.inject_packets(
        [
            MetaSectionHeader('My Section'),
            KeyValue('x', '1.3'),
            KeyValue('important parameter', r'\eta = 1.24e-9 mW'),
            KeyValue('s', '"string value"')
            ]
        )
Esempio n. 5
0
def test_equality():
    assert MetaSection(identifier='foo',
                       entries=[KeyValue('1', '2'),
                                Comment('bar')
                                ]) == MetaSection(identifier='foo',
                                                  entries=[
                                                      KeyValue('1', '2'),
                                                      Comment('bar')
                                                  ])
Esempio n. 6
0
def test_create_meta_section():
    MetaSection(identifier='my metadata section',
                entries=[
                    Comment('this is a metadata section'),
                    KeyValue('important parameter', '\Gamma = 0.137e-12 J/K'),
                    Comment('useless comment'),
                    KeyValue('x', '2'),
                    Comment('All but global comments that occur'),
                    Comment('before the first section belong to a section.')
                ])
Esempio n. 7
0
def test_inject_anonymous_table(lhrstream):
    lhrstream.inject_packets(
        [
            DataDefinitionsHeader(),
            KeyValue('distance', 'd [m]'),
            KeyValue('time', 't [s]'),

            DataHeader(),
            Comment('d    t'),
            DataRow(("2.3", "4.555")),
            DataRow(("1.2", "4.15"))
            ]
        )
Esempio n. 8
0
 def inject_into(self, stream):
     stream.set_protocol(cl1.LINE_BASED_PROTOCOL)
     stream.inject_packets([
         Header(),
         ReferenceSectionHeader(),
         Title('Title'),
         Creator('Creator'),
         Created('Timestamp'),
         Place('Place'),
         MetaSectionHeader('My Section'),
         KeyValue('x', '1.3'),
         KeyValue('important parameter', r'\eta = 1.24e-9 mW'),
         TableDefinitionsHeader(),
         KeyValue('table1', 't_1'),
         KeyValue('table2', 't_2'),
         DataDefinitionsHeader('t_1'),
         KeyValue('distance', 'd [m]'),
         KeyValue('time', 't [s]'),
         DataHeader('t_1'),
         Comment('d    t'),
         DataRow(("2.3", "4.555")),
         DataRow(("1.2", "4.15")),
         DataDefinitionsHeader('t_2'),
         KeyValue('Energy', 'E [J]'),
         KeyValue('distance', 'd [m]'),
         DataHeader('t_2'),
     ] + columns2rows((
         DataColumn(("1.2", "1.3", "1.5", "1.6")),
         DataColumn(("2.2", "2.3", "2.5", "2.6")),
     )) + [END_FMF])
Esempio n. 9
0
def test_add_single_anonymous_table(empty_fmf):
    from pyfmflib.cl1 import DataDefinitions, Data, KeyValue, Comment, DataRow
    empty_fmf.table_sections = [
        DataDefinitions(
            symbol=None,  # default value, just to illustrate
            entries=[
                KeyValue('time', 't [s]'),
                KeyValue('distance', 's(t) [m]'),
                Comment('useless comment')
            ]),
        Data(symbol=None,
             entries=[
                 Comment('t / s    s(t) / m'),
                 DataRow(('0', '-2')),
                 DataRow(('1.1', '0.1')),
                 DataRow(('2.0', '3.1')),
                 DataRow(('2.9', '4.5')),
             ])
    ]
Esempio n. 10
0
def test_add_multiple_tables(empty_fmf):
    from pyfmflib.cl1 import (TableDefinitions, DataDefinitions, Data,
                              KeyValue, DataRow)
    empty_fmf.table_sections = [
        TableDefinitions([
            KeyValue('Table 1', 't_1'),
            KeyValue('Table 2', 't_2'),
        ]),
        DataDefinitions(symbol='t_1',
                        entries=[
                            KeyValue('time', 't [s]'),
                            KeyValue('distance', 's(t) [m]'),
                        ]),
        Data(symbol='t_1',
             entries=[
                 DataRow(('0', '-2')),
                 DataRow(('1.1', '0.1')),
                 DataRow(('2.0', '3.1')),
                 DataRow(('2.9', '4.5'))
             ]),
        DataDefinitions(symbol='t_2',
                        entries=[
                            KeyValue('another time', 't [ms]'),
                            KeyValue('another distance', 's(t) [mum]'),
                        ]),
        Data(symbol='t_2',
             entries=[DataRow(('1', '2')),
                      DataRow(('2.1', '3.4'))])
    ]
Esempio n. 11
0
def fmf_template_with_tables():
    from pyfmflib.cl1 import (TableDefinitions, DataDefinitions, Data,
                              KeyValue, DataRow, Comment)
    fmf = cl1.new_fmf_template()
    fmf.table_sections = [
        TableDefinitions([
            Comment('A comment'),
            KeyValue('Table 1', 't_1'),
            KeyValue('Table 2', 't_2'),
        ]),
        DataDefinitions(symbol='t_1',
                        entries=[
                            KeyValue('time', 't [s]'),
                            Comment('Another comment'),
                            KeyValue('distance', 's(t) [m]'),
                        ]),
        Data(symbol='t_1',
             entries=[
                 DataRow(('0', '-2')),
                 DataRow(('1.1', '0.1')),
                 DataRow(('2.0', '3.1')),
                 Comment('Yet another comment'),
                 DataRow(('2.9', '4.5'))
             ]),
        DataDefinitions(symbol='t_2',
                        entries=[
                            KeyValue('another time', 't [ms]'),
                            KeyValue('another distance', 's(t) [mum]'),
                        ]),
        Data(symbol='t_2',
             entries=[DataRow(('1', '2')),
                      DataRow(('2.1', '3.4'))])
    ]
    return fmf
Esempio n. 12
0
def test_create_whole_fmf():
    from pyfmflib.cl1 import (FMF, Header, Comment, MetaSection, KeyValue,
                              DataDefinitions, Data, DataRow, ReferenceSection,
                              Creator, Created, Title, Place)
    FMF(header=Header(),
        global_comments=[Comment('A comment'),
                         Comment('Another Comment')],
        meta_sections=[
            ReferenceSection([
                Creator('Creator'),
                Comment('A Comment'),
                Created('Timestamp'),
                Title('Title'),
                Place('Place')
            ]),
            MetaSection(identifier='A Section',
                        entries=[Comment('A comment'),
                                 KeyValue('a', 'b')]),
            MetaSection(identifier='Another Section',
                        entries=[KeyValue('c', 'd')]),
        ],
        table_sections=[
            DataDefinitions(entries=[
                KeyValue('time', 't [s]'),
                KeyValue('distance', 's(t) [m]'),
                Comment('useless comment')
            ]),
            Data(symbol=None,
                 entries=[
                     Comment('t / s    s(t) / m'),
                     DataRow(('0', '-2')),
                     DataRow(('1.1', '0.1')),
                     DataRow(('2.0', '3.1')),
                     DataRow(('2.9', '4.5')),
                 ])
        ])
Esempio n. 13
0
def test_whole_fmf(lstream):
    lstream.inject_packets(
        [
            Header(),

            ReferenceSectionHeader(),
            Title('Title'),
            Creator('Creator'),
            Created('Timestamp'),
            Place('Place'),

            MetaSectionHeader('My Section'),
            KeyValue('x', '1.3'),
            KeyValue('important parameter', r'\eta = 1.24e-9 mW'),

            TableDefinitionsHeader(),
            KeyValue('table1', 't_1'),
            KeyValue('table2', 't_2'),

            DataDefinitionsHeader('t_1'),
            KeyValue('distance', 'd [m]'),
            KeyValue('time', 't [s]'),

            DataHeader('t_1'),
            Comment('d    t'),
            DataRow(("2.3", "4.555")),
            DataRow(("1.2", "4.15")),

            DataDefinitionsHeader('t_2'),
            KeyValue('Energy', 'E [J]'),
            KeyValue('distance', 'd [m]'),

            DataHeader('t_2')
            ]
        )
    lstream.inject_packets(
        columns2rows(
            (
                DataColumn(("1.2", "1.3", "1.5", "1.6")),
                DataColumn(("2.2", "2.3", "2.5", "2.6"))
                )
            ) + [END_FMF]
        )
Esempio n. 14
0
def test_inject_tables(lhrstream):
    lhrstream.inject_packets(
        [
            TableDefinitionsHeader(),
            KeyValue('table1', 't_1'),
            KeyValue('table2', 't_2'),

            DataDefinitionsHeader('t_1'),
            KeyValue('distance', 'd [m]'),
            KeyValue('time', 't [s]'),

            DataHeader('t_1'),
            Comment('d    t'),
            DataRow(("2.3", "4.555")),
            DataRow(("1.2", "4.15")),

            DataDefinitionsHeader('t_2'),
            KeyValue('Energy', 'E [J]'),
            KeyValue('distance', 'd [m]'),

    # data streaming examples:
    # ------------------------

            DataHeader('t_2'),
            DataRow(("2.3", "1.33")),
            DataRow(["100.2", "44.2"]),

            format_data_row((2.3, 4.555), lambda x: '%.2f' % (x, ))
            ]
        )
    # Stream columns that are all known at a time.
    # For sequential streaming see test_column_data_protocol.py
    lhrstream.inject_packets(
        cl1.columns2rows(
            (
                cl1.DataColumn(("1.0", "3.4", "5.2")),
                cl1.DataColumn(("2.0", "2.4", "2.2")),
                )
            )
        )

    # stream nested iterable
    lhrstream.inject_packets(
        cl1.rowmajor2rows(
            (
                ('0', '-2'),
                ('1.1', '0.1'),
                ('2.0', '3.1'),
                ('2.9', '4.5')
                )
            )
        )

    # use a formatter:
    lhrstream.inject_packets(
        cl1.columnmajor2rows(
            (
                (0, 1.1, 2.0, 2.9),
                (-2, 0.1, 3.1, 4.5)
                ),
            formatter=lambda x: '%.2f' % (x, )
            )
        )
Esempio n. 15
0
def test_append_to_meta_section_by_key(meta_section):
    # this should append at the end of the section
    old_len = len(meta_section.entries)
    meta_section['an unused key'] = 'a new value'
    assert len(meta_section.entries) == old_len + 1
    assert meta_section.entries[-1] == KeyValue('an unused key', 'a new value')
Esempio n. 16
0
def test_create_anonymous_data_definitions():
    DataDefinitions(
        symbol=None,  # default value, just to illustrate
        entries=[KeyValue('time', 't [s]')])
Esempio n. 17
0
def test_whole_fmf_raw_protocol(lstream):

    lstream.inject_packets(
        [
            Header(),

            Comment('A comment'),

            SectionHeader('*reference'),
            KeyValue('title', 'Title'),
            KeyValue('creator', 'Creator'),
            KeyValue('created', 'Timestamp'),
            KeyValue('place', 'Place'),

            SectionHeader('My Section'),
            KeyValue('x', '1.3'),
            KeyValue('important parameter', r'\eta = 1.24e-9 mW'),

            SectionHeader('*table definitions'),

            KeyValue('table1', 't_1'),
            KeyValue('table2', 't_2'),

            SectionHeader('*data definitions: t_1'),
            KeyValue('distance', 'd [m]'),
            KeyValue('time', 't [s]'),

            SectionHeader('*data: t_1'),
            Comment('d    t'),
            DataRow(("2.3", "4.555")),
            DataRow(("1.2", "4.15")),

            SectionHeader('*data definitions: t_2'),
            KeyValue('Energy', 'E [J]'),
            KeyValue('distance', 'd [m]'),

            SectionHeader('*data: t_2')
            ]
        )

    lstream.inject_packets(
        cl1.columns2rows(
            (
                cl1.DataColumn(("1.2", "1.3", "1.5", "1.6")),
                cl1.DataColumn(("2.2", "2.3", "2.5", "2.6"))
                )
            )
        )

    lstream.inject_packet(END_FMF)
Esempio n. 18
0
def test_create_table_definitions():
    TableDefinitions([
        Comment('just to illustrate'),
        KeyValue('time distance measurement 1', 'TDM_1')
    ])