MDF4

asammdf tries to emulate the mdf structure using Python builtin data types.

The header attibute is an OrderedDict that holds the file metadata.

The groups attribute is a dictionary list with the following keys:

  • data_group : DataGroup object

  • channel_group : ChannelGroup object

  • channels : list of Channel objects with the same order as found in the mdf file

  • channel_conversions : list of ChannelConversion objects in 1-to-1 relation with the channel list

  • channel_sources : list of SourceInformation objects in 1-to-1 relation with the channels list

  • data_block : DataBlock object

  • texts : dictionay containing TextBlock objects used throughout the mdf

    • channels : list of dictionaries that contain TextBlock objects ralated to each channel

      • name_addr : channel name
      • comment_addr : channel comment
    • channel group : list of dictionaries that contain TextBlock objects ralated to each channel group

      • acq_name_addr : channel group acquisition comment
      • comment_addr : channel group comment
    • conversion_tab : list of dictionaries that contain TextBlock objects related to TABX and RTABX channel conversions

      • text_{n} : n-th text of the VTABR conversion
      • default_addr : default text
    • conversions : list of dictionaries that containt TextBlock obejcts related to channel conversions

      • name_addr : converions name
      • unit_addr : channel unit_addr
      • comment_addr : converison comment
      • formula_addr : formula text; only valid for algebraic conversions
    • sources : list of dictionaries that containt TextBlock obejcts related to channel sources

      • name_addr : source name
      • path_addr : source path_addr
      • comment_addr : source comment

The file_history attribute is a list of (FileHistory, TextBlock) pairs .

The channel_db attibute is a dictionary that holds the (data group index, channel index) pair for all signals. This is used to speed up the get_signal_by_name method.

The master_db attibute is a dictionary that holds the channel index of the master channel for all data groups. This is used to speed up the get_signal_by_name method.

API

class asammdf.mdf4.MDF4(name=None, memory='full', version='4.10')

If the name exist it will be memoryed otherwise an empty file will be created that can be later saved to disk

Parameters:

name : string

mdf file name

memory : str

memory optimization option; default full

  • if full the data group binary data block will be memoryed in RAM
  • if low the channel data is read from disk on request, and the

metadata is memoryed into RAM * if minimum only minimal data is memoryed into RAM

version : string

mdf file version (‘4.00’, ‘4.10’, ‘4.11’); default ‘4.10’

Attributes

name (string) mdf file name
groups (list) list of data groups
header (HeaderBlock) mdf file header
file_history (list) list of (FileHistory, TextBlock) pairs
comment (TextBlock) mdf file comment
identification (FileIdentificationBlock) mdf file start block
memory (str) memory optimization option
version (str) mdf version
channels_db (dict) used for fast channel access by name; for each name key the value is a list of (group index, channel index) tuples
masters_db (dict) used for fast master channel access; for each group index key the value is the master channel index

Methods

append(signals[, source_info, common_timebase]) Appends a new data group.
attach(data[, file_name, comment, …]) attach embedded attachment as application/octet-stream
close() if the MDF was created with memory=False and new
extract_attachment(index) extract attachemnt index data. If it is an embedded attachment,
get([name, group, index, raster, …]) Gets channel samples.
get_channel_comment([name, group, index]) Gets channel comment.
get_channel_unit([name, group, index]) Gets channel unit.
get_master(index[, data]) returns master channel samples for given group
info() get MDF information as a dict
save([dst, overwrite, compression]) Save MDF to dst.
append(signals, source_info='Python', common_timebase=False)

Appends a new data group.

For channel depencies type Signals, the samples attribute must be a numpy.recarray

Parameters:

signals : list

list on Signal objects

source_info : str

source information; default ‘Python’

common_timebase : bool

flag to hint that the signals have the same timebase

Examples

>>> # case 1 conversion type None
>>> s1 = np.array([1, 2, 3, 4, 5])
>>> s2 = np.array([-1, -2, -3, -4, -5])
>>> s3 = np.array([0.1, 0.04, 0.09, 0.16, 0.25])
>>> t = np.array([0.001, 0.002, 0.003, 0.004, 0.005])
>>> names = ['Positive', 'Negative', 'Float']
>>> units = ['+', '-', '.f']
>>> info = {}
>>> s1 = Signal(samples=s1, timstamps=t, unit='+', name='Positive')
>>> s2 = Signal(samples=s2, timstamps=t, unit='-', name='Negative')
>>> s3 = Signal(samples=s3, timstamps=t, unit='flts', name='Floats')
>>> mdf = MDF3('new.mdf')
>>> mdf.append([s1, s2, s3], 'created by asammdf v1.1.0')
>>> # case 2: VTAB conversions from channels inside another file
>>> mdf1 = MDF3('in.mdf')
>>> ch1 = mdf1.get("Channel1_VTAB")
>>> ch2 = mdf1.get("Channel2_VTABR")
>>> sigs = [ch1, ch2]
>>> mdf2 = MDF3('out.mdf')
>>> mdf2.append(sigs, 'created by asammdf v1.1.0')
attach(data, file_name=None, comment=None, compression=True, mime='application/octet-stream')

