def _init_from_graph(ctf, debug): ctf.gen_header_code = sctx.gen_header.code.cell() ctf.gen_header_params = sctx.gen_header_params.cell() ctf.gen_header = transformer(sctx.gen_header_params.value) ctf.gen_header_code.connect(ctf.gen_header.code) ctf.integrator_code = sctx.integrator.code.cell() ctf.integrator_params = sctx.integrator_params.cell() ctf.integrator = transformer(sctx.integrator_params.value) ctf.integrator.debug_.cell().set(debug) ctf.integrator_code.connect(ctf.integrator.code) ctf.executor_code = sctx.executor.code.cell() ctf.executor_params = sctx.executor_params.cell() ctf.executor = transformer(sctx.executor_params.value) if debug: ctf.executor.debug = True ctf.executor_code.connect(ctf.executor.code)
def translate_module(node, root, namespace, inchannels, outchannels): module_type = node["module_type"] language = node["language"] dependencies = node["dependencies"] path = node["path"] parent = get_path(root, path[:-1], None, None) name = path[-1] for c in inchannels + outchannels: assert not len(c) #should have been checked by highlevel subcontext = context(toplevel=False) setattr(parent, name, subcontext) codecell = core_cell("plain") subcontext.code = codecell if node.get("fingertip_no_recompute"): codecell._fingertip_recompute = False if node.get("fingertip_no_remote"): codecell._fingertip_remote = False pathstr = "." + ".".join(path) checksum = node.get("checksum") if checksum is not None: codecell._set_checksum(checksum, initial=True) if "mount" in node: codecell2 = core_cell("text") subcontext.code2 = codecell2 codecell2.mount(**node["mount"]) mode = node["mount"].get("mode", "rw") if mode == "rw": codecell2.bilink(codecell) elif mode == "r": codecell2.connect(codecell) elif mode == "w": codecell.connect(codecell2) subcontext.module_cell = core_cell("plain") subcontext.gen_module_cell = transformer({ "module_type": ("input", "str"), "language": ("input", "str"), "module_code": ("input", "plain"), "dependencies": ("input", "plain"), "result": ("output", "plain") }) subcontext.gen_module_cell.code.cell().set( inspect.getsource(gen_module_cell)) subcontext.gen_module_cell.module_type.cell().set(module_type) subcontext.gen_module_cell.language.cell().set(language) subcontext.gen_module_cell.dependencies.cell().set(dependencies) codecell.connect(subcontext.gen_module_cell.module_code) subcontext.gen_module_cell.result.connect(subcontext.module_cell) namespace[path, "source"] = subcontext.module_cell, node namespace[path, "target"] = codecell, node return subcontext
def load(ctx): ctx.readme = libcell(".readme") ctx.tf = transformer({ "iterations": "input", "pi": "output" }) ctx.code = libcell(".code") ctx.code.connect(ctx.tf.code) ctx.iterations = link(ctx.tf.iterations) ctx.result = link(ctx.tf.pi)
def _init_from_library(ctf, debug): # Just to register the "compiled_transformer" lib from seamless.lib.compiled_transformer import compiled_transformer as _ with library.bind("compiled_transformer"): ctf.gen_header_code = libcell(".gen_header.code") ctf.gen_header_params = libcell(".gen_header_params") ctf.gen_header = transformer(ctf.gen_header_params.value) ctf.gen_header_code.connect(ctf.gen_header.code) ctf.compiler_code = libcell(".compiler.code") ctf.compiler_params = libcell(".compiler_params") ctf.compiler = transformer(ctf.compiler_params.value) ctf.compiler_code.connect(ctf.compiler.code) ctf.translator_code = libcell(".translator.code") ctf.translator_params = libcell(".translator_params") ctf.translator = transformer(ctf.translator_params.value) if debug: ctf.translator.debug = True ctf.translator_code.connect(ctf.translator.code)
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 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)
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 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 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 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 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)
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)
ctx.y = cell("text").set("y" + str(param)) ctx.z = cell("text") ctx.q = cell("text").set("q" + str(param)) ctx.qq = cell("text") ctx.q_link = link(ctx.q) ctx.r = cell("text").set("r" + str(param)) ctx.r_link = link(ctx.r) ctx.rr = cell("text") ctx.rr_link = link(ctx.rr) ctx.async_submacro = macro({}) ctx.not_async_submacro = macro({}) """) ctx.macro_code.connect(ctx.macro.code) ctx.tfx = transformer({ "x0": "input", "x": "output" }) ctx.tfx.code.cell().set("x = x0 + '!'") ctx.macro.ctx.x0.connect(ctx.tfx.x0) ctx.x = cell("text") ctx.tfx.x.connect(ctx.macro.ctx.x_link) ctx.tfx.x.connect(ctx.x) ctx.y = cell("text") ctx.macro.ctx.y.connect(ctx.y) ctx.e = cell("json") ctx.e2 = cell("json") p_d = path(ctx.macro.ctx).d p_d.connect(ctx.e) p_tf2 = path(ctx.macro.ctx).tf2 ctx.e.connect(p_tf2.e) p_tf2.e2.connect(ctx.e2)
ctx.testmodule.connect(ctx.macro.testmodule) print("START") ctx.compute() print(ctx.macro.exception) with macro_mode_on(): ctx = context(toplevel=True) ctx.param = cell("plain").set(1) ctx.tf = transformer({ "param": { "io": "input", "celltype": "plain", }, "testmodule": { "io": "input", "celltype": "plain", "subcelltype": "module", }, "result": "output" }) ctx.param.connect(ctx.tf.param) ctx.tf_code = cell("transformer").set(code) ctx.tf_code.connect(ctx.tf.code) ctx.testmodule = cell("plain").set(testmodule) ctx.testmodule.connect(ctx.tf.testmodule) ctx.result = cell("plain") ctx.tf.result.connect(ctx.result)
###################################################################### 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) tf.code.cell().set(""" from .testmodule import lib print("ADD", lib.addfunc(a,b)) result = testmodule.lib.addfunc(a,b) """) ctx.result = cell("json") ctx.tf.result.connect(ctx.result)
ctx.x0 = cell("text").set("x" + str(param)) ctx.x = cell("text") ctx.x_link = link(ctx.x) ctx.y = cell("text").set("y" + str(param)) ctx.z = cell("text") ctx.q = cell("text").set("q" + str(param)) ctx.qq = cell("text") ctx.q_link = link(ctx.q) ctx.r = cell("text").set("r" + str(param)) ctx.r_link = link(ctx.r) ctx.rr = cell("text") ctx.rr_link = link(ctx.rr) """) ctx.macro_code.connect(ctx.macro.code) ctx.tfx = transformer({ "x0": "input", "x": "output" }) ctx.tfx.code.cell().set("x = x0 + '!'") ctx.tfx_x0 = cell("text") ctx.tfx_x0.connect(ctx.tfx.x0) ctx.macro.ctx.x0.connect(ctx.tfx_x0) ctx.x = cell("text") ctx.tfxx = cell("text") ctx.tfx.x.connect(ctx.tfxx) ctx.tfxx.connect(ctx.macro.ctx.x_link) ctx.tfx.x.connect(ctx.x) ctx.y = cell("text") ctx.macro.ctx.y.connect(ctx.y) ctx.e = cell("plain") ctx.e2 = cell("plain") p_d = path(ctx.macro.ctx).d
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 = [("a",)], outchannels = [()] ) ctx.a = cell("json") ctx.inp.connect_inchannel(ctx.a, ("a",)) ctx.tf = transformer({ "inp": ("input", "copy", "silk"), "c": "output", }) ctx.inp.connect_outchannel((), ctx.tf.inp) ctx.tf.code.cell().set("c = inp.a.x * inp.dat + inp.b") ctx.result = cell() ctx.tf.c.connect(ctx.result) ctx.mount("/tmp/mount-test") ###ctx.equilibrate() print("part 1") print(ctx.tf.status()) print(ctx.result.value) print()
def translate_bash_transformer(node, root, namespace, inchannels, outchannels, lib_path00, is_lib): #TODO: simple translation, without a structured cell #TODO: there is a lot of common code with py transformer assert not "code" in node ### node["code"] is an outdated attribute # Just to register the "bash_transformer" lib from seamless.core.macro_mode import get_macro_mode, curr_macro from seamless.lib.bash_transformer import bash_transformer as _ inchannels = [ic for ic in inchannels if ic[0] != "code"] parent = get_path(root, node["path"][:-1], None, None) name = node["path"][-1] lib_path0 = lib_path00 + "." + name if lib_path00 is not None else None ctx = context(toplevel=False) setattr(parent, name, ctx) result_name = node["RESULT"] input_name = node["INPUT"] for c in inchannels: assert (not len(c)) or c[0] != result_name #should have been checked by highlevel with_result = node["with_result"] buffered = node["buffered"] pins = node["pins"].copy() for extrapin in ("bashcode", "pins"): assert extrapin not in node["pins"], extrapin pins[extrapin] = { "transfer_mode": "ref", "access_mode": "plain", "content_type": None, } ctx.pins = core_cell("plain").set(list(pins.keys())) interchannels = [as_tuple(pin) for pin in pins] plain = node["plain"] mount = node.get("mount", {}) inp, inp_ctx = build_structured_cell( ctx, input_name, True, plain, buffered, inchannels, interchannels, lib_path0, return_context=True ) setattr(ctx, input_name, inp) if "input_schema" in mount: inp_ctx.schema.mount(**mount["input_schema"]) for inchannel in inchannels: path = node["path"] + inchannel namespace[path, True] = inp.inchannels[inchannel], node assert result_name not in pins #should have been checked by highlevel all_pins = {} for pinname, pin in pins.items(): p = {"io": "input"} p.update(pin) all_pins[pinname] = p all_pins[result_name] = {"io": "output", "transfer_mode": "copy"} if node["SCHEMA"]: assert with_result all_pins[node["SCHEMA"]] = { "io": "input", "transfer_mode": "json", "access_mode": "json", "content_type": "json" } ctx.tf = transformer(all_pins) if node["debug"]: ctx.tf.debug = True if lib_path00 is not None: lib_path = lib_path00 + "." + name + ".code" ctx.code = libcell(lib_path) else: ctx.code = core_cell("text") if "code" in mount: ctx.code.mount(**mount["code"]) ctx.code._sovereign = True ctx.pins.connect(ctx.tf.pins) ctx.code.connect(ctx.tf.bashcode) checksum = node.get("checksum", {}) if "code" in checksum: ctx.code.set_checksum(checksum["code"]) if "input" in checksum: inp.set_checksum(checksum["input"]) with library.bind("bash_transformer"): ctx.executor_code = libcell(".executor_code") ctx.executor_code.connect(ctx.tf.code) namespace[node["path"] + ("code",), True] = ctx.code, node namespace[node["path"] + ("code",), False] = ctx.code, node for pin in list(node["pins"].keys()): target = getattr(ctx.tf, pin) inp.outchannels[(pin,)].connect(target) if with_result: plain_result = node["plain_result"] result, result_ctx = build_structured_cell( ctx, result_name, True, plain_result, False, [()], outchannels, lib_path0, return_context=True ) if "result_schema" in mount: result_ctx.schema.mount(**mount["result_schema"]) setattr(ctx, result_name, result) result_pin = getattr(ctx.tf, result_name) result_pin.connect(result.inchannels[()]) if node["SCHEMA"]: schema_pin = getattr(ctx.tf, node["SCHEMA"]) result.schema.connect(schema_pin) if "result" in checksum: result.set_checksum(checksum["result"]) if "schema" in checksum: result.schema.set_checksum(checksum["schema"]) else: for c in outchannels: assert len(c) == 0 #should have been checked by highlevel result = getattr(ctx.tf, result_name) namespace[node["path"] + (result_name,), False] = result, node namespace[node["path"], True] = inp, node namespace[node["path"], False] = result, node
}) ctx.inputpins = cell("json").set(["a", "b"]) # Just to register the "compiled_transformer" lib from seamless.lib.compiled_transformer import compiled_transformer as _ libdict = library._lib["compiled_transformer"] libcells = [k for k in libdict.keys() if k.find("_STRUC") == -1] print(libcells) # 2: set up library with macro_mode_on(), library.bind("compiled_transformer"): ctf.gen_header_code = libcell(".gen_header.code") ctf.gen_header_params = libcell(".gen_header_params") ctf.gen_header = transformer(ctf.gen_header_params.value) ctf.gen_header_code.connect(ctf.gen_header.code) ctf.gen_header.result_name.cell().set("result") ctf.gen_header.input_name.cell().set("input") ctf.compiler_code = libcell(".compiler.code") ctf.compiler_params = libcell(".compiler_params") ctf.compiler = transformer(ctf.compiler_params.value) ctf.compiler_code.connect(ctf.compiler.code) ctf.translator_code = libcell(".translator.code") ctf.translator_params = libcell(".translator_params") ctf.translator = transformer(ctf.translator_params.value) ctf.translator_code.connect(ctf.translator.code) ctf.translator.result_name.cell().set("result") ctf.translator.input_name.cell().set("input")
def translate_py_transformer(node, root, namespace, inchannels, outchannels, lib_path00, is_lib): #TODO: simple translation, without a structured cell assert not "code" in node ### node["code"] is an outdated attribute inchannels = [ic for ic in inchannels if ic[0] != "code"] parent = get_path(root, node["path"][:-1], None, None) name = node["path"][-1] lib_path0 = lib_path00 + "." + name if lib_path00 is not None else None ctx = context(toplevel=False) setattr(parent, name, ctx) result_name = node["RESULT"] if node["language"] == "ipython": assert result_name == "result" input_name = node["INPUT"] for c in inchannels: assert (not len(c)) or c[0] != result_name #should have been checked by highlevel with_result = node["with_result"] buffered = node["buffered"] interchannels = [as_tuple(pin) for pin in node["pins"]] plain = node["plain"] mount = node.get("mount", {}) silk = (buffered or not plain) inp, inp_ctx = build_structured_cell( ctx, input_name, silk, plain, buffered, inchannels, interchannels, lib_path0, return_context=True ) setattr(ctx, input_name, inp) if "input_schema" in mount: inp_ctx.schema.mount(**mount["input_schema"]) for inchannel in inchannels: path = node["path"] + inchannel namespace[path, True] = inp.inchannels[inchannel], node assert result_name not in node["pins"] #should have been checked by highlevel all_pins = {} for pinname, pin in node["pins"].items(): p = {"io": "input"} p.update(pin) all_pins[pinname] = p all_pins[result_name] = {"io": "output", "transfer_mode": "copy"} if node["SCHEMA"]: assert with_result all_pins[node["SCHEMA"]] = { "io": "input", "transfer_mode": "json", "access_mode": "json", "content_type": "json" } ctx.tf = transformer(all_pins) if node["debug"]: ctx.tf.debug = True if lib_path00 is not None: lib_path = lib_path00 + "." + name + ".code" ctx.code = libcell(lib_path) else: if node["language"] == "ipython": ctx.code = core_cell("ipython") else: ctx.code = core_cell("transformer") if "code" in mount: ctx.code.mount(**mount["code"]) ctx.code._sovereign = True ctx.code.connect(ctx.tf.code) checksum = node.get("checksum", {}) if "code" in checksum: ctx.code.set_checksum(checksum["code"]) if "input" in checksum: inp.set_checksum(checksum["input"]) namespace[node["path"] + ("code",), True] = ctx.code, node namespace[node["path"] + ("code",), False] = ctx.code, node for pin in list(node["pins"].keys()): target = getattr(ctx.tf, pin) inp.outchannels[(pin,)].connect(target) if with_result: plain_result = node["plain_result"] result, result_ctx = build_structured_cell( ctx, result_name, True, plain_result, False, [()], outchannels, lib_path0, return_context=True ) if "result_schema" in mount: result_ctx.schema.mount(**mount["result_schema"]) setattr(ctx, result_name, result) result_pin = getattr(ctx.tf, result_name) result_pin.connect(result.inchannels[()]) if node["SCHEMA"]: schema_pin = getattr(ctx.tf, node["SCHEMA"]) result.schema.connect(schema_pin) if "result" in checksum: result.set_checksum(checksum["result"]) if "schema" in checksum: result.schema.set_checksum(checksum["schema"]) else: for c in outchannels: assert len(c) == 0 #should have been checked by highlevel result = getattr(ctx.tf, result_name) namespace[node["path"] + (result_name,), False] = result, node namespace[node["path"], True] = inp, node namespace[node["path"], False] = result, node
def translate_bash_transformer(node, root, namespace, inchannels, outchannels, *, has_meta_connection): from .translate import set_structured_cell_from_checksum from ..highlevel.Environment import Environment from ..core.environment import (validate_capabilities, validate_conda_environment, validate_docker) env0 = Environment(None) env0._load(node.get("environment")) env = env0._to_lowlevel() is_docker_transformer = False if env is not None and env.get("docker") is not None: ok1 = validate_capabilities(env)[0] ok2 = validate_conda_environment(env)[0] ok3 = validate_docker(env)[0] if not (ok1 or ok2 or ok3): is_docker_transformer = True if is_docker_transformer: from .translate_bashdocker_transformer import translate_bashdocker_transformer docker = env.pop("docker") docker_image = docker["name"] docker_options = docker["options"] # TODO: pass on version and checksum as well? if "powers" not in env: env["powers"] = [] env["powers"].append("docker") return translate_bashdocker_transformer( node, root, namespace, inchannels, outchannels, has_meta_connection=has_meta_connection, env=env, docker_image=docker_image, docker_options=docker_options) inchannels = [ic for ic in inchannels if ic[0] != "code"] parent = get_path(root, node["path"][:-1], None, None) name = node["path"][-1] ctx = context(toplevel=False) setattr(parent, name, ctx) result_name = node["RESULT"] input_name = node["INPUT"] result_cell_name = result_name + "_CELL" forbidden = [result_name, result_cell_name, "bashcode", "pins_"] pin_intermediate = {} for pin in node["pins"].keys(): pin_intermediate[pin] = input_name + "_PIN_" + pin forbidden.append(pin_intermediate[pin]) for c in inchannels: assert ( not len(c) ) or c[0] not in forbidden #should have been checked by highlevel pins = node["pins"].copy() pins["bashcode"] = {"celltype": "text"} pins["pins_"] = {"celltype": "plain"} ctx.pins = cell("plain").set(list(pins.keys())) interchannels = [as_tuple(pin) for pin in pins] mount = node.get("mount", {}) inp, inp_ctx = build_structured_cell( ctx, input_name, inchannels, interchannels, fingertip_no_remote=node.get("fingertip_no_remote", False), fingertip_no_recompute=node.get("fingertip_no_recompute", False), hash_pattern=node.get("hash_pattern"), return_context=True) setattr(ctx, input_name, inp) namespace[node["path"] + ("SCHEMA", ), "source"] = inp.schema, node if "input_schema" in mount: inp_ctx.schema.mount(**mount["input_schema"]) for inchannel in inchannels: path = node["path"] + inchannel namespace[path, "target"] = inp.inchannels[inchannel], node assert result_name not in pins #should have been checked by highlevel all_pins = {} for pinname, pin in pins.items(): p = {"io": "input"} p.update(pin) all_pins[pinname] = p all_pins[result_name] = {"io": "output"} if node["SCHEMA"]: raise NotImplementedError all_pins[node["SCHEMA"]] = { "io": "input", "transfer_mode": "json", "access_mode": "json", "content_type": "json" } ctx.tf = transformer(all_pins) if node["debug"]: ctx.tf.debug = True ctx.code = cell("text") if "code" in mount: ctx.code.mount(**mount["code"]) ctx.pins.connect(ctx.tf.pins_) ctx.code.connect(ctx.tf.bashcode) checksum = node.get("checksum", {}) if "code" in checksum: ctx.code._set_checksum(checksum["code"], initial=True) inp_checksum = convert_checksum_dict(checksum, "input") set_structured_cell_from_checksum(inp, inp_checksum) ctx.executor_code = sctx.executor_code.cell() ctx.executor_code.connect(ctx.tf.code) namespace[node["path"] + ("code", ), "target"] = ctx.code, node namespace[node["path"] + ("code", ), "source"] = ctx.code, node for pinname, pin in node["pins"].items(): target = ctx.tf.get_pin(pinname) celltype = pin.get("celltype", "mixed") if celltype == "code": celltype = "text" intermediate_cell = cell(celltype) cell_setattr(node, ctx, pin_intermediate[pinname], intermediate_cell) inp.outchannels[(pinname, )].connect(intermediate_cell) intermediate_cell.connect(target) meta = deepcopy(node.get("meta", {})) meta["transformer_type"] = "bash" ctx.tf.meta = meta if has_meta_connection: ctx.meta = cell("plain") ctx.meta.connect(ctx.tf.META) namespace[node["path"] + ("meta", ), "target"] = ctx.meta, node result, result_ctx = build_structured_cell( ctx, result_name, [()], outchannels, fingertip_no_remote=node.get("fingertip_no_remote", False), fingertip_no_recompute=node.get("fingertip_no_recompute", False), return_context=True) namespace[node["path"] + ("RESULTSCHEMA", ), "source"] = result.schema, node if "result_schema" in mount: result_ctx.schema.mount(**mount["result_schema"]) setattr(ctx, result_name, result) result_pin = ctx.tf.get_pin(result_name) result_cell = cell("mixed") cell_setattr(node, ctx, result_cell_name, result_cell) result_pin.connect(result_cell) result_cell.connect(result.inchannels[()]) if node["SCHEMA"]: schema_pin = ctx.tf.get_pin(node["SCHEMA"]) result.schema.connect(schema_pin) result_checksum = {} for k in checksum: if not k.startswith("result"): continue k2 = "value" if k == "result" else k[len("result_"):] result_checksum[k2] = checksum[k] set_structured_cell_from_checksum(result, result_checksum) if env is not None: ctx.tf.env = env namespace[node["path"], "target"] = inp, node namespace[node["path"], "source"] = result, node
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")
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer, pytransformercell, link import sys code = "raise Exception(a)" if len(sys.argv) == 2 and sys.argv[1] == "1": code = "result = a" with macro_mode_on(): ctx = context(toplevel=True) ctx.cell1 = cell().set(1) ctx.result = cell() ctx.tf = transformer({ "a": "input", }) ctx.cell1.connect(ctx.tf.a) ctx.tf.code.set(code) ctx.equilibrate(5)
def map_dict_nested(ctx, elision, elision_chunksize, graph, inp, keyorder, *, lib_module_dict, lib_codeblock, lib, has_uniform): from seamless.core import cell, macro, context, path, transformer assert len(inp) == len(keyorder) length = len(inp) #print("NEST", length, keyorder[0]) if elision and elision_chunksize > 1 and length > elision_chunksize: merge_subresults = lib_module_dict["helper"]["merge_subresults_dict"] ctx.lib_module_dict = cell("plain").set(lib_module_dict) ctx.lib_codeblock = cell("plain").set(lib_codeblock) ctx.main_code = cell("python").set(lib_module_dict["map_dict"]["main"]) ctx.lib_module = cell("plain").set({ "type": "interpreted", "language": "python", "code": lib_codeblock }) ctx.graph = cell("plain").set(graph) ctx.elision = cell("bool").set(elision) ctx.elision_chunksize = cell("int").set(elision_chunksize) ctx.has_uniform = cell("bool").set(has_uniform) chunk_index = 0 macro_params = { 'elision_': { 'celltype': 'bool' }, 'elision_chunksize': { 'celltype': 'int' }, 'graph': { 'celltype': 'plain' }, "lib_module_dict": { 'celltype': 'plain' }, "lib_codeblock": { 'celltype': 'plain' }, "lib": { 'celltype': 'plain', 'subcelltype': 'module' }, 'inp': { 'celltype': 'plain' }, 'has_uniform': { 'celltype': 'bool' }, 'keyorder': { 'celltype': 'plain' }, } if has_uniform: ctx.uniform = cell("mixed") subresults = {} chunksize = elision_chunksize while chunksize * elision_chunksize < length: chunksize *= elision_chunksize for n in range(0, length, chunksize): chunk_keyorder = keyorder[n:n + chunksize] chunk_inp = {k: inp[k] for k in chunk_keyorder} chunk_index += 1 subresult = cell("checksum") m = macro(macro_params) m.allow_elision = True setattr(ctx, "m{}".format(chunk_index), m) ctx.main_code.connect(m.code) ctx.elision.connect(m.elision_) ctx.elision_chunksize.connect(m.elision_chunksize) ctx.has_uniform.connect(m.has_uniform) ctx.graph.connect(m.graph) ctx.lib_module_dict.connect(m.lib_module_dict) ctx.lib_codeblock.connect(m.lib_codeblock) ctx.lib_module.connect(m.lib) m.inp.cell().set(chunk_inp) m.keyorder.cell().set(chunk_keyorder) subr = "subresult{}".format(chunk_index) setattr(ctx, subr, subresult) subresults[subr] = subresult result_path = path(m.ctx).result result_path.connect(subresult) input_cells = {} if has_uniform: uniform_path = path(m.ctx).uniform ctx.uniform.connect(uniform_path) input_cells = {ctx.uniform: uniform_path} ctx._get_manager().set_elision(macro=m, input_cells=input_cells, output_cells={ subresult: result_path, }) transformer_params = {} for subr in subresults: transformer_params[subr] = {"io": "input", "celltype": "checksum"} transformer_params["result"] = {"io": "output", "celltype": "checksum"} ctx.merge_subresults = transformer(transformer_params) ctx.merge_subresults.code.cell().set(merge_subresults) tf = ctx.merge_subresults for subr, c in subresults.items(): c.connect(getattr(tf, subr)) ctx.all_subresults = cell("plain") tf.result.connect(ctx.all_subresults) # ctx.all_subresults has the correct checksum, but there is no valid conversion # (because it is unsafe). # Use a macro to do it ctx.get_result = macro( {"result_checksum": { "io": "input", "celltype": "checksum" }}) get_result = lib_module_dict["helper"]["get_result_dict"] ctx.get_result.code.cell().set(get_result) ctx.all_subresults.connect(ctx.get_result.result_checksum) p = path(ctx.get_result.ctx).result ctx.result = cell("mixed", hash_pattern={"*": "#"}) p.connect(ctx.result) else: lib.map_dict(ctx, graph, inp, has_uniform, elision) return ctx
from seamless.core import context, cell, transformer import seamless.core.execute seamless.core.execute.DIRECT_PRINT = True with open("cell-ipython.ipy", "w") as f: with open("cell-ipython-ORIGINAL.ipy", "r") as f2: f.write(f2.read()) with macro_mode_on(): ctx = context(toplevel=True) ctx.i = cell().set(100) ctx.result = cell() ctx.tf = transformer({ "i": "input", "testmodule": ("input", "plain", "module"), "result": "output", }) ctx.gen_html = transformer({ "testmodule": ("input", "plain", "module"), "html": "output", }) ctx.i.connect(ctx.tf.i) ctx.code = cell("python").set(""" import time from .testmodule import func, func_html t = time.time() result = func(i) print("Time: %.1f" % (time.time() - t)) """)
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 = StructuredCell("inp", ctx.inp_struc.data, storage=ctx.inp_struc.storage, form=ctx.inp_struc.form, schema=None, buffer=None, inchannels=None, outchannels=[()]) ctx.tf = transformer({ "inp": ("input", "copy", "silk"), "c": "output", }) ctx.inp.connect_outchannel((), ctx.tf.inp) ctx.tf.code.cell().set("c = inp.a * inp.dat + inp.b") ctx.result = cell("array") ctx.tf.c.connect(ctx.result) ctx.equilibrate() print(ctx.tf.status()) print(ctx.result.value) inp = ctx.inp.handle inp["a"] = 10 inp["b"] = 12
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()
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=[("m1", ), ("m2", )], outchannels=[()]) ctx.code = cell("transformer").set("z = x * y") ctx.mixer1 = transformer({ "x": "input", "y": "input", "z": "output", }) ctx.a = cell("json").set(3) ctx.b = cell("json").set(8) ctx.code.connect(ctx.mixer1.code) ctx.a.connect(ctx.mixer1.x) ctx.b.connect(ctx.mixer1.y) ctx.mixer2 = transformer({ "x": "input", "y": "input", "z": "output", }) ctx.c = cell("json").set(2) ctx.d = cell("json").set(12)
def get_meta(tf): ctx = tf._root() tf_cache = ctx._get_manager().cachemanager.transformation_cache tf_checksum = tf_cache.transformer_to_transformations[tf] transformation = tf_cache.transformations[tf_checksum] meta_checksum = transformation["__meta__"] meta = json.loads(buffer_cache.get_buffer(meta_checksum)) return meta ctx = context(toplevel=True) ctx.tf = transformer({ "a": { "io": "input", "celltype": "int", }, "result": { "io": "output", "celltype": "int", }, }) def tf(a): import time time.sleep(a) return a + 42 ctx.tf.code.cell().set(tf) ctx.a = cell("int").set(1) ctx.a.connect(ctx.tf.a)
assert (is_identical_debug(binary_module, binary_module2)) ###################################################################### # 5: test it in a context ###################################################################### 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.module.connect(tf.testmodule) tf.a.cell().set(2) tf.b.cell().set(3) tf.code.cell().set(""" from .testmodule import lib print("ADD", lib.add(a,b)) result = testmodule.lib.add(a,b) """) ctx.result = cell("json") ctx.tf.result.connect(ctx.result) ctx.equilibrate() print(ctx.result.value)
import seamless from seamless.core import macro_mode_on from seamless.core import context, cell, transformer import seamless.core.execute seamless.core.execute.DIRECT_PRINT = True 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", "testmodule": ("input", "plain", "module"), "c": "output", }) ctx.cell1.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = cell("transformer").set(""" print(testmodule) print(testmodule.q) from .testmodule import q print(q) import sys print([m for m in sys.modules if m.find("testmodule") > -1]) c = a + b """) ctx.code.connect(ctx.tf.code) ctx.tf.c.connect(ctx.result)
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("""
ctx = context(toplevel=True) ctx.mount("/tmp/mount-seamless", persistent=None) ctx.progress = cell("transformer").set(progress) tf_params = { "limit": ("input", "int"), "delay": ("input", "float"), "factor": ("input", "float"), "offset": ("input", "float"), "result": ("output", "float"), } add_params = { "a": ("input", "float"), "b": ("input", "float"), "result": ("output", "float"), } ctx.tf1 = transformer(tf_params) ctx.tf2 = transformer(tf_params) ctx.tf3 = transformer(tf_params) ctx.tf4 = transformer(tf_params) ctx.progress.connect(ctx.tf1.code) ctx.progress.connect(ctx.tf2.code) ctx.progress.connect(ctx.tf3.code) ctx.progress.connect(ctx.tf4.code) ctx.tf1.limit.cell().set(9) ctx.tf1.factor.cell().set(1000) ctx.tf1.delay.cell().set(1.5) ctx.tf1.offset.cell().set(0) ctx.tf1_result = ctx.tf1.result.cell()
return self.f2 else: raise IndexError(item) schema = MyClass.schema 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", "silk"), "f_SCHEMA": "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) ctx.f = cell("plain").set({"f1": 10, "f2": 20}) ctx.f.connect(ctx.tf.f) ctx.f_schema = cell("plain").set(schema) ctx.f_schema.connect(ctx.tf.f_SCHEMA) #ctx.mount("/tmp/mount-test")
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 = [("m1",), ("m2",)], outchannels = [()] ) ctx.code = cell("transformer").set("z = x * y") ctx.mixer1 = transformer({ "x": "input", "y": "input", "z": "output", }) ctx.a = cell("json").set(3) ctx.b = cell("json").set(8) ctx.code.connect(ctx.mixer1.code) ctx.a.connect(ctx.mixer1.x) ctx.b.connect(ctx.mixer1.y) ctx.mixer2 = transformer({ "x": "input", "y": "input", "z": "output", }) ctx.c = cell("json").set(2) ctx.d = cell("json").set(12)
}, "link_options" : ["-lm"], "public_header": { "language": "c", "code": "float add(int a, int b);" } } from seamless.core import context, cell, transformer, macro_mode_on with macro_mode_on(): ctx = context(toplevel=True) ctx.testmodule = cell("plain") ctx.testmodule.set(testmodule) tf = ctx.tf = transformer({ "a": ("input", "plain"), "b": ("input", "plain"), "testmodule": ("input", "plain", "module"), "result": ("output", "plain"), }) ctx.testmodule.connect(tf.testmodule) tf.a.cell().set(2) tf.b.cell().set(3) tf.code.cell().set(""" from .testmodule import lib print("ADD", lib.add(a,b)) result = testmodule.lib.add(a,b) """) ctx.result = cell("plain") ctx.tf.result.connect(ctx.result) ctx.compute() print(ctx.result.value)
import seamless from seamless.core import context, cell, transformer, macro, reactor, path from seamless.core import macro_mode_on with macro_mode_on(): ctx = context(toplevel=True) ctx.header = cell("str").set("HEADER+") ctx.code = cell("str").set("CODE") ctx.tf_code = cell("transformer").set("'TRANSFORMER: ' + header + code") ctx.result = cell("str") ctx.tf = transformer({ "header": "input", "code_": { "io": "input", "as": "code", }, "result": "output" }) ctx.header.connect(ctx.tf.header) ctx.code.connect(ctx.tf.code_) ctx.tf_code.connect(ctx.tf.code) ctx.tf.result.connect(ctx.result) ctx.macro = macro({ "header": "str", "code_": { "celltype": "str", "as": "code", }, }) def run(ctx, code, header): ctx.result = cell("str").set("MACRO: " + header + code)
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']") ctx.result = cell("array") ctx.tf.c.connect(ctx.result) ctx.equilibrate() print(ctx.tf.status) print(ctx.result.value) inp = ctx.inp.handle
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)
channel_name, tf_name = channel_names[n], tf_names[n] c = cell("mixed") cell_name = "outchannel_" + tf_name[0] setattr(ctx.params_struc, cell_name, c) ctx.params.outchannels[channel_name].connect(c) stf = getattr(ctx, "stf" + str(n + 1)) c.connect(stf.input.inchannels[()]) stf.result.connect(ctx.result.inchannels[channel_name]) add_params = { "a": ("input", "float"), "b": ("input", "float"), "result": ("output", "float"), } ctx.add = transformer(add_params) ctx.add.code.set("result = a + b") tf1, tf2, tf3, tf4 = tf_names ctx.result.outchannels[tf1].connect(ctx.add.a.cell()) ctx.result.outchannels[tf2].connect(ctx.add.b.cell()) ctx.add.result.cell().connect(ctx.params.inchannels[tf3 + ("offset", )]) ctx.add.result.cell().connect(ctx.params.inchannels[tf4 + ("offset", )]) ctx.compute() print("START") ctx.params.handle.set({ "tf1": {}, "tf2": {}, "tf3": {}, "tf4": {},
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 ctx.ttf = transformer(params)
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, pythoncell 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", "testmodule": ("input", "ref", "module", "python"), "c": "output", }) ctx.cell1.connect(ctx.tf.a) ctx.cell2.connect(ctx.tf.b) ctx.code = pytransformercell().set(""" print("macro execute") print(testmodule) print(testmodule.q) from .testmodule import q print(q) import sys print([m for m in sys.modules if m.find("testmodule") > -1]) c = a + b print("/macro execute")""") ctx.code.connect(ctx.tf.code) ctx.tf.c.connect(ctx.result) ctx.testmodule = pythoncell().set("q = 10")
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")
eparams = { "value": {"io": "edit", "transfer_mode": "copy"}, "title": "input", } teparams = { "value": {"io": "edit", "transfer_mode": "copy"}, "title": "input", } from seamless.core import macro_mode_on from seamless.core import context, transformer, reactor, cell ctx = context(toplevel=True) cont = ctx.cont = transformer(tparams) c_data = cont.value.cell() c_data.set(4) c_code = cont.code.cell() c_output = cont.outp.cell() c_code.set("outp = value*2") c_output2 = ctx.c_output2 = cell("plain") cont2 = ctx.cont2 = reactor({ "x": "input", "xcopy": "edit", }) c_output.connect(cont2.x) ###cont2.xcopy.connect(c_output2) c_output2.connect(cont2.xcopy)