-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrun.py
64 lines (55 loc) · 2.34 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Functions to call when running the function.
This module should contain a function called `run_module`, that is executed
when the module is run with `python -m MODULE_NAME`.
"""
import glob
import multiprocessing as mp
import subprocess
from delphi_utils import read_params
from .constants import SIGNALS, GEO_RESOLUTIONS
from .process import process, files_in_past_week
def run_module():
"""Creates the Safegraph indicator."""
params = read_params()
export_dir = params["export_dir"]
raw_data_dir = params["raw_data_dir"]
n_core = int(params["n_core"])
aws_access_key_id = params["aws_access_key_id"]
aws_secret_access_key = params["aws_secret_access_key"]
aws_default_region = params["aws_default_region"]
aws_endpoint = params["aws_endpoint"]
wip_signal = params["wip_signal"]
def process_file(current_filename):
"""Wrapper around `process()` that only takes a single argument.
A single argument function is necessary to use `pool.map()` below.
Because each call to `process()` has two arguments that are dependent
on the input file name (`current_filename` and `previous_filenames`),
we choose to use this wrapper rather than something like
`functools.partial()`.
"""
return process(current_filename,
files_in_past_week(current_filename),
signal_names=SIGNALS,
wip_signal=wip_signal,
geo_resolutions=GEO_RESOLUTIONS,
export_dir=export_dir,
)
# Update raw data
# Why call subprocess rather than using a native Python client, e.g. boto3?
# Because boto3 does not have a simple rsync-like call that can perform
# the following behavior elegantly.
subprocess.run(
f'aws s3 sync s3://sg-c19-response/social-distancing/v2/ '
f'{raw_data_dir}/social-distancing/ --endpoint {aws_endpoint}',
env={
'AWS_ACCESS_KEY_ID': aws_access_key_id,
'AWS_SECRET_ACCESS_KEY': aws_secret_access_key,
'AWS_DEFAULT_REGION': aws_default_region,
},
shell=True,
check=True,
)
files = glob.glob(f'{raw_data_dir}/social-distancing/**/*.csv.gz',
recursive=True)
with mp.Pool(n_core) as pool:
pool.map(process_file, files)