AmpelAstro/extcats

binaryserach

Closed this issue · 1 comments

After running the notebook (insert_example.ipynb), I have the milliquas in my local db.

The findwithin function works (and it is fast!), but binaryserach is giving some trouble. Using method='2dsphere' gives the same result.

from extcats import CatalogQuery     
mqc_query = CatalogQuery.CatalogQuery('milliquas', ra_key = 'ra', dec_key = 'dec', coll_name='srcs')

INFO:extcats.CatalogQuery:using mongo client at localhost:27017
INFO:extcats.CatalogQuery:connected to collection srcs of database milliquas.
INFO:extcats.CatalogQuery:found 1998464 documents in source collection srcs.
INFO:extcats.CatalogQuery:set HEALPIX partition of order 16 with key hpxid_16. Nested: True, Inexed: True, Resolution ["]: 3.221
INFO:extcats.CatalogQuery:set 2dsphere key pos with format geoJSON. Inexed: True
INFO:extcats.CatalogQuery:source collection has the following indexes: _i, hpxid_16, pos_2dsphe
WARNING:extcats.CatalogQuery:2dsphere key pos is not indexed.

>>>print(mqc_query.findwithin(124.880179, +21.560564, 0.5))
          _id                alt_name_1    ...  rmag      xray_name      
------------------------ ----------------- ... ----- --------------------
5a9f07a7c9b80e54f39a8bbb 1WGA J0819.5+2133 ... 17.85 CXO J081931.1+213336

---------------------------------------------------------------------------

>>>print(mqc_query.binaryserach(124.880179, +21.560564, 1.))

TypeError                                 Traceback (most recent call last)
<ipython-input-22-0c455263c845> in <module>()
      4 
      5 print(mqc_query.findwithin(124.880179, +21.560564, 0.5, method='2dsphere'))
----> 6 print(mqc_query.binaryserach(124.880179, +21.560564, 1.))

/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/extcats-1.3-py3.6.egg/extcats/CatalogQuery.py in binaryserach(self, ra, dec, rs_arcsec, method, **qfunc_args)
    487 
    488         nearby = self.findwithin(
--> 489             ra = ra, dec = dec, rs_arcsec = rs_arcsec, method = method, find_one = True, **qfunc_args)
    490         if nearby is None:
    491             return False

/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/extcats-1.3-py3.6.egg/extcats/CatalogQuery.py in findwithin(self, ra, dec, rs_arcsec, method, **qfunc_args)
    393         """
    394         if method == "healpix":
--> 395             return self.findwithin_HEALPix(ra, dec, rs_arcsec, **qfunc_args)
    396         elif method == "2dsphere":
    397             return self.findwithin_2Dsphere(ra, dec, rs_arcsec, **qfunc_args)

/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/extcats-1.3-py3.6.egg/extcats/CatalogQuery.py in findwithin_HEALPix(self, ra, dec, rs_arcsec, circular, find_one)
    240             hp_key = self.hp_key, hp_order = self.hp_order,
    241             hp_nest = self.hp_nest, hp_resol = self.hp_resol,
--> 242             circular = circular, ra_key = self.ra_key, dec_key = self.dec_key, find_one = find_one)
    243 
    244 

/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/extcats-1.3-py3.6.egg/extcats/catquery_utils.py in searcharound_HEALPix(ra, dec, rs_arcsec, src_coll, hp_key, hp_order, hp_nest, hp_resol, circular, ra_key, dec_key, find_one)
    116         return Table(qresults)
    117     else:
--> 118         tab = Table(qresults)
    119         dists = get_distances(ra = ra, dec = dec, table = tab, ra_key = ra_key, dec_key = dec_key)
    120         circular_tab = tab[dists <= rs_arcsec]

/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/astropy/table/table.py in __init__(self, data, masked, names, dtype, meta, copy, rows, copy_indices, **kwargs)
    406 
    407         # Finally do the real initialization
--> 408         init_func(data, names, dtype, n_cols, copy)
    409 
    410         # Whatever happens above, the masked property should be set to a boolean

/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/astropy/table/table.py in _init_from_list(self, data, names, dtype, n_cols, copy)
    687             cols.append(col)
    688 
--> 689         self._init_from_cols(cols)
    690 
    691     def _init_from_ndarray(self, data, names, dtype, n_cols, copy):

/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/astropy/table/table.py in _init_from_cols(self, cols)
    749         """Initialize table from a list of Column or mixin objects"""
    750 
--> 751         lengths = set(len(col) for col in cols)
    752         if len(lengths) != 1:
    753             raise ValueError('Inconsistent data column lengths: {0}'

/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/astropy/table/table.py in <genexpr>(.0)
    749         """Initialize table from a list of Column or mixin objects"""
    750 
--> 751         lengths = set(len(col) for col in cols)
    752         if len(lengths) != 1:
    753             raise ValueError('Inconsistent data column lengths: {0}'

TypeError: len() of unsized object

Thanks for pointing that out. The problem was that binarysearch queries the collection using 'find_one' instead of 'find'. In both cases the results were casted into a list:
qresults = [o for o in QUERY_RETURN ]
In the case of find_one, a single document is returned by the query. The list ten contained just the keys of the document (iterating on a dictionary), and that was annoying the astropy.Table. I've uploaded a fix.