def test_graph2(self): """ This test is failing with Python 3.4 if many pictures are drawn. """ fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if is_travis_or_appveyor() == "appveyor": return fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache2") stock = StockPrices("BNP.PA", folder=cache) stock2 = StockPrices("CA.PA", folder=cache) fig, ax = plt.subplots(figsize=(16, 8)) ax = stock.plot(ax=ax) ax = stock2.plot(ax=ax) img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image2.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') assert os.path.exists(img)
def test_graph_corrplot(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if "travis" in sys.executable: # it requires scipy which is not included in the requirements.txt return fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt plt.style.use('ggplot') temp = get_temp_folder(__file__, "temp_corrplot") letters = "ABCDEFGHIJKLMNOP"[0:10] df = pandas.DataFrame( dict(((k, numpy.random.random(10) + ord(k) - 65) for k in letters))) df = df.corr() fig = plt.figure(num=None, facecolor='white') ax = plt.subplot(1, 1, 1, aspect='equal', axisbg='white') c = Corrplot(df) ax = c.plot(fig=fig, ax=ax) fLOG("save") img = os.path.join(temp, "corrplot.png") fig.savefig(img) fLOG("close") plt.close('all') fLOG("end") assert os.path.exists(img)
def test_example_pydy(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_example_pydy") fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 8)) try: example_pydy(ax=ax) except Exception as e: if 'can only concatenate list (not "tuple") to list' in str(e): warnings.warn("Pydy needs to be updated for Python 3.7") return else: raise e self.assertNotEmpty(ax) img = os.path.join(temp, "img.png") fig.savefig(img) self.assertExists(img) if __name__ == "__main__": fig.show() plt.close('all') fLOG("end")
def test_graph4(self): """ This test is failing with Python 3.4 if many pictures are drawn. """ fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache4") stock = StockPrices(TestStockGraph4.tick, folder=cache, url=TestStockGraph4.source) ret = stock.returns()["2012-04-01":"2014-04-15"] fig, ax = plt.subplots(figsize=(16, 8)) ax = stock.plot(ax=ax) ax = ret.plot(ax=ax, axis=2) img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image4.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img)
def test_american_cities(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[:2] <= (3, 4): warnings.warn( "Issue with Python 3.4, bug probably related to wrong pointers") return fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt import mpld3 filter = {"NewYork": "NY", "Chicago": "CH", "SanFrancisco": "SF", "Seattle": "Sea"} temp = get_temp_folder(__file__, "temp_american_cities") data = os.path.join(temp, "..", "data", "american_cities.txt") df = pandas.read_csv(data) df["Longitude"] = -df["Longitude"] df["City"] = df["City"].apply(lambda v: filter.get(v, "")) fig, ax = plt.subplots(figsize=(32, 32)) df = df[df.Latitude < 52] df = df[df.Longitude > -130].copy() ax = graph_cities(df, ax=ax, markersize=3, fontcolor=(0, 1.0, 0), fontsize='40', fontname="Courrier", fontweight="bold") assert ax is not None img = os.path.join(temp, "img.png") fig.savefig(img) assert os.path.exists(img) name2 = os.path.join(temp, "picture.html") mpld3.save_html(fig, name2) assert os.path.exists(name2) if __name__ == "__main__": fig.show() plt.close('all') fLOG("end")
def test_notebook_hackathon(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if is_travis_or_appveyor() == "appveyor": return fix_tkinter_issues_virtualenv() temp = get_temp_folder(__file__, "temp_hackathon_2015") keepnote = ls_notebooks("hackathon_2015") assert len(keepnote) > 0 keepnote = [ _ for _ in keepnote if "database_schemas" not in _ and "process_clean_files" not in _ and "times_series" not in _ and "upload" not in _] def valid_cell(cell): if "%blob" in cell: return False if "blob_service" in cell: return False return True if len(keepnote) > 0: for _ in keepnote: fLOG(_) res = execute_notebooks(temp, keepnote, filter=lambda i, n: True, valid_cell=valid_cell, fLOG=fLOG, clean_function=clean_function_notebook) unittest_raise_exception_notebook(res, fLOG) else: warnings.warn("No notebook was tested for the hackathon 2015")
def test_interactive2_RadioWidget(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv(fLOG=fLOG) import matplotlib.pyplot as plt def plot(amplitude, color, sele): fig, ax = plt.subplots(figsize=(4, 3), subplot_kw={'axisbelow': True}) ax.grid(color='w', linewidth=2, linestyle='solid') x = np.linspace(0, 10, 1000) ax.plot(x, amplitude * np.sin(x), color=color, lw=5, alpha=0.4) ax.set_xlim(0, 10) ax.set_ylim(-1.1, 1.1) return fig res = StaticInteract(plot, amplitude=RangeWidget(0.1, 0.3, 0.1, default=0.2), color=RadioWidget( ['blue', 'green'], default='blue'), sele=DropDownWidget(['a', 'b'])) self.assertNotEmpty(res) ht = res.html() self.assertNotEmpty(ht) plt.close('all') fLOG("end")
def test_graph_ohlc(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache_ohlc") stock = StockPrices(TestStockGraphOHLC.tick, folder=cache, url=TestStockGraphOHLC.source) fig, ax = plt.subplots(figsize=(16, 8)) ax = stock.plot(ax=ax, field="ohlc") img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image_ohlc.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img)
def test_graph4(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache4") stock = StockPrices(TestStockGraph4.tick, folder=cache, url=TestStockGraph4.source) ret = stock.returns()["2012-04-01":"2014-04-15"] fig, ax = plt.subplots(figsize=(16, 8)) ax = stock.plot(ax=ax) ax = ret.plot(ax=ax, axis=2) img = os.path.abspath( os.path.join(os.path.split(__file__)[0], "temp_image4.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img)
def test_viz_scatter_scatter_xyid(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[:2] <= (3, 4): return fix_tkinter_issues_virtualenv() if __name__ == "__main__": rcParams["backend"] = "TkAgg" fold = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.join(fold, "temp_graph_xyz") im = os.path.join(fold, self._testMethodName + ".png") if not os.path.exists(fold): os.mkdir(fold) for img in [im]: if os.path.exists(img): os.remove(img) nuage1 = self.generate_gauss(0, 0, 3) nuage2 = self.generate_gauss(3, 4, 2) nuage = [(a, b, 0) for a, b in nuage1] + [(a, b, 1) for a, b in nuage2] matplotlib.use(matplotlib.get_backend(), warn=False, force=True) fig, ax = scatter_xy_id( nuage, title=self._testMethodName, legend={0: "c0", 1: "c1"}) fig.savefig(im) assert os.path.exists(im) if __name__ == "__main__": rcParams["backend"] = TestMatplotlibHelperVizScatterPlots.backend plt.close('all') fLOG(plt.style.available)
def test_graph_XY_date(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_graph_XY") fix_tkinter_issues_virtualenv(fLOG=fLOG) from matplotlib import pyplot as plt df = TableFormula( dict(xx=[ datetime(2015, 1, 1), datetime(2016, 1, 1), datetime(2017, 1, 1) ], yy=[1.1, 1.2, 13])) subplot_kw = dict(aspect='equal', facecolor='white') fig, ax = plt.subplots(1, 1, subplot_kw=subplot_kw) df.graph_XY([[lambda row: row["xx"], lambda row: row["yy"], "xyl"]], ax=ax, title="a title") img = os.path.join(temp, "graph_XY.png") fig.savefig(img) # if __name__ == "__main__": # plt.show() plt.close('all') self.assertTrue(os.path.exists(img))
def test_graph(self): """ This test is failing with Python 3.4 if many pictures are drawn. """ fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache2") stocks = [ StockPrices(t, folder=cache, url=TestStockGraph.source) for t in TestStockGraph.tick ] if True: fLOG("A", sys.executable) fig, ax = plt.subplots() ax = StockPrices.draw(stocks, figsize=(16, 8), field=["Open", "Close"], ax=ax) img = os.path.abspath( os.path.join(os.path.split(__file__)[0], "temp_image.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) if True: fLOG("B") fig, ax = plt.subplots() ax = StockPrices.draw(stocks, begin="2010-01-01", ax=ax) img = os.path.abspath( os.path.join(os.path.split(__file__)[0], "temp_image2.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) if True: fLOG("C") fig, ax = plt.subplots() ax = StockPrices.draw(stocks[:1], begin="2010-01-01", ax=ax) img = os.path.abspath( os.path.join(os.path.split(__file__)[0], "temp_image3.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) fLOG("thisend")
def setUp(self): add_missing_development_version([ "pyensae", "pymyinstall", "pymmails", "pyrsslocal", "mlstatpy", "jyquickhelper" ], __file__, hide=True) fix_tkinter_issues_virtualenv()
def test_long_init(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fLOG("do nothing") fix_tkinter_issues_virtualenv()
def test_graph_style(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv(fLOG=fLOG) mg = MagicGraph() cmd = "ggplot" mg.mpl_style(cmd)
def test_american_cities(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() temp = get_temp_folder(__file__, "temp_matplotlib_video") name = os.path.join(temp, "..", "data", "american_cities.txt") img = os.path.join(temp, "img.png") res = american_cities(name, 40, img, fLOG) self.assertNotEmpty(res)
def test_faq_matplotlib(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 1], [1, 1]) change_legend_location(ax) avoid_overlapping_dates(fig) plt.close('all')
def test_faq_matplotlib(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 1], [1, 1]) change_legend_location(ax) avoid_overlapping_dates(fig) plt.close('all')
def test_plot_gallery(self): temp = get_temp_folder(__file__, "temp_plot_gallery") zipimg = os.path.join(temp, "..", "..", "..", "_doc", "notebooks", "explore", "data", "dog-cat-pixabay.zip") files = unzip_files(zipimg, where_to=temp) fix_tkinter_issues_virtualenv(fLOG=noLOG) from matplotlib import pyplot as plt fig, _ = plot_gallery_images(files[:2], return_figure=True) img = os.path.join(temp, "gallery.png") fig.savefig(img) plt.close('all')
def test_american_cities(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[:2] <= (3, 4): warnings.warn( "Issue with Python 3.4, bug probably related to wrong pointers") return fix_tkinter_issues_virtualenv() temp = get_temp_folder(__file__, "temp_matplotlib_video") name = os.path.join(temp, "..", "data", "american_cities.txt") img = os.path.join(temp, "img.png") res = american_cities(name, 40, img, fLOG) assert res is not None
def test_american_cities(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[:2] <= (3, 4): warnings.warn( "Issue with Python 3.4, bug probably related to wrong pointers") return fix_tkinter_issues_virtualenv() temp = get_temp_folder(__file__, "temp_matplotlib_video") name = os.path.join(temp, "..", "data", "american_cities.txt") img = os.path.join(temp, "img.png") res = american_cities(name, 40, img, fLOG) self.assertNotEmpty(res)
def test_notebook_challenge_city_tour(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() temp = get_temp_folder(__file__, "temp_challenge_city_tour") keepnote = ls_notebooks(os.path.join("challenges", "city_tour")) assert len(keepnote) > 0 keepnote = [_ for _ in keepnote] if len(keepnote) > 0: res = execute_notebooks( temp, keepnote, lambda i, n: True, fLOG=fLOG, clean_function=clean_function_notebook ) unittest_raise_exception_notebook(res, fLOG) else: raise Exception("no notebook")
def test_plot_map_france(self): temp = get_temp_folder(__file__, "temp_plot_map_france") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt # pylint: disable=C0415 import cartopy.crs as ccrs # pylint: disable=C0415 fig = plt.figure(figsize=(7, 7)) ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree()) ax.set_extent([-5, 10, 38, 52]) plot_map_france(ax=ax) img = os.path.join(temp, "france.png") fig.savefig(img) plt.close('all') self.assertExists(img)
def test_notebook_hackathon_skip(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() temp = get_temp_folder(__file__, "temp_cheat_sheet") keepnote = ls_notebooks("cheat_sheets") assert len(keepnote) > 0 keepnote = [_ for _ in keepnote if "chsh_files" in _] if len(keepnote) > 0: res = execute_notebooks( temp, keepnote, lambda i, n: "deviner" not in n, fLOG=fLOG, clean_function=clean_function_notebook ) unittest_raise_exception_notebook(res, fLOG) else: raise Exception("no notebook")
def test_ml_benchmark(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv(fLOG=fLOG) import matplotlib.pyplot as plt # pylint: disable=C0415 import dill # pylint: disable=C0415 self.assertTrue(plt is not None) temp = get_temp_folder(__file__, "temp_ml_grid_benchmark") params = [dict(model=lambda: KMeans(n_clusters=3), name="KMeans-3", shortname="km-3"), dict(model=lambda: AgglomerativeClustering(), name="AgglomerativeClustering", shortname="aggclus")] datasets = [dict(X=make_blobs(100, centers=3)[0], Nclus=3, name="blob-100-3", shortname="b-100-3", no_split=True), dict(X=make_blobs(100, centers=5)[0], Nclus=5, name="blob-100-5", shortname="b-100-5", no_split=True)] for no_split in [True, False]: bench = MlGridBenchMark("TestName", datasets, fLOG=fLOG, clog=temp, path_to_images=temp, cache_file=os.path.join( temp, "cache.pickle"), pickle_module=dill, repetition=3, graphx=["_time", "time_train", "Nclus"], graphy=["silhouette", "Nrows"], no_split=no_split) bench.run(params) df = bench.to_df() ht = df.to_html(float_format="%1.3f", index=False) self.assertTrue(len(df) > 0) self.assertIsNotNone(ht) self.assertEqual(df.shape[0], 12) report = os.path.join(temp, "report.html") csv = os.path.join(temp, "report.csv") rst = os.path.join(temp, "report.rst") bench.report(filehtml=report, filecsv=csv, filerst=rst, title="A Title", description="description") self.assertTrue(os.path.exists(report)) self.assertTrue(os.path.exists(csv)) self.assertTrue(os.path.exists(rst)) graph = bench.plot_graphs() self.assertIsNotNone(graph) self.assertEqual(graph.shape, (3, 2))
def test_notebook_examen(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from actuariat_python.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_notebook temp = get_temp_folder(__file__, "temp_examen") keepnote = ls_notebooks("examen") self.assertTrue(len(keepnote) > 0) res = execute_notebooks(temp, keepnote, lambda i, n: "deviner" not in n, fLOG=fLOG, clean_function=clean_function_notebook) execute_notebook_list_finalize_ut(res, fLOG=fLOG, dump=actuariat_python)
def test_graph_changes(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") abc = fix_tkinter_issues_virtualenv() for a in abc: fLOG(a) path = os.path.abspath(os.path.split(__file__)[0]) data = os.path.join(path, "data", "changes.txt") df = pandas.read_csv(data, sep="\t") fLOG(type(df.loc[0, "date"]), df.loc[0, "date"]) code = produce_code_graph_changes(df) fLOG(code) enabled = True if enabled: # this is the code which is generate import matplotlib.pyplot as plt x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52] y = [0, 5, 4, 1, 1, 1, 0, 3, 0, 15, 5, 2, 1, 0, 5, 3, 1, 0, 3, 2, 0, 4, 5, 2, 12, 12, 5, 11, 2, 19, 21, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] xl = ['2014-w20', '2014-w21', '2014-w22', '2014-w23', '2014-w24', '2014-w25', '2014-w26', '2014-w27', '2014-w28', '2014-w29', '2014-w30', '2014-w31', '2014-w32', '2014-w33', '2014-w34', '2014-w35', '2014-w36', '2014-w37', '2014-w38', '2014-w39', '2014-w40', '2014-w41', '2014-w42', '2014-w43', '2014-w44', '2014-w45', '2014-w46', '2014-w47', '2014-w48', '2014-w49', '2014-w50', '2014-w51', '2014-w52', '2015-w01', '2015-w02', '2015-w03', '2015-w04', '2015-w05', '2015-w06', '2015-w07', '2015-w08', '2015-w09', '2015-w10', '2015-w11', '2015-w12', '2015-w13', '2015-w14', '2015-w15', '2015-w16', '2015-w17', '2015-w18', '2015-w19', '2015-w20'] plt.close('all') plt.style.use('ggplot') with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) _, ax = plt.subplots(nrows=1, ncols=1, figsize=(10, 4)) ax.bar(x, y) tig = ax.get_xticks() labs = [] for t in tig: if t in x: labs.append(xl[x.index(t)]) else: labs.append("") ax.set_xticklabels(labs) ax.grid(True) ax.set_title("commits") if __name__ != "__main__": code = code.replace("plt.show", "#plt.show") obj = compile(code, "", "exec") try: exec(obj, globals(), locals()) except Exception as e: raise Exception("unable to run code:\n{0}".format(code)) from e
def test_plot_streets_network(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") shapes = self._shapes fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt fig, ax = plt.subplots() subset = list(range(0, 10)) vertices, edges = build_streets_vertices(subset, shapes) plot_streets_network(subset, edges, vertices, shapes, ax=ax) temp = get_temp_folder(__file__, "temp_plot_streets_network") img = os.path.join(temp, "out_img.png") fig.savefig(img) plt.close('all') assert os.path.exists(img)
def test_example_cartopy(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_example_networkx") fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 8)) example_cartopy(ax=ax) assert ax is not None img = os.path.join(temp, "img.png") fig.savefig(img) self.assertExists(img) if __name__ == "__main__": fig.show() plt.close('all') fLOG("end")
def test_american_cities(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[:2] <= (3, 4): warnings.warn( "Issue with Python 3.4, bug probably related to wrong pointers" ) return fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt import mpld3 filter = { "NewYork": "NY", "Chicago": "CH", "SanFrancisco": "SF", "Seattle": "Sea" } temp = get_temp_folder(__file__, "temp_american_cities") data = os.path.join(temp, "..", "data", "american_cities.txt") df = pandas.read_csv(data) df["Longitude"] = -df["Longitude"] df["City"] = df["City"].apply(lambda v: filter.get(v, "")) fig, ax = plt.subplots(figsize=(32, 32)) df = df[df.Latitude < 52] df = df[df.Longitude > -130].copy() ax = graph_cities(df, ax=ax, markersize=3, fontcolor=(0, 1.0, 0), fontsize='40', fontname="Courrier", fontweight="bold") assert ax is not None img = os.path.join(temp, "img.png") fig.savefig(img) assert os.path.exists(img) name2 = os.path.join(temp, "picture.html") mpld3.save_html(fig, name2) assert os.path.exists(name2) if __name__ == "__main__": fig.show() plt.close('all') fLOG("end")
def test_ml_benchmark(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv(fLOG=fLOG) import matplotlib.pyplot as plt import dill self.assertTrue(plt is not None) temp = get_temp_folder(__file__, "temp_ml_grid_benchmark") params = [dict(model=lambda: KMeans(n_clusters=3), name="KMeans-3", shortname="km-3"), dict(model=lambda: AgglomerativeClustering(), name="AgglomerativeClustering", shortname="aggclus")] datasets = [dict(X=make_blobs(100, centers=3)[0], Nclus=3, name="blob-100-3", shortname="b-100-3", no_split=True), dict(X=make_blobs(100, centers=5)[0], Nclus=5, name="blob-100-5", shortname="b-100-5", no_split=True)] bench = MlGridBenchMark("TestName", datasets, fLOG=fLOG, clog=temp, path_to_images=temp, cache_file=os.path.join(temp, "cache.pickle"), pickle_module=dill, repetition=3, graphx=["_time", "time_train", "Nclus"], graphy=["silhouette", "Nrows"]) bench.run(params) df = bench.to_df() ht = df.to_html(float_format="%1.3f", index=False) self.assertTrue(len(df) > 0) self.assertIsNotNone(ht) self.assertEqual(df.shape[0], 12) report = os.path.join(temp, "report.html") csv = os.path.join(temp, "report.csv") rst = os.path.join(temp, "report.rst") bench.report(filehtml=report, filecsv=csv, filerst=rst, title="A Title", description="description") self.assertTrue(os.path.exists(report)) self.assertTrue(os.path.exists(csv)) self.assertTrue(os.path.exists(rst)) graph = bench.plot_graphs() self.assertIsNotNone(graph) self.assertEqual(graph.shape, (3, 2))
def test_viz_scatter_scatter_xyc(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[:2] <= (3, 4): return fix_tkinter_issues_virtualenv() if __name__ == "__main__": from matplotlib import rcParams rcParams["backend"] = "TkAgg" fold = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.join(fold, "temp_graph_xyz") im = os.path.join(fold, self._testMethodName + ".png") if not os.path.exists(fold): os.mkdir(fold) for img in [im]: if os.path.exists(img): os.remove(img) def f(a, b): return (a**2 + b**2)**0.5 nuage1 = self.generate_gauss(0, 0, 3) nuage2 = self.generate_gauss(3, 4, 2) nuage = [(a, b, f(a, b)) for a, b in nuage1] + \ [(a, b, f(a, b)) for a, b in nuage2] import matplotlib.pyplot as plt fig, ax = scatter_xyc(nuage, title=self._testMethodName) fig.savefig(im) assert os.path.exists(im) fig, ax = scatter_xyc(nuage, smooth=1, title=self._testMethodName + " - smooth 10") fig.savefig(im.replace(".png", ".s10.png")) assert os.path.exists(im) if __name__ == "__main__": import matplotlib matplotlib.use(matplotlib.get_backend(), warn=False, force=True) from matplotlib import rcParams rcParams["backend"] = self.backend plt.close('all')
def test_optimize_position(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") positions = random_positions(24) names = list(name for name, x, y in positions) names.sort() edges = [(names[0], names[1]), (names[0], names[2]), (names[0], names[3]), (names[3], names[4]), (names[3], names[5]), (names[0], names[6])] if __name__ == "__main__": temp = get_temp_folder(__file__, "temp_optimization") new_positions, iter = optimize_positions(positions, edges, fLOG=fLOG, max_iter=20, plot_folder=temp) for i in iter: fLOG(i) positions = [(k, ) + v for k, v in new_positions.items()] fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 8)) plot_positions(positions, edges=edges, ax=ax) self.assertTrue(ax is not None) img = os.path.join(temp, "img.png") fig.savefig(img) self.assertTrue(os.path.exists(img)) if __name__ == "__main__": fig.show() plt.close('all') fLOG("end") else: new_positions, iter = optimize_positions(positions, edges, fLOG=fLOG, max_iter=20) for i in iter: fLOG(i) self.assertTrue(iter is not None) self.assertTrue(new_positions is not None)
def test_population_france2015_plots(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt from actuariat_python.data import population_france_year from actuariat_python.plots import plot_population_pyramid df = population_france_year() self.assertEqual(df.shape[1], 5) self.assertEqual(list(df.columns), ['naissance', 'age', 'hommes', 'femmes', 'ensemble']) ax = plot_population_pyramid(df["hommes"], df["femmes"]) assert ax is not None # avoid matplotlib to crash later plt.close('all')
def test_graph_corrplot(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if "travis" in sys.executable: # it requires scipy which is not included in the requirements.txt return temp = get_temp_folder(__file__, "temp_corrplot") clog = CustomLog(temp) clog("fix") fix_tkinter_issues_virtualenv(fLOG=fLOG) clog("import") from matplotlib import pyplot as plt letters = "ABCDEFGHIJKLMNOP"[0:10] df = pandas.DataFrame( dict(((k, numpy.random.random(10) + ord(k) - 65) for k in letters))) df = df.corr() clog("figure") subplot_kw = dict(aspect='equal', facecolor='white') fig, ax = plt.subplots(1, 1, subplot_kw=subplot_kw) clog("corrplot") c = Corrplot(df) clog("plot") ax = c.plot(fig=fig, ax=ax) clog("save") fLOG("save") img = os.path.join(temp, "corrplot.png") fig.savefig(img) fLOG("close") clog("close") if __name__ == "__main__": plt.show() plt.close('all') clog("end") fLOG("end") assert os.path.exists(img)
def test_viz_scatter_scatter_xyc(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[:2] <= (3, 4): return fix_tkinter_issues_virtualenv() if __name__ == "__main__": from matplotlib import rcParams rcParams["backend"] = "TkAgg" fold = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.join(fold, "temp_graph_xyz") im = os.path.join(fold, self._testMethodName + ".png") if not os.path.exists(fold): os.mkdir(fold) for img in [im]: if os.path.exists(img): os.remove(img) def f(a, b): return (a ** 2 + b ** 2) ** 0.5 nuage1 = self.generate_gauss(0, 0, 3) nuage2 = self.generate_gauss(3, 4, 2) nuage = [(a, b, f(a, b)) for a, b in nuage1] + \ [(a, b, f(a, b)) for a, b in nuage2] import matplotlib.pyplot as plt fig, _ = scatter_xyc(nuage, title=self._testMethodName) fig.savefig(im) assert os.path.exists(im) fig, _ = scatter_xyc( nuage, smooth=1, title=self._testMethodName + " - smooth 10") fig.savefig(im.replace(".png", ".s10.png")) assert os.path.exists(im) if __name__ == "__main__": import matplotlib matplotlib.use(matplotlib.get_backend(), warn=False, force=True) from matplotlib import rcParams rcParams["backend"] = self.backend plt.close('all')
def test_viz_scatter_scatter_xyid(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.version_info[:2] <= (3, 4): return fix_tkinter_issues_virtualenv() if __name__ == "__main__": from matplotlib import rcParams rcParams["backend"] = "TkAgg" fold = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.join(fold, "temp_graph_xyz") im = os.path.join(fold, self._testMethodName + ".png") if not os.path.exists(fold): os.mkdir(fold) for img in [im]: if os.path.exists(img): os.remove(img) nuage1 = self.generate_gauss(0, 0, 3) nuage2 = self.generate_gauss(3, 4, 2) nuage = [(a, b, 0) for a, b in nuage1] + [(a, b, 1) for a, b in nuage2] fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt fig, ax = scatter_xy_id(nuage, title=self._testMethodName, legend={ 0: "c0", 1: "c1" }) fig.savefig(im) assert os.path.exists(im) if __name__ == "__main__": import matplotlib matplotlib.use(matplotlib.get_backend(), warn=False, force=True) from matplotlib import rcParams rcParams["backend"] = self.backend plt.close('all') fLOG(plt.style.available)
def test_american_cities(self): fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt filters = {"NewYork": "NY", "Chicago": "CH", "SanFrancisco": "SF", "Seattle": "Sea"} temp = get_temp_folder(__file__, "temp_american_cities") data = os.path.join(temp, "..", "data", "american_cities.txt") df = pandas.read_csv(data) df["Longitude"] = -df["Longitude"] df["City"] = df["City"].apply(lambda v: filters.get(v, "")) df = df[df.Latitude < 52] df = df[df.Longitude > -130].copy() fig, ax = graph_cities(df, markersize=3, fontcolor=(0, 1.0, 0), fontsize='40', fontweight="bold", figsize=(32, 32)) self.assertNotEmpty(ax) img = os.path.join(temp, "img.png") if __name__ == "__main__": fig.show() fig.savefig(img) self.assertExists(img) plt.close('all')
def test_graph_corrplot(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_corrplot") clog = CustomLog(temp) clog("fix") fix_tkinter_issues_virtualenv(fLOG=fLOG) clog("import") from matplotlib import pyplot as plt letters = "ABCDEFGHIJKLMNOP"[0:10] df = pandas.DataFrame( dict(((k, numpy.random.random(10) + ord(k) - 65) for k in letters))) df = df.corr() clog("figure") subplot_kw = dict(aspect='equal', facecolor='white') fig, ax = plt.subplots(1, 1, subplot_kw=subplot_kw) clog("corrplot") c = Corrplot(df) clog("plot") for up in ['lower', 'upper', 'method', 'both']: ax = c.plot(fig=fig, ax=ax) clog("save") fLOG("save") img = os.path.join(temp, "corrplot.png") fig.savefig(img) fLOG("close") clog("close") if __name__ == "__main__": plt.show() plt.close('all') clog("end") fLOG("end") self.assertExists(img)
def test_graph_corrplot(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_corrplot") clog = CustomLog(temp) clog("fix") fix_tkinter_issues_virtualenv(fLOG=fLOG) clog("import") from matplotlib import pyplot as plt letters = "ABCDEFGHIJKLMNOP"[0:10] df = pandas.DataFrame( dict( ((k, numpy.random.random(10) + ord(k) - 65) for k in letters))) df = df.corr() clog("figure") subplot_kw = dict(aspect='equal', facecolor='white') fig, ax = plt.subplots(1, 1, subplot_kw=subplot_kw) clog("corrplot") c = Corrplot(df) clog("plot") for up in ['lower', 'upper', 'method', 'both']: ax = c.plot(fig=fig, ax=ax, colorbar=up == 'lower') clog("save") fLOG("save") img = os.path.join(temp, "corrplot.png") fig.savefig(img) fLOG("close") clog("close") if __name__ == "__main__": plt.show() plt.close('all') clog("end") fLOG("end") self.assertExists(img)
def test_example_cartopy(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") try: import cairo except ImportError as e: warnings.warn("Unable to import cairo %r." % e) return temp = get_temp_folder(__file__, "temp_example_networkx") fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 8)) example_cartopy(ax=ax) assert ax is not None img = os.path.join(temp, "img.png") fig.savefig(img) self.assertExists(img) if __name__ == "__main__": fig.show() plt.close('all') fLOG("end")
def test_voyageur_commerce_simple(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") points = [(105.61666870117188, 69.01666259765625), (192.61666870117188, 92.01666259765625), (372.6166687011719, 123.01666259765625), (360.6166687011719, 249.01666259765625), (190.61666870117188, 213.01666259765625), (102.61666870117188, 166.01666259765625), (104.61666870117188, 271.01666259765625), (209.61666870117188, 278.01666259765625), (321.6166687011719, 198.01666259765625), (261.6166687011719, 153.01666259765625), (196.61666870117188, 162.01666259765625), (317.6166687011719, 306.01666259765625)] d0 = distance_circuit(points) newp = voyageur_commerce_simple(points) d1 = distance_circuit(newp) fLOG(d0, d1) assert d1 < d0 fix_tkinter_issues_virtualenv(fLOG=fLOG) import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) plot_circuit(newp, ax=ax) temp = get_temp_folder(__file__, "temp_voyageur_commerce_simple") img = os.path.join(temp, "tsp.png") fig.savefig(img) assert os.path.exists(img) if __name__ == "__main__": plt.show() plt.close("all")
def test_plot_map_france_polygon(self): temp = get_temp_folder(__file__, "temp_plot_map_france_polygon") df = load_french_departements(cache=temp) fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt # pylint: disable=C0415 import cartopy.crs as ccrs # pylint: disable=C0415 fig = plt.figure(figsize=(7, 7)) ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree()) ax.set_extent([-5, 10, 38, 52]) N = float(df.shape[0]) plot_map_france_polygon(ax=ax, geometry=df['geometry'], colors=[(i / N, i / N, i / N) for i in range(df.shape[0])]) img = os.path.join(temp, "france.png") fig.savefig(img) if __name__ == "__main__": plt.show() plt.close('all') self.assertExists(img)
def test_graph_with_label(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") x = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43 ] y = [ 1, 3, 10, 6, 3, 5, 3, 6, 4, 2, 3, 2, 11, 10, 4, 5, 2, 5, 4, 1, 1, 1, 3, 15, 5, 2, 1, 5, 3, 1, 3, 2, 4, 5, 2, 12, 12, 5, 11, 2, 19, 21, 5, 2 ] xl = [ '2014-w04', '2014-w05', '2014-w06', '2014-w07', '2014-w08', '2014-w09', '2014-w10', '2014-w11', '2014-w12', '2014-w13', '2014-w14', '2014-w15', '2014-w16', '2014-w17', '2014-w18', '2014-w19', '2014-w20', '2014-w21', '2014-w22', '2014-w23', '2014-w24', '2014-w25', '2014-w27', '2014-w29', '2014-w30', '2014-w31', '2014-w32', '2014-w34', '2014-w35', '2014-w36', '2014-w38', '2014-w39', '2014-w41', '2014-w42', '2014-w43', '2014-w44', '2014-w45', '2014-w46', '2014-w47', '2014-w48', '2014-w49', '2014-w50', '2014-w51', '2014-w52' ] if sys.version_info[:2] <= (3, 4): warnings.warn( "Issue with Python 3.4, bug probably related to wrong pointers" ) return fix_tkinter_issues_virtualenv(fLOG=fLOG) import matplotlib.pyplot as plt _, ax = plt.subplots(figsize=(8, 3)) graph_with_label(x, y, xl, ax=ax) if __name__ == "__main__": plt.show() plt.close('all') fLOG("end")
def test_notebook_hackathon(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() temp = get_temp_folder(__file__, "temp_cheat_sheet") keepnote = ls_notebooks("cheat_sheets") assert len(keepnote) > 0 keepnote = [_ for _ in keepnote if "chsh_files" not in _] if len(keepnote) > 0: fold = os.path.dirname(keepnote[0]) copy = [os.path.join(fold, "geo_data.zip")] for c in copy: shutil.copy(c, temp) res = execute_notebooks(temp, keepnote, lambda i, n: "deviner" not in n, fLOG=fLOG, clean_function=clean_function_notebook) unittest_raise_exception_notebook(res, fLOG) else: raise Exception("no notebook")
def test_notebook_population(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from actuariat_python.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_notebook temp = get_temp_folder(__file__, "temp_sessions_population") keepnote = [ _ for _ in ls_notebooks("sessions") if "seance6_graphes_ml_enonce" in _ ] self.assertTrue(len(keepnote) > 0) for k in keepnote: fLOG(k) res = execute_notebooks(temp, keepnote, lambda i, n: "deviner" not in n, fLOG=fLOG, clean_function=clean_function_notebook) execute_notebook_list_finalize_ut(res, fLOG=fLOG, dump=actuariat_python)
def test_graph_XY(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_graph_XY") fix_tkinter_issues_virtualenv(fLOG=fLOG) from matplotlib import pyplot as plt df = TableFormula(dict(xx=[0.1, 0.2, 0.3], yy=[1.1, 1.2, 1.3])) subplot_kw = dict(aspect='equal', facecolor='white') fig, ax = plt.subplots(1, 1, subplot_kw=subplot_kw) df.graph_XY([[lambda row: row["xx"], lambda row: row["yy"], "xyl"]], ax=ax, title="a title") img = os.path.join(temp, "graph_XY.png") fig.savefig(img) # if __name__ == "__main__": # plt.show() plt.close('all') self.assertTrue(os.path.exists(img))
def test_notebook_population_gerry_mandering(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from actuariat_python.automation.notebook_test_helper import ls_notebooks, execute_notebooks, clean_function_notebook temp = get_temp_folder(__file__, "temp_gerry_mandering") clog = CustomLog(temp) keepnote = [ _ for _ in ls_notebooks("sessions") if "election_carte_electorale_correction" in _ ] self.assertTrue(len(keepnote) > 0) res = execute_notebooks(temp, keepnote, lambda i, n: "deviner" not in n, fLOG=clog, deepfLOG=clog, detailed_log=clog, clean_function=clean_function_notebook) execute_notebook_list_finalize_ut(res, fLOG=clog, dump=actuariat_python)
def test_plot_gallery_url(self): fix_tkinter_issues_virtualenv(fLOG=noLOG) from matplotlib import pyplot as plt root = "http://www.xavierdupre.fr/enseignement/complements/dog-cat-pixabay/" files = [root + 'cat-2603300__480.jpg', root + 'cat-2947188__480.jpg'] temp = get_temp_folder(__file__, "temp_plot_gallery_url") try: fig, ax = plot_gallery_images(files, return_figure=True) except http.client.RemoteDisconnected as e: warnings.warn("Unable to fetch image {0}'".format(e)) return img = os.path.join(temp, "gallery.png") fig.savefig(img) plt.close('all') # ax try: ax = plot_gallery_images(files, return_figure=False, ax=ax) self.assertNotEmpty(ax) except http.client.RemoteDisconnected as e: warnings.warn("Unable to fetch image {0}'".format(e)) return
def test_graph_ohlc(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache_ohlc") stock = StockPrices(TestStockGraphOHLC.tick, folder=cache, url=TestStockGraphOHLC.source) fig, ax = plt.subplots(figsize=(16, 8)) ax = stock.plot(ax=ax, field="ohlc") img = os.path.abspath( os.path.join(os.path.split(__file__)[0], "temp_image_ohlc.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img)
def test_random_position(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_random_position") clog = CustomLog(temp) clog("random_positions") fLOG("random_positions") positions = random_positions(24) self.assertEqual(len(positions), 24) clog("beginning") fLOG("beginning") fix_tkinter_issues_virtualenv() clog("fix") fLOG("fix") import matplotlib.pyplot as plt clog("plotting") fLOG("plotting") fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 8)) clog("plotting positions") fLOG("plotting positions") plot_positions(positions, ax=ax) self.assertTrue(ax is not None) clog("savefig") img = os.path.join(temp, "img.png") fig.savefig(img) self.assertTrue(os.path.exists(img)) clog("noshow") if __name__ == "__main__": fig.show() clog("close") plt.close('all') clog("end") fLOG("end")
def test_matplotlib_example(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") progs = ["ffmpeg"] if not sys.platform.startswith("win"): progs.append("avconv") errs = [] prog = None for prog in progs: out, err = run_cmd(prog, wait=True, fLOG=fLOG) exps = "usage:" if (exps not in out and exps not in err) or err is None or len(err) == 0: errs.append((prog, err)) else: break if len(errs) >= len(progs): if sys.platform.startswith("win"): fLOG("download ffmpeg") add_missing_development_version( ["pyensae"], __file__, hide=True) from pyensae.datasource import download_data download_data("ffmpeg.zip", website="xd") else: raise FileNotFoundError( "Unable to find '{1}'.\nPATH='{0}'\n--------\n[OUT]\n{2}\n[ERR]\n{3}".format( os.environ["PATH"], prog, out, "\n----\n".join("{0}:\n{1}".format(*_) for _ in errs))) temp = get_temp_folder(__file__, "temp_example_example") fix_tkinter_issues_virtualenv() # update a distribution based on new data. import numpy as np import matplotlib.pyplot as plt import scipy.stats as ss from matplotlib.animation import FuncAnimation, writers # To get the list of available writers if not writers.is_available(prog): writers.register(prog) fLOG(writers.list()) class UpdateDist: def __init__(self, ax, prob=0.5): self.success = 0 self.prob = prob self.line, = ax.plot([], [], 'k-') self.x = np.linspace(0, 1, 200) self.ax = ax # Set up plot parameters self.ax.set_xlim(0, 1) self.ax.set_ylim(0, 15) self.ax.grid(True) # This vertical line represents the theoretical value, to # which the plotted distribution should converge. self.ax.axvline(prob, linestyle='--', color='black') def init(self): self.success = 0 self.line.set_data([], []) return self.line, def __call__(self, i): # This way the plot can continuously run and we just keep # watching new realizations of the process if i == 0: return self.init() # Choose success based on exceed a threshold with a uniform # pick if np.random.rand(1,) < self.prob: # pylint: disable=W0143 self.success += 1 y = ss.beta.pdf(self.x, self.success + 1, (i - self.success) + 1) self.line.set_data(self.x, y) return self.line, fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ud = UpdateDist(ax, prob=0.7) anim = FuncAnimation(fig, ud, frames=np.arange(100), init_func=ud.init, interval=100, blit=True) try: Writer = writers[prog] except KeyError as e: if prog == "avconv": from matplotlib.animation import AVConvWriter Writer = AVConvWriter else: raise e writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800) anim.save(os.path.join(temp, 'lines2.mp4'), writer=writer) plt.close('all') fLOG("end")
def test_algo_euler4(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") folder = os.path.join( os.path.abspath( os.path.dirname(__file__)), "temp_algo_euler4") if not os.path.exists(folder): os.mkdir(folder) edges = get_data(whereTo=folder, fLOG=fLOG) edges = edges[:3] vertices = {} for e in edges: for i in range(0, 2): _ = e[i] p = e[i + 3] vertices[_] = p connex = connected_components(edges) v = [v for k, v in connex.items()] mi, ma = min(v), max(v) while mi != ma: edges.append((mi, ma, 2, vertices[mi], vertices[ma], distance_haversine(* (vertices[mi] + vertices[ma])))) connex = connected_components(edges) v = [v for k, v in connex.items()] mi, ma = min(v), max(v) fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt import networkx as nx plt.figure() G = nx.Graph() for e in edges: a, b = e[:2] G.add_edge(a, b) pos = nx.spring_layout(G) nx.draw(G, pos, node_color='#A0CBE2') plt.savefig(os.path.join(folder, "graph1.png")) plt.close('all') added = eulerien_extension(edges, fLOG=lambda *l: None, distance=distance_paris) for e in added: a, b = e[:2] G.add_edge(a, b) plt.figure() pos = nx.spring_layout(G) graph_degree(edges + added) #labels={ v:"{0}".format(deg[v]) for v in G.nodes() } nx.draw(G, pos, node_color='#A0CBE2' # ,labels=labels ) plt.savefig(os.path.join(folder, "graph2.png")) plt.close('all') path = euler_path(edges, added) alls = edges + added fLOG(len(alls), len(path))
def test_graph_ggplots(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") x = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43] y = [ 1, 3, 10, 6, 3, 5, 3, 6, 4, 2, 3, 2, 11, 10, 4, 5, 2, 5, 4, 1, 1, 1, 3, 15, 5, 2, 1, 5, 3, 1, 3, 2, 4, 5, 2, 12, 12, 5, 11, 2, 19, 21, 5, 2] xl = [ '2014-w04', '2014-w05', '2014-w06', '2014-w07', '2014-w08', '2014-w09', '2014-w10', '2014-w11', '2014-w12', '2014-w13', '2014-w14', '2014-w15', '2014-w16', '2014-w17', '2014-w18', '2014-w19', '2014-w20', '2014-w21', '2014-w22', '2014-w23', '2014-w24', '2014-w25', '2014-w27', '2014-w29', '2014-w30', '2014-w31', '2014-w32', '2014-w34', '2014-w35', '2014-w36', '2014-w38', '2014-w39', '2014-w41', '2014-w42', '2014-w43', '2014-w44', '2014-w45', '2014-w46', '2014-w47', '2014-w48', '2014-w49', '2014-w50', '2014-w51', '2014-w52'] if sys.version_info[:2] <= (3, 4): warnings.warn( "Issue with Python 3.4, bug probably related to wrong pointers") return fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(8, 3)) graph_ggplot_with_label(x, y, xl, ax=ax) if __name__ == "__main__": import matplotlib.pyplot as plt plt.show() plt.close('all') fLOG("end")
def test_graph(self): """ This test is failing with Python 3.4 if many pictures are drawn. """ fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fix_tkinter_issues_virtualenv() from matplotlib import pyplot as plt cache = os.path.abspath(os.path.split(__file__)[0]) cache = os.path.join(cache, "temp_cache2") stocks = [StockPrices(t, folder=cache, url=TestStockGraph.source) for t in TestStockGraph.tick] if True: fLOG("A", sys.executable) fig, ax = plt.subplots() ax = StockPrices.draw( stocks, figsize=( 16, 8), field=[ "Open", "Close"], ax=ax) img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image.png")) if os.path.exists(img): os.remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) if True: fLOG("B") fig, ax = plt.subplots() ax = StockPrices.draw(stocks, begin="2010-01-01", ax=ax) img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image2.png")) if os.path.exists(img): os .remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) if True: fLOG("C") fig, ax = plt.subplots() ax = StockPrices.draw(stocks[:1], begin="2010-01-01", ax=ax) img = os.path.abspath( os.path.join( os.path.split(__file__)[0], "temp_image3.png")) if os.path.exists(img): os .remove(img) fig.savefig(img) plt.close('all') self.assertExists(img) fLOG("thisend")