Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deserializer #15

Merged
merged 3 commits into from
Jan 23, 2025
Merged

Add deserializer #15

merged 3 commits into from
Jan 23, 2025

Conversation

superstar54
Copy link
Member

@superstar54 superstar54 commented Jan 23, 2025

Fix #13: this PR allows the users to set custom de-serializers and serializers either as input or in the pythonjob.json configuration file.

If you want to pass AiiDA Data node as input, and the node does not have a value attribute, then one must provide a deserializer for it.

deserializer input for one calculation

from aiida import orm  # noqa: E402


def make_supercell(structure, n=2):
    return structure * [n, n, n]


structure = orm.StructureData(cell=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
structure.append_atom(position=(0.0, 0.0, 0.0), symbols="Li")

inputs = prepare_pythonjob_inputs(
    make_supercell,
    function_inputs={"structure": structure},
    deserializers={
        "aiida.orm.nodes.data.structure.StructureData": "aiida_pythonjob.data.deserializer.structure_data_to_atoms"
    },
    # override the default `AtomsData`
    serializers={
        "ase.atoms.Atoms": "aiida_pythonjob.data.serializer.atoms_to_structure_data"
    }

)
result, node = run_get_node(PythonJob, inputs=inputs)

where the serializer and deserializer are:

def structure_data_to_atoms(structure):
    return structure.get_ase()

def atoms_to_structure_data(structure):
    return orm.StructureData(ase=structure)

In this case, both the input and output data are StructureData, and the AtomsData is not used.

Configuration file for all calculation

One can set the deserializer in the pythonjob.json configuration file under .aiida folder

   {
       "deserializers": {
           "aiida.orm.nodes.data.structure.StructureData": "aiida_pythonjob.data.deserializer.structure_data_to_atoms"
       }
   }

The orm.List, orm.Dict and orm.StructureData data types already have built-in deserializers.

@superstar54 superstar54 force-pushed the fix/13/deserializer_config branch from 404dee6 to b57c36c Compare January 23, 2025 01:53
@codecov-commenter
Copy link

codecov-commenter commented Jan 23, 2025

Codecov Report

Attention: Patch coverage is 82.69231% with 18 lines in your changes missing coverage. Please review.

Project coverage is 87.88%. Comparing base (16c854c) to head (948c4d7).

Files with missing lines Patch % Lines
src/aiida_pythonjob/data/deserializer.py 82.92% 7 Missing ⚠️
src/aiida_pythonjob/data/serializer.py 91.66% 3 Missing ⚠️
src/aiida_pythonjob/calculations/pythonjob.py 75.00% 2 Missing ⚠️
src/aiida_pythonjob/launch.py 60.00% 2 Missing ⚠️
src/aiida_pythonjob/parsers/pythonjob.py 66.66% 2 Missing ⚠️
src/aiida_pythonjob/utils.py 75.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #15      +/-   ##
==========================================
- Coverage   88.73%   87.88%   -0.86%     
==========================================
  Files          14       14              
  Lines         586      652      +66     
==========================================
+ Hits          520      573      +53     
- Misses         66       79      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@superstar54 superstar54 force-pushed the fix/13/deserializer_config branch from b57c36c to 104281c Compare January 23, 2025 01:57
@superstar54 superstar54 force-pushed the fix/13/deserializer_config branch 3 times, most recently from e8deea0 to c59d19e Compare January 23, 2025 03:26
@superstar54 superstar54 linked an issue Jan 23, 2025 that may be closed by this pull request
@superstar54 superstar54 force-pushed the fix/13/deserializer_config branch from c59d19e to c732449 Compare January 23, 2025 07:52
@superstar54 superstar54 merged commit 083f592 into main Jan 23, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Providing custom de-serializer for AiiDA data
2 participants