arq5x / scurgen

A tool for detecting patterns in genomic data with space filling curves
9 stars 4 forks source link

d2xy and xy2d inconsistent #14

Closed daler closed 11 years ago

daler commented 11 years ago

d2xy() and xy2d() don't appear to be working correctly.

(I haven't been able to get get_chrom_range() to be consistent, and I think I narrowed it down to d2xy and xy2d)

My understanding is if you feed a distance d to d2xy() and get back coords x and y, and then turn around and feed that same x and y back into xy2d(), you ought to get back the original d. But this snippet shows that's not the case:

from scurgen import hilbert
n = 4
for d in range(n * n):
    x, y = hilbert.d2xy(n, d)
    d2 = hilbert.xy2d(n, x, y)
    flag = ""
    if d != d2:
        flag = "***"
    print d, d2, flag

Here's the output, with mismatches flagged:

0 0 
1 3 ***
2 2 
3 1 ***
4 4 
5 5 
6 6 
7 7 
8 8 
9 9 
10 10 
11 11 
12 14 ***
13 13 
14 12 ***
15 15 

I don't fully understand all the AND and XORs in these two algorithms, so I'm stuck at this point.

arq5x commented 11 years ago

Great catch, goof on my part. Just pushed a fix.

daler commented 11 years ago

Great -- just tested it, works like a charm. Thanks.