Linked Questions

Popular Questions

I am setting up a data processing script for a short research paper and have trouble with the npTDMS package. I set up the plugin successfully in spyder and already read an example .tdms file I got.

from nptdms import TdmsFile 
from pathlib import Path
import numpy as np
data_folder = Path(r"C://Users/brue_sb/Studienarbeit/Beispieldaten/Test1-10/")
filename = data_folder / "S1.tdms"
tdms_file = TdmsFile.read(filename)

Labview records 5 channels of data only in the first group of the .tdms file. However my code can only call the first 4 channels. I have not yet been able to get the data from the 5th channel ("Untitled 4" instead of "Untitled 3") to be read.

for group in tdms_file.groups() :
    group_name = group.name
    
    for channel in group.channels():
        print(channel)
        channel_name = channel.name
        #print(channel_name)
        # Access dictionary of properties:
        properties = channel.properties
        # Access numpy array of data for channel:
        data = channel[:]

channel = tdms_file["Untitled"]["Untitled 3"]

I set up the script similar to the package documentation and also tried having ChatGPT write a simple script resulting in exactly same problem: the last channel is not taken into account. I have no idea what I am doing wrong or if it is the file or something else that is not according to specs. Getting the data from all the other channels Untitled, Untitled 1,... works but calling the 5th channel the same way by its title Untitled 4 results in an error message saying:


  File ~\Anaconda3\envs\spyder-env\Lib\site-packages\nptdms\tdms.py:407 in __getitem__
    return self._channels[channel_name]

KeyError: 'Untitled 4'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File ~\Anaconda3\envs\spyder-env\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\brue_sb\.spyder-py3\tdms-auswertung.py:30
    channel = tdms_file["Untitled"]["Untitled 4"]

  File ~\Anaconda3\envs\spyder-env\Lib\site-packages\nptdms\tdms.py:409 in __getitem__
    raise KeyError(

KeyError: "There is no channel named 'Untitled 4' in group 'Untitled' of the TDMS file"

When opened in Excel the file clearly contains 5 channels, Untitled 4 is just simply not being read:

Excel view of the file

Second view of the data

Somehow python does not recognize the fifth channel. When pasting the channel names in the for-loop getting all the channels in the group, Untitled 4 is missing:

runfile('C:/Users/brue_sb/.spyder-py3/TDMS-auswertung.py', wdir='C:/Users/brue_sb/.spyder-py3')
[<TdmsGroup with path /'Untitled'>]
<TdmsChannel with path /'Untitled'/'Untitled'>
<TdmsChannel with path /'Untitled'/'Untitled 1'>
<TdmsChannel with path /'Untitled'/'Untitled 2'>
<TdmsChannel with path /'Untitled'/'Untitled 3'>

Unfortunately I do not have another file with multiple channels on hand yet. Other files with just a single channel work just fine, I have tried those already.

Edit: tdms file attached on this cloud folder https://cloud.tu-braunschweig.de/s/GQEBdMGF9Qo3R5o

Edit2: This is the code line that is causing

from nptdms import TdmsFile
from pathlib import Path
import numpy as np
data_folder = Path(r"C://Users/brue_sb/Studienarbeit/Beispieldaten/Test1-10/")
filename = data_folder / "S1.tdms"
tdms_file = TdmsFile.read(filename)
groupcount = tdms_file.groups()
print(groupcount)
#data = np.arange(10).reshape(2,5)
#print(data)
for group in tdms_file.groups() :
    group_name = group.name
    
    for channel in group.channels():
        print(channel)
        channel_name = channel.name
        #print(channel_name)
        # Access dictionary of properties:
        properties = channel.properties
        # Access numpy array of data for channel:
        data = channel[:]

channel = tdms_file["Untitled"]["Untitled 4"]

Causing this outout:

runfile('C:/Users/brue_sb/.spyder-py3/TDMS-auswertung.py', wdir='C:/Users/brue_sb/.spyder-py3')
[<TdmsGroup with path /'Untitled'>]
<TdmsChannel with path /'Untitled'/'Untitled'>
<TdmsChannel with path /'Untitled'/'Untitled 1'>
<TdmsChannel with path /'Untitled'/'Untitled 2'>
<TdmsChannel with path /'Untitled'/'Untitled 3'>
Traceback (most recent call last):

  File ~\Anaconda3\envs\spyder-env\Lib\site-packages\nptdms\tdms.py:407 in __getitem__
    return self._channels[channel_name]

KeyError: 'Untitled 4'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File ~\Anaconda3\envs\spyder-env\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\brue_sb\.spyder-py3\tdms-auswertung.py:30
    channel = tdms_file["Untitled"]["Untitled 4"]

  File ~\Anaconda3\envs\spyder-env\Lib\site-packages\nptdms\tdms.py:409 in __getitem__
    raise KeyError(

KeyError: "There is no channel named 'Untitled 4' in group 'Untitled' of the TDMS file"

The first 4 channels are successfully read by the for loop, the 5th one isn't though. When calling the 5th channel explicitly again in the last row python cannot find a 5th channel. @kosist: what code did you run to get all channels?

Related Questions