Skip to content

Commit

Permalink
many fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
leliel12 committed Apr 25, 2020
1 parent 0ee04eb commit f0b349b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
14 changes: 10 additions & 4 deletions arcovid19/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ def safe_log(array):

class CasesPlot(core.Plotter):

default_plot_name_method = "curva_epi_pais"

def _plot_df(
self, *, odf, prov_name, prov_code,
confirmed, active, recovered, deceased, norm=1.
Expand Down Expand Up @@ -649,17 +651,21 @@ def grate_full_period(self, provincia=None):
return pd.Series(index=self.dates[1:], data=growth_rate)


def load_cases(url=CASES_URL, force=False):
def load_cases(cases_url=CASES_URL, areas_pop_url=AREAS_POP_URL, force=False):
"""Utility function to parse all the actual cases of the COVID-19 in
Argentina.
Parameters
----------
url: str
cases_url: str
The url for the excel table to parse. Default is ivco19 team table.
areas_pop_url: str
The url for the csv population table to parse.
Default is ivco19 team table.
force : bool (default=False)
If you want to ignore the local cache and retrieve a new value.
Expand All @@ -676,11 +682,11 @@ def load_cases(url=CASES_URL, force=False):
"""
df_infar = cache.from_cache(
tag="cases.load_cases", force=force,
function=pd.read_excel, io=url, sheet_name=0, nrows=96)
function=pd.read_excel, io=cases_url, sheet_name=0, nrows=96)

areapop = cache.from_cache(
tag="cases.load_caces[areapop]", force=force,
function=pd.read_csv, filepath_or_buffer=AREAS_POP_URL)
function=pd.read_csv, filepath_or_buffer=areas_pop_url)

# load table and replace Nan by zeros
df_infar = df_infar.fillna(0)
Expand Down
4 changes: 2 additions & 2 deletions arcovid19/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def __repr__(self):

def __call__(self, plot_name=None, ax=None, **kwargs):
"""x.__call__() == x()"""
plot_name = plot_name or ""
plot_name = plot_name or self.default_plot_name_method

if plot_name.startswith("_"):
raise ValueError(f"Invalid plot_name '{plot_name}'")

plot = getattr(self, plot_name, self.grate_full_period_all)
plot = getattr(self, plot_name)
ax = plot(ax=ax, **kwargs)
return ax

Expand Down
79 changes: 55 additions & 24 deletions tests/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

LOCAL_CASES = PATH.parent / "databases" / "cases.xlsx"

LOCAL_AREA_POP = PATH.parent / "databases" / "extra" / "arg_provs.dat"


# =============================================================================
# SETUP
Expand Down Expand Up @@ -75,39 +77,52 @@ def setup_function(func):
# =============================================================================

def test_load_cases_local():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
assert isinstance(df, arcovid19.cases.CasesFrame)


def test_delegation():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

assert repr(df) == repr(df.df)
assert df.transpose == df.df.transpose


def test_areapop():
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
assert np.all(df.areapop == df.extra["areapop"])


def test_dates():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
assert isinstance(df.dates, list)


def test_totcases():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
assert isinstance(df.tot_cases, float)


def test_last_grate():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
assert isinstance(df.last_growth_rate(), float)


def test_full_grate():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
assert isinstance(df.grate_full_period(), pd.Series)


def test_full_grate_provincias():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
for name, code in arcovid19.cases.PROVINCIAS.items():
wname = df.grate_full_period(provincia=name)
wcode = df.grate_full_period(provincia=code)
Expand All @@ -116,7 +131,8 @@ def test_full_grate_provincias():


def test_grate_provincias():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
for name, code in arcovid19.cases.PROVINCIAS.items():
wname = df.last_growth_rate(provincia=name)
wcode = df.last_growth_rate(provincia=code)
Expand All @@ -128,20 +144,23 @@ def test_grate_provincias():


def test_grate_provincia_invalida():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
with pytest.raises(ValueError):
df.last_growth_rate(provincia="colorado")


