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

Replace deprecated pkg_resources #667

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Python-packages/covidcast-py/covidcast/covidcast.py
Expand Up @@ -3,12 +3,12 @@
from datetime import timedelta, date
from functools import reduce
from typing import Union, Iterable, Tuple, List
from importlib.metadata import version, PackageNotFoundError

import pandas as pd
import numpy as np
from delphi_epidata import Epidata
from delphi_epidata.delphi_epidata import _HEADERS
from pkg_resources import get_distribution, DistributionNotFound
from epiweeks import Week

from .errors import NoDataWarning
Expand All @@ -18,8 +18,8 @@
Epidata.BASE_URL = "https://api.covidcast.cmu.edu/epidata"
# Prepend to Epidata client's user agent to specify this package and version
try:
_ver = get_distribution("covidcast").version
except DistributionNotFound:
_ver = version("covidcast")
except PackageNotFoundError:
_ver = "0.0.0"
_HEADERS['user-agent'] = f"covidcast/{_ver} " + _HEADERS['user-agent']

Expand Down
21 changes: 13 additions & 8 deletions Python-packages/covidcast-py/covidcast/geography.py
Expand Up @@ -4,14 +4,19 @@
from typing import Union, Iterable

import pandas as pd
import pkg_resources

COUNTY_CENSUS = pd.read_csv(
pkg_resources.resource_filename(__name__, "geo_mappings/county_census.csv"), dtype=str)
MSA_CENSUS = pd.read_csv(
pkg_resources.resource_filename(__name__, "geo_mappings/msa_census.csv"), dtype=str)
STATE_CENSUS = pd.read_csv(
pkg_resources.resource_filename(__name__, "geo_mappings/state_census.csv"), dtype=str)
import importlib_resources

county_census = importlib_resources.files(__name__) / 'geo_mappings' / 'county_census.csv'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is the top-level namespace for this sub-package, we should either prefix this variable name with an underscore or avoid creating the variable in the first place. this is especially true because its name is the lowercase version of the name of a constant, so maybe make the name more descriptive if you want to keep it around.

Suggested change
county_census = importlib_resources.files(__name__) / 'geo_mappings' / 'county_census.csv'
_county_census_file_path = importlib_resources.files(__name__) / 'geo_mappings' / 'county_census.csv'

with importlib_resources.as_file(county_census) as path:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this context manager afford us anything? it looks like pd.read_csv() should be able to accept the county_census variable directly.

COUNTY_CENSUS = pd.read_csv(path, dtype=str)

msa_census = importlib_resources.files(__name__) / 'geo_mappings' / 'msa_census.csv'
with importlib_resources.as_file(msa_census) as path:
MSA_CENSUS = pd.read_csv(path, dtype=str)

state_census = importlib_resources.files(__name__) / 'geo_mappings' / 'state_census.csv'
with importlib_resources.as_file(state_census) as path:
STATE_CENSUS = pd.read_csv(path, dtype=str)

# Filter undesired rows from CSVs.
# They're not removed from the files to keep them identical to rda files.
Expand Down
12 changes: 7 additions & 5 deletions Python-packages/covidcast-py/covidcast/plotting.py
Expand Up @@ -9,7 +9,7 @@
import imageio
import numpy as np
import pandas as pd
import pkg_resources
import importlib_resources
from matplotlib import pyplot as plt
from matplotlib import figure, axes
from tqdm import tqdm
Expand Down Expand Up @@ -182,8 +182,9 @@ def get_geo_df(data: pd.DataFrame,
raise ValueError("Unsupported geography type; "
"only `state`, `county`, `hrr`, and `msa` supported.")

shapefile_path = pkg_resources.resource_filename(__name__, SHAPEFILE_PATHS[geo_type])
geo_info = gpd.read_file(shapefile_path)
shapefile = importlib_resources.files(__name__) / SHAPEFILE_PATHS[geo_type]
with importlib_resources.as_file(shapefile) as shapefile_path:
geo_info = gpd.read_file(shapefile_path)

if geo_type == "state":
output = _join_state_geo_df(data, geo_value_col, geo_info, join_type)
Expand Down Expand Up @@ -294,8 +295,9 @@ def _plot_background_states(figsize: tuple) -> tuple:
"""
fig, ax = plt.subplots(1, figsize=figsize)
ax.axis("off")
state_shapefile_path = pkg_resources.resource_filename(__name__, SHAPEFILE_PATHS["state"])
state = gpd.read_file(state_shapefile_path)
state_shapefile = importlib_resources.files(__name__) / SHAPEFILE_PATHS["state"]
with importlib_resources.as_file(state_shapefile) as state_shapefile_path:
state = gpd.read_file(state_shapefile_path)
for state in _project_and_transform(state, "STATEFP"):
state.plot(color="0.9", ax=ax, edgecolor="0.8", linewidth=0.5)
ax.set_xlim(plt.xlim())
Expand Down
3 changes: 2 additions & 1 deletion Python-packages/covidcast-py/setup.py
Expand Up @@ -30,7 +30,8 @@
"imageio-ffmpeg",
"imageio",
"tqdm",
"epiweeks"
"epiweeks",
"importlib_resources>=1.3",
],
package_data={"covidcast": ["shapefiles/*/*", "geo_mappings/*"]}
)