asammdf
asammdf copied to clipboard
Empty channels --> UnboundLocalError: local variable 'index' referenced before assignment
Python version
('python=3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit ' '(AMD64)]') 'os=Windows-10-10.0.18362-SP0' 'numpy=1.20.3' 'asammdf=6.2.0'
Code
MDF version
4.10
Code snippet
df = self.mdf.to_dataframe(channel_list, time_from_zero=False)
Traceback
venv\lib\site-packages\asammdf\mdf.py:3686: in to_dataframe
signals = [sig for sig in signals if len(sig)]
if signals:
diffs = np.diff(group_master, prepend=-np.inf) > 0
if np.all(diffs):
index = pd.Index(group_master, tupleize_cols=False)
else:
idx = np.argwhere(diffs).flatten()
group_master = group_master[idx]
index = pd.Index(group_master, tupleize_cols=False)
for sig in signals:
sig.samples = sig.samples[idx]
sig.timestamps = sig.timestamps[idx]
size = len(index)
E UnboundLocalError: local variable 'index' referenced before assignment
Description
I have a group with no master channel and three empty channels. When I try to convert one or more of these channels with to_dataframe() I got the Error.
My explenation is this:
Because of signals = [sig for sig in signals if len(sig)] signals is an empty list.
Because of this the block after if signals: is skipped, so index is not assigned.
Same problem with a group with master and other empty channels. In this case I can avoid the error with the empty_channels="zeros" option.
Thank you and best regards
@mw25 please try the development branch code (6.3.0.dev39)
Thanks for the quick fix, it works.
Even though a group without a master and empty signals is probably a rare case, I have a suggestion for improvement.
My temporary solution from yesterday was:
try:
df = self.mdf.to_dataframe(channel_list, time_from_zero=False)
except UnboundLocalError:
df = DataFrame(columns=signal_names)
The advantage is that even if the signals do not contain any data, the data frame still shows which signals were queried and are empty.
What do you think, could you include the signal names in the data frame?
What do you think, could you include the signal names in the data frame?
The empty_channels argument should be used in this case
The fix of the UnboundLocalError is working in Version 6.3.0. Thank you.
But when I try to convert only empty channels, the Dataframe has no columns at all. The emty_channels argument doesn't change this.
I think because of line signals = [sig for sig in signals if len(sig)]. All empty signals are removed there and cannot get into df.
I don't know if you want to change this behaviour. My workaround that I can also live with is:
df = self.mdf.to_dataframe(channel_list, time_from_zero=False, empty_channels="zeros")
if len(df.columns) == 0:
df = DataFrame(columns=channel_names)