def make_cmd_std(cmd_params): nonlocal filehashes tf_params = { "PARAMS": { "pin": "input", "dtype": "json", }, } in_connections = [] out_connections = [] for file_ in cmd_params["files"]: fh = filehash(file_) filehashes += 1 fhname = "filehash_%d" % filehashes tf_params[fhname] = {"pin": "input", "dtype": "str"} fhname = "filehash_%d" % filehashes setattr(ctx, fhname, fh) in_connections.append((fh.filehash.cell(), fhname)) for inp, typ in cmd_params["inputs"].items(): if typ == "doc": dtype = "text" prefix = "doc_" elif typ == "variable": dtype = "str" prefix = "var_" else: raise TypeError((cmd_params["lineno"], cmd_params["source"], inp, typ)) #must be a bug tf_params[inp] = { "pin": "input", "dtype": dtype, } in_connections.append((ctx.CHILDREN[prefix + inp], inp)) if len(cmd_params["outputs"]) > 1: raise NotImplementedError("Multiple outputs not yet implemented") for output in cmd_params["outputs"]: if hasattr(ctx, "ctx_" + output): dtype = "json" prefix = "ctx_" else: dtype = "text" prefix = "doc_" tf_params[output] = { "pin": "output", "dtype": dtype, } #TODO: in case of multiple outputs => single JSON cell + subcells (to be implemented) out_connections.append((output, ctx.CHILDREN[prefix + output])) tf = transformer(tf_params) tf.PARAMS.cell().set(cmd_params) for con in in_connections: pin = getattr(tf, con[1]) con[0].connect(pin) for con in out_connections: pin = getattr(tf, con[0]) pin.connect(con[1]) ctx.cell_cmd_std.connect(tf.code) return tf
def add_all(inputs): from seamless import transformer pattern = "inp" params = { "outp": {"pin": "output", "dtype": "int"} } code = "return " for n in range(inputs): p = pattern + str(n+1) params[p] = {"pin": "input", "dtype": "int"} code += p + "+ " code = code[:-2] t = transformer(params) t.code.cell().set(code) return t
def construct_silk_model(ctx, mode): from seamless import transformer params = {"value": {"pin": "output", "dtype": "text"}} if mode == "array": params["N"] = {"pin": "input", "dtype": "int"} code = """s = SilkModel() return str(SilkModelArray([s for n in range(N)])) """ else: code = "return str(SilkModel())" ctx.transf = transformer(params) ctx.transf.code.cell().set(code) ctx.registrar.silk.connect("SilkModel", ctx.transf) if mode == "array": ctx.registrar.silk.connect("SilkModelArray", ctx.transf) ctx.export(ctx.transf)
def construct_silk_model(ctx, mode): from seamless import transformer params = {"value": {"pin": "output", "dtype": "text"}} if mode == "array": params["N"] = {"pin": "input", "dtype": "int"} code = """s = SilkModel() return str(SilkModelArray([s for n in range(N)])) """ else: code = "return str(SilkModel())" ctx.transf = transformer(params) ctx.transf.code.cell().set(code) ctx.registrar.silk.connect("SilkModel", ctx.transf) if mode == "array": ctx.registrar.silk.connect("SilkModelArray", ctx.transf) ctx.export(ctx.transf)
def operator(ctx, formula): from seamless import cell, transformer tparams = ctx.tparams = cell("object").set({ "value": { "pin": "input", "dtype": "int" }, "output": { "pin": "output", "dtype": "int" } }) cont = ctx.cont = transformer(tparams) c_code = cont.code.cell() c_code.set(formula) ctx.export(cont)
def operator(ctx, formula ): from seamless import cell, transformer tparams = ctx.tparams = cell("object").set( { "value": { "pin": "input", "dtype": "int" }, "output": { "pin": "output", "dtype": "int" } } ) cont = ctx.cont = transformer(tparams) c_code = cont.code.cell() c_code.set(formula) ctx.export(cont)
ctx.frag_shader = cell(("text", "code", "fragmentshader")) ctx.link_vert_shader = link(ctx.vert_shader, ".", "vert_shader.glsl", file_dominant=file_dominant) ctx.link_frag_shader = link(ctx.frag_shader, ".", "frag_shader.glsl", file_dominant=file_dominant) # Program template ctx.program_template = cell("cson") ctx.link_program_template = link(ctx.program_template, ".", "program_template.cson", file_dominant=file_dominant) # Program and program generator ctx.program = cell("json") ctx.program.resource.save_policy = 4 #always save the program ctx.gen_program = transformer({"program_template": {"pin": "input", "dtype": "json"}, "program": {"pin": "output", "dtype": "json"}}) ctx.registrar.silk.connect("VertexData", ctx.gen_program) ctx.link_gen_program = link(ctx.gen_program.code.cell(), ".", "cell-gen-program.py", file_dominant=file_dominant) ctx.program_template.connect(ctx.gen_program.program_template) ctx.gen_program.program.connect(ctx.program) #GL program ctx.equilibrate() #ctx.program has to be generated first p = ctx.glprogram = glprogram(ctx.program, window_title="Seamless fireworks demo") ctx.frag_shader.connect(p.fragment_shader) ctx.vert_shader.connect(p.vertex_shader) # Vertexdata generator ctx.N = cell("int").set(10000)
import seamless from seamless import context, cell, transformer from seamless.lib.filelink import link ctx = context() ctx.value = cell("int") ctx.result = cell("int") #ctx.result.resource.save_policy = 4 #always save value ctx.result.resource.save_policy = 2 #always save hash ctx.tf = transformer({ "value": { "pin": "input", "dtype": "int" }, "result": { "pin": "output", "dtype": "int" }, }) ctx.tf.code.cell().set("""print("evaluate!"); return value""") ctx.value.connect(ctx.tf.value) ctx.tf.result.connect(ctx.result) ctx.value.set(42) ctx.link_value = link(ctx.value, ".", "hashcache-value.txt") ctx.link_result = link(ctx.result, ".", "hashcache-result.txt", file_dominant=True) ctx.equilibrate() ctx.tofile("test-hashcache.seamless", backup=False) ctx = seamless.fromfile("test-hashcache.seamless") ctx.equilibrate()
from seamless import cell, context, transformer, time ctx = context() typ = ("json", "seamless", "transformer_params") #typ = "json" c = ctx.c = cell(typ).set({"test":10}) t = ctx.t = transformer({ "input":{"pin": "input", "dtype": typ}, "value":{"pin": "output", "dtype": "int"}, } ) t.code.cell().set("""return input['test']""") c.connect(t.input) x = t.value.cell() ctx.equilibrate() print('X',x.data)
"input":{"pin": "input", "dtype": "array"}, "scale":{"pin": "input", "dtype": "float"}, "output":{"pin": "output", "dtype": "array"} } ctx.subdivisions = cell("int").set(3) ctx.minimizations = cell("int").set(20) ctx.scale = cell("float").set(3.5) ctx.coordinates = cell("array").set_store("GL") ctx.normals = cell("array").set_store("GL") ctx.edges = cell("array").set_store("GL") ctx.triangle_indices = cell("array").set_store("GL") ctx.triangle_normals = cell("array").set_store("GL") ctx.triangle_coordinates = cell("array").set_store("GL") ctx.coordinates_prescale = cell("array") ctx.do_scale = transformer(do_scale_params) ctx.scale.connect(ctx.do_scale.scale) ctx.coordinates_prescale.connect(ctx.do_scale.input) ctx.do_scale.output.connect(ctx.coordinates) ctx.do_scale.code.set("return scale * input") ctx.triangle_coordinates_prescale = cell("array") ctx.do_scale2 = transformer(do_scale_params) ctx.scale.connect(ctx.do_scale2.scale) ctx.triangle_coordinates_prescale.connect(ctx.do_scale2.input) ctx.do_scale2.output.connect(ctx.triangle_coordinates) ctx.do_scale2.code.set("return scale * input") ctx.subdivisions.connect(ctx.gen_sphere.subdivisions) ctx.minimizations.connect(ctx.gen_sphere.minimizations) ctx.gen_sphere.coordinates.connect(ctx.coordinates_prescale)
grep 'Energy' !dockstruc0 | awk '-v' 'm=20' '{v=$3; if (v>m)v=m; print v}' | sort -n > energies python ./scripts/count-structures.py !dockstruc > nstrucdone $ATTRACTDIR/collect !dockstruc !pdbA !pdbB > pdb python ./scripts/euler2rotmat.py !dockstruc > poses cat !poses > result @export pdb @export energies @export result @export nstrucdone @export poses """) ctx.select_pose = transformer({ "poses": {"pin": "input", "dtype": "json"}, "struc": {"pin": "input", "dtype": "int"}, "selected_pose": {"pin": "output", "dtype": "json"} }) export(ctx.select_pose.selected_pose) export(ctx.slash.poses) ctx.poses.connect(ctx.select_pose.poses) ctx.select_pose.code.cell().set("return poses[struc-1][1]") export(ctx.select_pose.struc).set(1) edit(ctx.struc, "Selected structure") v = ctx.view_complex = ngl(["receptor", "ligand"]) v.data_receptor.cell().fromfile("unbound/1AVXA.pdb") v.data_ligand.cell().fromfile("unbound/1AVXB.pdb") ctx.representations.connect(v.representations) h = ctx.view_complex_html = export(v.html) link(h, ".", "view-complex.html")
tparams = { "value": { "pin": "input", "dtype": "int" }, "output": { "pin": "output", "dtype": "int" } } from seamless import context, transformer ctx = context() cont = ctx.cont = transformer(tparams) c_data = cont.value.cell() c_data.set(4) c_code = cont.code.cell() c_output = cont.output.cell() c_code.set("return value*2") ctx.equilibrate() print(c_data.data, "'" + c_code.data + "'", c_output.data) c_data.set(5) c_code.set("return value*3") cont2 = ctx.cont2 = transformer(tparams) c_code.connect(cont2.code) c_data.connect(cont2.value)
teparams2 = { "value": { "pin": "edit", "dtype": ("text", "code", "python") }, "title": { "pin": "input", "dtype": "str" }, } from seamless import context, transformer, reactor ctx = context() 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("return value*2") ctx.equilibrate() print("VALUE", c_data.data, "'" + c_code.data + "'", c_output.data) c_data.set(5) c_code.set("return value*3") editor_pycell = os.path.join( os.path.dirname(__file__), "test-editor_pycell.py" )
### ctx.texture = cell("array") ctx.texture.set_store("GLTex", 2) ctx.display_numpy.title.set("Texture") ctx.texture.connect(ctx.display_numpy.array) import numpy as np print("START") ctx.equilibrate() ctx.gen_texture = transformer({ "filename": {"pin": "input", "dtype": "str"}, "inscription": {"pin": "input", "dtype": "str"}, "texture": {"pin": "output", "dtype": "array"} }) ctx.gen_texture.texture.connect(ctx.texture) link(ctx.gen_texture.code.cell(), ".", "cell-gen-texture.py") ctx.equilibrate() ctx.normal_map = cell("array").set_store("GLTex", 2) ctx.gen_normal_map = transformer({ "normal_texture_filename": {"pin": "input", "dtype": "str"}, "inscription": {"pin": "input", "dtype": "str"}, "texture": {"pin": "input", "dtype": "array"}, "normal_map": {"pin": "output", "dtype": "array"} }) ctx.inscription = cell("text").set("")
display(ctx.irmsds, "i-RMSD") export(ctx.slash.clusters) display(ctx.clusters, "clusters") ctx.colors = cell("text").fromfile("colors.txt") link(ctx.colors) ctx.gen_data = transformer({ "colors": { "pin": "input", "dtype": "text" }, "irmsds": { "pin": "input", "dtype": "text" }, "energies": { "pin": "input", "dtype": "text" }, "clusters": { "pin": "input", "dtype": "text" }, "data": { "pin": "output", "dtype": "json" }, }) ctx.gen_data.code.cell().fromfile("cell-gen-data.py") link(ctx.gen_data.code.cell()) ctx.colors.connect(ctx.gen_data.colors) ctx.clusters.connect(ctx.gen_data.clusters) ctx.irmsds.connect(ctx.gen_data.irmsds)
""" Careful with this feature: for now, ALL pins react on preliminary cell values, which is definitely NOT what you want for e.g. JSON or python cells In the future, inputpins will have to be specifically declared to support preliminary cell values """ import seamless from seamless import context, cell, transformer ctx = context() ctx.value = cell("int") ctx.value.resource.save_policy = 4 #always save cell value tf = ctx.tf = transformer({ "pulses": {"pin": "input", "dtype": "int"}, "delay": {"pin": "input", "dtype": "float"}, "value": {"pin": "output", "dtype": "int"}, }) tf.value.connect(ctx.value) tf.code.cell().set(""" import time for n in range(pulses): return_preliminary(n) time.sleep(delay) return pulses """) ctx.delay = cell("float").set(0.1) ctx.delay.connect(tf.delay) ctx.pulses = cell("int").set(3) ctx.pulses.connect(tf.pulses) reporter = ctx.reporter = transformer({"value": {"pin": "input", "dtype": "int"}})
from seamless import cell, context, transformer, reactor from seamless import export ctx = context() tf = ctx.tf = transformer({ "inp": { "pin": "input", "dtype": "int" }, "outp": { "pin": "output", "dtype": "int" }, }) print(tf.outp.status()) print(tf.status()) export(tf.inp) ctx.inp.set(2) try: ctx.inp.set("a") except: pass print(tf.status()) export(tf.outp) print(tf.status()) export(tf.code) print(tf.status()) tf.code.set("return inp + 42") print(tf.status()) tf.inp.set(0) print(tf.status())
cat !poses > result @export pdb @export energies @export result @export nstrucdone @export poses """) ctx.select_pose = transformer({ "poses": { "pin": "input", "dtype": "json" }, "struc": { "pin": "input", "dtype": "int" }, "selected_pose": { "pin": "output", "dtype": "json" } }) export(ctx.select_pose.selected_pose) export(ctx.slash.poses) ctx.poses.connect(ctx.select_pose.poses) ctx.select_pose.code.cell().set("return poses[struc-1][1]") export(ctx.select_pose.struc).set(1) edit(ctx.struc, "Selected structure") v = ctx.view_complex = ngl(["receptor", "ligand"], remote_ngl=True) #False if ngl.js in local dir
import seamless from seamless import context, transformer, cell from seamless.lib.filelink import link ctx = context() ctx.cson = cell("cson") ctx.cson_link = link(ctx.cson, ".", "test-cson.cson") ctx.json = cell("json") ctx.json_link = link(ctx.json, ".", "test-cson.json") tparams = { "inp": {"pin": "input", "dtype": "json"}, "outp": {"pin": "output", "dtype": "json"} } ctx.tf = transformer(tparams) ctx.cson.connect(ctx.tf.inp) ctx.tf.outp.connect(ctx.json) ctx.tf.code.cell().set(""" return inp """) import os ctx.tofile(os.path.splitext(__file__)[0] + ".seamless", backup=False) #if not seamless.ipython: # seamless.mainloop()
import seamless from seamless import context, cell, transformer from seamless.lib.filelink import link ctx = context() ctx.value = cell("int") ctx.result = cell("int") #ctx.result.resource.save_policy = 4 #always save value ctx.result.resource.save_policy = 2 #always save hash ctx.tf = transformer({ "value": { "pin": "input", "dtype": "int" }, "result": { "pin": "output", "dtype": "int" }, }) ctx.tf.code.cell().set("""print("evaluate!"); return value""") ctx.value.connect(ctx.tf.value) ctx.tf.result.connect(ctx.result) ctx.value.set(42) ctx.link_value = link(ctx.value, ".", "hashcache-value.txt") ctx.link_result = link(ctx.result, ".", "hashcache-result.txt", file_dominant=True) ctx.equilibrate() ctx.tofile("test-hashcache.seamless",backup=False) ctx = seamless.fromfile("test-hashcache.seamless") ctx.equilibrate() print(ctx.result.value) print("LOAD 1") ctx.destroy()
from seamless import context, cell, pythoncell, reactor, transformer from seamless.lib.filelink import link from seamless.lib.gui.basic_display import display from seamless.lib.gui.browser import browse from seamless.lib.gui.basic_editor import edit ctx = context() reg = ctx.registrar.ipython ctx.code = cell(("text", "code", "ipython")) link(ctx.code, ".", "cell-test-ipython-registrar.ipy") reg.register(ctx.code) tf = ctx.get_func_html = transformer({ "outp": { "pin": "output", "dtype": ("text", "html") }, }) tf.code.cell().set("return func_html") reg.connect("func_html", tf) browse(tf.outp.cell()) tf = ctx.run_func = transformer({ "i": { "pin": "input", "dtype": "int" }, "outp": { "pin": "output", "dtype": "float" },
ctx.vertex_shader = cell(("text", "code", "vertexshader")).set(vertex_code) ctx.fragment_shader = cell(("text", "code", "fragmentshader")).set(fragment_code) # Program program = { "arrays": [], "uniforms": {}, "render": { "command": "points", "glstate": {}, "attributes": {}, }, } ctx.pre_program = cell("json").set(program) ctx.gen_program = transformer({"program": {"pin": "input", "dtype": "json"}, "result": {"pin": "output", "dtype": "json"}}) ctx.pre_program.connect(ctx.gen_program.program) ctx.gen_program.code.cell().set("return program") ctx.program = ctx.gen_program.result.cell() ctx.equilibrate() p = ctx.glprogram = glprogram(ctx.program) p.uniforms.cell().set({}) ctx.vertex_shader.connect(p.vertex_shader) ctx.fragment_shader.connect(p.fragment_shader) p.painted.cell().connect(p.update) #if this connection is broken, no more crash! """ BUG: ctx.program.touch() will always re-create the window
def __init__(self, a, b, c): self.a = a self.b = b self.c = c def __str__(self): return "MyClass: {0} {1} {2}".format(self.a, self.b, self.c) """) ro = ctx.registrar.python.register(ctx.code) # Repeated registration v = ctx.code.value #ctx.code.destroy() # Should not be necessary #ro.destroy() # Should not be necessary ctx.code = pythoncell().set(v) ctx.registrar.python.register(ctx.code) ctx.equilibrate() rc = ctx.rc = reactor({}) ctx.registrar.python.connect("MyClass", rc) rc.code_start.cell().set("print( 'start', MyClass(1,2,3) )") rc.code_update.cell().set("print( 'update', MyClass(1,2,3) )") rc.code_stop.cell().set("print('stop')") tf = ctx.tf = transformer({}) ctx.registrar.python.connect("MyClass", tf) tf.code.cell().set("print( 'transform', MyClass(1,2,3) ); return") ctx.equilibrate() ctx.code.set(ctx.code.value + " ") ctx.equilibrate()
# Program template ctx.program_template = cell("cson") ctx.link_program_template = link(ctx.program_template, ".", "program_template.cson", file_dominant=file_dominant) # Program and program generator ctx.program = cell("json") ctx.program.resource.save_policy = 4 #always save the program ctx.gen_program = transformer({ "program_template": { "pin": "input", "dtype": "json" }, "program": { "pin": "output", "dtype": "json" } }) ctx.registrar.silk.connect("VertexData", ctx.gen_program) ctx.link_gen_program = link(ctx.gen_program.code.cell(), ".", "cell-gen-program.py", file_dominant=file_dominant) ctx.program_template.connect(ctx.gen_program.program_template) ctx.gen_program.program.connect(ctx.program) #GL program ctx.equilibrate() #ctx.program has to be generated first p = ctx.glprogram = glprogram(ctx.program,
from seamless.lib.gui.basic_editor import edit from seamless.lib.gui.basic_display import display import numpy as np ctx = context() a1 = np.arange(10, 20, 1) a2 = np.arange(100, 120, 2) ctx.array1 = cell("array").set(a1) ctx.array2 = cell("array").set(a2) ctx.tf = transformer({ "a1": { "pin": "input", "dtype": "array" }, "a2": { "pin": "input", "dtype": "array" }, "a3": { "pin": "output", "dtype": "array" } }) ctx.array1.connect(ctx.tf.a1) ctx.array2.connect(ctx.tf.a2) ctx.tf.code.cell().set( "print('RUN');import numpy as np; return np.concatenate((a1,a2))") ctx.equilibrate() print(ctx.tf.a3.cell().value) ctx.array1.set_store("GL") ctx.rc = reactor({"a1": {"pin": "input", "dtype": "array"}})
ctx.gen_sphere.coordinates.connect(ctx.coordinates) ctx.gen_sphere.normals.connect(ctx.normals) ctx.gen_sphere.edges.connect(ctx.edges) ctx.gen_sphere.triangle_indices.connect(ctx.triangle_indices) ctx.gen_sphere.triangle_coordinates.connect(ctx.triangle_coordinates) ctx.gen_sphere.triangle_normals.connect(ctx.triangle_normals) ctx.params.gen_uniforms = cell("json").set({ "input": {"pin": "input", "dtype": "json"}, "output": {"pin": "output", "dtype": "json"}, }) ctx.window = glwindow("Seamless OpenGL 3D Example") # Uniforms ctx.gen_uniforms = transformer(ctx.params.gen_uniforms) ctx.gen_uniforms.code.cell().set(""" result = { "u_modelview_matrix": input["modelview_matrix"], "u_projection_matrix": input["projection_matrix"], "u_normal_matrix": input["normal_matrix"], "u_mvp_matrix": input["mvp_matrix"], } return result """) identity = np.eye(4).tolist() ctx.uniforms = cell("json") ctx.window.camera.connect(ctx.uniforms) ctx.uniforms.connect(ctx.gen_uniforms.input) # Atom shaders
}, "output": { "pin": "output", "dtype": "int" } } if __name__ == "__main__": from seamless import cell, pythoncell, transformer, context ctx = context() ctx.c_data = cell("int").set(4) ctx.c_output = cell("int") ctx.c_code = pythoncell() ctx.cont = transformer(tparams) ctx.c_data.connect(ctx.cont.value) ctx.c_code.connect(ctx.cont.code) ctx.c_code.set("return value*2") print(ctx.c_data.data, "'" + ctx.c_code.data + "'", ctx.c_output.data) ctx.cont.output.connect(ctx.c_output) ctx.equilibrate() print(ctx.c_data.data, "'" + ctx.c_code.data + "'", ctx.c_output.data) ctx.c_data.set(5) ctx.c_code.set("return value*3")
code = """ @input_doc pulse_py @input_var pulses @input_var delay @intern count @intern value python3 !pulse_py $pulses $delay > count tail -1 !count > value @export value """ ctx = context() ctx.pre_code = cell(("text", "code", "slash-0")).set(code) gen_code = ctx.gen_code = transformer({ "in_code": {"pin": "input", "dtype": ("text", "code", "slash-0")}, "monitor": {"pin": "input", "dtype": "bool"}, "out_code": {"pin": "output", "dtype": ("text", "code", "slash-0")}, }) gen_code.monitor.set(False) gen_code.code.set(""" if not monitor: return in_code lines = [] for l in in_code.splitlines(): if l.find("python3 !pulse_py") > -1: l += " @monitor 0.01" lines.append(l) return "\\n".join(lines) """) ctx.pre_code.connect(gen_code.in_code) ctx.code = gen_code.out_code.cell()
""" from seamless import context, cell, pythoncell, transformer from seamless.lib import edit, display ctx = context() # Create 3 int cells: a=2, b=3, and result ctx.a = cell("int").set(2) ctx.b = cell("int").set(3) ctx.result = cell("int") # Set up a transformer that computes "result" as a function of "a" and "b" t = ctx.transform = transformer({ "a": {"pin": "input", "dtype": "int"}, "b": {"pin": "input", "dtype": "int"}, "result": {"pin": "output", "dtype": "int"} }) # Connect the cells to the transformer pins ctx.a.connect(t.a) ctx.b.connect(t.b) t.result.connect(ctx.result) # Every transformer has an implicit extra input pin, called "code" # It must be connected to a Python cell ctx.formula = pythoncell().set("return a + b") ctx.formula.connect(t.code) # Transformers execute asynchronously; ctx.equilibrate() will wait until all # transformations have finished
program = { "arrays": [], "uniforms": {}, "render": { "command": "points", "glstate": {}, "attributes": {}, }, } ctx.pre_program = cell("json").set(program) ctx.gen_program = transformer({ "program": { "pin": "input", "dtype": "json" }, "result": { "pin": "output", "dtype": "json" } }) ctx.pre_program.connect(ctx.gen_program.program) ctx.gen_program.code.cell().set("return program") ctx.program = ctx.gen_program.result.cell() ctx.equilibrate() p = ctx.glprogram = glprogram(ctx.program) p.uniforms.cell().set({}) ctx.vertex_shader.connect(p.vertex_shader) ctx.fragment_shader.connect(p.fragment_shader) p.painted.cell().connect(
def make_cmd_std(cmd_params): nonlocal filehashes tf_params = { "PARAMS": { "pin": "input", "dtype": "json", }, } in_connections = [] out_connections = [] for file_ in cmd_params["files"]: fh = filehash(file_) filehashes += 1 fhname = "filehash_%d" % filehashes tf_params[fhname] = { "pin": "input", "dtype": "str" } fhname = "filehash_%d" % filehashes setattr(ctx, fhname, fh) in_connections.append((fh.filehash.cell(), fhname)) for inp,typ in cmd_params["inputs"].items(): if typ == "doc": dtype = "text" prefix = "doc_" elif typ == "variable": dtype = "str" prefix = "var_" else: raise TypeError((cmd_params["lineno"], cmd_params["source"], inp, typ)) #must be a bug tf_params[inp] = { "pin": "input", "dtype": dtype, } in_connections.append((ctx.CHILDREN[prefix + inp], inp)) if len(cmd_params["outputs"]) > 1: raise NotImplementedError("Multiple outputs not yet implemented") for output in cmd_params["outputs"]: if hasattr(ctx, "ctx_" + output): dtype = "json" prefix = "ctx_" else: dtype = "text" prefix = "doc_" tf_params[output] = { "pin": "output", "dtype": dtype, } #TODO: in case of multiple outputs => single JSON cell + subcells (to be implemented) out_connections.append((output, ctx.CHILDREN[prefix + output])) tf = transformer(tf_params) tf.PARAMS.cell().set(cmd_params) for con in in_connections: pin = getattr(tf, con[1]) con[0].connect(pin) for con in out_connections: pin = getattr(tf, con[0]) pin.connect(con[1]) ctx.cell_cmd_std.connect(tf.code) return tf
ctx.gen_sphere.triangle_normals.connect(ctx.triangle_normals) ctx.params.gen_uniforms = cell("json").set({ "input": { "pin": "input", "dtype": "json" }, "output": { "pin": "output", "dtype": "json" }, }) ctx.window = glwindow("Seamless OpenGL 3D Example") # Uniforms ctx.gen_uniforms = transformer(ctx.params.gen_uniforms) ctx.gen_uniforms.code.cell().set(""" result = { "u_modelview_matrix": input["modelview_matrix"], "u_projection_matrix": input["projection_matrix"], "u_normal_matrix": input["normal_matrix"], "u_mvp_matrix": input["mvp_matrix"], } return result """) identity = np.eye(4).tolist() ctx.uniforms = cell("json") ctx.window.camera.connect(ctx.uniforms) ctx.uniforms.connect(ctx.gen_uniforms.input) # Atom shaders
ctx = ctx_bak ### ctx.texture = cell("array") ctx.texture.set_store("GLTex", 2) ctx.display_numpy.title.set("Texture") ctx.texture.connect(ctx.display_numpy.array) import numpy as np print("START") ctx.equilibrate() ctx.gen_texture = transformer({ "filename": { "pin": "input", "dtype": "str" }, "texture": { "pin": "output", "dtype": "array" } }) ctx.gen_texture.texture.connect(ctx.texture) link(ctx.gen_texture.code.cell(), ".", "cell-gen-texture.py") ctx.equilibrate() ctx.texture_filename = cell(str).set("textures/Bricks.png") ctx.texture_filename.connect(ctx.gen_texture.filename) ctx.normal_map = cell("array").set_store("GLTex", 2) ctx.gen_normal_map = transformer( {"normal_map": { "pin": "output",
ctx = context() # Create 3 int cells: a=2, b=3, and result ctx.a = cell("int").set(2) ctx.b = cell("int").set(3) ctx.result = cell("int") # Set up a transformer that computes "result" as a function of "a" and "b" t = ctx.transform = transformer({ "a": { "pin": "input", "dtype": "int" }, "b": { "pin": "input", "dtype": "int" }, "result": { "pin": "output", "dtype": "int" } }) # Connect the cells to the transformer pins ctx.a.connect(t.a) ctx.b.connect(t.b) t.result.connect(ctx.result) # Every transformer has an implicit extra input pin, called "code" # It must be connected to a Python cell ctx.formula = pythoncell().set("return a + b")
import seamless from seamless import cell, transformer ctx = seamless.context() ctx.count = cell("int").set(10) ctx.tf = transformer({ "count": {"pin": "input", "dtype": "int"} }) ctx.count.connect(ctx.tf.count) ctx.tf.code.cell().set(""" import time for n in range(count): print(n+1) time.sleep(0.5) return """) ctx.equilibrate(2) ctx.count.set(20) ctx.equilibrate(2)
ctx.display_numpy = reactor({ "array": {"pin": "input", "dtype": "array"}, "title": {"pin": "input", "dtype": "str"}, }) ctx.registrar.python.connect("AspectLayout", ctx.display_numpy) ctx.array.connect(ctx.display_numpy.array) ctx.title.connect(ctx.display_numpy.title) ctx.display_numpy.code_update.set("update()") ctx.display_numpy.code_stop.set("destroy()") ctx.code = pythoncell() ctx.code.connect(ctx.display_numpy.code_start) ctx.code.fromfile("cell-display-numpy.py") ### ctx = ctx_bak ctx.texture = cell("array") ctx.texture.set_store("GLTex", 2) ctx.texture.connect(ctx.display_numpy.array) import numpy as np print("START") ctx.equilibrate() ctx.gen_texture = transformer({"texture": {"pin": "output", "dtype": "array"}}) ctx.gen_texture.texture.connect(ctx.texture) link(ctx.gen_texture.code.cell(), ".", "cell-gen-texture.py") ctx.equilibrate() ctx.texture_coords.connect(ctx.tri_program.array_texture_coords) ctx.triangle_texture_coords.connect(ctx.ftri_program.array_texture_coords)
"title": { "pin": "input", "dtype": "str" }, }) ctx.registrar.python.connect("AspectLayout", ctx.display_numpy) ctx.array.connect(ctx.display_numpy.array) ctx.title.connect(ctx.display_numpy.title) ctx.display_numpy.code_update.set("update()") ctx.display_numpy.code_stop.set("destroy()") ctx.code = pythoncell() ctx.code.connect(ctx.display_numpy.code_start) ctx.code.fromfile("cell-display-numpy.py") ### ctx = ctx_bak ctx.texture = cell("array") ctx.texture.set_store("GLTex", 2) ctx.texture.connect(ctx.display_numpy.array) import numpy as np print("START") ctx.equilibrate() ctx.gen_texture = transformer({"texture": {"pin": "output", "dtype": "array"}}) ctx.gen_texture.texture.connect(ctx.texture) link(ctx.gen_texture.code.cell(), ".", "cell-gen-texture.py") ctx.equilibrate() ctx.texture_coords.connect(ctx.tri_program.array_texture_coords) ctx.triangle_texture_coords.connect(ctx.ftri_program.array_texture_coords)
ctx.display_numpy.code_stop.set("destroy()") ctx.code = pythoncell() ctx.code.connect(ctx.display_numpy.code_start) ctx.code.fromfile("cell-display-numpy.py") ### ctx = ctx_bak ctx.texture = cell("array") ctx.texture.set_store("GLTex", 2) ctx.texture.connect(ctx.display_numpy.array) import numpy as np print("START") ctx.equilibrate() ctx.gen_texture = transformer({ "filename": {"pin": "input", "dtype": "str"}, "texture": {"pin": "output", "dtype": "array"} }) ctx.gen_texture.texture.connect(ctx.texture) link(ctx.gen_texture.code.cell(), ".", "cell-gen-texture.py") ctx.equilibrate() ctx.texture_coords.connect(ctx.tri_program.array_texture_coords) ctx.triangle_texture_coords.connect(ctx.ftri_program.array_texture_coords) ctx.texture.connect(ctx.tri_program.array_s_texture) ctx.texture.connect(ctx.ftri_program.array_s_texture) import os ctx.texture_filename = cell(str).set("textures/Bricks.png") ctx.texture_filename.connect(ctx.gen_texture.filename)
what you want for e.g. JSON or python cells In the future, inputpins will have to be specifically declared to support preliminary cell values """ import seamless from seamless import context, cell, transformer ctx = context() ctx.value = cell("int") ctx.value.resource.save_policy = 4 #always save cell value tf = ctx.tf = transformer({ "pulses": { "pin": "input", "dtype": "int" }, "delay": { "pin": "input", "dtype": "float" }, "value": { "pin": "output", "dtype": "int" }, }) tf.value.connect(ctx.value) tf.code.cell().set(""" import time for n in range(pulses): return_preliminary(n) time.sleep(delay) return pulses """) ctx.delay = cell("float").set(0.1)
ctx = ctx_bak ### ctx.texture = cell("array") ctx.texture.set_store("GLTex", 2) ctx.display_numpy.title.set("Texture") ctx.texture.connect(ctx.display_numpy.array) import numpy as np print("START") ctx.equilibrate() ctx.gen_texture = transformer({ "filename": { "pin": "input", "dtype": "str" }, "texture": { "pin": "output", "dtype": "array" } }) ctx.gen_texture.texture.connect(ctx.texture) link(ctx.gen_texture.code.cell(), ".", "cell-gen-texture.py") ctx.equilibrate() ctx.texture_filename = cell(str).set("textures/Bricks.png") ctx.texture_filename.connect(ctx.gen_texture.filename) ctx.normal_map = cell("array").set_store("GLTex", 2) ctx.gen_normal_map = transformer( {"normal_map": { "pin": "output",
def plotly(ctx, *, dynamic_html, mode): from seamless import context, cell, transformer from seamless.lib.templateer import templateer from seamless.core.worker import \ ExportedInputPin, ExportedOutputPin assert mode in ("nx", "nxy", "manual"), mode data_dtype = "json" if mode == "manual" else "text" #csv # Subcontexts ctx.values = context() ctx.templates = context() ctx.params = context() ctx.code = context() # Static HTML output ctx.values.html = cell(("text", "html")) ctx.html = ExportedOutputPin(ctx.values.html) # Templates ctx.templates.html_head_body = cell(("text", "html"))\ .fromfile("template-html-head-body.jinja") ctx.templates.head = cell("text")\ .fromfile("template-head.jinja") ctx.templates.body = cell("text")\ .fromfile("template-body.jinja") ctx.templates.body_dynamic = cell("text")\ .fromfile("template-body-dynamic.jinja") # Values: here is where all authoritative state goes ctx.values.title = cell("str").set("Seamless Plotly") ctx.values.data = cell(data_dtype) ctx.values.attrib = cell("json") ctx.values.layout = cell("json") ctx.values.config = cell("json").set({}) ctx.values.width = cell("int").set(500) ctx.values.height = cell("int").set(500) ctx.values.divname = cell("str").set("plotlydiv") # Input pins ctx.title = ExportedInputPin(ctx.values.title) ctx.data = ExportedInputPin(ctx.values.data) ctx.attrib = ExportedInputPin(ctx.values.attrib) ctx.layout = ExportedInputPin(ctx.values.layout) ctx.config = ExportedInputPin(ctx.values.config) # Static HTML: templateer_static params_static = {"environment": {"title": "str", "divname": "str", "width": "int", "height": "int", "plotly_data": "json", "layout": "json", "config": "json", }, "templates": ["body", "head", "head_body"], "result": "head_body"} ctx.params.templateer_static = cell("json").set(params_static) ctx.templateer_static = templateer(ctx.params.templateer_static) ctx.values.height.connect(ctx.templateer_static.height) ctx.values.width.connect(ctx.templateer_static.width) ctx.values.divname.connect(ctx.templateer_static.divname) ctx.values.config.connect(ctx.templateer_static.config) ctx.values.layout.connect(ctx.templateer_static.layout) ctx.values.title.connect(ctx.templateer_static.title) ctx.templates.body.connect(ctx.templateer_static.body) ctx.templates.head.connect(ctx.templateer_static.head) ctx.templates.html_head_body.connect(ctx.templateer_static.head_body) ctx.templateer_static.RESULT.connect(ctx.values.html) #plotly_data temporary ctx.temp_plotly_data = cell("json") ctx.temp_plotly_data.connect(ctx.templateer_static.plotly_data) # Data integrator ctx.integrate_data = transformer({ "data": {"pin": "input", "dtype": "json"}, "attrib": {"pin": "input", "dtype": "json"}, "plotly_data": {"pin": "output", "dtype": "json"}, }) ctx.code.integrate_data = cell(("text", "code","python"))\ .fromfile("cell-integrate-data.py") ctx.code.integrate_data.connect(ctx.integrate_data.code) ctx.values.attrib.connect(ctx.integrate_data.attrib) ctx.integrate_data.plotly_data.connect(ctx.temp_plotly_data) if mode != "manual": #loaded_data temporary ctx.temp_loaded_data = cell("json") ctx.temp_loaded_data.connect(ctx.integrate_data.data) # Pandas data loader ctx.load_data = transformer({ "csv": {"pin": "input", "dtype": "text"}, "data": {"pin": "output", "dtype": "json"}, }) c = ctx.code.load_data = cell(("text", "code","python")) if mode == "nxy": c.fromfile("cell-load-data-nxy.py") elif mode == "nx": c.fromfile("cell-load-data-nx.py") ctx.code.load_data.connect(ctx.load_data.code) ctx.values.data.connect(ctx.load_data.csv) ctx.load_data.data.connect(ctx.temp_loaded_data) else: ctx.values.data.connect(ctx.integrate_data.data) if not dynamic_html: return from seamless.lib.dynamic_html import dynamic_html # Dynamic HTML output ctx.values.dynamic_html = cell(("text", "html")) ctx.dynamic_html = ExportedOutputPin(ctx.values.dynamic_html) # Dynamic HTML: templateer_dynamic ctx.params.templateer_dynamic = cell("json") params = {"environment": {"title": "text", "divname": "text", "width": "int", "height": "int", "dynamic_html": ("text","html") }, "templates": ["body", "head", "head_body"], "result": "head_body"} ctx.params.templateer_dynamic.set(params) ctx.templateer_dynamic = templateer(ctx.params.templateer_dynamic) ctx.values.height.connect(ctx.templateer_dynamic.height) ctx.values.width.connect(ctx.templateer_dynamic.width) ctx.values.divname.connect(ctx.templateer_dynamic.divname) ctx.values.title.connect(ctx.templateer_dynamic.title) ctx.templates.body_dynamic.connect(ctx.templateer_dynamic.body) ctx.templates.head.connect(ctx.templateer_dynamic.head) ctx.templates.html_head_body.connect(ctx.templateer_dynamic.head_body) ctx.templateer_dynamic.RESULT.connect(ctx.values.dynamic_html) # Dynamic HTML maker # TODO: more efficient plot regeneration ctx.params.dynamic_html_maker = cell("json") dynamic_html_params = { "divname": {"type": "var", "dtype": "str"}, "plotly_data": {"type": "var", "dtype": "json", "evals":["make_plot"]}, #"data": {"type": "var", "dtype": "json", "evals":["update_data"]}, #"attrib": {"type": "var", "dtype": "json", "evals":["update_attrib"]}, #"layout": {"type": "var", "dtype": "json", "evals":["update_layout"]}, "data": {"type": "var", "dtype": "json"}, "attrib": {"type": "var", "dtype": "json", "evals":["make_plot"]}, "layout": {"type": "var", "dtype": "json", "evals":["make_plot"]}, "config": {"type": "var", "dtype": "json", "evals":["make_plot"]}, "update_data": {"type": "eval", "on_start": False}, "update_attrib": {"type": "eval", "on_start": False}, "update_layout": {"type": "eval", "on_start": False}, "make_plot": {"type": "eval", "on_start": True}, } ctx.params.dynamic_html_maker.set(dynamic_html_params) ctx.dynamic_html_maker = dynamic_html(ctx.params.dynamic_html_maker) ctx.dynamic_html_maker.dynamic_html.cell().connect( ctx.templateer_dynamic.dynamic_html ) ctx.temp_plotly_data.connect(ctx.dynamic_html_maker.plotly_data) if mode == "manual": ctx.values.data.connect(ctx.dynamic_html_maker.data) else: ctx.temp_loaded_data.connect(ctx.dynamic_html_maker.data) ctx.values.attrib.connect(ctx.dynamic_html_maker.attrib) ctx.values.config.connect(ctx.dynamic_html_maker.config) ctx.values.layout.connect(ctx.dynamic_html_maker.layout) ctx.dynamic_html_maker.make_plot.cell().set(""" Plotly.newPlot(divname, plotly_data, layout, config); """) ctx.dynamic_html_maker.update_data.cell().set(""" var i, ii, subdata, update, attribname; for (i = 0; i < plotly_data.length; i++) { subdata = data[i]; update = {}; for (var attribname in subdata) { update[attribname] = [subdata[attribname]]; } /*if (i==0) { x = document.getElementById("echo"); x.innerHTML = "<pre>" + JSON.stringify(update) + "</pre>"; }*/ Plotly.restyle(divname, update, [i]); } """) ctx.dynamic_html_maker.update_attrib.cell().set(""" var i; for (i = 0; i < plotly_data.length; i++) { Plotly.restyle(divname, attrib[i], [i]); } """) ctx.dynamic_html_maker.update_layout.cell().set(""" Plotly.relayout(divname, layout); """) ctx.values.divname.connect(ctx.dynamic_html_maker.divname)
ctx.texture = cell("array") ctx.texture.set_store("GLTex", 2) ctx.display_numpy.title.set("Texture") ctx.texture.connect(ctx.display_numpy.array) import numpy as np print("START") ctx.equilibrate() ctx.gen_texture = transformer({ "filename": { "pin": "input", "dtype": "str" }, "inscription": { "pin": "input", "dtype": "str" }, "texture": { "pin": "output", "dtype": "array" } }) ctx.gen_texture.texture.connect(ctx.texture) link(ctx.gen_texture.code.cell(), ".", "cell-gen-texture.py") ctx.equilibrate() ctx.normal_map = cell("array").set_store("GLTex", 2) ctx.gen_normal_map = transformer({ "normal_texture_filename": { "pin": "input", "dtype": "str"
"pin": "input", "dtype": "int" }, "outp": { "pin": "output", "dtype": ("text", "html") } } tf_code = """ import jinja2 d = dict(IDENTIFIER=identifier, socket=socket) return jinja2.Template(inp).render(d) """ ctx.client1 = cell(("text", "html")) ctx.tf_client1 = transformer(tf_params) ctx.server.socket.cell().connect(ctx.tf_client1.socket) ctx.client_template.connect(ctx.tf_client1.inp) ctx.tf_client1.code.cell().set(tf_code) ctx.tf_client1.identifier.cell().set("First WebSocket client") ctx.tf_client1.outp.connect(ctx.client1) browse(ctx.client1) ctx.client2 = cell(("text", "html")) ctx.tf_client2 = transformer(tf_params) ctx.server.socket.cell().connect(ctx.tf_client2.socket) ctx.client_template.connect(ctx.tf_client2.inp) ctx.tf_client2.code.cell().set(tf_code) ctx.tf_client2.identifier.cell().set("Second WebSocket client") ctx.tf_client2.outp.connect(ctx.client2) browse(ctx.client2)
export(ctx.slash.energies) export(ctx.slash.ntop, "int").set(100) export(ctx.slash.clust_cutoff, "float").set(10) edit(ctx.ntop, "Top structures") edit(ctx.clust_cutoff, "Clustering cutoff") display(ctx.energies, "Energies") export(ctx.slash.irmsds) display(ctx.irmsds, "i-RMSD") export(ctx.slash.clusters) display(ctx.clusters, "clusters") ctx.colors = cell("text").fromfile("colors.txt") link(ctx.colors) ctx.gen_data = transformer({ "colors": {"pin": "input", "dtype":"text"}, "irmsds": {"pin": "input", "dtype":"text"}, "energies": {"pin": "input", "dtype":"text"}, "clusters": {"pin": "input", "dtype":"text"}, "data": {"pin": "output", "dtype": "json"}, }) ctx.gen_data.code.cell().fromfile("cell-gen-data.py") link(ctx.gen_data.code.cell()) ctx.colors.connect(ctx.gen_data.colors) ctx.clusters.connect(ctx.gen_data.clusters) ctx.irmsds.connect(ctx.gen_data.irmsds) ctx.energies.connect(ctx.gen_data.energies) ctx.data2 = cell("json") ctx.gen_data.data.connect(ctx.data2) ctx.gen_attrib = transformer({ "colors": {"pin": "input", "dtype":"text"}, "clusters": {"pin": "input", "dtype":"text"}, "attrib": {"pin": "output", "dtype": "json"},
ctx.client_template = cell("text") link(ctx.client_template, ".", "test-websockets_client.jinja") tf_params = {"inp":{"pin": "input", "dtype": "text"}, "identifier":{"pin": "input", "dtype": "text"}, "socket":{"pin": "input", "dtype": "int"}, "outp":{"pin": "output", "dtype": ("text", "html")} } tf_code = """ import jinja2 d = dict(IDENTIFIER=identifier, socket=socket) return jinja2.Template(inp).render(d) """ ctx.client1 = cell(("text", "html")) ctx.tf_client1 = transformer(tf_params) ctx.server.socket.cell().connect(ctx.tf_client1.socket) ctx.client_template.connect(ctx.tf_client1.inp) ctx.tf_client1.code.cell().set(tf_code) ctx.tf_client1.identifier.cell().set("First WebSocket client") ctx.tf_client1.outp.connect(ctx.client1) browse(ctx.client1) ctx.client2 = cell(("text", "html")) ctx.tf_client2 = transformer(tf_params) ctx.server.socket.cell().connect(ctx.tf_client2.socket) ctx.client_template.connect(ctx.tf_client2.inp) ctx.tf_client2.code.cell().set(tf_code) ctx.tf_client2.identifier.cell().set("Second WebSocket client") ctx.tf_client2.outp.connect(ctx.client2) browse(ctx.client2)
@intern count @intern value python3 !pulse_py $pulses $delay > count tail -1 !count > value @export value """ ctx = context() ctx.pre_code = cell(("text", "code", "slash-0")).set(code) gen_code = ctx.gen_code = transformer({ "in_code": { "pin": "input", "dtype": ("text", "code", "slash-0") }, "monitor": { "pin": "input", "dtype": "bool" }, "out_code": { "pin": "output", "dtype": ("text", "code", "slash-0") }, }) gen_code.monitor.set(False) gen_code.code.set(""" if not monitor: return in_code lines = [] for l in in_code.splitlines(): if l.find("python3 !pulse_py") > -1: l += " @monitor 0.01" lines.append(l)
from seamless import cell, context, transformer, time ctx = context() typ = ("json", "seamless", "transformer_params") #typ = "json" c = ctx.c = cell(typ).set({"test": 10}) t = ctx.t = transformer({ "input": { "pin": "input", "dtype": typ }, "value": { "pin": "output", "dtype": "int" }, }) t.code.cell().set("""return input['test']""") c.connect(t.input) x = t.value.cell() ctx.equilibrate() print('X', x.data)