def collatz(ctx, value, macro_code, macro_params): import sys #kludge print("COLLATZ", value) ctx.series = cell("json") if value == 1: ctx.series.set([1]) return if value % 2: newvalue = value * 3 + 1 else: newvalue = value // 2 ###ctx.value = cell("int").set(value) ###ctx.newvalue = cell("int").set(newvalue) ctx.value = cell().set(value) ctx.newvalue = cell().set(newvalue) ctx.macro_params = cell("json").set(macro_params) m = ctx.macro = macro(ctx.macro_params.value) ctx.newvalue.connect(m.value) ctx.macro_code = cell("macro").set(macro_code) ctx.macro_code.connect(m.code) ctx.macro_code.connect(m.macro_code) ctx.macro_params.connect(m.macro_params) if sys.USE_TRANSFORMER_CODE: ###transformer code version ctx.tf = transformer({"a": "input", "b": "input", "c": "output"}) m.gen_context.series.connect(ctx.tf.a) ctx.value.connect(ctx.tf.b) ctx.tf.code.set("c = [b] + a") ctx.tf.c.connect(ctx.series) print("/COLLATZ", value) else: #no transformer code, exploits that macro is synchronous series = [value] + m.gen_context.series.value ctx.series.set(series) print("/COLLATZ", series)
def code(ctx, a): ctx.answer = cell().set(a) ctx.double = transformer({"test": "input", "result": "output"}) ctx.answer.connect(ctx.double.test) ctx.double.code.cell().set("test * 2") ctx.result = cell() ctx.double.result.connect(ctx.result)
def load_e(ctx): ctx.tf = transformer({ "iterations": "input", "e": "output" }) ctx.code = libcell(".e.code") ctx.code.connect(ctx.tf.code) ctx.iterations = cell() ctx.iterations.connect(ctx.tf.iterations) ctx.result = cell() ctx.tf.e.connect(ctx.result)
def main(): global ctx, compute ctx = context(toplevel=True) ctx.select_compute = libcell("compute.select") ctx.compute = macro(select_params, lib="compute") ctx.select_compute.connect(ctx.compute.code) ctx.compute.which.cell().set("pi") compute = ctx.compute.gen_context ctx.iterations = cell().set(10000) ctx.iterations.connect(compute.iterations) ctx.result = cell() compute.result.connect(ctx.result)
def select(ctx, which): assert which in ("pi", "e") ctx.readme = libcell(".readme") ctx.loader = macro({}) if which == "pi": ctx.load = libcell(".pi.load") else: ctx.load = libcell(".e.load") ctx.load.connect(ctx.loader.code) compute = ctx.loader.ctx ctx.iterations = cell() ctx.iterations.connect(compute.iterations) ctx.result = cell() compute.result.connect(ctx.result)
def select(ctx, which): assert which in ("pi", "e") ctx.readme = libcell(".readme") ctx.loader = macro({}) if which == "pi": ctx.load = libcell(".pi.load") else: ctx.load = libcell(".e.load") ctx.load.connect(ctx.loader.code) compute = ctx.loader.gen_context ctx.iterations = cell() ctx.iterations.connect(compute.iterations) ctx.result = cell() compute.result.connect(ctx.result)
def define_ctx(): with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell("json").set(1) ctx.cell2 = cell("json").set(2) ctx.result = cell("json") ctx.tf = transformer({"a": "input", "b": "input", "c": "output"}) ctx.cell1_link = link(ctx.cell1) ctx.cell1_link.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = pytransformercell().set("c = a + b") ctx.code.connect(ctx.tf.code) ctx.result_link = link(ctx.result) ctx.tf.c.connect(ctx.result_link) return ctx
def main(): global ctx, compute ctx = context(toplevel=True) ctx.select_compute = libcell("compute.select") ctx.compute = macro(select_params, lib="compute") ctx.select_compute.connect(ctx.compute.code) ###ctx.compute.which.cell().set("pi") # TODO: malfunctioning ### KLUDGE ctx.which_cell = cell().set("pi") ctx.which_cell.connect(ctx.compute.which) ### /KLUDGE compute = ctx.compute.ctx ctx.iterations = cell().set(10000) ctx.iterations.connect(compute.iterations) ctx.result = cell() compute.result.connect(ctx.result)
def collatz(ctx, value, macro_code, macro_params): print("COLLATZ", value) ctx.series = cell() if value == 1: ctx.series.set([1]) return if value % 2: newvalue = value * 3 + 1 else: newvalue = value // 2 ###ctx.value = cell("int").set(value) ###ctx.newvalue = cell("int").set(newvalue) ctx.value = cell().set(value) ctx.newvalue = cell().set(newvalue) ctx.macro_params = cell().set(macro_params) m = ctx.macro = macro(macro_params) ctx.newvalue.connect(m.value) ctx.macro_code = cell("macro").set(macro_code) ctx.macro_code.connect(m.code) ctx.macro_code.connect(m.macro_code) ctx.macro_params.connect(m.macro_params) ctx.tf = transformer({"a": "input", "b": "input", "c": "output"}) ctx.a = cell() ctx.a.connect(ctx.tf.a) ctx.b = cell() ctx.b.connect(ctx.tf.b) m.ctx.series.connect(ctx.a) ctx.value.connect(ctx.b) ctx.tf.code.set("[b] + a") ctx.tf.c.connect(ctx.series) print("/COLLATZ", value)
def collatz(ctx, value, macro_code, macro_params): print("COLLATZ", value) ctx.series = cell() if value == 1: ctx.series.set([1]) return if value % 2: newvalue = value * 3 + 1 else: newvalue = value // 2 ctx.value = cell("int").set(value) ctx.newvalue = cell("int").set(newvalue) ctx.macro_params = cell().set(macro_params) m = ctx.macro = macro(macro_params) ctx.newvalue.connect(m.value) ctx.macro_code = cell("macro").set(macro_code) ctx.macro_code.connect(m.code) ctx.macro_code.connect(m.macro_code) ctx.macro_params.connect(m.macro_params) ctx.tf = transformer({"a": "input", "b": "input", "c": "output"}) ctx.a = cell() ctx.a.connect(ctx.tf.a) ctx.b = cell("int") ctx.b.connect(ctx.tf.b) m.ctx.series.connect(ctx.a) ctx.value.connect(ctx.b) ctx.tf.code.set("c = [b] + a") ctx.tf.c.connect(ctx.series) print("/COLLATZ", value)
def _finalize(ctx, ctf, inp, c_inp, result, c_result, input_name, result_name, inchannels, node): result_cell_name1 = result_name + "_CELL1" result_cell_name2 = result_name + "_CELL2" input_cell_name = input_name + "_CELL" link_options_cell_name = "LINK_OPTIONS_CELL" main_module_cell_name = input_name + "_MAIN_MODULE_CELL" forbidden = (result_name, result_cell_name1, result_cell_name2, input_cell_name, link_options_cell_name, main_module_cell_name) for c in inchannels: assert ( not len(c) ) or c[0] not in forbidden #should have been checked by highlevel result_cell1 = cell("mixed") cell_setattr(node, ctx, result_cell_name1, result_cell1) result_cell2 = cell("mixed") cell_setattr(node, ctx, result_cell_name2, result_cell2) input_cell = cell("mixed") cell_setattr(node, ctx, input_cell_name, input_cell) link_options_cell = cell("plain") cell_setattr(node, ctx, link_options_cell_name, link_options_cell) main_module_cell = cell("plain") cell_setattr(node, ctx, main_module_cell_name, main_module_cell) link_options_cell.connect(ctx.main_module.inchannels[("link_options", )]) link_options_cell.set(node.get("link_options", [])) #1: between transformer and library ctx.inputpins.connect(ctf.gen_header.inputpins) ctx.pins.connect(ctf.executor.pins) ctf.executor.result.connect(result_cell1) result_cell1.connect(result.inchannels[()]) inp.outchannels[()].connect(input_cell) input_cell.connect(ctf.executor.kwargs) c_inp.schema.connect(ctf.gen_header.input_schema) c_result.schema.connect(ctf.gen_header.result_schema) c_inp.schema.connect(ctf.executor.input_schema) c_result.schema.connect(ctf.executor.result_schema) ctf.gen_header.input_name.cell().set(input_name) ctf.gen_header.result_name.cell().set(result_name) ctf.executor.input_name.cell().set(input_name) ctf.executor.result_name.cell().set(result_name) #2: among library cells ctx.header = cell("text") ctf.gen_header.result.connect(ctx.header) ctx.header.connect(ctf.integrator.header) ctx.language.connect(ctf.integrator.lang) ctx.code.connect(ctf.integrator.compiled_code) ctx.main_module.outchannels[()].connect(main_module_cell) main_module_cell.connect(ctf.integrator.main_module) ctx.module = cell("mixed") ctf.integrator.result.connect(ctx.module) ctx.module.connect(ctf.executor.module)
def define_ctx(): with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell().set(1) ctx.cell2 = cell().set(2) ctx.result = cell() ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.cell1_link = link(ctx.cell1) ctx.cell1_link.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = pytransformercell().set("c = a + b") ctx.code.connect(ctx.tf.code) ctx.result_link = link(ctx.result) ctx.tf.c.connect(ctx.result_link) return ctx
def transform_job(rqdata): x = decode(rqdata, as_cells=True) transformer_params, output_signature, cells, _, _ = x inputs = cells.keys() with macro_mode_on(): ctx = context(toplevel=True) tf = ctx.TRANSFORMER = transformer(transformer_params) for k in inputs: setattr(ctx, k, cells[k]) cells[k].connect(getattr(tf, k)) outputpin = getattr(tf, tf._output_name) for n, outp in enumerate(output_signature): name = "RESULT" + str(n + 1) if outp == "mixed": c2 = cell("text") setattr(ctx, name + "_storage", c2) c3 = cell("json") setattr(ctx, name + "_form", c3) c = cell("mixed", storage_cell=c2, form_cell=c3) else: c = cell(outp) setattr(ctx, name, c) outputpin.connect(c) while tf.status() not in ("OK", "ERROR"): ctx.equilibrate(1) result = {} if tf.status() != "OK": result["ERROR"] = ctx.tf.transformer.EXCEPTION else: for n, outp in enumerate(output_signature): name = "RESULT" + str(n + 1) data = getattr(ctx, name) if outp == "mixed": storage = getattr(ctx, name + "_storage") result["STORAGE"] = storage form = getattr(ctx, name + "_form") result["FORM"] = form result[outp] = data for key, c in list(result.items()): val = c.serialize_buffer() result[key] = val return result
def macro_code(ctx, param): ctx.sub = context() ctx.a = cell().set(1000 + param) ctx.b = cell().set(2000 + param) ctx.result = cell() ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.a.connect(ctx.tf.a) ctx.b.connect(ctx.tf.b) ctx.code = cell("transformer").set("print('TRANSFORM'); import time; time.sleep(2); c = a + b") ctx.code.connect(ctx.tf.code) ctx.tf.c.connect(ctx.result) assert param != 999 # on purpose if param > 1: ctx.d = cell().set(42) # raise Exception("on purpose") #causes the macro reconstruction to fail pass # For some reason, comments at the end are not captured with inspect.get_source?
def build(ctx, param, run): print("BUILD") tf_params = { "a": "input", "b": "input", "c": "output", } ctx.tf1 = transformer(tf_params) ctx.tf2 = transformer(tf_params) tf = ctx.tf2 if param == "PARAM" else ctx.tf1 ctx.run = cell("transformer").set(run) ctx.run.connect(tf.code) ctx.a = cell() ctx.b = cell() ctx.a.connect(tf.a) ctx.b.connect(tf.b) ctx.c = cell() tf.c.connect(ctx.c) ctx.param = cell().set(param)
def reset_backend(share_schemas=True, with_hash_pattern=True): hp = hash_pattern if with_hash_pattern else None global ctx, s, s2, s3 if ctx is not None: ctx.compute() # makes no difference, but could be easier debugging ctx.destroy() ctx = context(toplevel=True) ctx.data = cell("mixed", hash_pattern=hp) ctx.buffer = cell("mixed", hash_pattern=hp) ctx.schema = cell("plain") ctx.sc = StructuredCell( buffer=ctx.buffer, data=ctx.data, schema=ctx.schema, hash_pattern=hp ) s = ctx.sc.handle ctx.data2 = cell("mixed", hash_pattern=hp) ctx.buffer2 = cell("mixed", hash_pattern=hp) if share_schemas: schema2 = ctx.schema else: ctx.schema2 = cell("plain") schema2 = ctx.schema2 ctx.sc2 = StructuredCell( buffer=ctx.buffer2, data=ctx.data2, schema=schema2, hash_pattern=hp ) s2 = ctx.sc2.handle hp3 = None # never use hash pattern for this one ctx.data3 = cell("mixed", hash_pattern=hp3) ctx.buffer3 = cell("mixed", hash_pattern=hp3) if share_schemas: schema3 = ctx.schema else: ctx.schema3 = cell("plain") schema3 = ctx.schema3 ctx.sc3 = StructuredCell( buffer=ctx.buffer3, data=ctx.data3, schema=schema3, hash_pattern=hp3 ) s3 = ctx.sc3.handle
def _finalize(ctx, ctf, inp, c_inp, result, c_result, input_name, result_name): #1: between transformer and library ctx.inputpins.connect(ctf.gen_header.inputpins) ctx.pins.connect(ctf.translator.pins) ctx.result.connect_inchannel(ctf.translator.translator_result_, ()) inp.connect_outchannel((), ctf.translator.kwargs) c_inp.schema.connect(ctf.gen_header.input_schema) c_result.schema.connect(ctf.gen_header.result_schema) c_inp.schema.connect(ctf.translator.input_schema) c_result.schema.connect(ctf.translator.result_schema) ctf.gen_header.input_name.cell().set(input_name) ctf.gen_header.result_name.cell().set(result_name) ctf.translator.input_name.cell().set(input_name) ctf.translator.result_name.cell().set(result_name) #2: among library cells ctx.header = cell("text") ctf.gen_header.result.connect(ctx.header) ctx.header.connect(ctf.compiler.header) ctx.language.connect(ctf.compiler.lang) ctx.code.connect(ctf.compiler.compiled_code) ctx.main_module.connect_outchannel((), ctf.compiler.main_module) ctx.compiler_verbose.connect(ctf.compiler.compiler_verbose) ctx.binary_module_storage = cell("text") ctx.binary_module_storage._sovereign = True ctx.binary_module_form = cell("json") ctx.binary_module_form._sovereign = True ctx.binary_module = cell( "mixed", storage_cell=ctx.binary_module_storage, form_cell=ctx.binary_module_form, ) ctx.binary_module._sovereign = True ctf.compiler.result.connect(ctx.binary_module) ctx.binary_module.connect(ctf.translator.binary_module)
def build(ctx, param, run): print("BUILD") tf = transformer( { "a": "input", "b": "input", "c": "output", }) if param.startswith("PARAM"): ctx.tf_alt1 = tf else: ctx.tf_alt2 = tf ctx.tf = link(tf) ctx.run = pytransformercell().set(run) ctx.run.connect(tf.code) ctx.a = cell() ctx.a.connect(tf.a) ctx.b = cell() ctx.b.connect(tf.b) ctx.c = cell() tf.c.connect(ctx.c) ctx.param = cell().set(param)
def structured_transformer(c): tf_params = { "limit": ("input", "int"), "delay": ("input", "float"), "factor": ("input", "float"), "offset": ("input", "float"), "result": ("output", "float"), } c.tf = transformer(tf_params) c.code = cell("transformer").set(progress) c.code.connect(c.tf.code) c.input_auth = cell("mixed") c.input_data = cell("mixed") c.input_buffer = cell("mixed") c.input_schema = cell("plain") c.input = StructuredCell( data=c.input_data, auth=c.input_auth, buffer=c.input_buffer, schema=c.input_schema, inchannels=[()], outchannels=[(k, ) for k in tf_params.keys() if k != "result"], ) c.result = cell("mixed") c.tf.result.connect(c.result) for param in tf_params: if param == "result": continue icellname = "input_param_" + param icell = cell("mixed") setattr(c, icellname, icell) outchannel = c.input.outchannels[(param, )] pin = getattr(c.tf, param) outchannel.connect(icell) icell.connect(pin) c.example_data = cell("mixed") c.example_buffer = cell("mixed") c.example = StructuredCell(data=c.example_data, buffer=c.example_buffer, schema=c.input_schema)
async def main(): ctx = context(toplevel=True) ctx.cell1 = cell("int").set(1) ctx.cell2 = cell("int").set(2) #ctx.code = cell("transformer") #ctx.code = cell("transformer").set("c = 'test'") #ctx.code = cell("transformer").set("raise Exception") #ctx.code = cell("transformer").set("import time; time.sleep(2); c = a + b") ctx.code = cell("transformer").set("a + b") ctx.result = cell("int") ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.cell1_unilink = unilink(ctx.cell1) ctx.cell1_unilink.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code_copy = cell("transformer") ctx.code.connect(ctx.code_copy) ctx.code_copy.connect(ctx.tf.code) ctx.result_unilink = unilink(ctx.result) ctx.tf.c.connect(ctx.result_unilink) ctx.result_copy = cell("int") ctx.result.connect(ctx.result_copy) await ctx.computation(1) print("STOP") print(ctx.cell1.value, ctx.cell1, ctx.cell1.status) print(ctx.cell2.value, ctx.cell2, ctx.cell2.status) print(ctx.code.value, ctx.code, ctx.code.status) print(ctx.code_copy.value, ctx.code_copy, ctx.code_copy.status) print(ctx.result.value, ctx.result, ctx.result.status) print(ctx.result_copy.value, ctx.result_copy, ctx.result_copy.status) print(ctx.tf.value, ctx.tf, ctx.tf.status) print(ctx.status) print(ctx.tf.exception) await ctx.computation() ctx.cell1.set(10) await ctx.computation() print(ctx.result.value, ctx.status) ctx.code.set("c = a + b + 1000") await ctx.computation() print(ctx.result.value, ctx.status) print("Introduce delay...") ctx.code.set("import time; time.sleep(2); c = -(a + b)") await ctx.computation(1.0) print("after 1.0 sec...") print(ctx.result.value, ctx.status) print("...") await ctx.computation() print(ctx.result.value, ctx.status)
def build(ctx, param, run): print("BUILD") tf = transformer({ "a": "input", "b": "input", "c": "output", }) if param.startswith("PARAM"): ctx.tf_alt1 = tf else: ctx.tf_alt2 = tf ctx.tf = link(tf) ctx.run = pytransformercell().set(run) ctx.run.connect(tf.code) ctx.a = link(tf.a).export() ctx.b = link(tf.b).export() ctx.c = link(tf.c).export() ctx.param = cell("json").set(param)
def create(ctx, with_buffer, with_schema, inchannels): with macro_mode_on(): ctx.struc = context(name="struc",context=ctx) ctx.struc.storage = cell("text") ctx.struc.form = cell("json") ctx.struc.data = cell("mixed", form_cell = ctx.struc.form, storage_cell = ctx.struc.storage, ) schema = None if with_schema: ctx.struc.schema = cell("json") schema = ctx.struc.schema bufferwrapper = None if with_buffer: ctx.struc.buffer_storage = cell("text") ctx.struc.buffer_form = cell("json") ctx.struc.buffer_data = cell("mixed", form_cell = ctx.struc.buffer_form, storage_cell = ctx.struc.buffer_storage, ) bufferwrapper = BufferWrapper( ctx.struc.buffer_data, ctx.struc.buffer_storage, ctx.struc.buffer_form ) ctx.hub = StructuredCell( "hub", ctx.struc.data, storage = ctx.struc.storage, form = ctx.struc.form, schema = schema, buffer = bufferwrapper, inchannels = inchannels, outchannels = [], )
def create(ctx, with_buffer, with_schema, inchannels): with macro_mode_on(): ctx.struc = context(name="struc", context=ctx) ctx.struc.storage = cell("text") ctx.struc.form = cell("json") ctx.struc.data = cell( "mixed", form_cell=ctx.struc.form, storage_cell=ctx.struc.storage, ) schema = None if with_schema: ctx.struc.schema = cell("json") schema = ctx.struc.schema bufferwrapper = None if with_buffer: ctx.struc.buffer_storage = cell("text") ctx.struc.buffer_form = cell("json") ctx.struc.buffer_data = cell( "mixed", form_cell=ctx.struc.buffer_form, storage_cell=ctx.struc.buffer_storage, ) bufferwrapper = BufferWrapper(ctx.struc.buffer_data, ctx.struc.buffer_storage, ctx.struc.buffer_form) ctx.hub = StructuredCell( "hub", ctx.struc.data, storage=ctx.struc.storage, form=ctx.struc.form, schema=schema, buffer=bufferwrapper, inchannels=inchannels, outchannels=[], )
from seamless.core import context, cell, StructuredCell from seamless.core import macro_mode_on with macro_mode_on(): ctx = context(toplevel=True) ctx.data = cell("mixed") ctx.sc = StructuredCell(data=ctx.data) data = ctx.sc.handle data.set(20) print(data) ctx.compute() print(data.data, ctx.data.value) data.set({}) data.a = "test" data.b = 12 data.b.set(5) data.c = {"d": {}} data.c.d.e = 12.0 print(data) ctx.compute() print(data.data, ctx.data.value)
"language": "python", "transformer": True, "checksum": 'b0480c66eb4dbb31f5311e09e89b9414c880360842b9d5ef7b6621fc31a5ab99', }], "connections": [{ "source": ("pi",), "target": ("doubleit", "a"), }, { "source": ("doubleit",), "target": ("twopi",), }, { "source": ("code",), "target": ("doubleit", "code"), }], } ctx0 = context(toplevel=True) ctx0.pi = cell("mixed").set(math.pi) assert ctx0.pi.checksum == '9809b7dfcfe29dd194c71c7d2da94af3aeef98f079eeff8e1d9e5099acef737c' ctx0.code = cell("python").set("result = a * 2") assert ctx0.code.checksum == 'b0480c66eb4dbb31f5311e09e89b9414c880360842b9d5ef7b6621fc31a5ab99' with macro_mode_on(): ctx = context(toplevel=True, manager=ctx0._get_manager()) translate(graph, ctx, [], False) ctx.equilibrate() print(ctx.twopi.value)
import seamless #seamless.core.cache.use_caching = False ### from seamless.core import macro_mode_on from seamless.core import context, cell, transformer, pymacrocell, macro with macro_mode_on(): ctx = context(toplevel=True) ctx.param = cell("json").set(1) ctx.macro = macro({ "param": "copy", }) ctx.param.connect(ctx.macro.param) ctx.inp = cell("text").set("INPUT") ctx.macro_code = pymacrocell().set(""" print("Execute macro") ctx.submacro = macro({ "inp": "copy" }) ctx.submacro_code = pymacrocell().set(''' print("Execute submacro") ctx.inp = cell("text").set(inp + "!!!") ''') ctx.submacro_code.connect(ctx.submacro.code) """) ctx.macro_code.connect(ctx.macro.code) ctx.inp.connect(ctx.macro.ctx.submacro.inp) print(ctx.macro.ctx.submacro.ctx.inp.value) ctx.macro.ctx.submacro.ctx.inp.set(10)
from seamless.core import context, cell, transformer ctx = context(toplevel=True) ctx.a = cell().set([10, 20, 50, 100, 200]) def triple_it(aa): return 3 * aa ctx.code = cell("python").set(triple_it) tf_params = {"aa": "input", "result": "output"} stream_params = {"aa": "map"} ctx.tf = transformer(tf_params, stream_params) ctx.code.connect(ctx.tf.code) ctx.a.connect(ctx.tf.aa) ctx.result = cell() ctx.tf.result.connect(ctx.result) ctx.equilibrate() print(ctx.result.value) ctx.a.set([5, 10, 20, 50, 100, 200]) ctx.equilibrate() print(ctx.result.value) ctx.a.set({"first": 1, "second": 2, "third": 3, "fourth": 4, "fifth": 5}) ctx.equilibrate() print(ctx.result.value)
from math import pi from seamless.core import context, cell ctx0 = context(toplevel=True) ctx0.cell = cell("mixed").set(pi) ctx0.cell2 = cell("python").set("lambda a: 2 * a") ctx0.cell.set({"a": pi}) import json graph = json.load(open("twopi.seamless")) from seamless.highlevel import load_graph ctx = load_graph(graph, cache_ctx=ctx0) ctx.translate(force=True) print(ctx.pi.checksum) print(ctx.pi.value) print("equilibrate") ctx.equilibrate() print(ctx.twopi.value) graph2 = json.load(open("twopi-result.seamless")) ctx2 = load_graph(graph2, cache_ctx=ctx0) print(ctx2.pi.checksum) print(ctx2.pi.value) print("equilibrate") ctx2.equilibrate() print(ctx2.twopi.value)
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, reactor, pythoncell, transformer with macro_mode_on(): ctx = context(toplevel=True) ctx.aa = cell().set(1) ctx.tf = transformer({ "aa": "input", "a": "output" }) ctx.aa.connect(ctx.tf.aa) ctx.code = pythoncell().set("""import time time.sleep(2) a = aa""") ctx.code.connect(ctx.tf.code) ctx.a = cell() ctx.tf.a.connect(ctx.a) ctx.b = cell().set(2) ctx.result = cell() ctx.rc = reactor({ "a": "input", "b": "input", "c": "output" }, pure=False) # change between pure=True/"semi"/False (default) ctx.a.connect(ctx.rc.a) ctx.b.connect(ctx.rc.b) ctx.code_start = pythoncell().set("") ctx.code_start.connect(ctx.rc.code_start) ctx.code_update = pythoncell().set("""
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer, pytransformercell, link with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell() ctx.cell2 = cell() ctx.cell1.set(5) ctx.cell1.set_label("five") print(ctx.cell1.label) print(ctx.cell1.value) ctx.cell1.set(7) ctx.cell2.from_label("five") print(ctx.cell2.label) print(ctx.cell2.value) print(ctx.cell1.label) print(ctx.cell1.value)
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer, pymacrocell, macro with macro_mode_on(): ctx = context(toplevel=True) ctx.param = cell("plain").set(1) ctx.mymacro = macro({ "param": "copy", }) ctx.param.connect(ctx.mymacro.param) ctx.inp = cell("text").set("INPUT") ctx.mymacro_code = pymacrocell().set(""" print("Executing 'mymacro'...") ctx.submacro = macro({ "inp": "copy" }) ctx.submacro_code = pymacrocell().set(''' print("Executing 'submacro, param = %s'...") ctx.myinp = cell("text").set(inp + "!!!") ''' % param) ctx.submacro_code.connect(ctx.submacro.code) ctx.inp2 = cell("text") ctx.inp2.connect(ctx.submacro.inp) """) ctx.mymacro_code.connect(ctx.mymacro.code) ctx.inp.connect(ctx.mymacro.ctx.inp2) print(ctx.mymacro.ctx.submacro.ctx.myinp.value)
""" Final test for compiled transformers, using stdlib.compiled_transformer """ from seamless.core import context, cell, transformer, macro, libcell, macro_mode_on from seamless.core import StructuredCell from seamless.core import library from copy import deepcopy # 1: set up example data with macro_mode_on(): ctx = context(toplevel=True) # 1a. Setup of StructuredCells ctx.inp_struc = context(name="inp_struc",context=ctx) ctx.inp_struc.storage = cell("text") ctx.inp_struc.form = cell("json") ctx.inp_struc.data = cell("mixed", form_cell = ctx.inp_struc.form, storage_cell = ctx.inp_struc.storage, ) ctx.inp_struc.schema = cell("json") ctx.inp = StructuredCell( "inp", ctx.inp_struc.data, storage = ctx.inp_struc.storage, form = ctx.inp_struc.form, schema = ctx.inp_struc.schema, buffer = None, inchannels = [], outchannels = [()]
await asyncio.sleep(5) print("... server 4") return h(43) except asyncio.CancelledError: print("... server 4 CANCELLED") raise seamless.set_ncores(0) from seamless.core.cache.cache_task import remote_transformer_result_servers remote_transformer_result_servers += [ server1, server2, server3, server4 ] with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell().set(2) ctx.cell2 = cell().set(3) ctx.code = cell().set("c = a + b") ctx.result = cell() ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.cell1.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.tf.c.connect(ctx.result) ctx.code.connect(ctx.tf.code) ctx.hashcell = cell() ctx.hashcell.set(42).set(43).set(99)
import seamless from seamless.core import context, cell, transformer, pytransformercell, link ctx = context(toplevel=True) ctx.cell1 = cell().set(1) ctx.cell2 = cell().set(2) ctx.result = cell() ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.cell1_link = link(ctx.cell1) ctx.cell1_link.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = pytransformercell().set("c = a + b") ctx.code.connect(ctx.tf.code) ctx.result_link = link(ctx.result) ctx.tf.c.connect(ctx.result_link) ctx.result_copy = cell() ctx.result.connect(ctx.result_copy) print(ctx.cell1.value) print(ctx.code.value) ctx.equilibrate() print(ctx.result.value, ctx.status) ctx.cell1.set(10) ctx.equilibrate() print(ctx.result.value, ctx.status) ctx.code.set("c = a + b + 1000") ctx.equilibrate()
outchannels = [], ) ctx.auth_json = context(name="auth_json", context=ctx) recreate(ctx.auth_json, "auth_json.struc", with_buffer=False, with_schema=False, inchannels=[]) ctx.auth = context(name="auth", context=ctx) recreate(ctx.auth, "auth.struc", with_buffer=False, with_schema=True, inchannels=[]) ctx.err = context(name="err", context=ctx) recreate(ctx.err, "err.struc", with_buffer=True, with_schema=True, inchannels=[]) ctx.nauth = context(name="nauth", context=ctx) try: recreate(ctx.nauth, "nauth.struc", with_buffer=False, with_schema=False, inchannels=[("z",)]) except: import traceback; traceback.print_exc() with macro_mode_on(): ctx.readme = cell().set("readme") ctx.load = cell("macro").set(load) ctx.auth_json = context(name="auth_json", context=ctx) create(ctx.auth_json, with_buffer=False, with_schema=False, inchannels=[]) auth_json = ctx.auth_json.hub auth_json.set("value of auth_json") ctx.auth = context(name="auth", context=ctx) create(ctx.auth, with_buffer=False, with_schema=True, inchannels=[]) auth = ctx.auth.hub auth.handle.a = "value of auth.a" auth.handle.b = "value of auth.b" print(auth.value) def val(self): assert isinstance(self.a, str)
try: c.y = -0.3 except ValidationError: pass c.z = 0.93 print(c.data) c.xyz = -1,0,0 print(c.data, c.xyz) c.xyz = 0.2,-0.3,0.93 print(c.data, c.xyz) pprint(c.schema.value) ctx.compute() ctx.destroy() ctx = context(toplevel=True) ctx.data = cell("mixed") ctx.buffer = cell("mixed") ctx.schema = cell("plain") ctx.sc = StructuredCell( buffer=ctx.buffer, data=ctx.data, schema=ctx.schema ) Test = ctx.sc.handle # singleton """ # will never work for a singleton backed up by a structured cell def __init__(self, a, b): self.a = a self.b = b """
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer, pytransformercell, StructuredCell with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell("mixed") ctx.cell2 = cell("mixed").set(2) ctx.result = cell("mixed") ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.a = StructuredCell( "a", ctx.cell1, schema = None, buffer = None, plain = True, inchannels = None, outchannels = [()] ) ctx.a.outchannels[()].connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = pytransformercell().set("c = a + b") ctx.code.connect(ctx.tf.code) ctx.tf.c.connect(ctx.result) ctx.mount("/tmp/mount-test")
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer, pytransformercell with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell().set(1) ctx.cell2 = cell().set(2) ctx.result = cell() ctx.tf = transformer({"a": "input", "b": "input", "c": "output"}) ctx.cell1.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = pytransformercell().set("c = a + b") ctx.code.connect(ctx.tf.code) ctx.tf.c.connect(ctx.result) shell = ctx.tf.shell()
from .testmodule.sub.mod1 import func from .testmodule.sub.mod2 import func as func2 print(testvalue) print(func is func2) print(func2()) print(testmodule.testvalue) from .testmodule import mod3 print(mod3.testvalue) print(mod3.testfunc(99)) result = 0 print("/execute") """ with macro_mode_on(): ctx = context(toplevel=True) ctx.param = cell("plain").set(1) ctx.macro = macro({ "param": "plain", "testmodule": ("plain", "module"), }) ctx.param.connect(ctx.macro.param) ctx.macro_code = cell("macro").set(code) ctx.macro_code.connect(ctx.macro.code) ctx.testmodule = cell("plain").set(testmodule) ctx.testmodule.connect(ctx.macro.testmodule) print("START") ctx.compute()
import seamless from seamless.core import context, cell, transformer, pytransformercell, link import numpy as np ctx = context(toplevel=True) x = np.arange(10) y = np.log(x+1) cell1 = cell("mixed").set({"x": x, "y": y, "z": [1,2,"test",[3,4]]}) cell1.mount("/tmp/mixedcell.mixed") ctx.cell1 = cell1 print(ctx.cell1.storage) print(ctx.cell1.form) print(ctx.cell1.value) ctx.cell2 = cell("mixed").set(80) ctx.result = cell("mixed") ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.cell1_link = link(ctx.cell1) ctx.cell1_link.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = pytransformercell().set("c = a['x'] * a['y'] + b") ctx.code.connect(ctx.tf.code) ctx.result_link = link(ctx.result) ctx.tf.c.connect(ctx.result_link) ctx.result_copy = cell("mixed") ctx.result.connect(ctx.result_copy)
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer with macro_mode_on(): topctx = context(toplevel=True) ctx = topctx.sub = context(toplevel=False) assert topctx.sub is ctx ctx.cell1 = cell().set(1) ctx.cell2 = cell().set(2) ctx.result = cell() ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.cell1.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = cell("transformer").set("c = a + b") ctx.code.connect(ctx.tf.code) ctx.tf.c.connect(ctx.result) topctx.compute() print(ctx.result.value) ctx.cell1.set(10) topctx.compute() print(ctx.result.value) ctx.code.set("c = a + b + 1000") topctx.compute() print(ctx.result.value) print(ctx.status)
import seamless from seamless.core import macro_mode_on from seamless.core import context,cell with macro_mode_on(): ctx = context(toplevel=True) ctx.json = cell("plain").set({}) ctx.json.mount("/tmp/test.json", "w") ctx.equilibrate() ctx.json.set({"a": 1})
ctx.macro_code = cell("macro").set(macro_code) ctx.macro_code.connect(m.code) ctx.macro_code.connect(m.macro_code) ctx.macro_params.connect(m.macro_params) ctx.tf = transformer({"a": "input", "b": "input", "c": "output"}) ctx.a = cell() ctx.a.connect(ctx.tf.a) ctx.b = cell() ctx.b.connect(ctx.tf.b) m.ctx.series.connect(ctx.a) ctx.value.connect(ctx.b) ctx.tf.code.set("[b] + a") ctx.tf.c.connect(ctx.series) print("/COLLATZ", value) ctx.start = cell() ctx.code = cell("macro").set(collatz) macro_params = {k: "ref" for k in ("value", "macro_code", "macro_params")} ctx.macro_params = cell().set(macro_params) m = ctx.macro = macro(ctx.macro_params.value) ctx.start.connect(m.value) ctx.code.connect(m.code) ctx.code.connect(m.macro_code) ctx.macro_params.connect(m.macro_params) ctx.series = cell() start = 27 ###start = 10 #7-level nesting ###start = 12 #10-level nesting ###start = 23 #16-level nesting ###start = 27 #111-level nesting
import seamless from seamless.core import cell, transformer, context ctx = context(toplevel=True) ctx.cell1 = cell("cson").set("a: 10") ctx.cell1a = cell("plain") ctx.cell1.connect(ctx.cell1a) print(ctx.cell1.value) print(ctx.cell1.semantic_checksum) print(ctx.cell1a.value) print(ctx.cell1a.semantic_checksum) params = {"v": "input", "result": "output"} def func(v): return v["a"] + 2 ctx.code = cell("transformer").set(func) ctx.tf = transformer(params) ctx.code.connect(ctx.tf.code) ctx.cell1.connect(ctx.tf.v) ctx.result = cell() ctx.tf.result.connect(ctx.result) ctx.equilibrate() print(ctx.result.value) tcache = ctx._get_manager().transform_cache tf1 = tcache.transformer_to_level1[ctx.tf] print("TF level 1", tf1.get_hash()) tf2 = tcache.hlevel1_to_level2[tf1.get_hash()] print("TF level 2", tf2.get_hash()) seamless.set_ncores(0) # no more local computations
from seamless.core import context, cell ctx = context(toplevel=True) ctx.bytes1 = cell("bytes") ctx.bytes1.set(b'this is a bytes value') ctx.compute() print(ctx.bytes1.buffer) print(ctx.bytes1.value, type(ctx.bytes1.value)) print() ctx.bin = cell("binary") ctx.bytes1.connect(ctx.bin) ctx.compute() print("Exception:", ctx.bin.exception) print(ctx.bin.buffer) print(ctx.bin.value, type(ctx.bin.value)) print() ctx.bytes2 = cell("bytes") ctx.bin.connect(ctx.bytes2) ctx.compute() print("Exception:", ctx.bytes2.exception) print(ctx.bytes2.buffer) print(ctx.bytes2.value, type(ctx.bytes2.value)) print() ctx.mix = cell("mixed") ctx.bytes1.connect(ctx.mix) ctx.compute() print("Exception:", ctx.mix.exception) print(ctx.mix.buffer) print(ctx.mix.value, type(ctx.mix.value))
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, reactor, pythoncell, ipythoncell with macro_mode_on(): ctx = context(toplevel=True) ctx.i = cell().set(100) ctx.result = cell() ctx.rc = reactor({ "i": "input", "testmodule": ("input", "ref", "module", "ipython"), "result": "output", "html": "output" }) ctx.i.connect(ctx.rc.i) ctx.code_start = pythoncell().set("") ctx.code_start.connect(ctx.rc.code_start) ctx.code_update = pythoncell().set(""" import time from .testmodule import func, func_html i = PINS.i.get() t = time.time() result = func(i) print("Time: %.1f" % (time.time() - t)) PINS.result.set(result) if PINS.testmodule.updated: PINS.html.set(func_html) """) ctx.code_update.connect(ctx.rc.code_update) ctx.code_stop = pythoncell().set("") ctx.code_stop.connect(ctx.rc.code_stop)
""" Retrieval of buffers and computation results from database cache First run simple.py with Seamless database running """ import seamless from seamless.core import context, cell, transformer, unilink seamless.set_ncores(0) # Forbids Seamless to add 1 and 2 by itself seamless.database_cache.connect() ctx = context(toplevel=True) ctx.cell1 = cell("int").set_checksum( "bc4bb29ce739b5d97007946aa4fdb987012c647b506732f11653c5059631cd3d" # 1 ) ctx.cell2 = cell("int").set_checksum( "191fb5fc4a9bf2ded9a09a0a2c4eb3eb90f15ee96deb1eec1a970df0a79d09ba" # 2 ) ctx.code = cell("transformer").set_checksum( "1cbba7cc10e067273fdec7cc350d2b87508c1959d26bbab4f628f6f59ec49607" # a + b ) ctx.result = cell("int") ctx.tf = transformer({"a": "input", "b": "input", "c": "output"}) ctx.cell1_unilink = unilink(ctx.cell1) ctx.cell1_unilink.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code_copy = cell("transformer") ctx.code.connect(ctx.code_copy) ctx.code_copy.connect(ctx.tf.code) ctx.result_unilink = unilink(ctx.result)
if self.st == 2: return self.st, self.progress, self.prelim elif self.st == 3: return self.st, self.result else: return self.st, None from seamless.communion_client import communion_client_manager m = communion_client_manager m.clients["transformation"] = [ DummyClient(), ] # dirty hack with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell().set(2) ctx.cell2 = cell().set(3) ctx.code = cell("python").set("c = a + b") ctx.result = cell() ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.cell1.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.tf.c.connect(ctx.result) ctx.code.connect(ctx.tf.code) ctx.hashcell = cell() for n in range(10):
import seamless from seamless.core import macro_mode_on from seamless.core import context,textcell, cell, transformer, pytransformercell with macro_mode_on(): ctx = context(toplevel=True) ctx.mount("/tmp/mount-test", persistent=None) ctx.cell1 = cell().set(1) ctx.cell2 = cell().set(2) result = cell().mount("/tmp/mount-test/myresult", persistent=True) ctx.result = result ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.cell1.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = pytransformercell().set("c = a + b") ctx.code.connect(ctx.tf.code) ctx.tf.c.connect(ctx.result) ctx.sub = context(toplevel=False) ctx.sub.mycell = textcell().set("This is my cell\nend") ctx.equilibrate() print(ctx.result.value) ctx.cell1.set(10) ctx.equilibrate() print(ctx.result.value) print(ctx.result.value)
storage, form = get_form(binary_module) x = to_stream(binary_module, storage, form) binary_module2 = from_stream(x, storage, form) assert (is_identical_debug(binary_module, binary_module2)) ###################################################################### # 5: test it in a debugging context ###################################################################### print("START") from seamless.core import context, cell, transformer, macro_mode_on with macro_mode_on(): ctx = context(toplevel=True) ctx.module_storage = cell("text") ctx.module_form = cell("json") ctx.module = cell("mixed", form_cell=ctx.module_form, storage_cell=ctx.module_storage) ctx.module.set(binary_module, auto_form=True) tf = ctx.tf = transformer({ "a": ("input", "ref", "json"), "b": ("input", "ref", "json"), "testmodule": ("input", "ref", "binary_module"), "result": ("output", "ref", "json"), }) ctx.tf.debug = True ctx.module.connect(tf.testmodule) tf.a.cell().set(2) tf.b.cell().set(3)
seamless.set_ncores(0) from seamless import communionserver communionserver.configure_master( value=True, transformer_job=True, transformer_result=True, transformer_result_level2=True ) communionserver.configure_servant(value=True) #redis_cache = seamless.RedisCache() with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell().set(2) ctx.cell2 = cell().set(3) ctx.result = cell() ctx.tf = transformer({ "a": "input", "b": "input", "c": "output" }) ctx.cell1.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = pytransformercell() ctx.code.connect(ctx.tf.code) ctx.tf.c.connect(ctx.result) import asyncio done = asyncio.sleep(1)
def translate_macro(node, root, namespace, inchannels, outchannels): from .translate import set_structured_cell_from_checksum parent = get_path(root, node["path"][:-1], None, None) name = node["path"][-1] ctx = context(toplevel=False) setattr(parent, name, ctx) param_name = node["PARAM"] all_inchannels = set(inchannels) param_inchannels = [] interchannels = [] pin_cells = {} pin_mpaths0 = {} for pinname in list(node["pins"].keys()): pin = node["pins"][pinname] if pin["io"] == "parameter": pin_cell_name = pinname + "_PARAM" assert pin_cell_name not in node["pins"] assert pin_cell_name not in all_inchannels pinname2 = as_tuple(pinname) interchannels.append(pinname2) if pinname2 in inchannels: param_inchannels.append(pinname2) elif pin["io"] in ("input", "output", "edit"): pin_cell_name = pinname else: raise ValueError((pin["io"], pinname)) pin_hash_pattern = pin.get("hash_pattern") celltype = pin.get("celltype", "mixed") if celltype == "mixed": pin_cell = cell(celltype, hash_pattern=pin_hash_pattern) else: pin_cell = cell(celltype) cell_setattr(node, ctx, pin_cell_name, pin_cell) pin_cells[pinname] = pin_cell if pin["io"] != "parameter": pin_mpaths0[pinname] = (pin["io"] in ("input", "edit")) mount = node.get("mount", {}) param = None if len(interchannels): param, param_ctx = build_structured_cell(ctx, param_name, param_inchannels, interchannels, return_context=True, fingertip_no_remote=False, fingertip_no_recompute=False, hash_pattern={"*": "#"}) setattr(ctx, param_name, param) namespace[node["path"] + ("SCHEMA", ), "source"] = param.schema, node if "param_schema" in mount: param_ctx.schema.mount(**mount["param_schema"]) param_pins = {} for pinname, pin in node["pins"].items(): if pin["io"] != "parameter": continue p = {"io": "input"} p.update(pin) param_pins[pinname] = p ctx.macro = macro(param_pins) if node.get("elision"): ctx.macro.allow_elision = True elision = {"macro": ctx.macro, "input_cells": {}, "output_cells": {}} for pinname in pin_mpaths0: is_input = pin_mpaths0[pinname] pin_mpath = getattr(core_path(ctx.macro.ctx), pinname) pin_cell = pin_cells[pinname] if is_input: if node["pins"][pinname]["io"] == "edit": pin_cell.bilink(pin_mpath) else: pin_cell.connect(pin_mpath) elision["input_cells"][pin_cell] = pin_mpath else: pin_mpath.connect(pin_cell) elision["output_cells"][pin_cell] = pin_mpath ctx._get_manager().set_elision(**elision) ctx.code = cell("macro") if "code" in mount: ctx.code.mount(**mount["code"]) ctx.code.connect(ctx.macro.code) checksum = node.get("checksum", {}) if "code" in checksum: ctx.code._set_checksum(checksum["code"], initial=True) if param is not None: param_checksum = convert_checksum_dict(checksum, "param") set_structured_cell_from_checksum(param, param_checksum) namespace[node["path"] + ("code", ), "target"] = ctx.code, node namespace[node["path"] + ("code", ), "source"] = ctx.code, node for pinname in node["pins"]: path = node["path"] + as_tuple(pinname) pin = node["pins"][pinname] if pin["io"] == "parameter": pinname2 = as_tuple(pinname) if pinname2 in inchannels: namespace[path, "target"] = param.inchannels[pinname], node target = getattr(ctx.macro, pinname) assert target is not None, pinname pin_cell = pin_cells[pinname] param.outchannels[pinname2].connect(pin_cell) pin_cell.connect(target) elif pin["io"] == "edit": namespace[path, "edit"] = pin_cells[pinname], node else: cmode = "target" if pin["io"] == "input" else "source" namespace[path, cmode] = pin_cells[pinname], node
raise NotImplementedError # Silk access is not working; the modified code below kludges around it import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer, StructuredCell import numpy as np with macro_mode_on(): ctx = context(toplevel=True) ctx.mount("/tmp/mount-test", persistent=None) #directory remains, but empty ctx.inp_struc = context(toplevel=False) ctx.inp_struc.data = cell("mixed") ctx.inp = StructuredCell( "inp", ctx.inp_struc.data, plain = False, schema = None, buffer = None, inchannels = None, outchannels = [()] ) ctx.tf = transformer({ ###"inp": ("input", "copy", "silk"), "inp": ("input", "copy", "mixed"), "c": "output", }) ctx.inp.outchannels[()].connect(ctx.tf.inp) ###ctx.tf.code.cell().set("c = inp.a * inp.dat + inp.b") ctx.tf.code.cell().set("c = inp['a'] * inp['dat'] + inp['b']")
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer, macro with macro_mode_on(): ctx = context(toplevel=True) ctx.param = cell("plain").set(1) ctx.mymacro = macro({ "param": "plain", }) ctx.param.connect(ctx.mymacro.param) ctx.inp = cell("text").set("INPUT") ctx.mymacro_code = cell("macro").set(""" print("Executing 'mymacro'...") ctx.submacro = macro({ "inp": "plain" }) ctx.submacro_code = cell("macro").set(''' print("Executing 'submacro, param = %s'...") ctx.myinp = cell("text").set(inp + "!!!") ''' % param) ctx.submacro_code.connect(ctx.submacro.code) ctx.inp2 = cell("text") ctx.inp2.connect(ctx.submacro.inp) """) ctx.mymacro_code.connect(ctx.mymacro.code) ctx.inp.connect(ctx.mymacro.ctx.inp2) ctx.compute()
buffer_cache.LIFETIME_TEMP = 0.01 buffer_cache.LIFETIME_TEMP_SMALL = 0.01 buffer_cache.LOCAL_MODE_FULL_PERSISTENCE = False from seamless.core.protocol import calculate_checksum_module as calculate_checksum calculate_checksum.checksum_cache.disable() from seamless.core import context, cell, transformer import seamless.core.execute seamless.core.execute.DIRECT_PRINT = True ctx = context(toplevel=True) manager = ctx._get_manager() ctx.a = cell("int") ctx.a.set(10) ctx.tf = transformer({"a": "input", "aa": "output"}) ctx.tf.code.cell().set("print('RUN', a); aa = 2 * a") ctx.a.connect(ctx.tf.a) ctx.aa = cell("int") ctx.tf.aa.connect(ctx.aa) ctx.compute() checksum = ctx.aa.checksum print(checksum) print(ctx.aa.value) ctx.a.set(5) print("START") ctx.compute() loop.run_until_complete(asyncio.sleep(0.1)) ctx.a.set(10)
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer, StructuredCell from seamless.core.structured_cell import BufferWrapper import numpy as np with macro_mode_on(): ctx = context(toplevel=True) ctx.hub_struc = context(name="hub_struc",context=ctx) ctx.hub_struc.storage = cell("text") ctx.hub_struc.form = cell("json") ctx.hub_struc.data = cell("mixed", form_cell = ctx.hub_struc.form, storage_cell = ctx.hub_struc.storage, ) ctx.hub_struc.schema = cell("json") ctx.hub_struc.buffer_storage = cell("text") ctx.hub_struc.buffer_form = cell("json") ctx.hub_struc.buffer_data = cell("mixed", form_cell = ctx.hub_struc.buffer_form, storage_cell = ctx.hub_struc.buffer_storage, ) bufferwrapper = BufferWrapper( ctx.hub_struc.buffer_data, ctx.hub_struc.buffer_storage, ctx.hub_struc.buffer_form ) ctx.hub = StructuredCell( "hub", ctx.hub_struc.data, storage = ctx.hub_struc.storage,
from math import pi from seamless.core import context, cell ctx0 = context(toplevel=True) ctx0.cell = cell("mixed").set(pi) ctx0.cell2 = cell("python").set("lambda a: 2 * a") ctx0.cell.set({"a": pi}) import json graph = json.load(open("twopi.seamless")) from seamless.highlevel import load_graph ctx = load_graph(graph, cache_ctx=ctx0) ctx.translate(force=True) print(ctx.pi.checksum) print(ctx.pi.value) print("compute") ctx.compute() print(ctx.twopi.value) graph2 = json.load(open("twopi-result.seamless")) ctx2 = load_graph(graph2, cache_ctx=ctx0) print(ctx2.pi.checksum) print(ctx2.pi.value) print("compute") ctx2.compute() print(ctx2.twopi.value)
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer, pytransformercell with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell().set(1) ctx.cell2 = cell().set(2) ctx.result = cell("plain") ctx.tf = transformer({ "a": "input", "b": "input", "f": ("input", "ref", "silk"), "c": "output" }) ctx.cell1.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = pytransformercell().set("c = a + b") ctx.code.connect(ctx.tf.code) ctx.tf.c.connect(ctx.result) ctx.f = cell("plain").set({"f1": 10, "f2": 20}) ctx.f.connect(ctx.tf.f) ctx.mount("/tmp/mount-test") ctx.equilibrate() print(ctx.result.value) ctx.cell1.set(10) ctx.equilibrate() print(ctx.result.value) ctx.code.set("""
def create(ctx, mount, state=None): with macro_mode_on(): ctx.hub_struc = context(name="hub_struc",context=ctx) ctx.hub_struc.storage = cell("text") ctx.hub_struc.form = cell("json") ctx.hub_struc.data = cell("mixed", form_cell = ctx.hub_struc.form, storage_cell = ctx.hub_struc.storage, ) ctx.hub_struc.schema = cell("json") ctx.hub_struc.buffer_storage = cell("text") ctx.hub_struc.buffer_form = cell("json") ctx.hub_struc.buffer_data = cell("mixed", form_cell = ctx.hub_struc.buffer_form, storage_cell = ctx.hub_struc.buffer_storage, ) bufferwrapper = BufferWrapper( ctx.hub_struc.buffer_data, ctx.hub_struc.buffer_storage, ctx.hub_struc.buffer_form ) ctx.hub = StructuredCell( "hub", ctx.hub_struc.data, storage = ctx.hub_struc.storage, form = ctx.hub_struc.form, schema = ctx.hub_struc.schema, buffer = bufferwrapper, inchannels = [("a", "factor1"), ("a", "factor2"), ("b",)], outchannels = [("a",), ("b",), ("c",)], state = state, ) ctx.code = cell("transformer").set("d = a.factor1 * b + a.factor2 * c + a.constant") ctx.mixer = transformer({ "a": ("input", "ref", "silk"), "b": ("input", "ref", "object"), "c": ("input", "ref", "object"), "d": ("output", "ref", "json"), }) ctx.a_factor1 = cell("json") ctx.a_factor2 = cell("json") ctx.b = cell("json") ctx.code.connect(ctx.mixer.code) ctx.hub.connect_inchannel(ctx.a_factor1, ("a", "factor1")) ctx.hub.connect_inchannel(ctx.a_factor2, ("a", "factor2")) ctx.hub.connect_inchannel(ctx.b, ("b",)) ctx.hub.connect_outchannel(("a",), ctx.mixer.a) ctx.hub.connect_outchannel(("b",), ctx.mixer.b) ctx.hub.connect_outchannel(("c",), ctx.mixer.c) ctx.result_struc = context(name="result_struc",context=ctx) ctx.result_struc.storage = cell("text") ctx.result_struc.form = cell("json") ctx.result_struc.data = cell("mixed", form_cell = ctx.result_struc.form, storage_cell = ctx.result_struc.storage, ) ctx.result_struc.schema = cell("json") ctx.result_struc.buffer_storage = cell("text") ctx.result_struc.buffer_form = cell("json") ctx.result_struc.buffer_data = cell("mixed", form_cell = ctx.result_struc.buffer_form, storage_cell = ctx.result_struc.buffer_storage, ) bufferwrapper = BufferWrapper( ctx.result_struc.buffer_data, ctx.result_struc.buffer_storage, ctx.result_struc.buffer_form ) ctx.result = StructuredCell( "result", ctx.result_struc.data, storage = ctx.result_struc.storage, form = ctx.result_struc.form, schema = ctx.result_struc.schema, buffer = bufferwrapper, inchannels = [()], outchannels = [] ) ctx.result.connect_inchannel(ctx.mixer.d, ()) if mount: ctx.mount("/tmp/mount-test")
assert which in ("pi", "e") ctx.readme = libcell(".readme") ctx.loader = macro({}) if which == "pi": ctx.load = libcell(".pi.load") else: ctx.load = libcell(".e.load") ctx.load.connect(ctx.loader.code) compute = ctx.loader.ctx ctx.iterations = cell() ctx.iterations.connect(compute.iterations) ctx.result = cell() compute.result.connect(ctx.result) lctx = context(toplevel=True) lctx.readme = cell("text").set("Compute pi or e iteratively") lctx.pi = context() lctx.pi.code = cell("python").set(compute_pi) lctx.pi.load = cell("macro").set(load_pi) lctx.e = context() lctx.e.code = cell("python").set(compute_e) lctx.e.load = cell("macro").set(load_e) lctx.select = cell("macro").set(select) lctx.equilibrate() lib = library.build(lctx) library.register("compute", lib) select_params = { "which": ("ref", "text"), }