Skip to content

Commit

Permalink
#38: generate a WfInstance from a .dot file
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfsilva committed Mar 19, 2024
1 parent 164f835 commit 507353d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
jsonschema~=3.2.0
matplotlib>=3.3.0
networkx>=2.4
networkx>=3.0
numpy>=1.19.1
python-dateutil>=2.8.1
requests>=2.24.0
Expand Down
2 changes: 1 addition & 1 deletion wfcommons/common/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def read_dot(self, dot_file_path: Optional[pathlib.Path] = None) -> None:

tasks_map = {}
for node in graph.nodes(data=True):
task_name = f"{node[1]['label']}_{node[0]}"
task_name = f"{node[1]['label']}_ID{node[0]}"
task = Task(name=task_name, task_type=TaskType.COMPUTE, runtime=0, task_id=node[0])
self.add_task(task)
tasks_map[node[0]] = task_name
Expand Down
51 changes: 32 additions & 19 deletions wfcommons/wfchef/duplicate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 The WfCommons Team.
# Copyright (c) 2021-2024 The WfCommons Team.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -92,25 +92,38 @@ def duplicate(path: pathlib.Path,
f"Cannot create synthentic graph with {num_nodes} nodes from base graph with {graph.order()} nodes")

all_microstructures = json.loads(base_path.joinpath("microstructures.json").read_text())
microstructures, freqs = map(list, zip(*[(ms, ms["frequency"]) for ms_hash, ms in all_microstructures.items()]))

p: List[float] = (np.array(freqs) / np.sum(freqs)).tolist()
while graph.order() < num_nodes and microstructures:
i = random.choice(range(len(microstructures)))
ms = microstructures[i]
while ms["nodes"]:
j = random.choice(range(len(ms["nodes"])))
structure = ms["nodes"][j]
if graph.order() + len(structure) > num_nodes:
del ms["nodes"][j]
else:
try:
microstructures, freqs = map(list, zip(*[(ms, ms["frequency"]) for ms_hash, ms in all_microstructures.items()]))

p: List[float] = (np.array(freqs) / np.sum(freqs)).tolist()
while graph.order() < num_nodes and microstructures:
i = random.choice(range(len(microstructures)))
ms = microstructures[i]
while ms["nodes"]:
j = random.choice(range(len(ms["nodes"])))
structure = ms["nodes"][j]
if graph.order() + len(structure) > num_nodes:
del ms["nodes"][j]
else:
break

if not ms["nodes"]: # delete microstructure
del microstructures[i]
del p[i]
continue

duplicate_nodes(graph, structure)

except ValueError as e:
nodes = graph.copy().nodes
while graph.order() < num_nodes:
structure = set[str]()
if graph.order() + len(nodes) - 2 > num_nodes:
break
for node in nodes:
if str(node) not in ["SRC", "DST"]:
structure.add(node)

if not ms["nodes"]: # delete microstructure
del microstructures[i]
del p[i]
continue

duplicate_nodes(graph, structure)
duplicate_nodes(graph, structure)

return graph
25 changes: 13 additions & 12 deletions wfcommons/wfchef/find_microstructures.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 The WfCommons Team.
# Copyright (c) 2021-2024 The WfCommons Team.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

import networkx as nx
from hashlib import sha256
from typing import Union, Set, Optional, Dict, Hashable, List
from uuid import uuid4
import pathlib
import json
from itertools import product
from networkx.readwrite import read_gpickle, write_gpickle
import math
import networkx as nx
import numpy as np
import pathlib
import pickle

from hashlib import sha256
from itertools import chain, combinations
import argparse
from .utils import create_graph, string_hash, type_hash, combine_hashes, annotate, draw
import math
from typing import Union, Set, Optional, Dict, List
from uuid import uuid4

from .utils import create_graph, combine_hashes, annotate, draw

this_dir = pathlib.Path(__file__).resolve().parent

Expand Down Expand Up @@ -233,7 +233,8 @@ def save_microstructures(workflow_path: Union[pathlib.Path],
g_savedir.mkdir(exist_ok=True, parents=True)

base_graph_path = g_savedir.joinpath("base_graph.pickle")
write_gpickle(graph, str(base_graph_path))
with open(base_graph_path, 'wb') as pf:
pickle.dump(graph, pf, pickle.HIGHEST_PROTOCOL)
summary["base_graphs"][graph.name] = {
"size": graph.size(),
"order": graph.order()
Expand Down

0 comments on commit 507353d

Please sign in to comment.