Skip to content

Commit

Permalink
fix parent calc bug, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lupeterm committed Mar 20, 2024
1 parent 4994db8 commit dcbab57
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/obr/signac_wrapper/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

def calc_nth_parent(to_go: int, parent: dict) -> str:
"""Recursively follow parent links and return parent id."""
if to_go == 0 or parent == {}:
if to_go == 1 or parent == {}:
pid = parent.get("parent_id", None)
return pid
return calc_nth_parent(to_go-1, parent.get("parent", {}))
Expand Down Expand Up @@ -71,7 +71,7 @@ def group_jobs(self, jobs: list[Job], summarize: int = 0) -> dict[str, list[Job]
"""Returns the list of jobs of the given OpenFOAMProject where the last `summarize` levels are grouped together at the corresponding parent view.
"""
grouped = {}
id_view_map = map_view_folder_to_job_id("view")
id_view_map = map_view_folder_to_job_id(os.path.join(self.path, "view"))
for job in jobs:
jobid = str(job.id)
# only consider leaf nodes for now
Expand All @@ -80,7 +80,7 @@ def group_jobs(self, jobs: list[Job], summarize: int = 0) -> dict[str, list[Job]
current = dict(job.statepoint)
p_view = None
# follow parent links "bottom-up" in statepoint <summarize> times
pid = calc_nth_parent(to_go=summarize, parent=current)
pid = calc_nth_parent(to_go=summarize, parent=current) if summarize else jobid
if summarize and pid is None:
# if pid is None, that means that the chosen summarize value was larger than the tree level of job
continue
Expand Down
26 changes: 25 additions & 1 deletion tests/test_create_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def emit_test_config():
}
},
],
},
}
}


Expand Down Expand Up @@ -202,3 +202,27 @@ def test_cache_folder(tmpdir, emit_test_config):

job_folder = Path(workspace_dir).iterdir().__next__()
assert Path(job_folder / "case" / "test").exists()


def test_group_jobs(tmpdir, emit_test_config):
project = OpenFOAMProject.init_project(path=tmpdir)
variation = {
"variation": [{
"operation": "op1",
"schema": "path/{foo}",
"common": {"c1": "v1"},
"values": [{"foo": 1}, {"foo": 2}, {"foo": 3}]
}]
}
emit_test_config.update(variation)
create_tree(project, emit_test_config, {"folder": tmpdir}, skip_foam_src_check=True)

project.run(names=["fetchCase"])
group = project.group_jobs(project.filter_jobs(filters=[]), 0)
non_base_jobs = [j.id for j in project.filter_jobs([]) if not j.statepoint.get("has_child")]
group_ids = set()
for job_g in group.values():
for j in job_g:
group_ids.add(j.id)
assert len(group) == 3
assert set(non_base_jobs) == group_ids

0 comments on commit dcbab57

Please sign in to comment.