Skip to content

Commit

Permalink
Added testing for SinglePoint methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 committed Sep 19, 2024
1 parent 5059c3c commit c382dd7
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 10 deletions.
18 changes: 15 additions & 3 deletions src/nomad_simulations/schema_packages/workflow/single_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,30 @@ def resolve_n_scf_steps(self) -> int:
Returns:
int: The number of SCF steps.
"""
# Initial check
if not self.outputs:
return 1
for output in self.outputs:
if not isinstance(output, SCFOutputs):
# Check if `self.outputs` has a `section`
if not output.section:
continue
if output.scf_steps is not None:
return len(output.scf_steps)
# Check if the section is `SCFOutputs`
if not isinstance(output.section, SCFOutputs):
continue
scf_output = output.section
# Check if there are `scf_steps`
if not scf_output.scf_steps:
continue
return len(scf_output.scf_steps)
return 1

def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
super().normalize(archive, logger)

# SinglePoint can only have one task; if it has more, delete the `tasks`
if self.tasks is not None and len(self.tasks) > 1:
logger.error('A `SinglePoint` workflow must have only one task.')
self.tasks = None
return

# Generate the `tasks` section if this does not exist
Expand Down
179 changes: 172 additions & 7 deletions tests/workflow/test_single_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import pytest
from nomad.datamodel import EntryArchive
from nomad.datamodel.metainfo.workflow import Link, Task, Workflow
from nomad.datamodel.metainfo.workflow import Link, Task

from nomad_simulations.schema_packages.model_method import BaseModelMethod, ModelMethod
from nomad_simulations.schema_packages.model_method import ModelMethod
from nomad_simulations.schema_packages.model_system import ModelSystem
from nomad_simulations.schema_packages.outputs import Outputs
from nomad_simulations.schema_packages.outputs import Outputs, SCFOutputs
from nomad_simulations.schema_packages.workflow import SinglePoint

from ..conftest import generate_simulation
Expand Down Expand Up @@ -87,14 +87,179 @@ def test_generate_task(
assert single_point_task.inputs[1].name == result.inputs[1].name
assert single_point_task.outputs[0].name == result.outputs[0].name

def test_resolve_n_scf_steps():
@pytest.mark.parametrize(
'scf_output, result',
[
# no outputs
(None, 1),
# output is not of type SCFOutputs
(Outputs(), 1),
# SCFOutputs without scf_steps
(SCFOutputs(), 1),
# 3 scf_steps
(SCFOutputs(scf_steps=[Outputs(), Outputs(), Outputs()]), 3),
],
)
def test_resolve_n_scf_steps(self, scf_output: Outputs, result: int):
"""
Test the `resolve_n_scf_steps` method of the `SinglePoint` section.
"""
assert True
archive = EntryArchive()
simulation = generate_simulation(
model_system=ModelSystem(), model_method=ModelMethod(), outputs=scf_output
)
archive.data = simulation
workflow = SinglePoint()
archive.workflow2 = workflow

# Add the scf output to the workflow.outputs
if scf_output is not None:
workflow.outputs = [
Link(name='SCF Output Data', section=archive.data.outputs[-1])
]

n_scf_steps = workflow.resolve_n_scf_steps()
assert n_scf_steps == result

def test_normalize():
@pytest.mark.parametrize(
'model_system, model_method, outputs, tasks, result_task, result_n_scf_steps',
[
# multiple tasks being stored in SinglePoint
(
ModelSystem(),
ModelMethod(),
Outputs(),
[Task(name='task 1'), Task(name='task 2')],
[],
None,
),
# only one task is being stored in SinglePoint
(
ModelSystem(),
ModelMethod(),
Outputs(),
[Task(name='parsed task')],
[Task(name='parsed task')],
1,
),
# no archive sections (empty generated task)
(None, None, None, None, [Task(name='generated task')], 1),
# only one section in archive.data
(ModelSystem(), None, None, None, [Task(name='generated task')], 1),
# another section in archive.data
(None, ModelMethod(), None, None, [Task(name='generated task')], 1),
# only two sections in archive.data
(
ModelSystem(),
ModelMethod(),
None,
None,
[Task(name='generated task')],
1,
),
# all sections in archive.data, so generated task has inputs and outputs
(
ModelSystem(),
ModelMethod(),
Outputs(),
None,
[
Task(
name='generated task',
inputs=[
Link(name='Input Model System', section=ModelSystem()),
Link(name='Input Model Method', section=ModelMethod()),
],
outputs=[
Link(name='Output Data', section=Outputs()),
],
)
],
1,
),
# Outputs is SCFOutputs but no scf_steps
(
ModelSystem(),
ModelMethod(),
SCFOutputs(),
None,
[
Task(
name='generated task',
inputs=[
Link(name='Input Model System', section=ModelSystem()),
Link(name='Input Model Method', section=ModelMethod()),
],
outputs=[
Link(name='Output Data', section=SCFOutputs()),
],
)
],
1,
),
# 3 scf_steps
(
ModelSystem(),
ModelMethod(),
SCFOutputs(scf_steps=[Outputs(), Outputs(), Outputs()]),
None,
[
Task(
name='generated task',
inputs=[
Link(name='Input Model System', section=ModelSystem()),
Link(name='Input Model Method', section=ModelMethod()),
],
outputs=[
Link(
name='Output Data',
section=SCFOutputs(
scf_steps=[Outputs(), Outputs(), Outputs()]
),
),
],
)
],
3,
),
],
)
def test_normalize(
self,
model_system: Optional[ModelSystem],
model_method: Optional[ModelMethod],
outputs: Optional[Outputs],
tasks: list[Task],
result_task: list[Task],
result_n_scf_steps: int,
):
"""
Test the `normalize` method of the `SinglePoint` section.
"""
assert True
archive = EntryArchive()
simulation = generate_simulation(
model_system=model_system, model_method=model_method, outputs=outputs
)
archive.data = simulation
workflow = SinglePoint()
archive.workflow2 = workflow

if tasks is not None:
workflow.tasks = tasks

workflow.normalize(archive=archive, logger=logger)

if not result_task:
assert workflow.tasks == result_task
else:
single_point_task = workflow.tasks[0]
if not result_task[0].inputs:
assert isinstance(single_point_task, Task)
assert not single_point_task.inputs and not single_point_task.outputs
else:
assert single_point_task.inputs[0].name == result_task[0].inputs[0].name
assert single_point_task.inputs[1].name == result_task[0].inputs[1].name
assert (
single_point_task.outputs[0].name == result_task[0].outputs[0].name
)
assert workflow.n_scf_steps == result_n_scf_steps

1 comment on commit c382dd7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/nomad_simulations
   __init__.py4250%3–4
   _version.py11282%5–6
src/nomad_simulations/schema_packages
   __init__.py15287%57–59
   atoms_state.py1902189%31–33, 219–222, 246, 301–302, 370–371, 373, 555, 567–568, 629–633, 648–652, 659
   basis_set.py2402888%8–9, 122–133, 172–185, 208, 391–395, 417–418, 462–465, 584, 615, 617
   general.py89891%22–25, 139, 203, 313–314, 324
   model_method.py2657771%28–30, 189–192, 195–202, 294–295, 315, 336–357, 373–399, 402–419, 773, 784, 826–833, 871, 890, 970, 1027, 1102, 1216
   model_system.py3002392%43–45, 382, 577–580, 627–634, 808–809, 1030–1034, 1040–1041, 1049–1050, 1055, 1078
   numerical_settings.py2596176%30–32, 235, 237–238, 241–244, 248–249, 256–259, 268–271, 275–278, 280–283, 288–291, 297–300, 487–514, 589, 624–627, 651, 654, 699, 701–704, 708, 712, 759, 763–784, 839–840, 907
   outputs.py1201092%27–28, 270–273, 313–316, 341, 343, 380, 399
   physical_property.py102793%38–40, 220, 349–351
   variables.py861286%26–28, 116, 139, 163, 185, 207, 229, 251, 274, 294
src/nomad_simulations/schema_packages/properties
   band_gap.py51590%26–28, 153–154
   band_structure.py1232580%27–29, 250–283, 296, 303, 339–340, 343, 390–391, 396
   energies.py42979%25–27, 54, 75, 100, 121, 137, 152
   fermi_surface.py17476%25–27, 58
   forces.py22673%26–28, 55, 75, 98
   greens_function.py991387%25–27, 228–229, 232, 253–254, 257, 278–279, 282, 418
   hopping_matrix.py29583%25–27, 76, 112
   permittivity.py48883%25–27, 115–123
   spectral_profile.py26012851%27–29, 75–78, 113–116, 217–318, 374–386, 411–414, 434, 439–442, 484–520, 544, 591–594, 610–611, 616–622
   thermodynamics.py752764%25–27, 53, 74, 90, 99, 108, 119, 128, 155, 165, 175, 190–192, 195, 211, 231–233, 236, 252, 272–274, 277
src/nomad_simulations/schema_packages/utils
   utils.py711579%27–32, 86–95, 104–105, 110, 113
src/nomad_simulations/schema_packages/workflow
   base_workflows.py56395%23–24, 169
   dft_plus_tb.py513629%23–24, 80–92, 98–119, 130–136, 139–166
   single_point.py44393%26–27, 92
TOTAL268354080% 

Tests Skipped Failures Errors Time
447 0 💤 0 ❌ 0 🔥 4.193s ⏱️

Please sign in to comment.