systole icon indicating copy to clipboard operation
systole copied to clipboard

TypeError: tuple indices must be integers or slices, not str

Open MeisterP opened this issue 3 years ago • 5 comments
trafficstars

After updating to v0.2.3 (September 2022) I get the following error when calling correct_rr(rr):

Cleaning the RR interval time series.
... correcting 3 missed interval(s).
... correcting 1 extra interval(s).
... correcting 89 ectopic interval(s).
... correcting 5 short interval(s).
... correcting 5 long interval(s).
Traceback (most recent call last):
  File "<string>", line 79, in <module>
TypeError: tuple indices must be integers or slices, not str

While trying to disable things one by one to debug (eg. correct_rr(rr, missed_correction=False) I got:

Cleaning the RR interval time series.
... correcting 1 extra interval(s).
... correcting 89 ectopic interval(s).
... correcting 5 short interval(s).
... correcting 5 long interval(s).
Traceback (most recent call last):
  File "<string>", line 77, in <module>
  File "/usr/lib/python3.10/site-packages/systole/correction.py", line 363, in correct_rr
    return _correct_rr(
  File "/usr/lib/python3.10/site-packages/systole/correction.py", line 263, in _correct_rr
    return clean_rr, (nMissed, nExtra, nEctopic, nShort, nLong)
UnboundLocalError: local variable 'nMissed' referenced before assignment

correct_rr(rr, n_iterations=2) doesn't work either:

Traceback (most recent call last):
  File "<string>", line 77, in <module>
TypeError: correct_rr() got an unexpected keyword argument 'n_iterations'

With version 0.2.2 correct_rr(rr, n_iterations=2, missed_correction=False) works as expected without errors.

MeisterP avatar Sep 18 '22 15:09 MeisterP

Thank you for reporting this, I suspect this might be due to artefacts close to each other, can you share the data you are using so I can reproduce the error?

LegrandNico avatar Sep 19 '22 09:09 LegrandNico

My code is failing because the output of the correct_rr() function changed.

import pandas as pd
from systole.correction import correct_rr

df = pd.read_csv('data.csv')
correct = correct_rr(df['R-R'])

with version 0.2.2:

print(correct)
{'clean_rr': array([565., 560., 560., ..., 507., 512., 509.]), 'ectopic': 66, 'short': 40, 'long': 11, 'extra': 17, 'missed': 0}

print(correct['clean_rr'])
[565. 560. 560. ... 507. 512. 509.]

with version 0.2.3:

print(correct)
(array([565., 560., 560., ..., 507., 512., 509.]), (0, 17, 21, 30, 10))

print(correct['clean_rr'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: tuple indices must be integers or slices, not str

MeisterP avatar Sep 19 '22 17:09 MeisterP

It looks like the function is working correctly but the way it returns the results have changed: https://embodied-computation-group.github.io/systole/generated/correction/systole.correction.correct_rr.html#systole.correction.correct_rr

It is now returning a tuple like:

clean_rr, (nMissed, nExtra, nEctopic, nShort, Long) = correct_rr(df['R-R'])

If you use the code above it should work. Sorry for this refactoring, the function has been completely rewritten in this release.

LegrandNico avatar Sep 19 '22 17:09 LegrandNico

There's no need to be sorry. Thanks for your work and support. Much appreciated. 👍

Not sure whether I'm misreading the docs... but I can't get arguments to work.

correct = correct_rr(df['R-R'], extra_correction=False)
Cleaning the RR interval time series.
... correcting 21 ectopic interval(s).
... correcting 30 short interval(s).
... correcting 12 long interval(s).
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/site-packages/systole/correction.py", line 363, in correct_rr
    return _correct_rr(
  File "/usr/lib/python3.10/site-packages/systole/correction.py", line 263, in _correct_rr
    return clean_rr, (nMissed, nExtra, nEctopic, nShort, nLong)
UnboundLocalError: local variable 'nExtra' referenced before assignment

correct = correct_rr(df['R-R'], n_iterations=2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: correct_rr() got an unexpected keyword argument 'n_iterations'

MeisterP avatar Sep 19 '22 17:09 MeisterP

I see the error comes from the absence of extra correction that is not correctly accounted for in the code, I will fix that as soon as possible. Thank you for pointing that out.

For the second example, the n_iteration argument has been removed in the new API to better fit with the original paper, so the function will not allow running multiple iterations of the algorithm.

LegrandNico avatar Sep 22 '22 11:09 LegrandNico