danielhrisca / asammdf

Fast Python reader and editor for ASAM MDF / MF4 (Measurement Data Format) files
GNU Lesser General Public License v3.0
612 stars 216 forks source link

ChannelsType does not accept single channel name strings #950

Closed mipro98 closed 7 months ago

mipro98 commented 7 months ago

Python version

'python=3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]'
'os=Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.35'
'numpy=1.26.1'
ldf is not supported
xls is not supported
xlsx is not supported
yaml is not supported
'asammdf=7.3.14'

Code

MDF version

4.10

Code snippet

with MDF(filename) as mdf_file:
    df = mdf_file.to_dataframe("someExistingChannelName")

Traceback

Traceback (most recent call last):
  File "parse_mf4.py", line 83, in <module>
    df: DataFrame = get_df(logfile, "someExistingChannelName")
  File "parse_mf4.py", line 69, in get_df
    return mdf_file.to_dataframe(channel_name)
  File "venv/lib/python3.10/site-packages/asammdf/mdf.py", line 4535, in to_dataframe
    mdf = self.filter(channels)
  File "venv/lib/python3.10/site-packages/asammdf/mdf.py", line 2146, in filter
    gps = self.included_channels(channels=channels)
  File "venv/lib/python3.10/site-packages/asammdf/blocks/mdf_v4.py", line 8134, in included_channels
    group, idx = self._validate_channel_selection(name)
  File "venv/lib/python3.10/site-packages/asammdf/blocks/mdf_common.py", line 87, in _validate_channel_selection
    raise MdfException(f'Channel "{name}" not found')
asammdf.blocks.utils.MdfException: Channel "s" not found

Description

When passing a single string as the channels parameter to MDF.to_dataframe() or MDF.filter(), an error is returned that the channel cannot be found. This is due to the fact, that the channels parameter is iterated in blocks/mdf_v4.included_channels() which results in iterating the string by its characters and leads to searching for a channel name with only the first letter of the provided string.

Of course, if you take a look at the definition of ChannelType, you see that it accepts a Sequence[str] rather than a str, but the documentation clearly states, that a single channel name string is allowed:

channels : list list of items to be filtered (default None); each item can be :

  • a channel name string
  • (channel name, group index, channel index) list or tuple
  • (channel name, group index) list or tuple
  • (None, group index, channel index) list or tuple

So it might be useful to change the documentation in that regard.

mipro98 commented 7 months ago

Well, I just noticed that the datatype in the documentation clearly states that channels has to be a list (tuples are nested into that list) so I just didn't read the documentation well enough.