Skip to content

Commit 507353d

Browse files
committed
#38: generate a WfInstance from a .dot file
1 parent 164f835 commit 507353d

4 files changed

Lines changed: 47 additions & 33 deletions

File tree

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
jsonschema~=3.2.0
22
matplotlib>=3.3.0
3-
networkx>=2.4
3+
networkx>=3.0
44
numpy>=1.19.1
55
python-dateutil>=2.8.1
66
requests>=2.24.0

wfcommons/common/workflow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def read_dot(self, dot_file_path: Optional[pathlib.Path] = None) -> None:
192192

193193
tasks_map = {}
194194
for node in graph.nodes(data=True):
195-
task_name = f"{node[1]['label']}_{node[0]}"
195+
task_name = f"{node[1]['label']}_ID{node[0]}"
196196
task = Task(name=task_name, task_type=TaskType.COMPUTE, runtime=0, task_id=node[0])
197197
self.add_task(task)
198198
tasks_map[node[0]] = task_name

wfcommons/wfchef/duplicate.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
#
4-
# Copyright (c) 2021 The WfCommons Team.
4+
# Copyright (c) 2021-2024 The WfCommons Team.
55
#
66
# This program is free software: you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -92,25 +92,38 @@ def duplicate(path: pathlib.Path,
9292
f"Cannot create synthentic graph with {num_nodes} nodes from base graph with {graph.order()} nodes")
9393

9494
all_microstructures = json.loads(base_path.joinpath("microstructures.json").read_text())
95-
microstructures, freqs = map(list, zip(*[(ms, ms["frequency"]) for ms_hash, ms in all_microstructures.items()]))
96-
97-
p: List[float] = (np.array(freqs) / np.sum(freqs)).tolist()
98-
while graph.order() < num_nodes and microstructures:
99-
i = random.choice(range(len(microstructures)))
100-
ms = microstructures[i]
101-
while ms["nodes"]:
102-
j = random.choice(range(len(ms["nodes"])))
103-
structure = ms["nodes"][j]
104-
if graph.order() + len(structure) > num_nodes:
105-
del ms["nodes"][j]
106-
else:
95+
try:
96+
microstructures, freqs = map(list, zip(*[(ms, ms["frequency"]) for ms_hash, ms in all_microstructures.items()]))
97+
98+
p: List[float] = (np.array(freqs) / np.sum(freqs)).tolist()
99+
while graph.order() < num_nodes and microstructures:
100+
i = random.choice(range(len(microstructures)))
101+
ms = microstructures[i]
102+
while ms["nodes"]:
103+
j = random.choice(range(len(ms["nodes"])))
104+
structure = ms["nodes"][j]
105+
if graph.order() + len(structure) > num_nodes:
106+
del ms["nodes"][j]
107+
else:
108+
break
109+
110+
if not ms["nodes"]: # delete microstructure
111+
del microstructures[i]
112+
del p[i]
113+
continue
114+
115+
duplicate_nodes(graph, structure)
116+
117+
except ValueError as e:
118+
nodes = graph.copy().nodes
119+
while graph.order() < num_nodes:
120+
structure = set[str]()
121+
if graph.order() + len(nodes) - 2 > num_nodes:
107122
break
123+
for node in nodes:
124+
if str(node) not in ["SRC", "DST"]:
125+
structure.add(node)
108126

109-
if not ms["nodes"]: # delete microstructure
110-
del microstructures[i]
111-
del p[i]
112-
continue
113-
114-
duplicate_nodes(graph, structure)
127+
duplicate_nodes(graph, structure)
115128

116129
return graph

wfcommons/wfchef/find_microstructures.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
#
4-
# Copyright (c) 2021 The WfCommons Team.
4+
# Copyright (c) 2021-2024 The WfCommons Team.
55
#
66
# This program is free software: you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
88
# the Free Software Foundation, either version 3 of the License, or
99
# (at your option) any later version.
1010

11-
import networkx as nx
12-
from hashlib import sha256
13-
from typing import Union, Set, Optional, Dict, Hashable, List
14-
from uuid import uuid4
15-
import pathlib
1611
import json
17-
from itertools import product
18-
from networkx.readwrite import read_gpickle, write_gpickle
12+
import math
13+
import networkx as nx
1914
import numpy as np
15+
import pathlib
16+
import pickle
17+
18+
from hashlib import sha256
2019
from itertools import chain, combinations
21-
import argparse
22-
from .utils import create_graph, string_hash, type_hash, combine_hashes, annotate, draw
23-
import math
20+
from typing import Union, Set, Optional, Dict, List
21+
from uuid import uuid4
22+
23+
from .utils import create_graph, combine_hashes, annotate, draw
2424

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

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

235235
base_graph_path = g_savedir.joinpath("base_graph.pickle")
236-
write_gpickle(graph, str(base_graph_path))
236+
with open(base_graph_path, 'wb') as pf:
237+
pickle.dump(graph, pf, pickle.HIGHEST_PROTOCOL)
237238
summary["base_graphs"][graph.name] = {
238239
"size": graph.size(),
239240
"order": graph.order()

0 commit comments

Comments
 (0)