def do_ecto(self): plasm = ecto.Plasm() plasm.connect(self.graph) # sched = ecto.Scheduler(plasm) # sched.execute(niter=1) ecto.view_plasm(plasm) # add ecto scheduler args. run_plasm(self.options, plasm, locals=vars())
def test_dealer(Scheduler): print "*" *80 print __name__, Scheduler plasm = ecto.Plasm() printer = ecto_test.Printer() cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] dealer = ecto.Dealer(tendril=printer.inputs.at('in'), iterable=cards) plasm.connect(dealer['out'] >> printer['in']) sched = Scheduler(plasm) sched.execute() assert dealer.outputs.at('out').type_name == 'double' assert dealer.outputs.out == 10
def makeplasm(): plasm = ecto.Plasm() ping = ecto_test.Ping("Ping") sleep0 = ecto_test.SleepPyObjectAbuser( list_o_sleeps=[random.random() * 0.1 for i in range(1, 10)]) sleep1 = ecto_test.SleepPyObjectAbuser( list_o_sleeps=[random.random() * 0.1 for i in range(1, 10)]) plasm.connect(ping[:] >> sleep0[:], sleep0[:] >> sleep1[:]) return plasm
def test_constant(): print "test running.." plasm = ecto.Plasm() c = ecto.Constant(value=0.50505) m = ecto_test.Multiply(factor=3.3335) passthrough = ecto_test.PassthroughAny() print passthrough.__doc__ print ">>>>DOC>>>>", c.__doc__ pr = ecto_test.Printer() p = ecto.Plasm() plasm.connect(c[:] >> m[:], m[:] >> passthrough[:], passthrough[:] >> pr[:]) plasm.execute() assert m.outputs.out == (0.50505 * 3.3335) plasm.execute() assert m.outputs.out == (0.50505 * 3.3335)
def test_plasm(): scatter = ecto_test.Scatter(n=3, x=3) scatter2 = ecto_test.Scatter(n=5, x=10) gather = ecto_test.Gather(n=3) gather2 = ecto_test.Gather(n=5) plasm = ecto.Plasm() p = ecto.Plasm() #test the old syntax. p.connect(scatter, "out_0000", gather, "in_0000") try: p2 = ecto.Plasm() p2.connect(scatter, "out_0000", gather, "idn_0001") util.fail() except ecto.NonExistant, e: print ">>>", e assert "'in_0000':type(int) 'in_0001':type(int) 'in_0002':type(int)" in str( e) print "(threw as expected)\n\n"
def test_fileI(Scheduler, file_like_object, realfile=False): global cards plasm = ecto.Plasm() if not realfile: file_like_object.writelines(filetext) file_like_object.seek(0) reader = ecto_test.FileI(file=ecto.istream(file_like_object)) printer = ecto_test.Printer() plasm.connect(reader[:] >> printer[:]) sched = Scheduler(plasm) sched.execute() assert reader.outputs.output == cards[-1]
def bad_syntax_errors(): scatter = ecto_test.Scatter(n=3, x=3) scatter2 = ecto_test.Scatter(n=5, x=10) gather = ecto_test.Gather(n=3) gather2 = ecto_test.Gather(n=5) try: plasm = ecto.Plasm() plasm.connect(scatter[:] >> gather2[:]) util.fail("Should not work as there is a size mismatch...") except RuntimeError, e: print e
def test_reconnect(): plasm = ecto.Plasm() g = ecto_test.Generate(start=2, step=2) m = ecto_test.Multiply(factor=2) m2 = ecto_test.Multiply(factor=2) gather = ecto_test.Gather_double(n=2) plasm.connect(g, "out", m , "in") plasm.connect(g, "out", m2 , "in") try: plasm.connect(m2,"out",m,"in") util.fail("reconnection bad...") except RuntimeError,e: pass
def test_required_param(): plasm = ecto.Plasm() print "<DOC>", ecto_test.RequiredParam.__doc__, "</DOC>" #test assert "REQUIRED" in ecto_test.RequiredParam.__doc__ #test doc default value printing printing assert "2.1253" in ecto_test.RequiredParam.__doc__ try: req = ecto_test.RequiredParam("Required") print "egh, didn't throw" util.fail() except RuntimeError, e: print "Yup, there is our throw:", e
def test_doc(): scatter = ecto_test.Scatter(n=6, x=3) gather = ecto_test.Gather(n=3) gather2 = ecto_test.Gather(n=3) plasm = ecto.Plasm() plasm.connect(scatter["out_0000", "out_0001", "out_0002"] >> gather[:], scatter["out_0003", "out_0004", "out_0005"] >> gather2[:]) plasm.execute() result = gather.outputs.out assert (result == 9) # 3 * 3 assert scatter.__doc__ != None assert gather.__doc__ != None assert type(plasm.viz()) == str
def test_async_stop_on_destructor(): generate = ecto_test.Generate() param_watcher = ecto_test.ParameterWatcher(value=2) sleep = ecto_test.Sleep() printer = ecto_test.Printer() plasm = ecto.Plasm() plasm.connect(generate["out"] >> param_watcher["input"], param_watcher['output'] >> printer[:]) plasm.insert(sleep) #ecto.view_plasm(plasm) sched = ecto.schedulers.Singlethreaded(plasm) sched.execute_async() time.sleep(3.0)
def test_fileO(Scheduler, file_like_object, realfile=False): global cards, filetext plasm = ecto.Plasm() printer = ecto_test.FileO(file=ecto.ostream(file_like_object)) dealer = ecto.Dealer(tendril=printer.inputs.at('input'), iterable=cards) plasm.connect(dealer['out'] >> printer['input']) sched = Scheduler(plasm) sched.execute() if not realfile: file_like_object.seek(0) result = ''.join([x for x in file_like_object]) print result assert result == filetext
def test_serialization(): plasm = ecto.Plasm() gen = ecto_test.Generate("Gen", step=1.0, start=0.0) inc = ecto_test.Increment("Increment 0", delay=2) plasm.connect(gen, "out", inc, "in") for j in range(5): # one has already been added inc_next = ecto_test.Increment("Increment_%u" % (j+1), delay=2) plasm.connect(inc, "out", inc_next, "in") inc = inc_next printer = ecto_test.Printer("Printy") plasm.connect(inc, "out", printer, "in") plasm.save('python_graph.ecto') plasm.execute(4);
def test_async_stop_on_destructor(): generate = ecto_test.Generate() param_watcher = ecto_test.ParameterWatcher(value=2) sleep = ecto_test.Sleep() printer = ecto_test.Printer() plasm = ecto.Plasm() plasm.connect(generate["out"] >> param_watcher["input"], param_watcher['output'] >> printer[:]) plasm.insert(sleep) #ecto.view_plasm(plasm) sched = ecto.Scheduler(plasm) sched.execute_async() for i in range(0, 10): sched.run_job()
def test_nodelay(): plasm = ecto.Plasm() ping = ecto_test.Ping("Ping") metrics = ecto_test.Metrics("Metrics", queue_size=10) plasm.connect(ping[:] >> metrics[:]) sched = ecto.Scheduler(plasm) sched.execute(niter=10000) print "Hz:", metrics.outputs.hz, " Latency in seconds: %f" % metrics.outputs.latency_seconds # these are kinda loose assert metrics.outputs.hz > 5000 assert metrics.outputs.latency_seconds < 0.0001
def test_bb_fail(options): mm = MyBlackBox("MaMaMa", start=10, step=3, fail=True) print mm.__doc__ assert 'fail' in mm.__doc__ assert mm.name() == 'MaMaMa' plasm = ecto.Plasm() plasm.insert(mm) try: run_plasm(options, plasm) fail() except ecto.CellException, e: print "Good:" print str(e) assert "I hate life" in str(e)
def __connect(self): ''' Connect oneself to the plasm. ''' plasm = ecto.Plasm() connections = self.connections() for x in connections: if not getattr(x, '__iter__', False): plasm.insert(x) else: plasm.connect(x) self.__impl = ecto.create_black_box(plasm, niter=self.niter, parameters=self.__params._tendrils, inputs=self.__inputs._tendrils, outputs=self.__outputs._tendrils)
def test_20hz(): plasm = ecto.Plasm() ping = ecto_test.Ping("Ping") throttle = ecto_test.Throttle("Throttle", rate=20) metrics = ecto_test.Metrics("Metrics", queue_size=10) plasm.connect(ping[:] >> throttle[:], throttle[:] >> metrics[:]) sched = ecto.Scheduler(plasm) sched.execute(niter=100) print "Hz:", metrics.outputs.hz, " Latency in seconds: %f" % metrics.outputs.latency_seconds # these are kinda loose assert 19 < metrics.outputs.hz < 21 assert 0.04 < metrics.outputs.latency_seconds < 0.06
def test_strand_basic_semantics(): s = ecto.Strand() print "s.id =", s.id orig_id = s.id c = ecto_test.DontCallMeFromTwoThreads("CRASHY", strand=s) c2 = ecto_test.DontCallMeFromTwoThreads("CRASHY2", strand=s) c3 = ecto_test.DontCallMeFromTwoThreads("CRASHY3", strand=s) p = ecto.Plasm() gen = ecto_test.Generate("GENERATE", step=1.0, start=1.0) p.connect(gen[:] >> c[:]) p.connect(c[:] >> c2[:]) p.connect(c2[:] >> c3[:]) sched = ecto.schedulers.Multithreaded(p) sched.execute(10)
def do_ecto(device_id=0, frame_id='base'): video_capture = highgui.VideoCapture('Video Camera', video_device=device_id) mat2image = ecto_ros.Mat2Image(frame_id=frame_id, encoding='bgr8') pub_rgb = ImagePub("image pub", topic_name='image') graph = [ video_capture["image"] >> mat2image["image"], mat2image["image"] >> pub_rgb["input"] ] plasm = ecto.Plasm() plasm.connect(graph) sched = ecto.schedulers.Threadpool(plasm) sched.execute()
def test_dealer_heterogenous_type_fail(Scheduler): print "*" * 80 print __name__, 'test_dealer_heterogenous_type_fail', Scheduler printer = ecto_test.Printer() cards = [1, 2, 3, 4, 5, 'hello', 7, 8, 9, 10] dealer = ecto.Dealer(tendril=printer.inputs.at('in'), iterable=cards) plasm = ecto.Plasm() plasm.connect(dealer['out'] >> printer['in']) sched = Scheduler(plasm) try: sched.execute() assert False == " Should have thrown." except ecto.FailedFromPythonConversion, e: print "Threw as expected:", str(e) assert re.findall('cpp_typename.*double', str(e))
def ecto_process(conn): import ecto from ecto_opencv.highgui import VideoCapture, imshow, FPSDrawer, ImageJpgWriter updator = ImageUpdator(conn=conn) video_cap = VideoCapture(video_device=0, width=1000, height=1000) fps = FPSDrawer() file = StringIO() writer = ImageJpgWriter(file=ecto.ostream(file)) plasm = ecto.Plasm() plasm.connect(video_cap['image'] >> fps['image'], fps['image'] >> writer['image'], writer['file'] >> updator['ostream'] ) sched = ecto.schedulers.Singlethreaded(plasm) sched.execute()
def test_feedback(): plasm = ecto.Plasm() g = ecto_test.Generate("Generator", step=1.0, start=1.0) add = ecto_test.Add() print ecto.EntangledPair.__doc__ source, sink = ecto.EntangledPair(value=add.inputs.at('left')) plasm.connect(source[:] >> add['left'], g[:] >> add['right'], add[:] >> sink[:]) previous = 0 for i in range(1, 5): plasm.execute(niter=1) print add.outputs.out print "expected: ", i + previous assert add.outputs.out == i + previous # 0 + 1 = 1 previous = i + previous
def generate_pointclouds_in_object_space(dbs, session, args): object_name = dbs[session.object_id]['object_name'] if not os.path.exists(object_name): os.mkdir(object_name) obs_ids = models.find_all_observations_for_session(dbs, session.id) if len(obs_ids) == 0: raise RuntimeError("There are no observations available.") db_reader = capture.ObservationReader('Database Source', db_params=dbtools.args_to_db_params(args)) #observation dealer will deal out each observation id. observation_dealer = ecto.Dealer(tendril=db_reader.inputs.at('observation'), iterable=obs_ids) depthTo3d = calib.DepthTo3d('Depth ~> 3D') rescale_depth = capture.RescaledRegisteredDepth('Depth scaling') #this is for SXGA mode scale handling. point_cloud_transform = reconstruction.PointCloudTransform('Object Space Transform',do_transform=False)#keeps the points in camera coordinates, but populates the global sensor position and orientatino. point_cloud_converter = conversion.MatToPointCloudXYZRGB('To Point Cloud') to_ecto_pcl = ecto_pcl.PointCloudT2PointCloud('converter', format=ecto_pcl.XYZRGB) plasm = ecto.Plasm() plasm.connect( observation_dealer[:] >> db_reader['observation'], db_reader['K'] >> depthTo3d['K'], db_reader['image'] >> rescale_depth['image'], db_reader['depth'] >> rescale_depth['depth'], rescale_depth[:] >> depthTo3d['depth'], depthTo3d['points3d'] >> point_cloud_converter['points'], db_reader['image'] >> point_cloud_converter['image'], db_reader['mask'] >> point_cloud_converter['mask'], db_reader['R', 'T'] >> point_cloud_transform['R', 'T'], point_cloud_converter['point_cloud'] >> to_ecto_pcl[:], to_ecto_pcl[:] >> point_cloud_transform['cloud'] ) ply_writer = ecto_pcl.PLYWriter('PLY Saver', filename_format='%s/cloud_%%05d.ply' % (object_name)) pcd_writer = ecto_pcl.PCDWriter('PCD Saver', filename_format='%s/cloud_%%05d.pcd' % (object_name)) plasm.connect(point_cloud_transform['view'] >> (ply_writer['input'], pcd_writer['input']) ) if args.visualize: global cloud_view plasm.connect( point_cloud_transform['view'] >> cloud_view, db_reader['image'] >> imshows['image'], db_reader['depth'] >> imshows['depth'], db_reader['mask'] >> imshows['mask'], ) from ecto.opts import run_plasm run_plasm(args, plasm, locals=vars())
def test_circular(): a1 = ecto_test.Add() a2 = ecto_test.Add() p = ecto.Plasm() p.connect(a1['out'] >> a2['left'], a2['out'] >> a1['right'] ) sched = ecto.Scheduler(p) try: sched.execute(niter=1) fail("that should have thrown") except ecto.EctoException as e: # Thrown when Lumberg's "BFS" is used. print "okay, threw" except ValueError as e: # Thrown when boost::topological_sort() is used. print "okay, threw"
def build_addergraph(nlevels): plasm = ecto.Plasm() #deprecating the movie_out stuff. #plasm.movie_out("frames/ecto%04u.viz"); prevlevel = [ ecto_test.Add("Adder 0_%u" % x) for x in range(2**(nlevels - 1)) ] for adder in prevlevel: gen0 = ecto_test.Generate("Generator", step=1.0, start=1.0) gen1 = ecto_test.Generate("Generator", step=1.0, start=1.0) conn1 = gen0["out"] >> adder["left"] conn2 = gen1["out"] >> adder["right"] print "conn1=", conn1 plasm.connect(conn1, conn2) print "prev has", len(prevlevel) for k in range(nlevels - 2, -1, -1): print "****** k=", k, " ***********" thislevel = [ ecto_test.Add("Adder %u_%u" % (k, x)) for x in range(2**k) ] print "prevlevel=", prevlevel print "thislevel=", thislevel index = 0 print "for...", range(2**k) for r in range(2**k): print "prev[%u] => cur[%u]" % (index, r) conn = prevlevel[index]["out"] >> thislevel[r]["left"] print "conn=", conn plasm.connect(conn) index += 1 print "prev[%u] => cur[%u]" % (index, r) conn2 = prevlevel[index]["out"] >> thislevel[r]["right"] print "conn2=", conn2 plasm.connect(conn2) index += 1 prevlevel = thislevel assert len(prevlevel) == 1 final_adder = prevlevel[0] printer = ecto_test.Printer("printy!") #plasm.connect(final_adder, "out", printer, "in") return (plasm, final_adder)
def detect(cls, source_params, sink_params, pipeline_subtype, pipeline_params, db_params, object_ids, args=None): ''' Returns a training plasm, that will be executed exactly once. :param object_id: The object id to train up. :param session_ids: A list of session ids that this model should be based on. :param observation_ids: A list of observation ids that will be dealt to the incremental model builder. :param pipeline_params: A dictionary of parameters that will be used to initialize the training pipeline. :param db_params: A DB parameters object that specifies where to save the model to. :param args: General command line args, for things like visualize or what have you. :returns: A plasm, only execute once please. ''' pipeline = cls() model_documents = pipeline.db_models(object_ids, pipeline_subtype, db_params) source = pipeline.source(source_params) sink = pipeline.sink(sink_params, object_ids, db_params) detector = pipeline.detector(pipeline_subtype, pipeline_params, db_params, model_documents, args) plasm = ecto.Plasm() # Connect the source to the detector for key in set(source.outputs.keys()).intersection( detector.inputs.keys()): plasm.connect(source[key] >> detector[key]) # Connect the detector to the sink for key in set(detector.outputs.keys()).intersection( sink.inputs.keys()): plasm.connect(detector[key] >> sink[key]) # TODO fix that # make sure that we also give the image_message, in case we want to publish a topic if 'image_message' in sink.inputs and 'image_message' in source.outputs: plasm.connect(source['image_message'] >> sink['image_message']) return plasm
def doit(): print "**** pre import of ecto_lifecycle ********" import ecto.ecto_lifecycle as ecto_lifecycle print "**** post import of ecto_lifecycle *******" print "**** pre allocation of LifeCycle cell ********" lifecycle = ecto_lifecycle.LifeCycle() #allocate a cell print "**** post allocation of LifeCycle cell *******" plasm = ecto.Plasm() print "**** pre insert ********" plasm.insert(lifecycle) print "**** post insert *******" print "**** pre execute ********" sched = ecto.Scheduler(plasm) sched.execute(1) print "**** post execute *******"
def do_ecto(): sub_rgb = ImageSub("image_sub", topic_name='/camera/rgb/image_mono') sub_depth = ImageSub("depth_sub", topic_name='/camera/depth/image') pub_rgb = ImagePub("image_pub", topic_name='my_image') pub_depth = ImagePub("depth_pub", topic_name='my_depth') graph = [ sub_rgb["output"] >> pub_rgb["input"], sub_depth["output"] >> pub_depth["input"] ] plasm = ecto.Plasm() plasm.connect(graph) ecto.view_plasm(plasm) sched = ecto.schedulers.Threadpool(plasm) sched.execute()
def test_parameter_callbacks(): generate = ecto_test.Generate() handle_holder = ecto_test.HandleHolder(value=2) plasm = ecto.Plasm() plasm.connect(generate, "out", handle_holder, "input") sched = ecto.schedulers.Singlethreaded(plasm) for i in range(0, 5): value = handle_holder.params.value * (i + 1) handle_holder.params.value = value print "execute..." sched.execute(niter=1) print "parameter:", handle_holder.outputs.value result = handle_holder.outputs.output print result assert handle_holder.outputs.value == 240 assert handle_holder.outputs.output == 1920