[Python] How can I modify a .tdms file using Python?

Discussão em 'Python' iniciado por Stack, Dezembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    I have a .tdms file of a measurement with some faulty data. In group_name = "Tracking data", the channel_name = "Shift (mm)" is filled with gibberish.

    Therefore I want to replace the data in "Shift (mm)", while keeping the rest of the file intact as is. As I know nothing about LabView, I wanted to do this in Python using the module npTDMS.

    I tried to implement the following function that correctly writes the "Shift (mm)" into a new .tdms file, overwriting my input file :

    from nptdms import TdmsFile, TdmsWriter, RootObject, GroupObject, ChannelObject
    import numpy as np

    # File paths
    tdms_file_path = r"P:\My Documents\Images tweezer\data_011.tdms"
    replacement_file_path = r"Y:\Gert-Jan\Tweezer data\220407\shift.txt"

    # Group and channel details
    group_name = "Tracking data"
    channel_name = "Shift (mm)"

    # Load the replacement data
    replacement_data = np.loadtxt(replacement_file_path)

    # Write a new .tdms file containing the correct shift derived from replacement data
    with TdmsWriter(tdms_file_path) as tdms_writer:
    data_array = replacement_data
    channel = ChannelObject(group_name, channel_name, data_array)
    tdms_writer.write_segment([channel])


    I tried to modify this function into something that would retain the original data, using the following script (not working):

    from nptdms import TdmsFile, TdmsWriter, RootObject, GroupObject, ChannelObject
    import numpy as np

    # File paths
    tdms_file_path = r"P:\My Documents\Images tweezer\data_011.tdms"
    replacement_file_path = r"Y:\Gert-Jan\Tweezer data\220407\shift.txt"
    copy_tdms_file_path = r"P:\My Documents\Images tweezer\data_01_copy.tdms"

    # Group and channel details
    group_name = "Tracking data"
    channel_name = "Shift (mm)"

    # Load the replacement data
    replacement_data = np.loadtxt(replacement_file_path)

    # Original datafile properties
    original_file = TdmsFile(tdms_file_path)
    original_groups = original_file.groups()
    original_channels = [chan for group in original_groups for chan in group.channels()]


    # Write a new .tdms file containing the shift derived from replacement data
    with TdmsWriter(copy_tdms_file_path) as copied_file:
    root_object = RootObject(original_file.properties)
    channels_to_copy = [chan for chan in original_channels if not chan("Shift (mm)")]
    data_array = replacement_data
    channel = ChannelObject(group_name, channel_name, data_array)
    copied_file.write_segment([root_object] + original_groups + channels_to_copy + [channel])


    I have tried many variations of the last part of the code. All run into seemingly unrelated errors or do not produce the correct output file. Does someone know how to implement what I want to do in a correct way?

    A similar question has been asked here: Add channels to / edit existing tdms files?, but there is no answer for Python.

    Continue reading...

Compartilhe esta Página