-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add workflow for reading CSV from s3, cleaning, saving to Parquet #738
Draft
douglasdavis
wants to merge
22
commits into
main
Choose a base branch
from
add-workflow-from-csv-to-parquet2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3a76080
Add matplotlib arxiv workflow
jrbourbeau 5aedaca
Remove stray print
jrbourbeau e185e5d
Merge branch 'main' of https://github.com/coiled/coiled-runtime into …
jrbourbeau 95585d4
Update fixture name
jrbourbeau 10a529b
Only use requester_pays for test_embarassingly_parallel
jrbourbeau 4504020
Rerun CI
jrbourbeau 96e66f5
Update instance type
jrbourbeau 0779b48
Run workflows on demand and during nightly cron job
jrbourbeau 7fb6792
Use specific range of years
jrbourbeau 437eb0b
Merge branch 'main' of https://github.com/coiled/coiled-runtime into …
jrbourbeau e4851df
Light asserts
jrbourbeau 2657885
add workflow
douglasdavis 6eb04d6
show something with use of pytest -s
douglasdavis 6f4c04e
Merge remote-tracking branch 'origin/main' into add-workflow-from-csv…
douglasdavis 222c695
rm unnecessary noqa comments
douglasdavis fc40687
var name
douglasdavis 670e3cc
adjust tests.yml based on James' suggestion
douglasdavis 16b0277
write some parquet to s3
douglasdavis b557bc5
this version actually passes
douglasdavis ccedaf8
check if read works
douglasdavis 37120c3
works with some excluded files
douglasdavis b3cfbaa
rm unnecessary line
douglasdavis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import io | ||
import tarfile | ||
import uuid | ||
|
||
import coiled | ||
import pandas as pd | ||
import pytest | ||
from dask.distributed import Client, wait | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def embarrassingly_parallel_cluster( | ||
dask_env_variables, | ||
cluster_kwargs, | ||
github_cluster_tags, | ||
): | ||
with coiled.Cluster( | ||
f"embarrassingly-parallel-{uuid.uuid4().hex[:8]}", | ||
environ=dask_env_variables, | ||
tags=github_cluster_tags, | ||
**cluster_kwargs["embarrassingly_parallel_cluster"], | ||
) as cluster: | ||
yield cluster | ||
|
||
|
||
@pytest.fixture | ||
def embarrassingly_parallel_client( | ||
embarrassingly_parallel_cluster, | ||
cluster_kwargs, | ||
upload_cluster_dump, | ||
benchmark_all, | ||
): | ||
n_workers = cluster_kwargs["embarrassingly_parallel_cluster"]["n_workers"] | ||
with Client(embarrassingly_parallel_cluster) as client: | ||
embarrassingly_parallel_cluster.scale(n_workers) | ||
client.wait_for_workers(n_workers) | ||
client.restart() | ||
with upload_cluster_dump(client), benchmark_all(client): | ||
yield client | ||
|
||
|
||
def test_embarassingly_parallel(embarrassingly_parallel_client, s3_factory): | ||
# How popular is matplotlib? | ||
s3 = s3_factory(requester_pays=True) | ||
directories = s3.ls("s3://arxiv/pdf") | ||
|
||
# We only analyze files from 1991-2022 here in order to have a consistent data volume. | ||
# This is benchmarking purposes only, as this dataset is updated monthly. | ||
years = list(range(91, 100)) + list(range(23)) | ||
directories = [ | ||
d | ||
for d in directories | ||
if d.endswith(".tar") and int(d.split("_")[2][:2]) in years | ||
] | ||
|
||
def extract(filename: str, fs): | ||
"""Extract and process one directory of arXiv data | ||
|
||
Returns | ||
------- | ||
filename: str | ||
contains_matplotlib: boolean | ||
""" | ||
out = [] | ||
with fs.open(filename) as f: | ||
bytes_ = f.read() | ||
with io.BytesIO() as bio: | ||
bio.write(bytes_) | ||
bio.seek(0) | ||
with tarfile.TarFile(fileobj=bio) as tf: | ||
for member in tf.getmembers(): | ||
if member.isfile() and member.name.endswith(".pdf"): | ||
data = tf.extractfile(member).read() | ||
out.append((member.name, b"matplotlib" in data.lower())) | ||
return out | ||
|
||
futures = embarrassingly_parallel_client.map(extract, directories, fs=s3) | ||
wait(futures) | ||
# We had one error in one file. Let's just ignore and move on. | ||
good = [future for future in futures if future.status == "finished"] | ||
data = embarrassingly_parallel_client.gather(good) | ||
|
||
# Convert to Pandas | ||
dfs = [pd.DataFrame(d, columns=["filename", "has_matplotlib"]) for d in data] | ||
df = pd.concat(dfs) | ||
|
||
def filename_to_date(filename): | ||
year = int(filename.split("/")[0][:2]) | ||
month = int(filename.split("/")[0][2:4]) | ||
if year > 80: | ||
year = 1900 + year | ||
else: | ||
year = 2000 + year | ||
|
||
return pd.Timestamp(year=year, month=month, day=1) | ||
|
||
df["date"] = df.filename.map(filename_to_date) | ||
result = df.groupby("date").has_matplotlib.mean() | ||
# Some light validation to ensure results are consistent. | ||
# This is only for benchmarking. | ||
assert result.idxmin() == pd.Timestamp("1991-07-01") # Earliest timestamp | ||
assert result.idxmax() == pd.Timestamp("2022-10-01") # Row with maximum value | ||
assert result.ne(0).idxmax() == pd.Timestamp("2005-06-01") # First non-zero row |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import os | ||
import uuid | ||
|
||
import coiled | ||
import dask.dataframe as dd | ||
import pytest | ||
from distributed import Client, LocalCluster, wait # noqa | ||
|
||
LOCAL_RUN = os.environ.get("LOCAL_WORKFLOW_RUN") | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def from_csv_to_parquet_cluster( | ||
dask_env_variables, | ||
cluster_kwargs, | ||
github_cluster_tags, | ||
): | ||
if LOCAL_RUN is not None: | ||
with LocalCluster() as cluster: | ||
yield cluster | ||
else: | ||
with coiled.Cluster( | ||
f"from-csv-to-parquet-{uuid.uuid4().hex[:8]}", | ||
environ=dask_env_variables, | ||
tags=github_cluster_tags, | ||
**cluster_kwargs["from_csv_to_parquet_cluster"], | ||
) as cluster: | ||
yield cluster | ||
|
||
|
||
@pytest.fixture | ||
def from_csv_to_parquet_client( | ||
from_csv_to_parquet_cluster, | ||
cluster_kwargs, | ||
upload_cluster_dump, | ||
benchmark_all, | ||
): | ||
if LOCAL_RUN is not None: | ||
with Client(from_csv_to_parquet_cluster) as client: | ||
yield client | ||
else: | ||
n_workers = cluster_kwargs["from_csv_to_parquet_cluster"]["n_workers"] | ||
with Client(from_csv_to_parquet_cluster) as client: | ||
from_csv_to_parquet_cluster.scale(n_workers) | ||
client.wait_for_workers(n_workers) | ||
client.restart() | ||
with upload_cluster_dump(client), benchmark_all(client): | ||
yield client | ||
|
||
|
||
COLUMNSV1 = { | ||
"GlobalEventID": "Int64", | ||
"Day": "Int64", | ||
"MonthYear": "Int64", | ||
"Year": "Int64", | ||
"FractionDate": "Float64", | ||
"Actor1Code": "string[pyarrow]", | ||
"Actor1Name": "string[pyarrow]", | ||
"Actor1CountryCode": "string[pyarrow]", | ||
"Actor1KnownGroupCode": "string[pyarrow]", | ||
"Actor1EthnicCode": "string[pyarrow]", | ||
"Actor1Religion1Code": "string[pyarrow]", | ||
"Actor1Religion2Code": "string[pyarrow]", | ||
"Actor1Type1Code": "string[pyarrow]", | ||
"Actor1Type2Code": "string[pyarrow]", | ||
"Actor1Type3Code": "string[pyarrow]", | ||
"Actor2Code": "string[pyarrow]", | ||
"Actor2Name": "string[pyarrow]", | ||
"Actor2CountryCode": "string[pyarrow]", | ||
"Actor2KnownGroupCode": "string[pyarrow]", | ||
"Actor2EthnicCode": "string[pyarrow]", | ||
"Actor2Religion1Code": "string[pyarrow]", | ||
"Actor2Religion2Code": "string[pyarrow]", | ||
"Actor2Type1Code": "string[pyarrow]", | ||
"Actor2Type2Code": "string[pyarrow]", | ||
"Actor2Type3Code": "string[pyarrow]", | ||
"IsRootEvent": "Int64", | ||
"EventCode": "string[pyarrow]", | ||
"EventBaseCode": "string[pyarrow]", | ||
"EventRootCode": "string[pyarrow]", | ||
"QuadClass": "Int64", | ||
"GoldsteinScale": "Float64", | ||
"NumMentions": "Int64", | ||
"NumSources": "Int64", | ||
"NumArticles": "Int64", | ||
"AvgTone": "Float64", | ||
"Actor1Geo_Type": "Int64", | ||
"Actor1Geo_Fullname": "string[pyarrow]", | ||
"Actor1Geo_CountryCode": "string[pyarrow]", | ||
"Actor1Geo_ADM1Code": "string[pyarrow]", | ||
"Actor1Geo_Lat": "Float64", | ||
"Actor1Geo_Long": "Float64", | ||
"Actor1Geo_FeatureID": "string[pyarrow]", | ||
"Actor2Geo_Type": "Int64", | ||
"Actor2Geo_Fullname": "string[pyarrow]", | ||
"Actor2Geo_CountryCode": "string[pyarrow]", | ||
"Actor2Geo_ADM1Code": "string[pyarrow]", | ||
"Actor2Geo_Lat": "Float64", | ||
"Actor2Geo_Long": "Float64", | ||
"Actor2Geo_FeatureID": "string[pyarrow]", | ||
"ActionGeo_Type": "Int64", | ||
"ActionGeo_Fullname": "string[pyarrow]", | ||
"ActionGeo_CountryCode": "string[pyarrow]", | ||
"ActionGeo_ADM1Code": "string[pyarrow]", | ||
"ActionGeo_Lat": "Float64", | ||
"ActionGeo_Long": "Float64", | ||
"ActionGeo_FeatureID": "string[pyarrow]", | ||
"DATEADDED": "Int64", | ||
"SOURCEURL": "string[pyarrow]", | ||
} | ||
|
||
|
||
def test_from_csv_to_parquet(from_csv_to_parquet_client, s3_factory): | ||
s3 = s3_factory(anon=True) | ||
df = dd.read_csv( | ||
"s3://gdelt-open-data/events/*.csv", | ||
names=COLUMNSV1.keys(), | ||
sep="\t", | ||
dtype=COLUMNSV1, | ||
storage_options=s3.storage_options, | ||
) | ||
|
||
df = df.partitions[-10:] | ||
|
||
result = from_csv_to_parquet_client.compute(df.GoldsteinScale.mean()) # noqa | ||
print(result) | ||
assert df.GlobalEventID.dtype == "Int64" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checking -- is
anon=True
needed to access the dataset?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, I'll give it a test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it is necessary