attach embedded attachment as application/octet-stream

Parameters:

data : bytes

data to be attached

file_name : str

string file name

comment : str

attachment comment

compression : bool

use compression for embedded attachment data

mime : str

mime type string

close()

if the MDF was created with memory=False and new channels have been appended, then this must be called just before the object is not used anymore to clean-up the temporary file

extract_attachment(index)

extract attachemnt index data. If it is an embedded attachment, then this method creates the new file according to the attachemnt file name information

Parameters:

index : int

attachment index

Returns:

data : bytes | str

attachment data

get(name=None, group=None, index=None, raster=None, samples_only=False, data=None)

Gets channel samples. Channel can be specified in two ways:

  • using the first positional argument name

    • if there are multiple occurances for this channel then the

    group and index arguments can be used to select a specific group. * if there are multiple occurances for this channel and either the group or index arguments is None then a warning is issued

  • using the group number (keyword argument group) and the channel

number (keyword argument index). Use info method for group and channel numbers

If the raster keyword argument is not None the output is interpolated accordingly

Parameters:

name : string

name of channel

group : int

0-based group index

index : int

0-based channel index

raster : float

time raster in seconds

samples_only : bool

if True return only the channel samples as numpy array; if False return a Signal object

Returns:

res : (numpy.array | Signal)

returns Signal if samples_only*=*False (default option), otherwise returns numpy.array The Signal samples are:

  • numpy recarray for channels that have composition/channel

array address or for channel of type BYTEARRAY, CANOPENDATE, CANOPENTIME * numpy array for all the rest

Raises:

MdfError :

* if the channel name is not found

* if the group index is out of range

* if the channel index is out of range

get_channel_comment(name=None, group=None, index=None)

Gets channel comment. Channel can be specified in two ways:

  • using the first positional argument name

    • if there are multiple occurances for this channel then the

    group and index arguments can be used to select a specific group. * if there are multiple occurances for this channel and either the group or index arguments is None then a warning is issued

  • using the group number (keyword argument group) and the channel

number (keyword argument index). Use info method for group and channel numbers

If the raster keyword argument is not None the output is interpolated accordingly.

Parameters:

name : string

name of channel

group : int

0-based group index

index : int

0-based channel index

Returns:

comment : str

found channel comment

get_channel_unit(name=None, group=None, index=None)

Gets channel unit. Channel can be specified in two ways:

  • using the first positional argument name

    • if there are multiple occurances for this channel then the

    group and index arguments can be used to select a specific group. * if there are multiple occurances for this channel and either the group or index arguments is None then a warning is issued

  • using the group number (keyword argument group) and the channel

number (keyword argument index). Use info method for group and channel numbers

If the raster keyword argument is not None the output is interpolated accordingly.

Parameters:

name : string

name of channel

group : int

0-based group index

index : int

0-based channel index

Returns:

unit : str

found channel unit

get_master(index, data=None)

returns master channel samples for given group

Parameters:

index : int

group index

data : bytes

data block raw bytes; default None

Returns:

t : numpy.array

master channel samples

info()

get MDF information as a dict

Examples

>>> mdf = MDF4('test.mdf')
>>> mdf.info()
save(dst='', overwrite=None, compression=0)

Save MDF to dst. If dst is not provided the the destination file name is the MDF name. If overwrite is True then the destination file is overwritten, otherwise the file name is appened with ‘_<cntr>’, were ‘<cntr>’ is the first conter that produces a new file name (that does not already exist in the filesystem)

Parameters:

dst : str

destination file name, Default ‘’

overwrite : bool

overwrite flag, default False

compression : int

use compressed data blocks, default 0; valid since version 4.10

  • 0 - no compression
  • 1 - deflate (slower, but produces smaller files)
  • 2 - transposition + deflate (slowest, but produces the smallest

files)