Skip to content

Commit 7e0b359

Browse files
committed
Simplify changing the row limit in Gaia queries
It is now possible to specify the row limit of the `query_object` and `cone_search` families of functions with a keyword argument. It is now also possible to change the row limit through the `conf.ROW_LIMIT` configuration item at runtime.
1 parent 7de9084 commit 7e0b359

File tree

4 files changed

+83
-26
lines changed

4 files changed

+83
-26
lines changed

astroquery/gaia/core.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class GaiaClass(TapPlus):
4141
MAIN_GAIA_TABLE = None
4242
MAIN_GAIA_TABLE_RA = conf.MAIN_GAIA_TABLE_RA
4343
MAIN_GAIA_TABLE_DEC = conf.MAIN_GAIA_TABLE_DEC
44-
ROW_LIMIT = conf.ROW_LIMIT
44+
ROW_LIMIT = None
4545
VALID_DATALINK_RETRIEVAL_TYPES = conf.VALID_DATALINK_RETRIEVAL_TYPES
4646

4747
def __init__(self, tap_plus_conn_handler=None,
@@ -356,7 +356,8 @@ def get_datalinks(self, ids, verbose=False):
356356
return self.__gaiadata.get_datalinks(ids=ids, verbose=verbose)
357357

358358
def __query_object(self, coordinate, radius=None, width=None, height=None,
359-
async_job=False, verbose=False, columns=[]):
359+
async_job=False, verbose=False, columns=[],
360+
row_limit=None):
360361
"""Launches a job
361362
TAP & TAP+
362363
@@ -378,6 +379,10 @@ def __query_object(self, coordinate, radius=None, width=None, height=None,
378379
flag to display information about the process
379380
columns: list, optional, default []
380381
if empty, all columns will be selected
382+
row_limit : int, optional
383+
The maximum number of retrieved rows. Will default to the value
384+
provided by the configuration system if not set explicitly. The
385+
value -1 removes the limit entirely.
381386
382387
Returns
383388
-------
@@ -395,7 +400,7 @@ def __query_object(self, coordinate, radius=None, width=None, height=None,
395400
heightQuantity = self.__getQuantityInput(height, "height")
396401
widthDeg = widthQuantity.to(units.deg)
397402
heightDeg = heightQuantity.to(units.deg)
398-
403+
row_limit = row_limit or self.ROW_LIMIT or conf.ROW_LIMIT
399404
if columns:
400405
columns = ','.join(map(str, columns))
401406
else:
@@ -424,7 +429,7 @@ def __query_object(self, coordinate, radius=None, width=None, height=None,
424429
)
425430
ORDER BY
426431
dist ASC
427-
""".format(**{'row_limit': "TOP {0}".format(self.ROW_LIMIT) if self.ROW_LIMIT > 0 else "",
432+
""".format(**{'row_limit': "TOP {0}".format(row_limit) if row_limit > 0 else "",
428433
'ra_column': self.MAIN_GAIA_TABLE_RA, 'dec_column': self.MAIN_GAIA_TABLE_DEC,
429434
'columns': columns, 'table_name': self.MAIN_GAIA_TABLE or conf.MAIN_GAIA_TABLE, 'ra': ra, 'dec': dec,
430435
'width': widthDeg.value, 'height': heightDeg.value})
@@ -435,7 +440,7 @@ def __query_object(self, coordinate, radius=None, width=None, height=None,
435440
return job.get_results()
436441

437442
def query_object(self, coordinate, radius=None, width=None, height=None,
438-
verbose=False, columns=[]):
443+
verbose=False, columns=[], row_limit=None):
439444
"""Launches a job
440445
TAP & TAP+
441446
@@ -453,15 +458,22 @@ def query_object(self, coordinate, radius=None, width=None, height=None,
453458
flag to display information about the process
454459
columns: list, optional, default []
455460
if empty, all columns will be selected
461+
row_limit : int, optional
462+
The maximum number of retrieved rows. Will default to the value
463+
provided by the configuration system if not set explicitly. The
464+
value -1 removes the limit entirely.
456465
457466
Returns
458467
-------
459468
The job results (astropy.table).
460469
"""
461-
return self.__query_object(coordinate, radius, width, height, async_job=False, verbose=verbose, columns=columns)
470+
return self.__query_object(coordinate, radius, width, height,
471+
async_job=False, verbose=verbose,
472+
columns=columns, row_limit=row_limit)
462473

463474
def query_object_async(self, coordinate, radius=None, width=None,
464-
height=None, verbose=False, columns=[]):
475+
height=None, verbose=False, columns=[],
476+
row_limit=None):
465477
"""Launches a job (async)
466478
TAP & TAP+
467479
@@ -479,12 +491,18 @@ def query_object_async(self, coordinate, radius=None, width=None,
479491
flag to display information about the process
480492
columns: list, optional, default []
481493
if empty, all columns will be selected
494+
row_limit : int, optional
495+
The maximum number of retrieved rows. Will default to the value
496+
provided by the configuration system if not set explicitly. The
497+
value -1 removes the limit entirely.
482498
483499
Returns
484500
-------
485501
The job results (astropy.table).
486502
"""
487-
return self.__query_object(coordinate, radius, width, height, async_job=True, verbose=verbose, columns=columns)
503+
return self.__query_object(coordinate, radius, width, height,
504+
async_job=True, verbose=verbose,
505+
columns=columns, row_limit=row_limit)
488506

489507
def __cone_search(self, coordinate, radius, table_name=None,
490508
ra_column_name=MAIN_GAIA_TABLE_RA,
@@ -493,7 +511,7 @@ def __cone_search(self, coordinate, radius, table_name=None,
493511
background=False,
494512
output_file=None, output_format="votable", verbose=False,
495513
dump_to_file=False,
496-
columns=[]):
514+
columns=[], row_limit=None):
497515
"""Cone search sorted by distance
498516
TAP & TAP+
499517
@@ -526,6 +544,10 @@ def __cone_search(self, coordinate, radius, table_name=None,
526544
if True, the results are saved in a file instead of using memory
527545
columns: list, optional, default []
528546
if empty, all columns will be selected
547+
row_limit : int, optional
548+
The maximum number of retrieved rows. Will default to the value
549+
provided by the configuration system if not set explicitly. The
550+
value -1 removes the limit entirely.
529551
530552
Returns
531553
-------
@@ -542,6 +564,7 @@ def __cone_search(self, coordinate, radius, table_name=None,
542564
columns = ','.join(map(str, columns))
543565
else:
544566
columns = "*"
567+
row_limit = row_limit or self.ROW_LIMIT or conf.ROW_LIMIT
545568

546569
query = """
547570
SELECT
@@ -561,7 +584,7 @@ def __cone_search(self, coordinate, radius, table_name=None,
561584
ORDER BY
562585
dist ASC
563586
""".format(**{'ra_column': ra_column_name,
564-
'row_limit': "TOP {0}".format(self.ROW_LIMIT) if self.ROW_LIMIT > 0 else "",
587+
'row_limit': "TOP {0}".format(row_limit) if row_limit > 0 else "",
565588
'dec_column': dec_column_name, 'columns': columns, 'ra': ra, 'dec': dec,
566589
'radius': radiusDeg, 'table_name': table_name or self.MAIN_GAIA_TABLE or conf.MAIN_GAIA_TABLE})
567590

@@ -586,7 +609,7 @@ def cone_search(self, coordinate, radius=None,
586609
output_file=None,
587610
output_format="votable", verbose=False,
588611
dump_to_file=False,
589-
columns=[]):
612+
columns=[], row_limit=None):
590613
"""Cone search sorted by distance (sync.)
591614
TAP & TAP+
592615
@@ -613,6 +636,10 @@ def cone_search(self, coordinate, radius=None,
613636
if True, the results are saved in a file instead of using memory
614637
columns: list, optional, default []
615638
if empty, all columns will be selected
639+
row_limit : int, optional
640+
The maximum number of retrieved rows. Will default to the value
641+
provided by the configuration system if not set explicitly. The
642+
value -1 removes the limit entirely.
616643
617644
Returns
618645
-------
@@ -628,15 +655,17 @@ def cone_search(self, coordinate, radius=None,
628655
output_file=output_file,
629656
output_format=output_format,
630657
verbose=verbose,
631-
dump_to_file=dump_to_file, columns=columns)
658+
dump_to_file=dump_to_file, columns=columns,
659+
row_limit=row_limit)
632660

633661
def cone_search_async(self, coordinate, radius=None,
634662
table_name=None,
635663
ra_column_name=MAIN_GAIA_TABLE_RA,
636664
dec_column_name=MAIN_GAIA_TABLE_DEC,
637665
background=False,
638666
output_file=None, output_format="votable",
639-
verbose=False, dump_to_file=False, columns=[]):
667+
verbose=False, dump_to_file=False, columns=[],
668+
row_limit=None):
640669
"""Cone search sorted by distance (async)
641670
TAP & TAP+
642671
@@ -665,6 +694,10 @@ def cone_search_async(self, coordinate, radius=None,
665694
flag to display information about the process
666695
dump_to_file : bool, optional, default 'False'
667696
if True, the results are saved in a file instead of using memory
697+
row_limit : int, optional
698+
The maximum number of retrieved rows. Will default to the value
699+
provided by the configuration system if not set explicitly. The
700+
value -1 removes the limit entirely.
668701
669702
Returns
670703
-------
@@ -680,7 +713,8 @@ def cone_search_async(self, coordinate, radius=None,
680713
output_file=output_file,
681714
output_format=output_format,
682715
verbose=verbose,
683-
dump_to_file=dump_to_file, columns=columns)
716+
dump_to_file=dump_to_file, columns=columns,
717+
row_limit=row_limit)
684718

685719
def __checkQuantityInput(self, value, msg):
686720
if not (isinstance(value, str) or isinstance(value, units.Quantity)):

astroquery/gaia/tests/test_gaia_remote.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33
from astropy.coordinates import SkyCoord
44

5+
from astroquery.gaia import conf
56
from .. import GaiaClass
67

78

@@ -13,15 +14,15 @@ def test_query_object_row_limit():
1314
height = u.Quantity(0.1, u.deg)
1415
r = Gaia.query_object_async(coordinate=coord, width=width, height=height)
1516

16-
assert len(r) == Gaia.ROW_LIMIT
17+
assert len(r) == conf.ROW_LIMIT
1718

1819
Gaia.ROW_LIMIT = 10
1920
r = Gaia.query_object_async(coordinate=coord, width=width, height=height)
2021

2122
assert len(r) == 10 == Gaia.ROW_LIMIT
2223

23-
Gaia.ROW_LIMIT = -1
24-
r = Gaia.query_object_async(coordinate=coord, width=width, height=height)
24+
r = Gaia.query_object_async(coordinate=coord, width=width, height=height,
25+
row_limit=-1)
2526

2627
assert len(r) == 176
2728

@@ -34,16 +35,15 @@ def test_cone_search_row_limit():
3435
j = Gaia.cone_search_async(coord, radius)
3536
r = j.get_results()
3637

37-
assert len(r) == Gaia.ROW_LIMIT
38+
assert len(r) == conf.ROW_LIMIT
3839

3940
Gaia.ROW_LIMIT = 10
4041
j = Gaia.cone_search_async(coord, radius)
4142
r = j.get_results()
4243

4344
assert len(r) == 10 == Gaia.ROW_LIMIT
4445

45-
Gaia.ROW_LIMIT = -1
46-
j = Gaia.cone_search_async(coord, radius)
46+
j = Gaia.cone_search_async(coord, radius, row_limit=-1)
4747
r = j.get_results()
4848

4949
assert len(r) == 1188

astroquery/gaia/tests/test_gaiatap.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,24 @@ def test_cone_search_async(self):
360360
# Cleanup.
361361
conf.reset('MAIN_GAIA_TABLE')
362362

363+
# Test the default value from conf.
364+
assert 'TOP 50' in job.parameters['query']
365+
# Test changing the row limit through conf at runtime.
366+
with conf.set_temp('ROW_LIMIT', 42):
367+
job = tap.cone_search_async(sc, radius)
368+
assert 'TOP 42' in job.parameters['query']
369+
# Changing the value through the class should overrule conf.
370+
tap.ROW_LIMIT = 17
371+
job = tap.cone_search_async(sc, radius)
372+
assert 'TOP 17' in job.parameters['query']
373+
# Function argument has highest priority.
374+
job = tap.cone_search_async(sc, radius, row_limit=9)
375+
assert 'TOP 9' in job.parameters['query']
376+
# No row limit
377+
job = tap.cone_search_async(sc, radius, row_limit=-1)
378+
assert 'TOP' not in job.parameters['query']
379+
380+
363381
def __check_results_column(self, results, columnName, description, unit,
364382
dataType):
365383
c = results[columnName]

docs/gaia/gaia.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,16 @@ degrees around an specific point in RA/Dec coordinates.
125125
0.021615117161838747 1635721458409799680 ...
126126
Length = 50 rows
127127
128-
Queries return a limited number of rows controlled by ``Gaia.ROW_LIMIT``. To change the default behaviour set this appropriately.
128+
By default the number of rows returned by a query is limited by the
129+
``astroquery.gaia.conf.ROW_LIMIT`` value. This value can be overruled in a
130+
single function call with the ``row_limit`` argument. Alternatively, if the
131+
class attribute ``Gaia.ROW_LIMIT`` is set then it will take precedence over
132+
``conf.ROW_LIMIT``.
129133

130134
.. code-block:: python
131135
132-
>>> Gaia.ROW_LIMIT = 8
136+
>>> from astroquery.gaia import conf
137+
>>> conf.ROW_LIMIT = 8 # Or Gaia.ROW_LIMIT = 8
133138
>>> r = Gaia.query_object_async(coordinate=coord, width=width, height=height)
134139
>>> r.pprint()
135140
@@ -145,12 +150,12 @@ Queries return a limited number of rows controlled by ``Gaia.ROW_LIMIT``. To cha
145150
0.007469463683838576 1635721458409799680 ...
146151
0.008202004514524316 1635721458409799680 ...
147152
148-
To return an unlimited number of rows set ``Gaia.ROW_LIMIT`` to -1.
153+
To return an unlimited number of rows set the row limit to ``-1``.
149154

150155
.. code-block:: python
151156
152-
>>> Gaia.ROW_LIMIT = -1
153-
>>> r = Gaia.query_object_async(coordinate=coord, width=width, height=height)
157+
>>> r = Gaia.query_object_async(coordinate=coord, width=width, height=height,
158+
... row_limit=-1)
154159
>>> r.pprint()
155160
156161
dist solution_id ... epoch_photometry_url
@@ -184,7 +189,7 @@ To return an unlimited number of rows set ``Gaia.ROW_LIMIT`` to -1.
184189
~~~~~~~~~~~~~~~~
185190

186191
This query performs a cone search centered at the specified RA/Dec coordinates with the provided
187-
radius argument.
192+
radius argument. The number of rows is limited just like in object queries.
188193

189194
.. code-block:: python
190195

0 commit comments

Comments
 (0)