pythonasammdf

load specific channels from mdf (.mf4) file


I am loading many large .mf4 files using the package asammdf. These files have many channels that I am not interested in and the input is the bottleneck of my program. So my question is:

Can I somehow only load specific channels from the file to speed up the input process?

What I am doing now is to load the whole file and specify the channels during the conversion to a pandas Dataframe.

from asammdf import MDF

if __name__ == '__main__':
    path = 'C:/path/to/some/file.mf4'
    mdfObj = MDF(path, version='4.10')
    columns=['A','B','C']
    df = mdfObj.to_dataframe(channels=columns)

Solution

  • You are already using the right methods. When the file is opened only the metadata is loaded, and the actual channel samples are only read as needed (when you call to_dataframe).

    The bottleneck might be the fact that you do not provide the raster argument (see the documentation here https://asammdf.readthedocs.io/en/latest/api.html#asammdf.mdf.MDF.to_dataframe) and in this case the output dataframe will be build using the union of all time stamp of the selected channels and doing interpolation for all the columns. Play around with the raster argument

    from asammdf import MDF
    
    if __name__ == '__main__':
        path = 'C:/path/to/some/file.mf4'
        mdfObj = MDF(path, version='4.10')
        columns=['A','B','C']
        # 0.1s
        df = mdfObj.to_dataframe(channels=columns, raster=0.1)
        # or use the time stamps of channel 'A'
        df = mdfObj.to_dataframe(channels=columns, raster='A')  
    

    EDIT: @Jonas Since version 6.1.0 it is possible to make a selective file load by using the channels argument https://asammdf.readthedocs.io/en/development/tips.html#selective-channel-loading

    from asammdf import MDF
    
    if __name__ == '__main__':
        path = 'C:/path/to/some/file.mf4'
        
        columns=['A','B','C']
        
        mdfObj = MDF(path, channels=columns)