npTDMS

1.10.0 · active · verified Fri Apr 17

npTDMS is a cross-platform, NumPy-based Python module designed for reading TDMS files, a binary data format produced by LabVIEW and LabWindows/CVI from National Instruments. It provides efficient access to measurement data and metadata. The current version is 1.10.0, and the library maintains an active development cycle with frequent releases addressing bug fixes, performance improvements, and new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open a TDMS file using `TdmsFile` and iterate through its groups and channels to access metadata and data. It includes error handling for `FileNotFoundError` as a common issue. Replace `example.tdms` with the actual path to your TDMS file.

import os
from nptdms import TdmsFile

# Create a dummy .tdms file for demonstration if it doesn't exist
# In a real scenario, you would have an existing TDMS file.
# For this quickstart, we'll simulate opening one.
# Note: nptdms primarily reads existing files; writing requires TdmsWriter.

tdms_file_path = "example.tdms"
# Normally, you'd ensure this file exists. For a runnable example without actual file creation,
# we'll just demonstrate the read pattern.

try:
    # Open the TDMS file
    with TdmsFile(tdms_file_path) as tdms_file:
        print(f"Successfully opened TDMS file: {tdms_file_path}")
        
        # Iterate through all groups and channels
        print("\nAvailable Groups and Channels:")
        for group in tdms_file.groups():
            print(f"  Group: {group.name}")
            for channel in group.channels():
                print(f"    Channel: {channel.name} (Data Type: {channel.dtype})")
                # Access data for a channel
                # data = channel.data # Uncomment to load actual data
                # print(f"      Data sample: {data[:5]}...") # Print first 5 elements
                
        # Access a specific group and channel by name (replace with actual names from your file)
        # try:
        #     my_group = tdms_file['MyGroupName']
        #     my_channel = my_group['MyChannelName']
        #     channel_data = my_channel.data
        #     print(f"\nData from 'MyChannelName': {channel_data[:5]}...")
        # except KeyError:
        #     print("\n'MyGroupName' or 'MyChannelName' not found in the TDMS file.")

except FileNotFoundError:
    print(f"Error: The file '{tdms_file_path}' was not found. Please provide a valid TDMS file.")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →