Skip to content

Commit 4080298

Browse files
committed
debugging unittests
1 parent 3f397e0 commit 4080298

File tree

8 files changed

+106
-211
lines changed

8 files changed

+106
-211
lines changed

pydra/design/tests/test_shell.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ def test_interface_template_with_overrides_and_optionals():
309309
type=tuple[int, str],
310310
sep=" ",
311311
position=5,
312-
sep=" ",
313312
),
314313
] + outargs + [ShellDef.additional_args]
315314
assert sorted_fields(Cp.Outputs) == outargs + [

pydra/engine/helpers_file.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from fileformats.generic import FileSet
1313
from pydra.engine.helpers import is_lazy, attrs_values, list_fields
1414

15+
if ty.TYPE_CHECKING:
16+
from pydra.engine.specs import TaskDef
17+
from pydra.design import shell
1518

1619
logger = logging.getLogger("pydra")
1720

@@ -235,18 +238,23 @@ def _template_formatting(field, definition, input_values):
235238
# as default, we assume that keep_extension is True
236239
if isinstance(template, (tuple, list)):
237240
formatted = [
238-
_string_template_formatting(field, t, definition, input_values)
241+
_single_template_formatting(field, t, definition, input_values)
239242
for t in template
240243
]
241244
else:
242245
assert isinstance(template, str)
243-
formatted = _string_template_formatting(
246+
formatted = _single_template_formatting(
244247
field, template, definition, input_values
245248
)
246-
return Path(formatted)
249+
return formatted
247250

248251

249-
def _string_template_formatting(field, template, definition, input_values):
252+
def _single_template_formatting(
253+
field: "shell.outarg",
254+
template: str,
255+
definition: "TaskDef",
256+
input_values: dict[str, ty.Any],
257+
) -> Path | None:
250258
from pydra.utils.typing import MultiInputObj, MultiOutputFile
251259

252260
inp_fields = re.findall(r"{\w+}", template)
@@ -328,7 +336,7 @@ def _string_template_formatting(field, template, definition, input_values):
328336
formatted_value = _element_formatting(
329337
template, val_dict, file_template, keep_extension=field.keep_extension
330338
)
331-
return formatted_value
339+
return Path(formatted_value) if formatted_value is not None else formatted_value
332340

333341

334342
def _element_formatting(

pydra/engine/tests/test_helpers_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,4 @@ def test_template_formatting(tmp_path: Path):
402402
input_values=inputs_dict,
403403
output_dir=tmp_path,
404404
spec_type="input",
405-
) == [str(tmp_path / "file.bvec"), str(tmp_path / "file.bval")]
405+
) == [tmp_path / "file.bvec", tmp_path / "file.bval"]

pydra/engine/tests/test_nipype1_convert.py

Lines changed: 0 additions & 100 deletions
This file was deleted.

pydra/engine/tests/test_node_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_task_init_3(
7777
if input_type == "array":
7878
a_in = np.array(a_in)
7979

80-
nn = FunAddTwo(name="NA").split(splitter=splitter, a=a_in)
80+
nn = FunAddTwo().split(splitter=splitter, a=a_in)
8181

8282
assert np.allclose(nn.inputs.a, [3, 5])
8383
assert nn.state.splitter == state_splitter

pydra/engine/tests/test_numpy_examples.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,41 @@
1616

1717

1818
@python.define(outputs=["b"])
19-
def arrayout(val):
19+
def ArrayOut(val):
2020
return np.array([val, val])
2121

2222

2323
def test_multiout(tmpdir):
2424
"""testing a simple function that returns a numpy array"""
25-
wf = Workflow("wf", input_spec=["val"], val=2)
26-
wf.add(arrayout(name="mo", val=wf.lzin.val))
2725

28-
wf.set_output([("array", wf.mo.lzout.b)])
29-
wf.cache_dir = tmpdir
26+
@workflow.define(outputs=["array"])
27+
def Workflow(val):
28+
mo = workflow.add(ArrayOut(val=val))
29+
return mo.b
3030

31-
with Submitter(worker="cf", n_procs=2) as sub:
32-
sub(runnable=wf)
31+
wf = Workflow(val=2)
3332

34-
results = wf.result(return_inputs=True)
33+
with Submitter(worker="cf", cache_dir=tmpdir, n_procs=2) as sub:
34+
results = sub(wf)
3535

36-
assert results[0] == {"wf.val": 2}
37-
assert np.array_equal(results[1].output.array, np.array([2, 2]))
36+
assert np.array_equal(results.outputs.array, np.array([2, 2]))
3837

3938

4039
def test_multiout_st(tmpdir):
4140
"""testing a simple function that returns a numpy array, adding splitter"""
42-
wf = Workflow("wf", input_spec=["val"], val=[0, 1, 2])
43-
wf.add(arrayout(name="mo"))
44-
wf.mo.split("val", val=wf.lzin.val).combine("val")
4541

46-
wf.set_output([("array", wf.mo.lzout.b)])
47-
wf.cache_dir = tmpdir
42+
@workflow.define(outputs=["array"])
43+
def Workflow(values):
44+
mo = workflow.add(ArrayOut().split(val=values).combine("val"))
45+
return mo.b
4846

49-
with Submitter(worker="cf", n_procs=2) as sub:
50-
sub(runnable=wf)
47+
wf = Workflow(values=[0, 1, 2])
5148

52-
results = wf.result(return_inputs=True)
49+
with Submitter(worker="cf", cache_dir=tmpdir, n_procs=2) as sub:
50+
results = sub(wf)
5351

54-
assert results[0] == {"wf.val": [0, 1, 2]}
5552
for el in range(3):
56-
assert np.array_equal(results[1].output.array[el], np.array([el, el]))
53+
assert np.array_equal(results.outputs.array[el], np.array([el, el]))
5754

5855

5956
def test_numpy_hash_1():
@@ -81,20 +78,19 @@ def test_numpy_hash_3():
8178

8279
def test_task_numpyinput_1(tmp_path: Path):
8380
"""task with numeric numpy array as an input"""
84-
nn = Identity(name="NA")
85-
nn.cache_dir = tmp_path
86-
nn.split(x=[np.array([1, 2]), np.array([3, 4])])
81+
nn = Identity().split(x=[np.array([1, 2]), np.array([3, 4])])
8782
# checking the results
88-
results = nn()
89-
assert (results[0].output.out == np.array([1, 2])).all()
90-
assert (results[1].output.out == np.array([3, 4])).all()
83+
outputs = nn(cache_dir=tmp_path)
84+
assert (outputs.out[0] == np.array([1, 2])).all()
85+
assert (outputs.out[1] == np.array([3, 4])).all()
9186

9287

9388
def test_task_numpyinput_2(tmp_path: Path):
9489
"""task with numpy array of type object as an input"""
95-
nn = Identity(name="NA")
96-
nn.cache_dir = tmp_path
97-
nn.split(x=[np.array(["VAL1"], dtype=object), np.array(["VAL2"], dtype=object)])
90+
nn = Identity().split(
91+
x=[np.array(["VAL1"], dtype=object), np.array(["VAL2"], dtype=object)]
92+
)
9893
# checking the results
99-
results = nn()
100-
assert (results[0].output.out == np.array(["VAL1"], dtype=object)).all()
94+
outputs = nn(cache_dir=tmp_path)
95+
assert outputs.out[0] == np.array(["VAL1"], dtype=object)
96+
assert outputs.out[1] == np.array(["VAL2"], dtype=object)
Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,45 @@
11
from ..helpers import load_task
2-
from pydra.design import python
2+
from pydra.design import python, workflow
3+
from pydra.engine.core import Task
4+
from pydra.engine.submitter import Submitter
35

46
import numpy as np
57
from pympler import asizeof
68
from pytest import approx
79

810

9-
def generate_list(l):
10-
return np.arange(l).tolist()
11+
def generate_list(n):
12+
return np.arange(n).tolist()
1113

1214

1315
@python.define
14-
def show_var(a):
16+
def ShowVar(a):
1517
return a
1618

1719

1820
def create_wf(size):
19-
wf = Workflow(name="wf", input_spec=["x"])
20-
wf.split("x", x=generate_list(size))
21-
wf.add(show_var(name="show", a=wf.lzin.x))
22-
wf.set_output([("out", wf.show.lzout.out)])
23-
wf.state.prepare_states(wf.inputs)
24-
wf.state.prepare_inputs()
25-
return wf
21+
@workflow.define
22+
def Workflow(x):
23+
show = workflow.add(ShowVar(a=x))
24+
return show.out
25+
26+
return Workflow().split(x=generate_list(size))
2627

2728

2829
def test_wf_memory():
2930
"""creating two workflow with relatively big splitter: 1000, 2000 and 4000 elements
3031
testings if the size of workflow grows linearly
3132
"""
3233

33-
wf_1000 = create_wf(size=1000)
34-
wf_1000_mem = asizeof.asizeof(wf_1000)
34+
wf_10000 = create_wf(size=10000)
35+
wf_10000_mem = asizeof.asizeof(wf_10000)
3536

36-
wf_2000 = create_wf(size=2000)
37-
wf_2000_mem = asizeof.asizeof(wf_2000)
37+
wf_20000 = create_wf(size=20000)
38+
wf_20000_mem = asizeof.asizeof(wf_20000)
3839

39-
wf_4000 = create_wf(size=4000)
40-
wf_4000_mem = asizeof.asizeof(wf_4000)
40+
wf_40000 = create_wf(size=40000)
41+
wf_40000_mem = asizeof.asizeof(wf_40000)
4142
# checking if it's linear with the size of the splitter
4243
# check print(asizeof.asized(wf_4000, detail=2).format()) in case of problems
43-
assert wf_4000_mem / wf_2000_mem == approx(2, 0.05)
44-
assert wf_2000_mem / wf_1000_mem == approx(2, 0.05)
45-
46-
47-
def test_load_task_memory():
48-
"""creating two workflow with relatively big splitter: 1000 and 4000 elements
49-
testings if load_task for a single element returns tasks of a similar size
50-
"""
51-
52-
wf_1000 = create_wf(size=1000)
53-
wf_1000_pkl = wf_1000.pickle_task()
54-
wf_1000_loaded = load_task(task_pkl=wf_1000_pkl, ind=1)
55-
wf_1000_single_mem = asizeof.asizeof(wf_1000_loaded)
56-
57-
wf_4000 = create_wf(size=4000)
58-
wf_4000_pkl = wf_4000.pickle_task()
59-
wf_4000_loaded = load_task(task_pkl=wf_4000_pkl, ind=1)
60-
wf_4000_single_mem = asizeof.asizeof(wf_4000_loaded)
61-
62-
# checking if it doesn't change with size of the splitter
63-
# check print(asizeof.asized(wf_4000_loaded, detail=2).format()) in case of problems
64-
assert wf_1000_single_mem / wf_4000_single_mem == approx(1, 0.05)
44+
assert wf_40000_mem / wf_20000_mem == approx(2, 0.05)
45+
assert wf_20000_mem / wf_10000_mem == approx(2, 0.05)

0 commit comments

Comments
 (0)