Skip to content

Commit 3c228d7

Browse files
ManonMarchandbsipocz
authored andcommitted
maint: make parse_result simpler
for tables, we use the fact that Table accepts dictionnaries. For MOCs, the dictionnary parsing has been improved in MOCpy > 0.12 and does not require to remove empty orders anymore
1 parent 6a69f9a commit 3c228d7

File tree

2 files changed

+35
-91
lines changed

2 files changed

+35
-91
lines changed

astroquery/mocserver/core.py

Lines changed: 34 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
from . import conf
99

1010
import os
11+
from ast import literal_eval
12+
from copy import copy
13+
1114
from astropy import units as u
1215
from astropy.table import Table
13-
from astropy.table import MaskedColumn
14-
from copy import copy
1516

1617
try:
1718
from mocpy import MOC
@@ -34,15 +35,15 @@ class MOCServerClass(BaseQuery):
3435
Query the `CDS MOCServer <http://alasky.unistra.fr/MocServer/query>`_
3536
3637
The `CDS MOCServer <http://alasky.unistra.fr/MocServer/query>`_ allows the user to retrieve all the data sets (with
37-
their meta-datas) having sources in a specific region. This region can be a `regions.CircleSkyRegion`, a
38+
their meta-data) having sources in a specific region. This region can be a `regions.CircleSkyRegion`, a
3839
`regions.PolygonSkyRegion` or a `mocpy.MOC` object.
3940
4041
This package implements two methods:
4142
4243
* :meth:`~astroquery.mocserver.MOCServerClass.query_region` retrieving data-sets
43-
(their associated MOCs and meta-datas) having sources in a given region.
44+
(their associated MOCs and meta-data) having sources in a given region.
4445
* :meth:`~astroquery.mocserver.MOCServerClass.find_datasets` retrieving data-sets
45-
(their associated MOCs and meta-datas) based on the values of their meta-datas.
46+
(their associated MOCs and meta-data) based on the values of their meta-data.
4647
4748
"""
4849
URL = conf.server
@@ -86,18 +87,18 @@ def query_region(self, *, region=None, get_query_payload=False, verbose=False, *
8687
max_norder : int, optional
8788
Has sense only if ``return_moc`` is set to True. Specifies the maximum precision order of the returned MOC.
8889
fields : [str], optional
89-
Has sense only if ``return_moc`` is set to False. Specifies which meta datas to retrieve. The returned
90+
Has sense only if ``return_moc`` is set to False. Specifies which meta data to retrieve. The returned
9091
`astropy.table.Table` table will only contain the column names given in ``fields``.
9192
9293
Specifying the fields we want to retrieve allows the request to be faster because of the reduced chunk of
9394
data moving from the MOCServer to the client.
9495
95-
Some meta-datas as ``obs_collection`` or ``data_ucd`` do not keep a constant type throughout all the
96+
Some meta-data as ``obs_collection`` or ``data_ucd`` do not keep a constant type throughout all the
9697
MOCServer's data-sets and this lead to problems because `astropy.table.Table` supposes values in a column
9798
to have an unique type. When we encounter this problem for a specific meta-data, we remove its corresponding
9899
column from the returned astropy table.
99100
meta_data : str, optional
100-
Algebraic expression on meta-datas for filtering the data-sets at the server side.
101+
Algebraic expression on meta-data for filtering the data-sets at the server side.
101102
Examples of meta data expressions:
102103
103104
* Retrieve all the Hubble surveys: "ID=*HST*"
@@ -132,7 +133,7 @@ def find_datasets(self, meta_data, *, get_query_payload=False, verbose=False, **
132133
Parameters
133134
----------
134135
meta_data : str
135-
Algebraic expression on meta-datas for filtering the data-sets at the server side.
136+
Algebraic expression on meta-data for filtering the data-sets at the server side.
136137
Examples of meta data expressions:
137138
138139
* Retrieve all the Hubble surveys: "ID=*HST*"
@@ -142,16 +143,16 @@ def find_datasets(self, meta_data, *, get_query_payload=False, verbose=False, **
142143
More example of expressions can be found following this `link
143144
<http://alasky.unistra.fr/MocServer/example>`_ (especially see the urls).
144145
fields : [str], optional
145-
Has sense only if ``return_moc`` is set to False. Specifies which meta datas to retrieve. The returned
146+
Has sense only if ``return_moc`` is set to False. Specifies which meta data to retrieve. The returned
146147
`astropy.table.Table` table will only contain the column names given in ``fields``.
147148
148149
Specifying the fields we want to retrieve allows the request to be faster because of the reduced chunk of
149150
data moving from the MOCServer to the client.
150151
151-
Some meta-datas such as ``obs_collection`` or ``data_ucd`` do not keep a constant type throughout all the
152+
Some meta-data such as ``obs_collection`` or ``data_ucd`` do not keep a constant type throughout all the
152153
MOCServer's data-sets and this lead to problems because `astropy.table.Table` supposes values in a column
153154
to have an unique type. This case is not common: it is mainly linked to a typing error in the text files
154-
describing the meta-datas of the data-sets. When we encounter this for a specific meta-data, we link the
155+
describing the meta-data of the data-sets. When we encounter this for a specific meta-data, we link the
155156
generic type ``object`` to the column. Therefore, keep in mind that ``object`` typed columns can contain
156157
values of different types (e.g. lists and singletons or string and floats).
157158
max_rec : int, optional
@@ -329,87 +330,30 @@ def _parse_result(self, response, *, verbose=False):
329330
result = response.json()
330331

331332
if not self.return_moc:
332-
"""
333-
The user will get `astropy.table.Table` object whose columns refer to the returned data-set meta-datas.
334-
"""
335-
# cast the data-sets meta-datas values to their correct Python type.
336-
typed_result = []
337-
for d in result:
338-
typed_d = {k: self._cast_to_float(v) for k, v in d.items()}
339-
typed_result.append(typed_d)
340-
341-
# looping over all the record's keys to find all the existing keys
342-
column_names_l = []
343-
for d in typed_result:
344-
column_names_l.extend(d.keys())
345-
346-
# remove all the doubles
347-
column_names_l = list(set(column_names_l))
348-
# init a dict mapping all the meta-data's name to an empty list
349-
table_d = {key: [] for key in column_names_l}
350-
type_d = {key: None for key in column_names_l}
351-
352-
masked_array_d = {key: [] for key in column_names_l}
353-
# fill the dict with the value of each returned data-set one by one.
354-
for d in typed_result:
355-
row_table_d = {key: None for key in column_names_l}
356-
row_table_d.update(d)
357-
358-
for k, mask_l in masked_array_d.items():
359-
entry_masked = False if k in d.keys() else True
360-
mask_l.append(entry_masked)
361-
362-
for k, v in row_table_d.items():
363-
if v:
364-
type_d[k] = type(v)
365-
366-
table_d[k].append(v)
367-
368-
# define all the columns using astropy.table.MaskedColumn objects
369-
columns_l = []
370-
for k, v in table_d.items():
371-
try:
372-
if k != '#':
373-
columns_l.append(MaskedColumn(v, name=k, mask=masked_array_d[k], dtype=type_d[k]))
374-
except ValueError:
375-
# some metadata can be of multiple types when looking on all the datasets.
376-
# this can be due to internal typing errors of the metadatas.
377-
columns_l.append(MaskedColumn(v, name=k, mask=masked_array_d[k], dtype=object))
378-
379-
# return an `astropy.table.Table` object created from columns_l
380-
return Table(columns_l)
381-
382-
"""
383-
The user will get `mocpy.MOC` object.
384-
"""
385-
# remove
386-
empty_order_removed_d = {}
387-
for order, ipix_l in result.items():
388-
if len(ipix_l) > 0:
389-
empty_order_removed_d.update({order: ipix_l})
333+
# return a table with the meta-data, we cast the string values for convenience
334+
result = [{key: _cast_to_float(value) for key, value in row.items()} for row in result]
335+
return Table(rows=result)
390336

391337
# return a `mocpy.MOC` object. See https://github.com/cds-astro/mocpy and the MOCPy's doc
392-
return MOC.from_json(empty_order_removed_d)
393-
394-
@staticmethod
395-
def _cast_to_float(value):
396-
"""
397-
Cast ``value`` to a float if possible.
398-
399-
Parameters
400-
----------
401-
value : str
402-
string to cast
338+
return MOC.from_json(result)
339+
340+
def _cast_to_float(value):
341+
"""
342+
Cast ``value`` to a float if possible.
403343
404-
Returns
405-
-------
406-
value : float or str
407-
A float if it can be casted so otherwise the initial string.
408-
"""
409-
try:
410-
return float(value)
411-
except (ValueError, TypeError):
412-
return value
344+
Parameters
345+
----------
346+
value : str
347+
string to cast
413348
349+
Returns
350+
-------
351+
value : float or str
352+
A float if it can be casted so otherwise the initial string.
353+
"""
354+
try:
355+
return float(value)
356+
except (ValueError, TypeError):
357+
return value
414358

415359
MOCServer = MOCServerClass()

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ docs=
160160
scipy
161161
fsspec[http]
162162
all=
163-
mocpy>=0.9
163+
mocpy>=0.12
164164
astropy-healpix
165165
boto3
166166
regions>=0.5

0 commit comments

Comments
 (0)