def my_callback(node): fname = safe_path_join(mandalka.unique_id(node), dirname=HISTORY_PATH) try: with open(fname, 'x') as f_out: descr = mandalka.describe(node, depth=1) uids = ' '.join([mandalka.unique_id(n) for n in mandalka.inputs(node)]) try: uid = mandalka.get_evaluation_stack(-1) except IndexError: uid = "" f_out.write(descr + '\n' + uids + '\n' + uid + '\n') except FileExistsError: pass
@node class Node: def __init__(self, a=1, b=2): pass assert Node() == Node(1, 2) assert Node(3) == Node(3, 2) assert Node(3) == Node(a=3) assert Node(a=4, b=5) == Node(4, 5) assert Node(1, b=7) == Node(b=7) assert fails(lambda: Node(c=1)) assert fails(lambda: Node(1, a=1)) assert describe(Node("a", "b")) == "Node(a='a', b='b')" assert arguments(Node("x", "y"))["a"] == "x" @node class VarNode: def __init__(self, a, *b, c=3): print(a, b, c) assert VarNode(1, c=3) == VarNode(1) assert VarNode(a=1) == VarNode(1) assert fails(lambda: VarNode()) assert describe(VarNode("a", "b")) == "VarNode('a', 'b', c=3)"
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import mandalka @mandalka.node class Foo: def __init__(self, x): self.x = x def foo(self): return self.x @mandalka.node class FooFactory: def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs def __call__(self): return Foo(*self.args, **self.kwargs) Foo10 = FooFactory(10) print(mandalka.describe(Foo10)) print(mandalka.describe(Foo10())) assert Foo10().foo() == 10
assert not fails(lambda: NotANode(Node(1, 2), 3)) assert fails(lambda: Node(Node(NotANode(1, 2), 3))) assert not fails(lambda: Node(Node(Node(3, 4), Node(3)))) modify_args = [1, {2: [3, 4]}] nodes = [ Node(*modify_args), Node(123, b=[]), Node({ "b": 1, "a": 2 }, b=Another({})), Another(a=["a", b"b"]), Node({ 1: 2, "a": ["b"] }), Node((3.14, (0.00159, 0.1 * 0.1))), Node(set([5, 2, 3, 4, 1])) ] modify_args[1][2].append(5) assert repr(nodes[0].b) == "{2: [3, 4]}" for n in nodes: print(describe(n, 0)) print(describe(n)) print(describe(n, 2))
def dump_nodes_to_html(nodes, dirname): describe = lambda node, depth: mandalka.unique_id( node) + ": " + mandalka.describe(node, depth) href_chembl_compound = lambda uid: href( "https://www.ebi.ac.uk/chembl/compound/inspect/{}".format(uid), uid, ) href_chembl_document = lambda uid: href( "https://www.ebi.ac.uk/chembl/doc/inspect/{}".format(uid), uid, ) def dump_node_to_html(node, fname): arr, header = to_arr_header(node.data) width = columns_width(arr, header, 30) arr, header = sanitize_html(arr), sanitize_html(header) for i, key in enumerate(header): if "uid" in key and not "doc" in key: arr[:, i] = np.vectorize(href_chembl_compound, otypes=(np.str, ))(arr[:, i]) if "uid" in key and "doc" in key: arr[:, i] = np.vectorize(href_chembl_document, otypes=(np.str, ))(arr[:, i]) with open(fname, 'x') as f_out: f_out.write( doc_template( style_template( table_style_1("data_table"), div_style_1(None), ) + '\n' + tablesorter(), to_html(arr, header, width, "data_table"), )) os.makedirs(dirname) os.makedirs(safe_path_join("per_uid", dirname=dirname)) os.makedirs(safe_path_join("per_node", dirname=dirname)) body = [] for node in nodes: nname = sanitize_html(describe(node, depth=1)) fname = "{}.html".format(node) dump_node_to_html(node, safe_path_join("per_node", fname, dirname=dirname)) body.append(div_template(href(fname, nname))) body.append('<pre white-space="pre-wrap">' + sanitize_html(str(node.data)) + '</pre>') with open(safe_path_join("per_node", "index.html", dirname=dirname), 'x') as f_out: f_out.write( doc_template( style_template(div_style_1(None), ), '\n'.join(body), )) href_uid = lambda uid: href("{}.html".format(uid), uid) all_uids = sorted(set([uid for n in nodes for uid in n.data["uid"]])) with open(safe_path_join("per_uid", "index.html", dirname=dirname), 'x') as f_out: f_out.write( doc_template( "", '\t'.join([href_uid(uid) for uid in all_uids]), )) body = [] for i in range(len(nodes)): u1 = set(nodes[i - 1].data["uid"]) if i > 0 else set() u2 = set(nodes[i].data["uid"]) new_uids = '\t'.join([href_uid(uid) for uid in sorted(u2 - u1)]) deleted_uids = '\t'.join([href_uid(uid) for uid in sorted(u1 - u2)]) body += [ div_template("⇨\t" + sanitize_html(describe(nodes[i], depth=1))), div_template("NEW UIDS"), div_template(new_uids), div_template("DELETED UIDS"), div_template(deleted_uids), ] with open(safe_path_join("per_uid", "deltas.html", dirname=dirname), 'x') as f_out: f_out.write( doc_template( style_template(div_style_1()), '\n'.join(body), )) for iuid, uid in enumerate(tqdm.tqdm(all_uids)): body = [] body.append(div_template(href_chembl_compound(uid))) for n in nodes: body.append(div_template(sanitize_html(describe(n, depth=1)))) data = n.data.slice[n.data["uid"] == uid] arr, header = to_arr_header(data) width = columns_width(arr, header, 30) arr, header = sanitize_html(arr), sanitize_html(header) for i, key in enumerate(header): if "uid" in key: arr[:, i] = np.vectorize(href_uid, otypes=(np.str, ))(arr[:, i]) body.append(to_html(arr, header, width, "data_table")) body.append( div_template(' '.join([ href_uid(all_uids[iuid - 1]), href("index.html", "INDEX"), href_uid(all_uids[(iuid + 1) % len(all_uids)]), ]))) with open(safe_path_join("per_uid", uid + ".html", dirname=dirname), 'x') as f_out: f_out.write( doc_template( style_template( table_style_1("data_table"), div_style_1() + '\n' + tablesorter(), ), '\n'.join(body), ))
def mandalka_save_cache(self): description = mandalka.describe(self, depth=1) + "___" + "STACK: " + str(mandalka.get_evaluation_stack()) self.data._save(self.mandalka_cache_path(), description) self.data._close()
def inputs(a): return ", ".join( sorted([mandalka.describe(i, -1) for i in mandalka.inputs(a)]))
# Prints "Computing..." only for the first time this script is run: print(model.weights) # =============================== # Example 3 # =============================== @mandalka.node class S: def __init__(*_): pass two = S(S(0)) three = S(S(S(0))) assert three == S(two) def add(a, b): counter = 0 while counter != b: a = S(a) counter = S(counter) return a five = add(two, three) print(mandalka.describe(five, -1)) # prints "S(S(S(S(S(0)))))"