mirror of
https://github.com/robertmartin8/PyPortfolioOpt.git
synced 2022-11-27 18:02:41 +03:00
relaxed plot tests, fixes #433
This commit is contained in:
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -112,7 +112,7 @@ 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
|
||||
@@ -120,7 +120,7 @@ def test_default_ef_plot():
|
||||
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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user