class MyCallback(Callback): def __init__(self) -> None: super().__init__() self.video = Video(Streamer()) def notify(self, algorithm): problem = algorithm.problem pcp = PCP(title=("Gen %s" % algorithm.n_gen, { 'pad': 30 }), bounds=(problem.xl, problem.xu), labels=["$x_%s$" % k for k in range(problem.n_var)]) pcp.set_axis_style(color="grey", alpha=0.5) pcp.add(algorithm.pop.get("X"), color="black", alpha=0.8, linewidth=1.5) if algorithm.off is not None: pcp.add(algorithm.off.get("X"), color="blue", alpha=0.8, linewidth=0.5) pcp.add(algorithm.opt.get("X"), color="red", linewidth=4) pcp.do() self.video.record()
def test_stream_simple(self): vid = Video(Streamer(mode="stream", delay=1000, close_window=True)) for k in range(3): X = np.random.random((100, 2)) plt.scatter(X[:, 0], X[:, 1]) vid.record() vid.close()
class PathVisualization(Callback): def __init__(self): super().__init__() self.vid = Video(File("tsp.mp4")) def notify(self, algorithm): if algorithm.n_gen % 10 == 0: x = algorithm.opt[0].get("X") visualize(problem, x, show=False) plt.title(f"Generation: {algorithm.n_gen}") self.vid.record()
def test_html_embed_simple(self): fname = "example.mp4" vid = Video(HTML(File(fname), embedded=True)) for k in range(10): X = np.random.random((100, 2)) plt.scatter(X[:, 0], X[:, 1]) vid.record() vid.close() self.assertTrue(os.path.exists("example.html")) os.remove("example.html")
def test_gif_simple(self): fname = "example.gif" vid = Video(GIF(fname)) for k in range(10): X = np.random.random((100, 2)) plt.scatter(X[:, 0], X[:, 1]) vid.record() vid.close() self.assertTrue(os.path.exists(fname)) os.remove(fname)
class AnimationCallback(Callback): def __init__(self, do_show=False, do_close=False, nth_gen=1, fname=None, dpi=None, video=None, exception_if_not_applicable=True): super().__init__() self.nth_gen = nth_gen self.do_show = do_show self.do_close = do_close self.exception_if_not_applicable = exception_if_not_applicable self.video = video if self.video is None and fname is not None: try: from pyrecorder.recorders.file import File from pyrecorder.video import Video from pyrecorder.converters.matplotlib import Matplotlib except: print( "Please install pyrecorder for animation support: pip install pyrecorder" ) self.video = Video(File(fname), converter=Matplotlib(dpi=dpi)) def notify(self, algorithm, **kwargs): if algorithm.n_gen == 1 or algorithm.n_gen % self.nth_gen == 0: try: ret = self.do(algorithm.problem, algorithm, **kwargs) if self.do_show: plt.show() if self.video is not None: self.video.record() if self.do_close: plt.close() return ret except Exception as ex: if self.exception_if_not_applicable: raise ex
def test_html_wrap_simple(self): fname = "example.mp4" vid = Video(HTML(File(fname))) for k in range(10): X = np.random.random((100, 2)) plt.scatter(X[:, 0], X[:, 1]) vid.record() vid.close() self.assertTrue(os.path.exists(fname)) html = fname.replace("mp4", "html") self.assertTrue(os.path.exists(html)) os.remove(fname) os.remove(html)
def test_record_simple(self): fname = "example.mp4" vid = Video(File(fname), converter=Plotly()) df = px.data.iris() for k in range(10): rnd = np.random.rand(len(df)) < 0.7 fig = px.scatter(df.loc[rnd], x="sepal_width", y="sepal_length", color="species", size='petal_length') vid.record(fig=fig) vid.close() self.assertTrue(os.path.exists(fname))
# START example import numpy as np from pyrecorder.recorders.file import File from pyrecorder.video import Video from pymoo.algorithms.nsga2 import NSGA2 from pymoo.visualization.scatter import Scatter vid = Video(File("example.mp4")) for k in range(10): X = np.random.random((100, 2)) Scatter(title=str(k)).add(X).do() vid.record() vid.close() # END example # START ga from pymoo.factory import get_problem from pymoo.optimize import minimize problem = get_problem("zdt1") algorithm = NSGA2(pop_size=100, eliminate_duplicates=True) ret = minimize(problem, algorithm, termination=('n_gen', 100), seed=1, save_history=True,