Exemple #1
0
def define_ctx():
    with macro_mode_on():
        ctx = context(toplevel=True)
        ctx.cell1 = cell().set(1)
        ctx.cell2 = cell().set(2)
    ctx.compute()
    with macro_mode_on():
        ctx.result = cell()
        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 = cell("transformer").set("c = a + b")
        ctx.code.connect(ctx.tf.code)
        ctx.result_unilink = unilink(ctx.result)
        ctx.tf.c.connect(ctx.result_unilink)
    return ctx
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
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 = [],
        )
Exemple #5
0
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=[],
        )
Exemple #6
0
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")
Exemple #7
0
raise NotImplementedError

import seamless
from seamless.core import macro_mode_on
from seamless.core import context, cell, transformer, StructuredCell
from seamless.core.structured_cell import BufferWrapper, StructuredCellState
import numpy as np

with macro_mode_on():
    ctx = context(toplevel=True)

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
Exemple #8
0
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()
"""
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 = [()]
Exemple #10
0
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")
Exemple #11
0
ctx.code = cell("macro").set(collatz)
macro_params = {
    "value": "int",
    "macro_params": "plain",
    "macro_code": ("python", "macro")
}
ctx.macro_params = cell().set(macro_params)
ctx.compute()
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()
with macro_mode_on(None):
    ctx.macro.ctx.series.connect(ctx.series)
start = 27
###start = 10 #7-level nesting
###start = 12 #10-level nesting
###start = 35 #12-level nesting
###start = 23 #16-level nesting
###start = 27 #111-level nesting
refe = refe_collatz(start)

t = time.time()
ctx.start.set(start)
print("building done: %d ms" % (1000 * (time.time() - t)))
ctx.compute()
print(ctx.series.value)
print(refe)
Exemple #12
0
"""
Final test for compiled transformers, using stdlib.compiled_transformer
Now using debugging
"""

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=[],