Skip to content

Commit e41e269

Browse files
authored
Merge pull request #6 from cmu-delphi/master
sync to delphi
2 parents 86f8e50 + b07022e commit e41e269

File tree

8 files changed

+259
-5
lines changed

8 files changed

+259
-5
lines changed

docs/api/covidcast.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ See [this documentation](README.md) for details on specifying epiweeks, dates, a
7777

7878
# Example URLs
7979

80-
### Delphi's COVID-19 Surveillance Streams from Facebook Survey ILI on 2020-04-06 - 2010-04-10 (county 06001)
80+
### Delphi's COVID-19 Surveillance Streams from Facebook Survey ILI on 2020-04-06 - 2020-04-10 (county 06001)
8181
https://delphi.midas.cs.cmu.edu/epidata/api.php?source=covidcast&data_source=fb_survey&signal=ili&time_type=day&geo_type=county&time_values=20200406-20200410&geo_value=06001
8282
```json
8383
{
@@ -102,4 +102,58 @@ https://delphi.midas.cs.cmu.edu/epidata/api.php?source=covidcast&data_source=fb_
102102

103103
# Code Samples
104104

105-
No client support yet.
105+
Libraries are available for [CoffeeScript](../../src/client/delphi_epidata.coffee), [JavaScript](../../src/client/delphi_epidata.js), [Python](../../src/client/delphi_epidata.py), and [R](../../src/client/delphi_epidata.R).
106+
The following samples show how to import the library and fetch Delphi's COVID-19 Surveillance Streams from Facebook Survey ILI for county 06001 and days `20200401` and `20200405-20200414` (11 days total).
107+
108+
### CoffeeScript (in Node.js)
109+
110+
````coffeescript
111+
# Import
112+
{Epidata} = require('./delphi_epidata')
113+
# Fetch data
114+
callback = (result, message, epidata) ->
115+
console.log(result, message, epidata?.length)
116+
Epidata.covidcast(callback, 'fb_survey', 'ili', 'day', 'county', [20200401, Epidata.range(20200405, 20200414)], '06001')
117+
````
118+
119+
### JavaScript (in a web browser)
120+
121+
````html
122+
<!-- Imports -->
123+
<script src="jquery.js"></script>
124+
<script src="delphi_epidata.js"></script>
125+
<!-- Fetch data -->
126+
<script>
127+
var callback = function(result, message, epidata) {
128+
console.log(result, message, epidata != null ? epidata.length : void 0);
129+
};
130+
Epidata.covidcast(callback, 'fb_survey', 'ili', 'day', 'county', [20200401, Epidata.range(20200405, 20200414)], '06001');
131+
</script>
132+
````
133+
134+
### Python
135+
136+
Optionally install the package using pip(env):
137+
````bash
138+
pip install delphi-epidata
139+
````
140+
141+
Otherwise, place `delphi_epidata.py` from this repo next to your python script.
142+
143+
````python
144+
# Import
145+
from delphi_epidata import Epidata
146+
# Fetch data
147+
res = Epidata.covidcast('fb_survey', 'ili', 'day', 'county', [20200401, Epidata.range(20200405, 20200414)], '06001')
148+
print(res['result'], res['message'], len(res['epidata']))
149+
````
150+
151+
### R
152+
153+
````R
154+
# Import
155+
source('delphi_epidata.R')
156+
# Fetch data
157+
res <- Epidata$covidcast('fb_survey', 'ili', 'day', 'county', list(20200401, Epidata$range(20200405, 20200414)), '06001')
158+
cat(paste(res$result, res$message, length(res$epidata), "\n"))
159+
````

docs/api/covidcast_meta.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,58 @@ https://delphi.midas.cs.cmu.edu/epidata/api.php?source=covidcast_meta
9393

9494
# Code Samples
9595

96-
<!-- TODO: fix -->
96+
Libraries are available for [CoffeeScript](../../src/client/delphi_epidata.coffee), [JavaScript](../../src/client/delphi_epidata.js), [Python](../../src/client/delphi_epidata.py), and [R](../../src/client/delphi_epidata.R).
97+
The following samples show how to import the library and fetch Delphi's COVID-19 Surveillance Streams metadata.
98+
99+
### CoffeeScript (in Node.js)
100+
101+
````coffeescript
102+
# Import
103+
{Epidata} = require('./delphi_epidata')
104+
# Fetch data
105+
callback = (result, message, epidata) ->
106+
console.log(result, message, epidata?.length)
107+
Epidata.covidcast_meta(callback)
108+
````
109+
110+
### JavaScript (in a web browser)
111+
112+
````html
113+
<!-- Imports -->
114+
<script src="jquery.js"></script>
115+
<script src="delphi_epidata.js"></script>
116+
<!-- Fetch data -->
117+
<script>
118+
var callback = function(result, message, epidata) {
119+
console.log(result, message, epidata != null ? epidata.length : void 0);
120+
};
121+
Epidata.covidcast_meta(callback);
122+
</script>
123+
````
124+
125+
### Python
126+
127+
Optionally install the package using pip(env):
128+
````bash
129+
pip install delphi-epidata
130+
````
131+
132+
Otherwise, place `delphi_epidata.py` from this repo next to your python script.
133+
134+
````python
135+
# Import
136+
from delphi_epidata import Epidata
137+
# Fetch data
138+
res = Epidata.covidcast_meta()
139+
print(res['result'], res['message'], len(res['epidata']))
140+
````
141+
142+
### R
143+
144+
````R
145+
# Import
146+
source('delphi_epidata.R')
147+
# Fetch data
148+
res <- Epidata$covidcast_meta()
149+
cat(paste(res$result, res$message, length(res$epidata), "\n"))
150+
````

docs/api/wiki.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,6 @@ Note:
186186
# Import
187187
source('delphi_epidata.R')
188188
# Fetch data
189-
res <- Epidata$fluview_clinical(list('nat'), NULL, list(201940, Epidata$range(202001, 202010)), list(0, 12), 'en')
189+
res <- Epidata$wiki(list('nat'), NULL, list(201940, Epidata$range(202001, 202010)), list(0, 12), 'en')
190190
cat(paste(res$result, res$message, length(res$epidata), "\n"))
191191
````

integrations/test_delphi_epidata.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Integration tests for delphi_epidata.py."""
2+
import unittest
3+
4+
# first party
5+
from delphi.epidata.client.delphi_epidata import Epidata
6+
7+
# py3tester coverage target
8+
__test_target__ = 'delphi.epidata.client.delphi_epidata'
9+
10+
class DelphiEpidataPythonClientTests(unittest.TestCase):
11+
"""Tests the Python client."""
12+
13+
def test_covidcast_happy_path(self):
14+
"""Test that the covidcast endpoint returns expected data."""
15+
16+
# Fetch data
17+
res = Epidata.covidcast('fb_survey', 'ili', 'day', 'county', [20200401, Epidata.range(20200405, 20200414)], '06001')
18+
# Check result
19+
self.assertEqual(res['result'], 1)
20+
self.assertEqual(res['message'], 'success')
21+
self.assertGreater(len(res['epidata']), 0)
22+
item = res['epidata'][0]
23+
self.assertIn('geo_value', item)
24+
self.assertIn('time_value', item)
25+
self.assertIn('direction', item)
26+
self.assertIn('value', item)
27+
self.assertIn('stderr', item)
28+
self.assertIn('sample_size', item)
29+
30+
31+
def test_covidcast_meta(self):
32+
"""Test that the covidcast_meta endpoint returns expected data."""
33+
34+
# Fetch data
35+
res = Epidata.covidcast_meta()
36+
# Check result
37+
self.assertEqual(res['result'], 1)
38+
self.assertEqual(res['message'], 'success')
39+
self.assertGreater(len(res['epidata']), 0)
40+
item = res['epidata'][0]
41+
self.assertIn('data_source', item)
42+
self.assertIn('time_type', item)
43+
self.assertIn('geo_type', item)
44+
self.assertIn('min_time', item)
45+
self.assertIn('max_time', item)
46+
self.assertIn('num_locations', item)
47+
48+
if __name__ == '__main__':
49+
unittest.main()

src/client/delphi_epidata.R

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,31 @@ Epidata <- (function() {
492492
return(.request(list(source='meta')))
493493
}
494494

495+
# Fetch Delphi's COVID-19 Surveillance Streams
496+
covidcast <- function(data_source, signal, time_type, geo_type, time_values, geo_value) {
497+
# Check parameters
498+
if(missing(data_source) || missing(signal) || missing(time_type) || missing(geo_type) || missing(time_values) || missing(geo_value)) {
499+
stop('`data_source`, `signal`, `time_type`, `geo_type`, `time_values`, and `geo_value` are all required')
500+
}
501+
# Set up request
502+
params <- list(
503+
source = 'covidcast',
504+
data_source = data_source,
505+
signal = signal,
506+
time_type = time_type,
507+
geo_type = geo_type,
508+
time_values = .list(time_values),
509+
geo_value = geo_value
510+
)
511+
# Make the API call
512+
return(.request(params))
513+
}
514+
515+
# Fetch Delphi's COVID-19 Surveillance Streams metadata
516+
covidcast_meta <- function() {
517+
return(.request(list(source='covidcast_meta')))
518+
}
519+
495520
# Export the public methods
496521
return(list(
497522
range = range,
@@ -515,6 +540,8 @@ Epidata <- (function() {
515540
dengue_sensors = dengue_sensors,
516541
nowcast = nowcast,
517542
dengue_nowcast = dengue_nowcast,
518-
meta = meta
543+
meta = meta,
544+
covidcast = covidcast,
545+
covidcast_meta = covidcast_meta
519546
))
520547
})()

src/client/delphi_epidata.coffee

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,26 @@ class Epidata
346346
@meta: (callback) ->
347347
_request(callback, {'source': 'meta'})
348348

349+
# Fetch Delphi's COVID-19 Surveillance Streams
350+
@covidcast: (callback, data_source, signal, time_type, geo_type, time_values, geo_value) ->
351+
# Check parameters
352+
unless data_source? and signal? and time_type? and geo_type? and time_values? and geo_values?
353+
throw { msg: '`data_source`, `signal`, `time_type`, `geo_type`, `time_values`, and `geo_value` are all required' }
354+
# Set up request
355+
params =
356+
'source': 'covidcast'
357+
'data_source': data_source
358+
'signal': signal
359+
'time_type': time_type
360+
'geo_type': geo_type
361+
'time_values': _list(time_values)
362+
'geo_value': geo_value
363+
# Make the API call
364+
_request(callback, params)
365+
366+
# Fetch Delphi's COVID-19 Surveillance Streams metadata
367+
@covidcast_meta: (callback) ->
368+
_request(callback, {'source': 'covidcast_meta'})
349369

350370
# Export the API to the global environment
351371
(exports ? window).Epidata = Epidata

src/client/delphi_epidata.js

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/delphi_epidata.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,29 @@ def dengue_nowcast(locations, epiweeks):
527527
def meta():
528528
"""Fetch API metadata."""
529529
return Epidata._request({'source': 'meta'})
530+
531+
# Fetch Delphi's COVID-19 Surveillance Streams
532+
@staticmethod
533+
def covidcast(data_source, signal, time_type, geo_type, time_values, geo_value):
534+
"""Fetch Delphi's COVID-19 Surveillance Streams"""
535+
# Check parameters
536+
if data_source is None or signal is None or time_type is None or geo_type is None or time_values is None or geo_value is None:
537+
raise Exception('`data_source`, `signal`, `time_type`, `geo_type`, `time_values`, and `geo_value` are all required')
538+
# Set up request
539+
params = {
540+
'source': 'covidcast',
541+
'data_source': data_source,
542+
'signal': signal,
543+
'time_type': time_type,
544+
'geo_type': geo_type,
545+
'time_values': Epidata._list(time_values),
546+
'geo_value': geo_value,
547+
}
548+
# Make the API call
549+
return Epidata._request(params)
550+
551+
# Fetch Delphi's COVID-19 Surveillance Streams metadata
552+
@staticmethod
553+
def covidcast_meta():
554+
"""Fetch Delphi's COVID-19 Surveillance Streams metadata"""
555+
return Epidata._request({'source': 'covidcast_meta'})

0 commit comments

Comments
 (0)