Skip to content

Commit 651866e

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 651866e

File tree

4 files changed

+75
-26
lines changed

4 files changed

+75
-26
lines changed

astroquery/gaia/core.py

Lines changed: 42 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,7 @@ 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=[], row_limit=None):
360360
"""Launches a job
361361
TAP & TAP+
362362
@@ -378,6 +378,10 @@ def __query_object(self, coordinate, radius=None, width=None, height=None,
378378
flag to display information about the process
379379
columns: list, optional, default []
380380
if empty, all columns will be selected
381+
row_limit : int, optional
382+
The maximum number of retrieved rows. Will default to the value
383+
provided by the configuration system if not set explicitly. The
384+
value -1 removes the limit entirely.
381385
382386
Returns
383387
-------
@@ -395,7 +399,7 @@ def __query_object(self, coordinate, radius=None, width=None, height=None,
395399
heightQuantity = self.__getQuantityInput(height, "height")
396400
widthDeg = widthQuantity.to(units.deg)
397401
heightDeg = heightQuantity.to(units.deg)
398-
402+
row_limit = row_limit or self.ROW_LIMIT or conf.ROW_LIMIT
399403
if columns:
400404
columns = ','.join(map(str, columns))
401405
else:
@@ -424,7 +428,7 @@ def __query_object(self, coordinate, radius=None, width=None, height=None,
424428
)
425429
ORDER BY
426430
dist ASC
427-
""".format(**{'row_limit': "TOP {0}".format(self.ROW_LIMIT) if self.ROW_LIMIT > 0 else "",
431+
""".format(**{'row_limit': "TOP {0}".format(row_limit) if row_limit > 0 else "",
428432
'ra_column': self.MAIN_GAIA_TABLE_RA, 'dec_column': self.MAIN_GAIA_TABLE_DEC,
429433
'columns': columns, 'table_name': self.MAIN_GAIA_TABLE or conf.MAIN_GAIA_TABLE, 'ra': ra, 'dec': dec,
430434
'width': widthDeg.value, 'height': heightDeg.value})
@@ -435,7 +439,7 @@ def __query_object(self, coordinate, radius=None, width=None, height=None,
435439
return job.get_results()
436440

437441
def query_object(self, coordinate, radius=None, width=None, height=None,
438-
verbose=False, columns=[]):
442+
verbose=False, columns=[], row_limit=None):
439443
"""Launches a job
440444
TAP & TAP+
441445
@@ -453,15 +457,21 @@ def query_object(self, coordinate, radius=None, width=None, height=None,
453457
flag to display information about the process
454458
columns: list, optional, default []
455459
if empty, all columns will be selected
460+
row_limit : int, optional
461+
The maximum number of retrieved rows. Will default to the value
462+
provided by the configuration system if not set explicitly. The
463+
value -1 removes the limit entirely.
456464
457465
Returns
458466
-------
459467
The job results (astropy.table).
460468
"""
461-
return self.__query_object(coordinate, radius, width, height, async_job=False, verbose=verbose, columns=columns)
469+
return self.__query_object(coordinate, radius, width, height,
470+
async_job=False, verbose=verbose,
471+
columns=columns, row_limit=row_limit)
462472

463473
def query_object_async(self, coordinate, radius=None, width=None,
464-
height=None, verbose=False, columns=[]):
474+
height=None, verbose=False, columns=[], row_limit=None):
465475
"""Launches a job (async)
466476
TAP & TAP+
467477
@@ -479,12 +489,17 @@ def query_object_async(self, coordinate, radius=None, width=None,
479489
flag to display information about the process
480490
columns: list, optional, default []
481491
if empty, all columns will be selected
492+
row_limit : int, optional
493+
The maximum number of retrieved rows. Will default to the value
494+
provided by the configuration system if not set explicitly. The
495+
value -1 removes the limit entirely.
482496
483497
Returns
484498
-------
485499
The job results (astropy.table).
486500
"""
487-
return self.__query_object(coordinate, radius, width, height, async_job=True, verbose=verbose, columns=columns)
501+
return self.__query_object(coordinate, radius, width, height, async_job=True,
502+
verbose=verbose, columns=columns, row_limit=row_limit)
488503

489504
def __cone_search(self, coordinate, radius, table_name=None,
490505
ra_column_name=MAIN_GAIA_TABLE_RA,
@@ -493,7 +508,7 @@ def __cone_search(self, coordinate, radius, table_name=None,
493508
background=False,
494509
output_file=None, output_format="votable", verbose=False,
495510
dump_to_file=False,
496-
columns=[]):
511+
columns=[], row_limit=None):
497512
"""Cone search sorted by distance
498513
TAP & TAP+
499514
@@ -526,6 +541,10 @@ def __cone_search(self, coordinate, radius, table_name=None,
526541
if True, the results are saved in a file instead of using memory
527542
columns: list, optional, default []
528543
if empty, all columns will be selected
544+
row_limit : int, optional
545+
The maximum number of retrieved rows. Will default to the value
546+
provided by the configuration system if not set explicitly. The
547+
value -1 removes the limit entirely.
529548
530549
Returns
531550
-------
@@ -542,6 +561,7 @@ def __cone_search(self, coordinate, radius, table_name=None,
542561
columns = ','.join(map(str, columns))
543562
else:
544563
columns = "*"
564+
row_limit = row_limit or self.ROW_LIMIT or conf.ROW_LIMIT
545565

546566
query = """
547567
SELECT
@@ -561,7 +581,7 @@ def __cone_search(self, coordinate, radius, table_name=None,
561581
ORDER BY
562582
dist ASC
563583
""".format(**{'ra_column': ra_column_name,
564-
'row_limit': "TOP {0}".format(self.ROW_LIMIT) if self.ROW_LIMIT > 0 else "",
584+
'row_limit': "TOP {0}".format(row_limit) if row_limit > 0 else "",
565585
'dec_column': dec_column_name, 'columns': columns, 'ra': ra, 'dec': dec,
566586
'radius': radiusDeg, 'table_name': table_name or self.MAIN_GAIA_TABLE or conf.MAIN_GAIA_TABLE})
567587

@@ -586,7 +606,7 @@ def cone_search(self, coordinate, radius=None,
586606
output_file=None,
587607
output_format="votable", verbose=False,
588608
dump_to_file=False,
589-
columns=[]):
609+
columns=[], row_limit=None):
590610
"""Cone search sorted by distance (sync.)
591611
TAP & TAP+
592612
@@ -613,6 +633,10 @@ def cone_search(self, coordinate, radius=None,
613633
if True, the results are saved in a file instead of using memory
614634
columns: list, optional, default []
615635
if empty, all columns will be selected
636+
row_limit : int, optional
637+
The maximum number of retrieved rows. Will default to the value
638+
provided by the configuration system if not set explicitly. The
639+
value -1 removes the limit entirely.
616640
617641
Returns
618642
-------
@@ -628,15 +652,15 @@ def cone_search(self, coordinate, radius=None,
628652
output_file=output_file,
629653
output_format=output_format,
630654
verbose=verbose,
631-
dump_to_file=dump_to_file, columns=columns)
655+
dump_to_file=dump_to_file, columns=columns, row_limit=row_limit)
632656

633657
def cone_search_async(self, coordinate, radius=None,
634658
table_name=None,
635659
ra_column_name=MAIN_GAIA_TABLE_RA,
636660
dec_column_name=MAIN_GAIA_TABLE_DEC,
637661
background=False,
638662
output_file=None, output_format="votable",
639-
verbose=False, dump_to_file=False, columns=[]):
663+
verbose=False, dump_to_file=False, columns=[], row_limit=None):
640664
"""Cone search sorted by distance (async)
641665
TAP & TAP+
642666
@@ -665,6 +689,10 @@ def cone_search_async(self, coordinate, radius=None,
665689
flag to display information about the process
666690
dump_to_file : bool, optional, default 'False'
667691
if True, the results are saved in a file instead of using memory
692+
row_limit : int, optional
693+
The maximum number of retrieved rows. Will default to the value
694+
provided by the configuration system if not set explicitly. The
695+
value -1 removes the limit entirely.
668696
669697
Returns
670698
-------
@@ -680,7 +708,7 @@ def cone_search_async(self, coordinate, radius=None,
680708
output_file=output_file,
681709
output_format=output_format,
682710
verbose=verbose,
683-
dump_to_file=dump_to_file, columns=columns)
711+
dump_to_file=dump_to_file, columns=columns, row_limit=row_limit)
684712

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

astroquery/gaia/tests/test_gaia_remote.py

Lines changed: 5 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,14 @@ 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, row_limit=-1)
2525

2626
assert len(r) == 176
2727

@@ -34,16 +34,15 @@ def test_cone_search_row_limit():
3434
j = Gaia.cone_search_async(coord, radius)
3535
r = j.get_results()
3636

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

3939
Gaia.ROW_LIMIT = 10
4040
j = Gaia.cone_search_async(coord, radius)
4141
r = j.get_results()
4242

4343
assert len(r) == 10 == Gaia.ROW_LIMIT
4444

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

4948
assert len(r) == 1188

astroquery/gaia/tests/test_gaiatap.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,23 @@ 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+
363380
def __check_results_column(self, results, columnName, description, unit,
364381
dataType):
365382
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)