Example #1
0
def test_map_list_N_uniform(mylib):
    from seamless.highlevel import Context, Cell
    ctx = Context()
    ctx.include(mylib.map_list_N)

    ctx.add = Context()
    ctx.add.uniform = Cell("mixed")
    ctx.add.inp = Context()
    ctx.add.inp.a = Cell("mixed")
    ctx.add.inp.b = Cell("mixed")

    def add(a, b, c):
        print("ADD", a, b, c)
        return a + b + c

    ctx.add.tf = add
    ctx.add.tf.debug = True
    ctx.add.tf.a = ctx.add.inp.a
    ctx.add.tf.b = ctx.add.inp.b
    ctx.add.tf.c = ctx.add.uniform
    ctx.add.result = ctx.add.tf
    ctx.add.result.celltype = "int"
    ctx.compute()

    ctx.a = [110, 120, 130, 140]
    ctx.a.hash_pattern = {"!": "#"}
    ctx.b = [2, 4, 8, 12]
    ctx.b.hash_pattern = {"!": "#"}
    ctx.c = 7000
    ctx.result = Cell()

    ctx.mapping = ctx.lib.map_list_N(context_graph=ctx.add,
                                     inp={
                                         "a": ctx.a,
                                         "b": ctx.b,
                                     },
                                     uniform=ctx.c,
                                     result=ctx.result,
                                     elision=True,
                                     elision_chunksize=2)
    ctx.compute()
    print(ctx.result.value)
    ctx.c = 8000
    ctx.compute()
    print(ctx.result.value)
from seamless.highlevel import Context, Cell
import traceback

ctx = Context()
ctx.a = Cell("int").set(20)
ctx.b = ctx.a
ctx.b.celltype = "int"
ctx.compute()
print(ctx.b.value)
try:
    ctx.b.mount("/tmp/x")
except Exception:
    traceback.print_exc()
try:
    ctx.b.share(readonly=False)
except Exception:
    traceback.print_exc()
ctx.compute()
Example #3
0
from seamless.highlevel import Context

ctx = Context()
def code():
    import numpy as np
    a = np.arange(20,80).astype(np.int8)
    return {
        "a": a
    }

ctx.tf = code
ctx.result = ctx.tf
ctx.result2 = ctx.result
ctx.result2.celltype = "mixed"
ctx.a = ctx.result.a
ctx.a.celltype = "bytes"
ctx.compute()
print(ctx.tf.status)
print(ctx.tf.exception)
print(ctx.result.value)
print(ctx.result2.value)
print(ctx.a.value)
Example #4
0
import seamless.core.execute

seamless.core.execute.DIRECT_PRINT = True

from seamless.highlevel import Context
from pprint import pprint

ctx = Context()
###ctx.mount("/tmp/mount-test")

ctx.a = 12
ctx.compute()
print(ctx.a.value)
print(ctx.a.schema)  # None


def triple_it(a):
    return 3 * a


def triple_it_b(a, b):
    print("RUN!")
    return 3 * a + b


ctx.transform = triple_it
ctx.transform.hash_pattern = {"*": "#"}
ctx.transform.debug = True
ctx.transform.a = 1
print("START")
ctx.compute()
Example #5
0
from seamless.highlevel import Context, Cell, Macro

ctx = Context()
ctx.a = Cell("int")
ctx.b = Cell("int")
def add(a,b):
    return a+b
ctx.add = add
ctx.add.a = ctx.a
ctx.add.b = ctx.b
ctx.result = ctx.add
ctx.result.celltype = "int"
ctx.compute()
graph = ctx.get_graph(runtime=True)

ctx = Context()
ctx.graph = Cell("plain").set(graph)
m = ctx.m = Macro()
ctx.par_static = 100
ctx.par_dynamic = 20
m.par_static = ctx.par_static
m.graph = ctx.graph
m.pins.par_dynamic = {"io": "input", "celltype": "int"}
m.pins.graph_result = {"io": "output", "celltype": "int"}
def run_macro(ctx, par_static, graph):
    print("RUN MACRO", par_static)
    ctx.subctx = HighLevelContext(graph)
    ctx.subctx.a.set(par_static)
    ctx.par_dynamic = cell("int")
    ctx.par_dynamic.connect(ctx.subctx.b)
Example #6
0
Fortunately, this is easy to spoof for Python.
Internally, ptvsd uses code from PyDev to debug the script. It relies on
inspect.currentframe().f_code.co_filename to find the filename
(although mappings can be defined).
This corresponds *exactly* to the identifier of seamless.core.cached_compile.
TODO: Therefore, a "virtual filename" attribute will be supported, which will be passed
 to cached_compile. It is your responsibility that this corresponds to a real file.
