Skip to content

Commit

Permalink
Rename and add doc string for utils: generate_archive_from_json
Browse files Browse the repository at this point in the history
  • Loading branch information
ka-sarthak committed Aug 14, 2024
1 parent 016c164 commit ca506e9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
37 changes: 36 additions & 1 deletion src/nomad_polymerization_reactions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,42 @@
from structlog.stdlib import BoundLogger


def generate_archive_from_llm_output(filepath: str, logger: 'BoundLogger' = None): # noqa: PLR0912
def generate_archive_from_json(filepath: str, logger: 'BoundLogger' = None): # noqa: PLR0912
"""
Generate an archive.yaml file from a JSON file coming from the LLM output.
Function expects a JSON of the following format:
```json
{
"file": "paper_0.json",
"monomer1_s": "C=C",
"monomer2_s": "C=O",
"monomer1": "ethylene",
"monomer2": "carbon monoxide",
"r_values": {
"constant_1": 22.0,
"constant_2": 0.0
},
"conf_intervals": {
"constant_conf_1": null,
"constant_conf_2": null
},
"temperature": 20.0,
"temperature_unit": "\u00b0C",
"solvent": null,
"method": "bulk",
"r-product": null,
"source": "https://doi.org/10.1002/pol.1963.110010415"
}
```
Args:
filepath (str): Path to the JSON file.
logger (BoundLogger): A structlog logger.
Returns:
dict: The dict used to generate archive.yaml file.
"""

class OrderedDumper(yaml.Dumper):
def represent_dict(self, data):
return self.represent_mapping('tag:yaml.org,2002:map', data.items())
Expand Down
2 changes: 2 additions & 0 deletions tests/data/processed_reactions/empty.archive.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data:
m_def: nomad_polymerization_reactions.schema_packages.mypackage.PolymerizationReaction
1 change: 1 addition & 0 deletions tests/data/processed_reactions/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
8 changes: 6 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest
import yaml
from nomad_polymerization_reactions.utils import generate_archive_from_llm_output
from nomad_polymerization_reactions.utils import generate_archive_from_json


@pytest.mark.parametrize(
Expand All @@ -20,10 +20,14 @@
'tests/data/processed_reactions/paper_5_reaction_1.archive.yaml'
),
},
{
'filepath': 'tests/data/processed_reactions/empty.json',
'reference': ('tests/data/processed_reactions/empty.archive.yaml'),
},
],
)
def test_generate_archive_from_llm_output(params):
output = generate_archive_from_llm_output(params['filepath'])
output = generate_archive_from_json(params['filepath'])
with open(params['reference']) as f:
reference = yaml.load(f, Loader=yaml.FullLoader)
assert output == reference
Expand Down

0 comments on commit ca506e9

Please sign in to comment.