pymatch icon indicating copy to clipboard operation
pymatch copied to clipboard

`chi2_distance` function fails when length of bins variable is equal to zero.

Open tammymendt opened this issue 5 years ago • 2 comments

def chi2_distance(t, c):
    tb, cb, bins = which_bin_hist(t, c)
    tb, cb = bin_hist(tb, cb, bins)
    return _chi2_distance(tb,cb)

Instead of failing, I would suggest ending gracefully but lifting a warning.

tammymendt avatar Apr 05 '19 09:04 tammymendt

I came to create a new issue, but I think it's this same issue. Is this (like) the error you get?

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-41-7a5faa30bec8> in <module>
----> 1 cc = m.compare_continuous(return_table=True)

/apps/python3/lib/python3.6/site-packages/pymatch/Matcher.py in compare_continuous(self, save, return_table)
    303                 std_diff_med_before, std_diff_mean_before = uf.std_diff(trb, cob)
    304                 std_diff_med_after, std_diff_mean_after = uf.std_diff(tra, coa)
--> 305                 pb, truthb = uf.grouped_permutation_test(uf.chi2_distance, trb, cob)
    306                 pa, trutha = uf.grouped_permutation_test(uf.chi2_distance, tra, coa)
    307                 ksb = round(uf.ks_boot(trb, cob, nboots=1000), 6)

/apps/python3/lib/python3.6/site-packages/pymatch/functions.py in grouped_permutation_test(f, t, c, n_samples)
     76 
     77 def grouped_permutation_test(f, t, c, n_samples=1000):
---> 78     truth = f(t, c)
     79     comb = np.concatenate((t, c))
     80     times_geq=0

/apps/python3/lib/python3.6/site-packages/pymatch/functions.py in chi2_distance(t, c)
     51 def chi2_distance(t, c):
     52     tb, cb, bins = which_bin_hist(t, c)
---> 53     tb, cb = bin_hist(tb, cb, bins)
     54     return _chi2_distance(tb,cb)
     55 

/apps/python3/lib/python3.6/site-packages/pymatch/functions.py in bin_hist(t, c, bins)
     72         return result
     73 
---> 74     return idx_to_value(tc, bins), idx_to_value(cc, bins)
     75 
     76 

/apps/python3/lib/python3.6/site-packages/pymatch/functions.py in idx_to_value(d, bins)
     69         result = {}
     70         for k, v, in d.items():
---> 71             result[int(bins[k-1])] = v
     72         return result
     73 

IndexError: index -1 is out of bounds for axis 0 with size 0

noahaskell avatar Jun 04 '19 17:06 noahaskell

Yes, it looks like the same issue.

On Tue, Jun 4, 2019, 7:52 PM Noah Silbert [email protected] wrote:

I came to create a new issue, but I think it's this same issue. Is this (like) the error you get?


IndexError Traceback (most recent call last) in ----> 1 cc = m.compare_continuous(return_table=True)

/apps/python3/lib/python3.6/site-packages/pymatch/Matcher.py in compare_continuous(self, save, return_table) 303 std_diff_med_before, std_diff_mean_before = uf.std_diff(trb, cob) 304 std_diff_med_after, std_diff_mean_after = uf.std_diff(tra, coa) --> 305 pb, truthb = uf.grouped_permutation_test(uf.chi2_distance, trb, cob) 306 pa, trutha = uf.grouped_permutation_test(uf.chi2_distance, tra, coa) 307 ksb = round(uf.ks_boot(trb, cob, nboots=1000), 6)

/apps/python3/lib/python3.6/site-packages/pymatch/functions.py in grouped_permutation_test(f, t, c, n_samples) 76 77 def grouped_permutation_test(f, t, c, n_samples=1000): ---> 78 truth = f(t, c) 79 comb = np.concatenate((t, c)) 80 times_geq=0

/apps/python3/lib/python3.6/site-packages/pymatch/functions.py in chi2_distance(t, c) 51 def chi2_distance(t, c): 52 tb, cb, bins = which_bin_hist(t, c) ---> 53 tb, cb = bin_hist(tb, cb, bins) 54 return _chi2_distance(tb,cb) 55

/apps/python3/lib/python3.6/site-packages/pymatch/functions.py in bin_hist(t, c, bins) 72 return result 73 ---> 74 return idx_to_value(tc, bins), idx_to_value(cc, bins) 75 76

/apps/python3/lib/python3.6/site-packages/pymatch/functions.py in idx_to_value(d, bins) 69 result = {} 70 for k, v, in d.items(): ---> 71 result[int(bins[k-1])] = v 72 return result 73

IndexError: index -1 is out of bounds for axis 0 with size 0

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/benmiroglio/pymatch/issues/14?email_source=notifications&email_token=ABUL6LLAXDOYYSQ7QH5XLFDPY2TWNA5CNFSM4HD2HOSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW5LOCY#issuecomment-498775819, or mute the thread https://github.com/notifications/unsubscribe-auth/ABUL6LLYHG7BAEQD7K2DOY3PY2TWNANCNFSM4HD2HOSA .

tammymendt avatar Jun 04 '19 17:06 tammymendt