Bläddra i källkod

Tested PlotManager against PlotBlocks

rparedis 5 år sedan
förälder
incheckning
7f76e9060d

+ 3 - 3
examples/SinGen/SinGen.py

@@ -4,18 +4,18 @@
 
 from CBD.CBD import *
 from CBD.lib.std import *
-from CBD.lib.interface.plotting import SignalPlotBlock
+from CBD.lib.interface.endpoints import SignalCollectorBlock
 
 DELTA_T = 0.1
 
 class SinGen(CBD):
-    def __init__(self, block_name, axes=(None)):
+    def __init__(self, block_name):
         super().__init__(block_name, input_ports=[], output_ports=[])
 
         # Create the Blocks
         self.addBlock(GenericBlock("sin", block_operator=("sin")))
         self.addBlock(TimeBlock("navfwlU7EP--ZkxJ3C-2-12"))
-        self.addBlock(SignalPlotBlock("plot", axes=(axes), type=("plot"), xlim=(SignalPlotBlock.full_domain), ylim=(SignalPlotBlock.full_domain)))
+        self.addBlock(SignalCollectorBlock("plot"))
 
         # Create the Connections
         self.addConnection("navfwlU7EP--ZkxJ3C-2-12", "sin", output_port_name='OUT1', input_port_name='IN1')

+ 20 - 7
examples/SinGen/SinGen_experiment.py

@@ -2,7 +2,7 @@
 # This file was automatically generated from drawio2cbd with the command:
 #   /home/red/git/DrawioConvert/__main__.py SinGen.xml -fav -F CBD -e SinGen -E delta=0.1
 
-from CBD.lib.interface.plotting import SignalPlotBlock
+from CBD.lib.interface.plottinghandler import PlotHandler, PlotManager, LinePlot
 from CBD.simulator import Simulator
 from SinGen import *
 import matplotlib.pyplot as plt
@@ -11,30 +11,43 @@ DELTA_T = 0.1
 
 fig = plt.figure(figsize=(15, 5), dpi=100)
 ax = fig.add_subplot(111)
+ax.set_ylim((-1, 1))
+# ax = None
 
-cbd = SinGen("SinGen", ax)
+cbd = SinGen("SinGen")
 
-plotter = cbd.getBlockByName("plot")
+manager = PlotManager()
+manager.register("sin", cbd.findBlock("plot")[0], (fig, ax), LinePlot())
+manager.connect('sin', 'update_event', lambda d, axis=ax: axis.set_xlim(PlotHandler.follow(d[0], 10.0, 0.0)))
+
+plt.show(block=False)
+
+# plotter = cbd.getBlockByName("plot")
 
 def term(*_):
 	plt.draw()
 	plt.pause(0.01)
-	return not plotter.is_opened()
+	return not manager.is_opened()
 
 
 # Run the Simulation
+time = 10.0
 sim = Simulator(cbd)
 sim.setRealTime(True)
 sim.setDeltaT(DELTA_T)
 sim.setTerminationCondition(term)
-plotter.start_animation(fig)
-sim.run(10.0)
-plotter.end_animation()
+# plotter.set_animation(fig)
+# plotter.start_animation(fig)
+sim.run(time)
+# plotter.end_animation()
 
 log = sim.getDurationLog()
 
 fig2 = plt.figure()
 ax2 = fig2.subplots()
+ax2.set_title("Block Computation [Plotting Manager] (T = {:.2f}, dt = {:.2f})".format(time, DELTA_T))
+ax2.set_xlabel("Iterations")
+ax2.set_ylabel("Time")
 ax2.plot([-1, len(log)], [DELTA_T, DELTA_T], c='red')
 ax2.plot([-1, len(log)], [0.01, 0.01], c='green')
 ax2.bar(range(len(log)), log)

BIN
examples/SinGen/durations-10.png


BIN
examples/SinGen/durations-100.png


BIN
examples/SinGen/durations-lp-10.png


BIN
examples/SinGen/durations-lp-100.png


BIN
examples/SinGen/durations-pm-10.png


BIN
examples/SinGen/durations-pm-100.png


+ 3 - 0
requirements.txt

@@ -0,0 +1,3 @@
+matplotlib
+bokeh
+tqdm  # for progress bar

+ 5 - 4
src/CBD/lib/interface/endpoints.py

@@ -22,7 +22,7 @@ class SignalCollectorBlock(CollectorBlock):
 	Collects a single signal to store w.r.t. the arrival time.
 	"""
 	def __init__(self, name):
-		CollectorBlock.__init__(name, ["IN1"], [])
+		CollectorBlock.__init__(self, name, ["IN1"], [])
 
 	def compute(self, curIteration):
 		time = self.getClock().getTime()
@@ -39,14 +39,15 @@ class PositionCollectorBlock(CollectorBlock):
 	Collects a X/Y position.
 	"""
 	def __init__(self, name):
-		CollectorBlock.__init__(name, ["X", "Y"], [])
+		CollectorBlock.__init__(self, name, ["X", "Y"], [])
 
 	def compute(self, curIteration):
 		x = self.getInputSignal(curIteration, "X").value
 		y = self.getInputSignal(curIteration, "Y").value
 		self.data.append((x, y))
 
-	def xy_data(self):
+	@property
+	def data_xy(self):
 		return [x for x, _ in self.data], [y for _, y in self.data]
 
 
@@ -62,7 +63,7 @@ class StatisticsCollectorBlock(CollectorBlock):
 	Note: this block only works for NUMERICAL data.
 	"""
 	def __init__(self, name):
-		CollectorBlock.__init__(name, ["IN1"], {})
+		CollectorBlock.__init__(self, name, ["IN1"], {})
 		self.clean()
 
 	def compute(self, curIteration):

+ 2 - 0
src/CBD/lib/interface/plottinghandler.py

@@ -70,6 +70,8 @@ class PlotHandler:
 		self.kind.update(self.elm, *data)
 		self.signal('update_event', data)
 
+	# TODO: follow_fill => does not center, but traces highest value(s)
+
 	@staticmethod
 	def follow(data, size, lower_bound=-float('inf'), upper_bound=float('inf')):
 		assert upper_bound - lower_bound >= size, "Invalid size: outside bounds."

+ 1 - 1
src/CBD/simulator.py

@@ -24,7 +24,7 @@ class Clock:
 	def getDeltaT(self):
 		return self.__delta_t
 
-
+# TODO: progress bar (tqdm?)
 class Simulator:
 	def __init__(self, model):
 		self.model = model