TODO: interpreted modules will also have a file path in their tree. You can map
 to their write-only, just as for binary modules, to facilitate visual debugging.
File names and paths must be stripped from checksum calculations.

"""
from seamless.highlevel import Context

ctx = Context()
ctx.a = 12


def triple_it(a):
    import sys, pdb
    class ForkedPdb(pdb.Pdb):
        """A Pdb subclass that may be used
        from a forked multiprocessing child

        """
        def interaction(self, *args, **kwargs):
            _stdin = sys.stdin
            try:
                sys.stdin = open('/dev/stdin')
                super().interaction(*args, **kwargs)
            finally:
Example #7
0
from seamless.highlevel import Context
ctx = Context()
ctx.a = {"x": 123}
z = ctx.get_zip()
print("ZIP size:", len(z))
ctx.b = ctx.a.x
ctx.compute()
print(ctx.b.value, ctx.b.checksum)
z = ctx.get_zip()
print("ZIP size:", len(z))
ctx.b.scratch = True
ctx.compute()
print(ctx.b.value, ctx.b.checksum)
z = ctx.get_zip()
print("ZIP size (should be 448):", len(z))
Example #8
0
# Adapted from /seamless/stdlib/switch-join/switch-join.py
from seamless.highlevel import Context, Cell
from seamless import stdlib

ctx = Context()
ctx.include(stdlib.switch)
ctx.include(stdlib.join)
ctx.a = 10.0
ctx.a1 = Cell("float")
ctx.a2 = Cell("float")
ctx.a3 = Cell("float")
ctx.f1 = 2.0
ctx.f2 = 3.0
ctx.f3 = 4.0


def add(a, b):
    return a + b


def sub(a, b):
    return a - b


def mul(a, b):
    return a * b


ctx.op1 = add
ctx.op1.a = ctx.a1
ctx.op1.b = ctx.f1
Example #9
0
def join(input, which):
    return input.get(which)


def func1(v):
    return v + 42


def func2(v):
    return -2 * v


ctx = Context()
ctx.join = join
ctx.a = 1000
ctx.which = "a"
ctx.switch = switch
ctx.switch.input = ctx.a
ctx.switch.which = ctx.which
ctx.switched = ctx.switch
ctx.func1 = func1
ctx.switched_a = ctx.switched.a
ctx.func1.v = ctx.switched_a
ctx.func2 = func2
ctx.switched_b = ctx.switched.b
ctx.func2.v = ctx.switched_b
ctx.tojoin = {}
ctx.func1_result = ctx.func1
ctx.func2_result = ctx.func2
ctx.tojoin.a = ctx.func1_result
Example #10
0
ctx.compute()
print(ctx.tf.status)
if ctx.tf.status != "Status: OK":
    print(ctx.tf.exception)
print(ctx.result.value)

ctx.tf.a = {"x": 100.1, "y": 200.1, "z": 300.1}
ctx.tf.celltypes = {"a": "int"}
ctx.b = -1000
ctx.compute()
print(ctx.tf.status)
if ctx.tf.status != "Status: OK":
    print(ctx.tf.exception)
print(ctx.result.value)

ctx.a = {"p": 1000, "q": 2000, "r": 3000}
ctx.tf.a = ctx.a
ctx.b = 1
ctx.compute()
print(ctx.tf.status)
if ctx.tf.status != "Status: OK":
    print(ctx.tf.exception)
    #print(ctx.tf.ctx.m.exception)
    #print(ctx.tf.ctx.m.ctx.subctx_p.tf.exception)
print(ctx.result.value)

ctx.a = {"pp": 100, "qq": 200, "rr": 300}
import asyncio
asyncio.get_event_loop().run_until_complete(
    asyncio.sleep(2))  # no re-translation
print(ctx.tf.status)
Example #11
0
seamless.database_cache.connect()
seamless.set_ncores(2)
seamless.set_parallel_evaluations(5)
"""
import logging
logging.basicConfig()
logging.getLogger("seamless").setLevel(logging.DEBUG)
"""

from seamless.highlevel import Context, Cell, Macro

sctx = Context()
sctx.inp = Cell("mixed")
sctx.inp2 = Cell()
sctx.inp2 = sctx.inp
sctx.a = Cell("str")
sctx.b = Cell("str")
sctx.a = sctx.inp2.a
sctx.b = sctx.inp2.b


def add(a, b):
    print("ADD", a[:10], b[:10])
    return a + b


sctx.add = add
sctx.add.a = sctx.a
sctx.add.b = sctx.b
sctx.result = sctx.add
sctx.result.celltype = "str"
Example #12
0
from seamless.highlevel import Context, Cell, Macro

sctx = Context()
sctx.inp = Cell("mixed")
sctx.inp2 = Cell()
sctx.inp2 = sctx.inp
sctx.a = Cell("int")
sctx.b = Cell("int")
sctx.a = sctx.inp2.a
sctx.b = sctx.inp2.b
def add(a,b):
    return a+b
sctx.add = add
sctx.add.a = sctx.a
sctx.add.b = sctx.b
sctx.result = sctx.add
sctx.result.celltype = "int"
sctx.compute()

data = [
    {
        "a": 5,
        "b": 6,
    },
    {
        "a": -2,
        "b": 8,
    },
    {
        "a": 3,
        "b": 14,
Example #13
0
from seamless.highlevel.library import LibraryContainer
mylib = LibraryContainer("mylib")
mylib.switch = ctx_switch
mylib.switch.constructor = ctx_switch.constructor_code.value
mylib.switch.params = ctx_switch.constructor_params.value
mylib.join = ctx_join
mylib.join.constructor = ctx_join.constructor_code.value
mylib.join.params = ctx_join.constructor_params.value

# 4: Run test example

ctx2 = Context()
ctx2.include(mylib.switch)
ctx2.include(mylib.join)
ctx2.a = 10.0
ctx2.a1 = Cell("float")
ctx2.a2 = Cell("float")
ctx2.a3 = Cell("float")
ctx2.f1 = 2.0
ctx2.f2 = 3.0
ctx2.f3 = 4.0

def add(a,b):
    return a + b
def sub(a,b):
    return a - b
def mul(a,b):
    return a * b

ctx2.op1 = add
Example #14
0
from seamless.highlevel import Context, Cell
ctx = Context()

ctx.a_first = Cell("int").set(5)
ctx.a_first.share(readonly=False)
ctx.a_step = Cell("int").set(3)
ctx.a_step.share(readonly=False)
ctx.a_length = Cell("int").set(5)
ctx.a_length.share(readonly=False)

ctx.a = Cell()
ctx.a.first = ctx.a_first
ctx.a.step = ctx.a_step
ctx.a.length = ctx.a_length

def validate(self):
    assert self.first > 0
    assert self.step > 0
    assert self.length > 0

ctx.schema = Cell("plain")
ctx.link(ctx.a.schema, ctx.schema)
ctx.a.example.first = 0
ctx.a.example.step = 0
ctx.a.example.length = 0
ctx.a.add_validator(validate, "validate")


ctx.b_first = Cell("int").set(8)
ctx.b_first.share(readonly=False)
ctx.b_step = Cell("int").set(1)
Example #15
0
from seamless.highlevel import Context, Macro
ctx = Context()
m = ctx.m = Macro()
m.pins.a = {"io": "input", "celltype": "int"}
m.pins.c = {"io": "output", "celltype": "int"}
ctx.a = 10
m.a = ctx.a
m.b = 20


def run_macro(ctx, b):
    print("RUN MACRO", b)
    pins = {
        "a": "input",
        "b": "input",
        "c": "output",
    }
    ctx.tf = transformer(pins)
    ctx.a = cell("int")
    ctx.a.connect(ctx.tf.a)
    ctx.tf.b.cell().set(b)
    ctx.tf.code.cell().set("c = a * b")
    ctx.c = cell("int")
    ctx.tf.c.connect(ctx.c)
    return


m.code = run_macro
ctx.result = ctx.m.c
ctx.compute()
print(ctx.result.value)
Example #16
0
from seamless.highlevel import Context, Transformer

ctx = Context()

ctx.a = 12

ipycode = """
%%timeit
def triple_it(a):
    return 3 * a
