Skip to content

Commit

Permalink
relaxed plot tests, fixes #433
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmartin8 committed Nov 26, 2022
1 parent b326f56 commit 00d7d39
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ def test_correlation_plot():
df = get_data()
S = risk_models.CovarianceShrinkage(df).ledoit_wolf()
ax = plotting.plot_covariance(S, showfig=False)
assert len(ax.findobj()) == 256
assert len(ax.findobj()) > 250
plt.clf()
ax = plotting.plot_covariance(S, plot_correlation=True, showfig=False)
assert len(ax.findobj()) == 256
assert len(ax.findobj()) > 250
plt.clf()
ax = plotting.plot_covariance(S, show_tickers=False, showfig=False)
assert len(ax.findobj()) == 136
assert len(ax.findobj()) > 130
plt.clf()
ax = plotting.plot_covariance(
S, plot_correlation=True, show_tickers=False, showfig=False
)
assert len(ax.findobj()) == 136
assert len(ax.findobj()) > 130
plt.clf()

temp_folder = tempfile.TemporaryDirectory()
temp_folder_path = temp_folder.name
plot_filename = os.path.join(temp_folder_path, "plot.png")
ax = plotting.plot_covariance(S, filename=plot_filename, showfig=False)
assert len(ax.findobj()) == 256
assert len(ax.findobj()) > 250
assert os.path.exists(plot_filename)
assert os.path.getsize(plot_filename) > 0
temp_folder.cleanup()
Expand All @@ -51,12 +51,12 @@ def test_dendrogram_plot():
hrp.optimize()

ax = plotting.plot_dendrogram(hrp, showfig=False)
assert len(ax.findobj()) == 185
assert len(ax.findobj()) > 180
assert type(ax.findobj()[0]) == matplotlib.collections.LineCollection
plt.clf()

ax = plotting.plot_dendrogram(hrp, show_tickers=False, showfig=False)
assert len(ax.findobj()) == 65
assert len(ax.findobj()) > 60
assert type(ax.findobj()[0]) == matplotlib.collections.LineCollection
plt.clf()
plt.close()
Expand All @@ -71,7 +71,7 @@ def test_dendrogram_plot():
str(w[0].message)
== "hrp param has not been optimized. Attempting optimization."
)
assert len(ax.findobj()) == 65
assert len(ax.findobj()) > 60
assert type(ax.findobj()[0]) == matplotlib.collections.LineCollection
plt.clf()
plt.close()
Expand All @@ -85,11 +85,11 @@ def test_cla_plot():
cla = CLA(rets, S)

ax = plotting.plot_efficient_frontier(cla, showfig=False)
assert len(ax.findobj()) == 142
assert len(ax.findobj()) > 130
plt.clf()

ax = plotting.plot_efficient_frontier(cla, show_assets=False, showfig=False)
assert len(ax.findobj()) == 160
assert len(ax.findobj()) > 150
plt.clf()
plt.close()

Expand All @@ -103,7 +103,7 @@ def test_cla_plot_ax():

fig, ax = plt.subplots(figsize=(12, 10))
plotting.plot_efficient_frontier(cla, ax=ax)
assert len(ax.findobj()) == 142
assert len(ax.findobj()) > 130
plt.close()
plt.close()

Expand All @@ -112,15 +112,15 @@ def test_default_ef_plot():
plt.figure()
ef = setup_efficient_frontier()
ax = plotting.plot_efficient_frontier(ef, show_assets=True)
assert len(ax.findobj()) == 124
assert len(ax.findobj()) > 120
plt.clf()

# with constraints
ef = setup_efficient_frontier()
ef.add_constraint(lambda x: x <= 0.15)
ef.add_constraint(lambda x: x[0] == 0.05)
ax = plotting.plot_efficient_frontier(ef)
assert len(ax.findobj()) == 124
assert len(ax.findobj()) > 120
plt.clf()
plt.close()

Expand All @@ -129,7 +129,7 @@ def test_default_ef_plot_labels():
plt.figure()
ef = setup_efficient_frontier()
ax = plotting.plot_efficient_frontier(ef, show_assets=True, show_tickers=True)
assert len(ax.findobj()) == 124 + len(ef.tickers)
assert len(ax.findobj()) > 125
plt.clf()


Expand All @@ -140,7 +140,7 @@ def test_ef_plot_utility():
ax = plotting.plot_efficient_frontier(
ef, ef_param="utility", ef_param_range=delta_range, showfig=False
)
assert len(ax.findobj()) == 124
assert len(ax.findobj()) > 120
plt.clf()
plt.close()

Expand Down Expand Up @@ -174,7 +174,7 @@ def test_ef_plot_risk():
ax = plotting.plot_efficient_frontier(
ef, ef_param="risk", ef_param_range=risk_range, showfig=False
)
assert len(ax.findobj()) == 124
assert len(ax.findobj()) > 120
plt.clf()
plt.close()

Expand All @@ -188,7 +188,7 @@ def test_ef_plot_return():
ax = plotting.plot_efficient_frontier(
ef, ef_param="return", ef_param_range=return_range, showfig=False
)
assert len(ax.findobj()) == 124
assert len(ax.findobj()) > 120
plt.clf()
plt.close()

Expand All @@ -202,7 +202,7 @@ def test_ef_plot_utility_short():
ax = plotting.plot_efficient_frontier(
ef, ef_param="utility", ef_param_range=delta_range, showfig=False
)
assert len(ax.findobj()) == 160
assert len(ax.findobj()) > 150
plt.clf()
plt.close()

Expand All @@ -218,7 +218,7 @@ def test_constrained_ef_plot_utility():
ax = plotting.plot_efficient_frontier(
ef, ef_param="utility", ef_param_range=delta_range, showfig=False
)
assert len(ax.findobj()) == 124
assert len(ax.findobj()) > 120
plt.clf()
plt.close()

Expand All @@ -238,7 +238,7 @@ def test_constrained_ef_plot_risk():
ax = plotting.plot_efficient_frontier(
ef, ef_param="risk", ef_param_range=risk_range, show_assets=True, showfig=False
)
assert len(ax.findobj()) == 136
assert len(ax.findobj()) > 130
plt.clf()
plt.close()

Expand All @@ -251,7 +251,7 @@ def test_weight_plot():
w = hrp.optimize()

ax = plotting.plot_weights(w, showfig=False)
assert len(ax.findobj()) == 197
assert len(ax.findobj()) > 190
plt.clf()
plt.close()

Expand All @@ -267,8 +267,8 @@ def test_weight_plot_multi():
plotting.plot_weights(w2, ax2, showfig=False)

assert len(fig.axes) == 2
assert len(fig.axes[0].findobj()) == 209
assert len(fig.axes[1].findobj()) == 209
assert len(fig.axes[0].findobj()) > 200
assert len(fig.axes[1].findobj()) > 200
plt.close()


Expand All @@ -279,7 +279,7 @@ def test_weight_plot_add_attribute():
w = ef.min_volatility()
ax = plotting.plot_weights(w)
ax.set_title("Test")
assert len(ax.findobj()) == 209
assert len(ax.findobj()) > 200
plt.close()


Expand Down

0 comments on commit 00d7d39

Please sign in to comment.