-
Notifications
You must be signed in to change notification settings - Fork 68
[Refactor] Clarify a few csv_importer
types and names
#949
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 |
---|---|---|
@@ -1,19 +1,31 @@ | ||
"""Collects and reads covidcast data from a set of local CSV files.""" | ||
|
||
# standard library | ||
from dataclasses import dataclass | ||
from datetime import date | ||
import glob | ||
import os | ||
import re | ||
|
||
# third party | ||
import pandas | ||
import pandas as pd | ||
import epiweeks as epi | ||
|
||
# first party | ||
from delphi_utils import Nans | ||
from delphi.utils.epiweek import delta_epiweeks | ||
from delphi.epidata.acquisition.covidcast.logger import get_structured_logger | ||
from .logger import get_structured_logger | ||
|
||
@dataclass | ||
class CsvRowValue: | ||
"""A container for the values of a single validated covidcast CSV row.""" | ||
geo_value: str | ||
value: float | ||
stderr: float | ||
sample_size: float | ||
missing_value: int | ||
missing_stderr: int | ||
missing_sample_size: int | ||
|
||
class CsvImporter: | ||
"""Finds and parses covidcast CSV files.""" | ||
|
@@ -37,6 +49,7 @@ class CsvImporter: | |
MIN_YEAR = 2019 | ||
MAX_YEAR = 2030 | ||
|
||
# The datatypes expected by pandas.read_csv. Int64 is like float in that it can handle both numbers and nans. | ||
DTYPES = { | ||
"geo_id": str, | ||
"val": float, | ||
|
@@ -47,20 +60,6 @@ class CsvImporter: | |
"missing_sample_size": "Int64" | ||
} | ||
|
||
# NOTE: this should be a Python 3.7+ `dataclass`, but the server is on 3.4 | ||
# See https://docs.python.org/3/library/dataclasses.html | ||
class RowValues: | ||
"""A container for the values of a single covidcast row.""" | ||
|
||
def __init__(self, geo_value, value, stderr, sample_size, missing_value, missing_stderr, missing_sample_size): | ||
self.geo_value = geo_value | ||
self.value = value | ||
self.stderr = stderr | ||
self.sample_size = sample_size | ||
self.missing_value = missing_value | ||
self.missing_stderr = missing_stderr | ||
self.missing_sample_size = missing_sample_size | ||
|
||
@staticmethod | ||
def is_sane_day(value): | ||
"""Return whether `value` is a sane (maybe not valid) YYYYMMDD date. | ||
|
@@ -184,7 +183,7 @@ def is_header_valid(columns): | |
return set(columns) >= CsvImporter.REQUIRED_COLUMNS | ||
|
||
@staticmethod | ||
def floaty_int(value): | ||
def floaty_int(value: str) -> int: | ||
"""Cast a string to an int, even if it looks like a float. | ||
|
||
For example, "-1" and "-1.0" should both result in -1. Non-integer floats | ||
|
@@ -253,7 +252,7 @@ def validate_missing_code(row, attr_quantity, attr_name, filepath=None, logger=N | |
|
||
@staticmethod | ||
def extract_and_check_row(row, geo_type, filepath=None): | ||
"""Extract and return `RowValues` from a CSV row, with sanity checks. | ||
"""Extract and return `CsvRowValue` from a CSV row, with sanity checks. | ||
|
||
Also returns the name of the field which failed sanity check, or None. | ||
|
||
|
@@ -330,14 +329,10 @@ def extract_and_check_row(row, geo_type, filepath=None): | |
missing_sample_size = CsvImporter.validate_missing_code(row, sample_size, "sample_size", filepath) | ||
|
||
# return extracted and validated row values | ||
row_values = CsvImporter.RowValues( | ||
geo_id, value, stderr, sample_size, | ||
missing_value, missing_stderr, missing_sample_size | ||
) | ||
return (row_values, None) | ||
return (CsvRowValue(geo_id, value, stderr, sample_size, missing_value, missing_stderr, missing_sample_size), None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this harder to read but I will hold my nose and accept it as the way of the future |
||
|
||
@staticmethod | ||
def load_csv(filepath, geo_type, pandas=pandas): | ||
def load_csv(filepath, geo_type): | ||
"""Load, validate, and yield data as `RowValues` from a CSV file. | ||
|
||
filepath: the CSV file to be loaded | ||
|
@@ -349,10 +344,10 @@ def load_csv(filepath, geo_type, pandas=pandas): | |
logger = get_structured_logger('load_csv') | ||
|
||
try: | ||
table = pandas.read_csv(filepath, dtype=CsvImporter.DTYPES) | ||
table = pd.read_csv(filepath, dtype=CsvImporter.DTYPES) | ||
except ValueError as e: | ||
logger.warning(event='Failed to open CSV with specified dtypes, switching to str', detail=str(e), file=filepath) | ||
table = pandas.read_csv(filepath, dtype='str') | ||
table = pd.read_csv(filepath, dtype='str') | ||
|
||
if not CsvImporter.is_header_valid(table.columns): | ||
logger.warning(event='invalid header', detail=table.columns, file=filepath) | ||
|
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -6,8 +6,7 @@ | |
import unittest | ||
from unittest.mock import MagicMock | ||
|
||
from delphi.epidata.acquisition.covidcast.csv_to_database import get_argument_parser, main, \ | ||
collect_files, upload_archive, make_handlers | ||
from delphi.epidata.acquisition.covidcast.csv_to_database import get_argument_parser, main, collect_files, upload_archive, make_handlers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh same, I don't like it but it's fine |
||
|
||
# py3tester coverage target | ||
__test_target__ = 'delphi.epidata.acquisition.covidcast.csv_to_database' | ||
|
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.
🏆