"""
ctx.transform = Transformer()
ctx.transform.language = "ipython"
ctx.transform.code = ipycode
ctx.transform.a = ctx.a
ctx.myresult = ctx.transform
ctx.equilibrate()
print(ctx.myresult.value)

ctx.a = 13
ctx.equilibrate()
print(ctx.myresult.value)
Example #17
0
from seamless.highlevel import Context, Cell
from pprint import pprint

ctx = Context()
###ctx.mount("/tmp/mount-test")

ctx.a = 0
ctx.translate()
ctx.a = 2
ctx.compute()
ctx.get_graph()
print(ctx.a.schema)
print(ctx.a.value)
print(ctx.a.exception)

ctx.a = 1
ctx.a.example = 0
ctx.compute()
print(ctx.a.schema)
print(ctx.a.value)

ctx.a = Cell()
ctx.compute()
print(ctx.a.schema)
print(ctx.a.value)

ctx.a.example = 50
print(ctx.a.value)
print(ctx.a.schema)
ctx.a.set(12)
Example #18
0
from seamless.highlevel import Context
ctx = Context()
ctx.a = {}
ctx.a.b = {}
ctx.a.b.c = {}
ctx.a.b.c.d = 10
print(ctx.a.value)
def report(a,**args):
    print("report", a, args)
ctx.report = report
ctx.report.a = ctx.a
ctx.report.b = ctx.a.b
ctx.report.c = ctx.a.b.c
ctx.report.d = ctx.a.b.c.d
ctx.equilibrate()
ctx.a.schema["b"].pop("c")
ctx.a.b.c = None
ctx.equilibrate()
print(ctx.report.status())
ctx.a.b.c = {"d": 10, "dd": 20}
ctx.equilibrate()
Example #19
0
from seamless.highlevel import Context, Cell
ctx = Context()
ctx.a = Cell("int").set(10)
ctx.c = Cell("int").set(30)
ctx.s = Cell()
ctx.translate()
ctx.s.a = ctx.a
ctx.s.c = ctx.c
ctx.ss = ctx.s
ctx.ss.celltype = "plain"
ctx.compute()
print(ctx.s.value)
print(ctx.ss.value)
ctx.s.set("NOT TO BE PRINTED")
ctx.compute()
print(ctx.s.value)
print(ctx.ss.value)
print(ctx.s.exception)
print("")
ctx.s = "NOT TO BE PRINTED 2"
ctx.s.a = ctx.a
ctx.s.c = ctx.c
ctx.compute()
print(ctx.s.value)
print(ctx.ss.value)
print(ctx.s.exception)
print("")
ctx.s.set({})
ctx.compute()
print(ctx.s.value)
print(ctx.ss.value)
Example #20
0
import seamless.core.execute
seamless.core.execute.DIRECT_PRINT = True

from seamless.highlevel import Context
ctx = Context()
ctx.a = {}
ctx.translate()
ctx.a.b = {}
ctx.a.b.c = {}
ctx.a.b.c.d = 10
ctx.compute()
print(ctx.a.value)
def report(a,**args):
    print("report", a, args)
ctx.report = report
ctx.report.a = ctx.a
ctx.report.b = ctx.a.b
ctx.report.c = ctx.a.b.c
ctx.report.d = ctx.a.b.c.d
ctx.compute()
print()
ctx.a.example = ctx.a.value
ctx.compute()
ctx.a.example.b = ctx.a.b.value
ctx.compute()
ctx.a.example.b.c = ctx.a.b.c.value
ctx.compute()
ctx.a.example.b.c.d = ctx.a.b.c.d.value
ctx.compute()
print("SCHEMA A", ctx.a.schema)
print("SCHEMA B",ctx.a.schema.properties.b)
Example #21
0
from seamless.highlevel import Context, Cell, Transformer

ctx = Context()
ctx.a = Cell()
ctx.a.celltype = "int"
ctx.compute()
ctx.a.set(1)
ctx.compute()
ctx.a.set("test")
ctx.compute()
print("*" * 80)
print(ctx.a.exception)
print(ctx.a.value)
print("*" * 80)

ctx.a = 12
ctx.compute()
ctx.a.celltype = "str"
ctx.b = ctx.a
ctx.b.celltype = "int"
ctx.compute()
print("*" * 80)
print("a", ctx.a.exception)
print("a", ctx.a.value)
print("*" * 80)
print("b", ctx.b.exception)
print("b", ctx.b.value)
print("*" * 80)

ctx.a = "test2"
ctx.compute()
Example #22
0
# 3: Package the contexts in a library

from seamless.highlevel.library import LibraryContainer
mylib = LibraryContainer("mylib")
mylib.instantiate = ctx
mylib.instantiate.constructor = ctx.constructor_code.value
mylib.instantiate.params = ctx.constructor_params.value

# 4: Run test example

ctx = Context()
ctx.include(mylib.instantiate)
ctx.a = Cell().set({
    "instance1": 3,
    "instance2": 5,
    "instance3": 7,
    "instance5": 9,
})
ctx.b = Cell().set({
    "instance1": 8,
    "instance2": 6,
    "instance3": 4,
    "instance5": 2,
})
ctx.result = Cell()
ctx.result2 = Cell()


def mul(fa, fb):
    return fa * fb
Example #23
0
import os, tempfile
from seamless.highlevel import Context, Cell

ctx = Context()

ctx.a = 10
ctx.a.celltype = "plain"

ctx.b = 30
ctx.b.celltype = "plain"


def build_transformer():
    del ctx.transform
    ctx.transform = lambda a, b: a + b
    ctx.translate()
    ctx.transform.example.a = 0
    ctx.transform.example.b = 0
    ctx.result = ctx.transform
    ctx.result.celltype = "plain"

    ctx.transform.a = ctx.a
    ctx.transform.b = ctx.b

    ctx.transform.language = "cpp"
    ctx.transform.main_module.compiler_verbose = False
    ctx.code = ctx.transform.code.pull()
    ctx.code = """
    extern "C" double add(int a, int b);
    extern "C" int transform(int a, int b, double *result) {
        *result = add(a,b);
Example #24
0
from seamless.highlevel import Context
ctx = Context()
ctx.a = {"test": 1, "test2": 2}
ctx.a.hash_pattern = {"*": "#"}
print(ctx.a._get_hcell())
ctx.compute()
print(ctx.a._get_cell().data)
print(ctx.a._get_cell().value)
print(ctx.a._get_cell().hash_pattern)
print(ctx.a.data)
print(ctx.a.value)
print(ctx.a.value.unsilk)
print(ctx.a.value.test)

print()
ctx.a.set({"test3": 10, "test4": 11})
ctx.compute()
print(ctx.a.data)
print(ctx.a.value)
print(ctx.a.value.unsilk)
print(ctx.a.value.test4)
Example #25
0
from seamless.highlevel import Context

# 0
ctx = Context()
ctx.mount("/tmp/mount-test")

# 1
ctx.a = 10
print(ctx.a.value)

# 1a
ctx.a = 12
ctx.translate()
print(ctx.a.value)

# 2
def double_it(a):
    return 2 * a

ctx.transform = double_it
ctx.transform.a = ctx.a
ctx.myresult = ctx.transform
ctx.equilibrate()
print(ctx.myresult.value)

# 3
ctx.a = 12
ctx.equilibrate()
print(ctx.myresult.value)

# 4
import os, tempfile
from seamless.highlevel import Context, Cell

ctx = Context()

ctx.a = 10
ctx.a.celltype = "json"

ctx.b = 30
ctx.b.celltype = "json"

def build_transformer():
    ctx.transform = lambda a,b: a + b
    ctx.transform.example.a = 0
    ctx.transform.example.b = 0
    ctx.result = ctx.transform
    ctx.result.celltype = "json"

    ctx.transform.a = ctx.a
    ctx.transform.b = ctx.b

    ctx.transform.language = "cpp"
    ctx.transform.main_module.compiler_verbose = False
    ctx.code >> ctx.transform.code
    ctx.code = """
    extern "C" double add(int a, int b);
    extern "C" double transform(int a, int b) {
        return add(a,b);
    }"""
    ctx.transform.result.example = 0.0 #example, just to fill the schema
Example #27
0
from seamless.highlevel import Context

ctx = Context()
ctx.a = 12


def triple_it(a):
    import sys, pdb

    class ForkedPdb(pdb.Pdb):
        """A Pdb subclass that may be used
        from a forked multiprocessing child

        """
        def interaction(self, *args, **kwargs):
            _stdin = sys.stdin
            try:
                sys.stdin = open('/dev/stdin')
                super().interaction(*args, **kwargs)
            finally:
                sys.stdin = _stdin

    #from pdb_clone.pdb import set_trace
    #from pdb import set_trace
    #from ipdb import set_trace
    #set_trace = ForkedPdb().set_trace
    from seamless.pdb import set_trace
    set_trace()
    return 3 * a

Example #28
0
import sys
print([m for m in sys.modules if m.find("testmodule") > -1])
result = a + b
'''

from seamless.highlevel import Transformer, Cell, Context, Module
ctx = Context()
ctx.testmodule = Module()
ctx.testmodule.code = "q = 10"

ctx.compute()
print(ctx.testmodule.type)
print(ctx.testmodule.language)
print(ctx.testmodule.code)

ctx.a = Cell("text").set("a=42")
ctx.testmodule = ctx.a
ctx.compute()
print(ctx.testmodule.code)
ctx.a = "a=43"
ctx.compute()
print(ctx.testmodule.code)

ctx.testmodule.code = "q = 9"
ctx.compute()
print(ctx.testmodule.code)

ctx.testmodule.set("q = 12")
ctx.compute()
print(ctx.testmodule.code)