Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multi-object search #292

Merged
merged 5 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 56 additions & 19 deletions apps/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
""".format(APIURL, APIURL, APIURL, APIURL, APIURL, APIURL, APIURL, APIURL, APIURL, APIURL, APIURL)

api_doc_object = """
## Retrieve single object data
## Retrieve object data

The list of arguments for retrieving object data can be found at https://fink-portal.org/api/v1/objects.

Expand Down Expand Up @@ -106,6 +106,24 @@
pdf = pd.read_json(r.content)
```

You can retrieve the data for several objects at once:

```python
mylist = ['ZTF21aaxtctv', 'ZTF21abfmbix', 'ZTF21abfaohe']

# get data for many objects
r = requests.post(
'https://fink-portal.org/api/v1/objects',
json={
'objectId': ','.join(mylist),
'output-format': 'json'
}
)

# Format output in a DataFrame
pdf = pd.read_json(r.content)
```

Note that for `csv` output, you need to use

```python
Expand Down Expand Up @@ -1216,7 +1234,7 @@ def layout(is_mobile):
{
'name': 'objectId',
'required': True,
'description': 'ZTF Object ID'
'description': 'single ZTF Object ID, or a comma-separated list of object names, e.g. "ZTF19acmdpyr,ZTF21aaxtctv"'
},
{
'name': 'withupperlim',
Expand Down Expand Up @@ -1509,17 +1527,28 @@ def return_object():
else:
cols = '*'
truncated = False
to_evaluate = "key:key:{}".format(request.json['objectId'])

if ',' in request.json['objectId']:
# multi-objects search
splitids = request.json['objectId'].split(',')
ids = ['key:key:{}'.format(i.strip()) for i in splitids]
else:
# single object search
ids = ["key:key:{}".format(request.json['objectId'])]

# We do not want to perform full scan if the objectid is a wildcard
client.setLimit(1000)

results = client.scan(
"",
to_evaluate,
cols,
0, True, True
)
# Get data from the main table
results = java.util.TreeMap()
for to_evaluate in ids:
result = client.scan(
"",
to_evaluate,
cols,
0, True, True
)
results.putAll(result)

schema_client = client.schema()

Expand All @@ -1535,18 +1564,26 @@ def return_object():

if 'withupperlim' in request.json and str(request.json['withupperlim']) == 'True':
# upper limits
resultsU = clientU.scan(
"",
"{}".format(to_evaluate),
"*", 0, False, False
)
resultsU = java.util.TreeMap()
for to_evaluate in ids:
resultU = clientU.scan(
"",
to_evaluate,
"*",
0, False, False
)
resultsU.putAll(resultU)

# bad quality
resultsUP = clientUV.scan(
"",
"{}".format(to_evaluate),
"*", 0, False, False
)
resultsUP = java.util.TreeMap()
for to_evaluate in ids:
resultUP = clientUV.scan(
"",
to_evaluate,
"*",
0, False, False
)
resultsUP.putAll(resultUP)

pdfU = pd.DataFrame.from_dict(resultsU, orient='index')
pdfUP = pd.DataFrame.from_dict(resultsUP, orient='index')
Expand Down
24 changes: 24 additions & 0 deletions tests/api_single_object_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ def test_bad_request() -> None:

assert pdf.empty

def test_multiple_objects() -> None:
"""
Examples
---------
>>> test_multiple_objects()
"""
OIDS_ = ['ZTF21abfmbix', 'ZTF21aaxtctv', 'ZTF21abfaohe']
OIDS = ','.join(OIDS_)
pdf = get_an_object(oid=OIDS)

n_oids = len(np.unique(pdf.groupby('i:objectId').count()['i:ra']))
assert n_oids == 3

n_oids_single = 0
len_object = 0
for oid in OIDS_:
pdf_ = get_an_object(oid=oid)
n_oid = len(np.unique(pdf_.groupby('i:objectId').count()['i:ra']))
n_oids_single += n_oid
len_object += len(pdf_)

assert n_oids == n_oids_single, '{} is not equal to {}'.format(n_oids, n_oids_single)
assert len_object == len(pdf), '{} is not equal to {}'.format(len_object, len(pdf))


if __name__ == "__main__":
""" Execute the test suite """
Expand Down