def test_get_item():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
value = df[df.provincia_status == f"CBA_C"]
expected = df.df[df.provincia_status == f"CBA_C"]
assert np.all(value == expected)


def test_restore_time_serie():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
tsdf = df.restore_time_serie()
for prov in arcovid19.cases.PROVINCIAS.values():
for code in arcovid19.cases.STATUS.values():
Expand All @@ -155,27 +174,30 @@ def test_restore_time_serie():
# =============================================================================

def test_invalid_plot_name():
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)
with pytest.raises(ValueError):
df.plot("_plot_df")


@check_figures_equal()
def test_plot_call(fig_test, fig_ref):
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

# fig test
test_ax = fig_test.subplots()
test_ax = df.plot(ax=test_ax)

# expected
exp_ax = fig_ref.subplots()
df.plot.grate_full_period_all(ax=exp_ax)
df.plot.curva_epi_pais(ax=exp_ax)


@check_figures_equal()
def test_plot_grate_full_period_all(fig_test, fig_ref):
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

# fig test
test_ax = fig_test.subplots()
Expand All @@ -188,7 +210,8 @@ def test_plot_grate_full_period_all(fig_test, fig_ref):

@check_figures_equal()
def test_plot_grate_full_period(fig_test, fig_ref):
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

# fig test
test_ax = fig_test.subplots()
Expand All @@ -201,7 +224,8 @@ def test_plot_grate_full_period(fig_test, fig_ref):

@check_figures_equal()
def test_plot_grate_full_period_all_equivalent_calls(fig_test, fig_ref):
cases = arcovid19.load_cases(url=LOCAL_CASES)
cases = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

# fig test
fig_test.set_size_inches(12, 12)
Expand All @@ -224,7 +248,8 @@ def test_plot_grate_full_period_all_equivalent_calls(fig_test, fig_ref):

@check_figures_equal()
def test_plot_time_serie_all(fig_test, fig_ref):
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

# fig test
test_ax = fig_test.subplots()
Expand All @@ -237,7 +262,8 @@ def test_plot_time_serie_all(fig_test, fig_ref):

@check_figures_equal()
def test_plot_time_serie(fig_test, fig_ref):
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

# fig test
test_ax = fig_test.subplots()
Expand All @@ -250,7 +276,8 @@ def test_plot_time_serie(fig_test, fig_ref):

@check_figures_equal()
def test_plot_time_serie_all_equivalent_calls(fig_test, fig_ref):
cases = arcovid19.load_cases(url=LOCAL_CASES)
cases = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

# fig test
fig_test.set_size_inches(12, 12)
Expand All @@ -273,7 +300,8 @@ def test_plot_time_serie_all_equivalent_calls(fig_test, fig_ref):

@check_figures_equal()
def test_plot_barplot(fig_test, fig_ref):
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

# fig test
test_ax = fig_test.subplots()
Expand All @@ -286,7 +314,8 @@ def test_plot_barplot(fig_test, fig_ref):

@check_figures_equal()
def test_plot_grate_full_period_all_commented(fig_test, fig_ref):
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

# fig test
test_ax = fig_test.subplots()
Expand All @@ -299,7 +328,8 @@ def test_plot_grate_full_period_all_commented(fig_test, fig_ref):

@check_figures_equal()
def test_plot_boxplot(fig_test, fig_ref):
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

# fig test
test_ax = fig_test.subplots()
Expand All @@ -319,7 +349,8 @@ def test_plot_boxplot(fig_test, fig_ref):
"time_serie", "time_serie_all",
"grate_full_period", "grate_full_period_all"])
def test_plot_all_dates_ticks(plot_name):
df = arcovid19.load_cases(url=LOCAL_CASES)
df = arcovid19.load_cases(
cases_url=LOCAL_CASES, areas_pop_url=LOCAL_AREA_POP)

expected = [str(d.date()) for d in df.dates]

Expand Down

0 comments on commit f0b349b

Please sign in to comment.