k-sys / covid-19

A collection of work related to COVID-19
1.38k stars 432 forks source link

TypeError when ML = 0 in highest_density_interval #20

Open mkrech opened 4 years ago

mkrech commented 4 years ago

Hi, I use the jhu data to compute the Rt and France seems not to fit :). A test file is attached. At 2020-04-17 the ML is 0 and the highest_density_interval function shows a Error see below. Hotfix for the highest_density_interval function could be:

  ...
  for i, value in enumerate(cumsum):
        for j, high_value in enumerate(cumsum[i+1:]):
            if (high_value-value > p) and (not best or j < best[1]-best[0]):
                best = (i, i+j+1)
                break

  -->if best == None:
           return pd.Series()

    low = pmf.index[best[0]]
    high = pmf.index[best[1]]
  ...

###################

TypeError Traceback (most recent call last) ~/Projekte/_work/covid-19/Realtime-R0jhu.py in 510 return pd.Series([low, high], index=[f'Low{p100:.0f}', f'High_{p100:.0f}']) 511 ---> 512 hdis = highest_density_interval_tmp(posteriors, p=.9) 513 514 most_likely = posteriors.idxmax().rename('ML')

~/Projekte/_work/covid-19/Realtime-R0_jhu.py in highest_density_interval_tmp(pmf, p) 495 # If we pass a DataFrame, just call this recursively on the columns 496 if(isinstance(pmf, pd.DataFrame)): ----> 497 return pd.DataFrame([highest_density_interval_tmp(pmf[col], p=p) for col in pmf], 498 index=pmf.columns) 499

~/Projekte/_work/covid-19/Realtime-R0_jhu.py in (.0) 495 # If we pass a DataFrame, just call this recursively on the columns 496 if(isinstance(pmf, pd.DataFrame)): ----> 497 return pd.DataFrame([highest_density_interval_tmp(pmf[col], p=p) for col in pmf], 498 index=pmf.columns) 499

~/Projekte/_work/covid-19/Realtime-R0_jhu.py in highest_density_intervaltmp(pmf, p) 506 break 507 ---> 508 low = pmf.index[best[0]] 509 high = pmf.index[best[1]] 510 return pd.Series([low, high], index=[f'Low{p100:.0f}', f'High_{p100:.0f}'])

TypeError: 'NoneType' object is not subscriptable ################

test.csv.zip

barrysmyth commented 4 years ago

I noticed this too. Also now happening for Spain.

It looks like the problem arises because on the 2020-04-15 for the France data. The total_p matrix has no entries >p