def type_and_instance_names(): m = ecto_test.Generate() name = m.name() print "name is:", name assert name.startswith("ecto_test::Generate<double>") t = m.type_name() print "type is:", t m2 = ecto_test.Generate("user-supplied name") assert m2.name() == "user-supplied name" print "m2.type_name =", m2.type_name() assert m2.type_name() == "ecto_test::Generate<double>"
def test_user_defined_strands(nlevels, SchedType, execfn, expect): s1 = ecto.Strand() s2 = s1 s3 = ecto.Strand() print "s1.id ==", s1.id print "s2.id ==", s2.id print "s3.id ==", s3.id assert s1.id == s2.id assert s3.id != s2.id assert s3.id != s1.id plasm = ecto.Plasm() # plasm.movie_out("strands_%03d.dot") gen = ecto_test.Generate("GENERATE", step=1.0, start=1.0) noncurr = ecto_test.DontCallMeFromTwoThreads("ALPHA", strand=s1) plasm.connect(gen[:] >> noncurr[:]) for k in range(nlevels): n = ecto_test.DontCallMeFromTwoThreads("BETA_%d" % k, strand=s2) plasm.connect(noncurr[:] >> n[:]) noncurr = n printer = ecto_test.Printer("PRINTER") plasm.connect(noncurr[:] >> printer[:]) sched = SchedType(plasm) print "sched=", sched execfn(sched) result = noncurr.outputs.out print "result=", result, "expect=", expect assert (result == expect)
def makeplasm(N): plasm = ecto.Plasm() gen = ecto_test.Generate(start=0, step=1) quitter = ecto_test.QuitAfter(N=N) plasm.connect(gen[:] >> quitter[:]) return (gen, plasm)
def execute_plasm(self, Sched, nthreads, niter, n_nodes, incdelay): plasm = ecto.Plasm() gen = ecto_test.Generate("Gen", step=1.0, start=0.0) inc = ecto_test.Increment("Increment 0", delay=incdelay) plasm.connect(gen, "out", inc, "in") for j in range(n_nodes - 1): # one has already been added inc_next = ecto_test.Increment("Increment_%u" % (j + 1), delay=incdelay) plasm.connect(inc, "out", inc_next, "in") inc = inc_next printer = ecto_test.Printer("Printy") plasm.connect(inc, "out", printer, "in") # # o = open('graph.dot', 'w') # print >>o, plasm.viz() # o.close() # print "\n", plasm.viz(), "\n" sched = Sched(plasm) sched.execute(niter) print("RESULT:", inc.outputs.out) shouldbe = float(n_nodes + niter - 1) print("expected:", shouldbe) assert inc.outputs.out == shouldbe
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 test_If(): plasm = ecto.Plasm() g = ecto_test.Generate("Generator", step=1.0, start=1.0) If = ecto.If(cell=g) truer = ecto.TrueEveryN(n=3, count=3) plasm.connect(truer['flag'] >> If['__test__']) plasm.execute(niter=27) assert g.outputs.out == 9 #should have only called execute 9 times.
def make_plasm(): generate = ecto_test.Generate() param_watcher = ecto_test.ParameterWatcher(value=2) sleep = ecto_test.Sleep(seconds=0.1) plasm = ecto.Plasm() plasm.connect(generate["out"] >> param_watcher["input"]) plasm.insert(sleep) return plasm
def test_modules_spec(): g = ecto_test.Generate(start=0, step=2) x = g["out"] x = g["out", "out"] try: x = g[2.0] util.fail() except TypeError, e: print e
def makeplasm(N): plasm = ecto.Plasm() gen = ecto_test.Generate(start=1, step=1) thrower = ecto_test.ThrowAfter(N=N) mult = ecto_test.Multiply() plasm.connect(gen[:] >> thrower[:], thrower[:] >> mult[:]) return plasm
def build_pipelines(): left_plasm = ecto.Plasm() right_plasm = ecto.Plasm() accumulator = ecto_test.Accumulator("Accumulator", connected_inputs_only=True) left_generator = ecto_test.Generate("Left Generator", step=1.0, start=1.0, stop=10.0) right_generator = ecto_test.Generate("Right Generator", step=2.0, start=1.0, stop=10.0) left_plasm.connect(left_generator["out"] >> accumulator["left"]) right_plasm.connect(right_generator["out"] >> accumulator["right"]) plasms = {"left": left_plasm, "right": right_plasm} scheduler = ecto.MultiPlasmScheduler(plasms, disable_qt_management=True) return (accumulator, scheduler)
def make(Schedtype): print 'Using :', Schedtype plasm = ecto.Plasm() gen = ecto_test.Generate("Gen", step=1.0, start=0.0) printer = ecto_test.Printer("Printy") ecto.log_to_file(fname) plasm.connect(gen[:] >> printer[:]) return Schedtype(plasm)
def test_modules_wrong_args(): not_allocable() do_fail(noarg) g = ecto_test.Generate() do_fail(wrong_type, RuntimeError, g) do_fail(already_set, RuntimeError, g) do_fail(too_many_positionalargs, RuntimeError) novel_sets(g) right_type(g) type_and_instance_names()
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_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_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 test_parameter_callbacks(): 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(1) sched.run_job() # check plasm, init parms, and compute_stack number = 1000 param_watcher.params.value = number sched.run() assert 1000 == param_watcher.outputs.value
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.Scheduler(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
def test_one_to_many(): plasm = ecto.Plasm() g = ecto_test.Generate(start=2, step=2) modules = [] for x in range(0,5): m = ecto_test.Multiply(factor=2) plasm.connect(g,"out",m,"in") modules.append(m) sched = ecto.Scheduler(plasm) sched.execute(niter=1) for x in modules: #print x.outputs.out assert(x.outputs.out == 4) sched.execute(niter=1) for x in modules: #print x.outputs.out assert(x.outputs.out == 8)
def test_0_save_plasm(self): n_nodes = 5 incdelay = 1 plasm = ecto.Plasm() gen = ecto_test.Generate("Gen", step=1.0, start=0.0) inc = ecto_test.Increment("Increment 0", delay=incdelay) plasm.connect(gen, "out", inc, "in") for j in range(n_nodes - 1): # one has already been added inc_next = ecto_test.Increment("Increment_%u" % (j + 1), delay=incdelay) plasm.connect(inc, "out", inc_next, "in") inc = inc_next printer = ecto_test.Printer("Printy") plasm.connect(inc, "out", printer, "in") plasm.save(self.FILE)
def test_modules_01(): g = ecto_test.Generate(start=0, step=2) g.process() assert g.outputs.out == 0 g.process() assert g.outputs.out == 2 g.configure() print type(g.outputs) print type(g.outputs.out) print g.outputs.out g.outputs.out = 7.0 g.process() assert g.outputs.out == 9 s = ecto_test.Scatter(n=4, x=3) s.process() assert (len(s.outputs) == 4) for out in s.outputs: print out[1].val assert (out[1].val == 3)
def test_nested_if(): plasm = ecto.Plasm() g = ecto_test.Generate("Generator", step=1.0, start=1.0) inside_if = ecto.If(input_tendril_name="on_threes", cell=g) outside_if = ecto.If(input_tendril_name="on_twos", cell=inside_if) truer_on_threes = ecto.TrueEveryN(n=3,count=0) truer_on_twos = ecto.TrueEveryN(n=2,count=0) plasm.connect([ truer_on_threes['flag'] >> outside_if['on_threes'], truer_on_twos['flag'] >> outside_if['on_twos'] ]) #for x in range(0,18): # plasm.execute(niter=1) # print("No of times executed: %s of %s" % (g.outputs.out, x)) # executes on the very first iteration (count = 0) and once every 3*2 iterations thereafter plasm.execute(niter=18) assert g.outputs.out == 3 # should have only called execute 3 times. plasm.execute(niter=1) assert g.outputs.out == 4 # should have executed once more
def shouldfail(): plasm = ecto.Plasm() gen = ecto_test.Generate(step=1.0, start=1.0) nc1 = ecto_test.DontCallMeFromTwoThreads() plasm.connect(gen, "out", nc1, "in") nc2 = ecto_test.DontCallMeFromTwoThreads() plasm.connect(nc1, "out", nc2, "in") printer = ecto_test.Printer() plasm.connect(nc2, "out", printer, "in") sched = ecto.Scheduler(plasm) try: print "about to execute... this should throw" sched.execute(niter=4) util.fail() except RuntimeError, e: print "good, python caught error", e
def test_python_module_plasm(Schedtype): print "*"*80 print Schedtype mod = MyModule(text="spam") g = ecto_test.Generate(start = 1 , step =1) plasm = ecto.Plasm() plasm.connect(g,"out",mod,"input") sched = Schedtype(plasm) for i in range(1,5): print "HERE" sched.execute(niter=1) sched.execute_async(niter=1) sched.run() assert g.outputs.out == i*2 assert mod.outputs.out == "spam"*i*2 sched.execute(niter=1) sched.execute_async(niter=1) sched.run() assert g.outputs.out == 10 assert mod.outputs.out == "spam"*10
def test_implicit_strands(nlevels, SchedType, execfn, expect): plasm = ecto.Plasm() gen = ecto_test.Generate(step=1.0, start=1.0) noncurr = ecto_test.CantCallMeFromTwoThreads() plasm.connect(gen, "out", noncurr, "in") for k in range(nlevels): next = ecto_test.CantCallMeFromTwoThreads() plasm.connect(noncurr, "out", next, "in") noncurr = next printer = ecto_test.Printer() plasm.connect(noncurr, "out", printer, "in") sched = SchedType(plasm) print "sched=", sched execfn(sched) result = noncurr.outputs.out print "result=", result assert (result == expect)
def test_parameter_callbacks(): generate = ecto_test.Generate() param_watcher = ecto_test.ParameterWatcher(value=2) plasm = ecto.Plasm() plasm.connect(generate, "out", param_watcher, "input") sched = ecto.Scheduler(plasm) sched.execute(niter=2) print "first value: ", param_watcher.params.value, param_watcher.outputs.output, generate.outputs.out assert param_watcher.params.value == 2 assert param_watcher.outputs.output == 4 assert generate.outputs.out == 2 for i in range(0, 5): value = param_watcher.params.value * (i + 1) param_watcher.params.value = value print "execute..." sched.execute(niter=1) print "parameter:", param_watcher.outputs.value result = param_watcher.outputs.output print result assert param_watcher.outputs.value == 240 assert param_watcher.outputs.output == 2880.0
def test_dual_line_plasm(nlevels): plasm = ecto.Plasm() gen = ecto_test.Generate(step=1.0, start=0.0) incl, incr = ecto_test.Increment(), ecto_test.Increment() plasm.connect(gen, "out", incl, "in") plasm.connect(gen, "out", incr, "in") for j in range(nlevels - 1): # one set of incs has already been added print j inc_nextl, inc_nextr = ecto_test.Increment(), ecto_test.Increment() plasm.connect(incl, "out", inc_nextl, "in") plasm.connect(incr, "out", inc_nextr, "in") incl, incr = inc_nextl, inc_nextr add = ecto_test.Add() plasm.connect(incl, "out", add, "left") plasm.connect(incr, "out", add, "right") printer = ecto_test.Printer() plasm.connect(add, "out", printer, "in") sched = ecto.Scheduler(plasm) sched.execute(niter=1) result = add.outputs.out print "result=", result assert (result == nlevels * 2) sched.execute(niter=2) result = add.outputs.out print "iter2 result=", result assert result == (nlevels + 2) * 2 sched.execute(niter=3) result = add.outputs.out print "iter3 result=", result assert result == (nlevels + 5) * 2
# notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the Willow Garage, Inc. nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # import ecto, sys, util import ecto.ecto_test as ecto_test try: gen = ecto_test.Generate(step='Foobar') util.fail() except ecto.FailedFromPythonConversion, e: print "caught: ", e print "which is exactly what we expected. Wunderbar." except: util.fail()
#!/usr/bin/env python import ecto, sys import ecto.ecto_test as ecto_test plasm = ecto.Plasm() gen = ecto_test.Generate("Gen", step=1.0, start=0.0) inc = ecto_test.Increment("Increment 0", delay=100) printer = ecto_test.Printer("Printy") plasm.connect(gen[:] >> inc[:], inc[:] >> printer[:]) sched = ecto.schedulers.Threadpool(plasm) sched.execute_async(niter=10) from IPython.Shell import IPShellEmbed ipshell = IPShellEmbed() ipshell()
def test_it(): g = ecto_test.Generate("Generator", step=1.0, start=1.0) bpc = ecto_test.BpObjectToCellPtr() bpc.inputs.cell = g bpc.configure()
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. import ecto import ecto.ecto_test as ecto_test # run the generator 10 times gen = ecto_test.Generate(start=0, step=1, stop=9) # break on every second iteration breaker = ecto_test.BreakEveryN(n=2) cnt = ecto.Counter() plasm = ecto.Plasm() plasm.connect([gen[:] >> breaker[:], breaker[:] >> cnt[:]]) plasm.execute(niter=0) # as cnt depends on breaker it will only be processed every second iteration result = cnt.outputs.count print "Counter ran " + str(result) + " times" assert (result == 5)