def test_inject_comments(lhstream): lhstream.inject_packets( [ Comment('a comment'), Comment('another comment') ] )
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 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
def test_equality(): assert MetaSection(identifier='foo', entries=[KeyValue('1', '2'), Comment('bar') ]) == MetaSection(identifier='foo', entries=[ KeyValue('1', '2'), Comment('bar') ])
def test_create_data_from_nested_iterable(): Data( symbol='TDM_1', entries=[ Comment('t / s s(t) / m'), Comment('here be data'), ] + rowmajor2rows( # row major order "C-style" (('0', '-2'), ('1.1', '0.1'), ('2.0', '3.1'), ('2.9', '4.5'))) + columnmajor2rows( # column major order "Fortran-style" (('0', '1.1', '2.0', '2.9'), ('-2', '0.1', '3.1', '4.5'))))
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.') ])
def test_create_data_from_columns(): # DataColumn accepts any iterable with string elements Data( symbol='TDM_1', entries=[ Comment('t / s s(t) / m'), Comment('here be columns'), ] + columns2rows((DataColumn( ('0', '1.1', '2.0', '2.9')), DataColumn( ('-2', '0.1', '3.1', '4.5')))) + [ Comment('here be rows'), DataRow(('3.2', '6.7')) # we can mix rows and columns ])
def test_create_data_from_custom_source(): # formatter is called for every cell, replacing the cell # formatter also available for: # columnmajor2rows, DataRow and DataColumn # # for more elaborate examples see test_streaming Data( symbol='TDM_1', entries=[ Comment('t / s s(t) / m'), Comment('here be data from custom source') ] + rowmajor2rows(((0, -2), (1.1, 0.1), (2.0, 3.1), (2.9, 4.5)), formatter=lambda x: '%.1f' % (x, )), )
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)
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])
def test_create_data_definitions(): DataDefinitions(symbol='TDM_1', entries=[ KeyValue('time', 't [s]'), KeyValue('distance', 's(t) [m]'), Comment('useless comment') ])
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)
def test_create_data(): # DataRow accepts any iterable with string elements Data(symbol='TDM_1', entries=[ Comment('t / s s(t) / m'), DataRow(('0', '-2')), DataRow(['1.1', '0.1']), DataRow(set(('2.0', '3.1'))), DataRow(iter(('2.9', '4.5'))), ])
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')), ]) ]
def test_inject_reference_section(lhstream): lhstream.inject_packets( [ ReferenceSectionHeader(), Title('Title'), Creator('Creator'), Comment('Invalid time stamp:'), Created('just now'), Place('Place'), ] )
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")) ] )
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] )
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')), ]) ])
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, ) ) )
def test_add_comments_before_first_section(fmf_template): from pyfmflib.cl1 import Comment fmf_template.global_comments = [Comment('Comment 1'), Comment('Comment 2')]
def test_insert_comment_into_meta_section(meta_section): meta_section.entries.insert( 2, Comment('A comment between the key value entries'))
def test_create_table_definitions(): TableDefinitions([ Comment('just to illustrate'), KeyValue('time distance measurement 1', 'TDM_1') ])