Skip to content

Commit

Permalink
adding the Kinds property.
Browse files Browse the repository at this point in the history
Issue: recursion if call get_kinds from the Kinds validator.
  • Loading branch information
mikibonacci committed Feb 23, 2024
1 parent b05dfce commit 1af101e
Show file tree
Hide file tree
Showing 10 changed files with 1,095 additions and 655 deletions.
181 changes: 147 additions & 34 deletions docs/source/user_guide/structuredata/user_guide_SData.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"output_type": "stream",
"text": [
"The whole list of currently supported properties is: \n",
"['cell', 'symbols', 'pbc', 'charge', 'positions', 'mass', 'custom']\n"
"['symbols', 'cell', 'mass', 'charge', 'positions', 'kinds', 'custom', 'pbc']\n"
]
}
],
Expand All @@ -130,7 +130,7 @@
{
"data": {
"text/plain": [
"<StructureData: uuid: 04631aff-7725-43e3-b4d0-9a307d3af3a2 (unstored)>"
"<StructureData: uuid: 2b7cd70e-8a33-4666-8043-d80df3e373b3 (unstored)>"
]
},
"execution_count": 4,
Expand Down Expand Up @@ -160,7 +160,7 @@
"output_type": "stream",
"text": [
"The cell property class: \n",
"parent=<StructureData: uuid: 04631aff-7725-43e3-b4d0-9a307d3af3a2 (unstored)> value=[[3.5, 0.0, 0.0], [0.0, 3.5, 0.0], [0.0, 0.0, 3.5]] domain='global'\n",
"parent=<StructureData: uuid: 2b7cd70e-8a33-4666-8043-d80df3e373b3 (unstored)> value=[[3.5, 0.0, 0.0], [0.0, 3.5, 0.0], [0.0, 0.0, 3.5]] domain='global'\n",
"\n",
"The cell property value: \n",
"[[3.5, 0.0, 0.0], [0.0, 3.5, 0.0], [0.0, 0.0, 3.5]]\n",
Expand All @@ -187,7 +187,7 @@
"output_type": "stream",
"text": [
"The positions property class: \n",
"parent=<StructureData: uuid: 04631aff-7725-43e3-b4d0-9a307d3af3a2 (unstored)> domain='intra-site' value=[[0.0, 0.0, 0.0], [1.5, 1.5, 1.5]]\n",
"parent=<StructureData: uuid: 2b7cd70e-8a33-4666-8043-d80df3e373b3 (unstored)> domain='intra-site' value=[[0.0, 0.0, 0.0], [1.5, 1.5, 1.5]]\n",
"\n",
"The positions property value: \n",
"[[0.0, 0.0, 0.0], [1.5, 1.5, 1.5]]\n",
Expand All @@ -214,7 +214,7 @@
"output_type": "stream",
"text": [
"Stored properties are: \n",
"['cell', 'pbc', 'symbols', 'positions']\n"
"['symbols', 'cell', 'mass', 'positions', 'pbc']\n"
]
}
],
Expand Down Expand Up @@ -259,7 +259,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand All @@ -270,7 +270,7 @@
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[9], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mstructure\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mproperties\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcell\u001b[49m \u001b[38;5;241m=\u001b[39m [[\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m],[\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m],[\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m]]\n",
"File \u001b[0;32m~/Envs/aiida-muonic/codes/aiida-atomistic/src/aiida_atomistic/data/structure/properties/property_utils.py:112\u001b[0m, in \u001b[0;36mPropertyMixinMetaclass.__new__.<locals>.<lambda>\u001b[0;34m(self, pname, pvalue)\u001b[0m\n\u001b[1;32m 106\u001b[0m func_get \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m \u001b[38;5;28mself\u001b[39m, type_hint\u001b[38;5;241m=\u001b[39mtype_hint, attr\u001b[38;5;241m=\u001b[39mattr: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_template_property(type_hint\u001b[38;5;241m=\u001b[39mtype_hint, attr\u001b[38;5;241m=\u001b[39mattr)\n\u001b[1;32m 108\u001b[0m \u001b[38;5;66;03m# We do not allow to set any property after the creation of the instance: \u001b[39;00m\n\u001b[1;32m 109\u001b[0m \u001b[38;5;66;03m#===> WE CAN DEACTIVATE THE METHOD, using the `allow_no_calls_decorator`.\u001b[39;00m\n\u001b[1;32m 110\u001b[0m \u001b[38;5;66;03m# Here below, we leave it there for now, in case it is needed in the future.\u001b[39;00m\n\u001b[1;32m 111\u001b[0m \u001b[38;5;66;03m#func_set = lambda self, type_hint=type_hint, attr=attr: self._template_property(type_hint=type_hint, attr=attr)\u001b[39;00m\n\u001b[0;32m--> 112\u001b[0m func_set \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m \u001b[38;5;28mself\u001b[39m, pname\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, pvalue\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m: \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_set_property\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 113\u001b[0m \u001b[38;5;28msetattr\u001b[39m(\u001b[38;5;28mcls\u001b[39m, attr, \u001b[38;5;28mproperty\u001b[39m(fget\u001b[38;5;241m=\u001b[39mfunc_get,fset\u001b[38;5;241m=\u001b[39mfunc_set))\n\u001b[1;32m 115\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m\n",
"File \u001b[0;32m~/Envs/aiida-muonic/codes/aiida-atomistic/src/aiida_atomistic/data/structure/properties/property_utils.py:113\u001b[0m, in \u001b[0;36mPropertyMixinMetaclass.__new__.<locals>.<lambda>\u001b[0;34m(self, pname, pvalue)\u001b[0m\n\u001b[1;32m 107\u001b[0m func_get \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m \u001b[38;5;28mself\u001b[39m, type_hint\u001b[38;5;241m=\u001b[39mtype_hint, attr\u001b[38;5;241m=\u001b[39mattr: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_template_property(type_hint\u001b[38;5;241m=\u001b[39mtype_hint, attr\u001b[38;5;241m=\u001b[39mattr)\n\u001b[1;32m 109\u001b[0m \u001b[38;5;66;03m# We do not allow to set any property after the creation of the instance: \u001b[39;00m\n\u001b[1;32m 110\u001b[0m \u001b[38;5;66;03m#===> WE CAN DEACTIVATE THE METHOD, using the `allow_no_calls_decorator`.\u001b[39;00m\n\u001b[1;32m 111\u001b[0m \u001b[38;5;66;03m# Here below, we leave it there for now, in case it is needed in the future.\u001b[39;00m\n\u001b[1;32m 112\u001b[0m \u001b[38;5;66;03m#func_set = lambda self, type_hint=type_hint, attr=attr: self._template_property(type_hint=type_hint, attr=attr)\u001b[39;00m\n\u001b[0;32m--> 113\u001b[0m func_set \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m \u001b[38;5;28mself\u001b[39m, pname\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, pvalue\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m: \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_set_property\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 114\u001b[0m \u001b[38;5;28msetattr\u001b[39m(\u001b[38;5;28mcls\u001b[39m, attr, \u001b[38;5;28mproperty\u001b[39m(fget\u001b[38;5;241m=\u001b[39mfunc_get,fset\u001b[38;5;241m=\u001b[39mfunc_set))\n\u001b[1;32m 116\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m\n",
"File \u001b[0;32m~/Envs/aiida-muonic/codes/aiida-atomistic/src/aiida_atomistic/data/structure/properties/property_utils.py:38\u001b[0m, in \u001b[0;36mallow_no_calls_decorator.<locals>.wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[38;5;28mlocals\u001b[39m \u001b[38;5;241m=\u001b[39m frame\u001b[38;5;241m.\u001b[39mf_back\u001b[38;5;241m.\u001b[39mf_locals\n\u001b[1;32m 37\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlocals\u001b[39m\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mself\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;129;01mis\u001b[39;00m args[\u001b[38;5;241m0\u001b[39m]:\n\u001b[0;32m---> 38\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAfter the initialization, `properties` and its attributes are read-only!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 39\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCalled from this class!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 40\u001b[0m func(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"\u001b[0;31mNotImplementedError\u001b[0m: After the initialization, `properties` and its attributes are read-only!"
]
Expand Down Expand Up @@ -304,7 +304,8 @@
"{'cell': {'value': [[3.5, 0.0, 0.0], [0.0, 3.5, 0.0], [0.0, 0.0, 3.5]]},\n",
" 'pbc': {'value': [True, True, True]},\n",
" 'positions': {'value': [[0.0, 0.0, 0.0], [1.5, 1.5, 1.5]]},\n",
" 'symbols': {'value': ['Li', 'Li']}}"
" 'symbols': {'value': ['Li', 'Li']},\n",
" 'mass': {'value': [6.941, 6.941]}}"
]
},
"execution_count": 9,
Expand All @@ -323,6 +324,20 @@
"Here below and example where we want to change the dimensionality of the structure: we elongate the cell along Z and we fix the pbc property consistently."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The `get_kinds()` method\n",
"\n",
"It is possible to get a list of kinds using the `get_kinds` method. \n",
"This will generate the corresponding predicted kinds for all the properties (the \"intra-site\" ones) \n",
"and then generate the list of global different kinds. \n",
"The default threshold used for each property can be found under the attribute ``\n",
"\n",
"This method should be used in the plugins which requires a kind-based definition of properties, e.g. the aiida-quantumespresso one."
]
},
{
"cell_type": "code",
"execution_count": 10,
Expand All @@ -348,31 +363,17 @@
"print(f\"The cell property value: \\n{new_structure.properties.cell.value}\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The `get_kinds()` method\n",
"\n",
"It is possible to get a list of kinds using the `get_kinds` method. \n",
"This will generate the corresponding predicted kinds for all the properties (the \"intra-site\" ones) \n",
"and then generate the list of global different kinds. \n",
"The default threshold used for each property can be found under the attribute ``\n",
"\n",
"This method should be used in the plugins which requires a kind-based definition of properties, e.g. the aiida-quantumespresso one."
]
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Li0', 'Li1']\n",
"{'charge': [1.0, 0.0], 'mass': [6.941, 6.941]}\n"
"{'mass': {'value': [6.941, 6.941]}, 'charge': {'value': [1.0, 0.0]}}\n"
]
}
],
Expand Down Expand Up @@ -416,7 +417,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand All @@ -425,7 +426,7 @@
"0.1"
]
},
"execution_count": 30,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -436,20 +437,20 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Li0', 'Li1']\n",
"{'charge': [1.0, 0.0], 'mass': [6.941, 6.941]}\n"
"['Li0', 'Li0']\n",
"{'mass': {'value': [6.941, 6.941]}, 'charge': {'value': [0.0, 0.0]}}\n"
]
}
],
"source": [
"kinds, kinds_values = structure.get_kinds(custom_thr={\"charge\":0.6})\n",
"kinds, kinds_values = structure.get_kinds(custom_thr={\"charge\":2})\n",
"\n",
"print(kinds)\n",
"print(kinds_values)"
Expand All @@ -468,15 +469,15 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Li1', 'Li2', 'Cu1', 'Cu1']\n",
"{'charge': [1.0, 0.0], 'mass': [6.941, 6.941]}\n"
"{'mass': {'value': [6.941, 6.941]}, 'charge': {'value': [1.0, 0.0]}}\n"
]
}
],
Expand All @@ -496,15 +497,15 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Li0', 'Li0']\n",
"{'mass': [6.941, 6.941]}\n"
"{'mass': {'value': [6.941, 6.941]}}\n"
]
}
],
Expand All @@ -514,6 +515,118 @@
"print(kinds)\n",
"print(kinds_values)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is possible to combine the `to_dict` and the `get_kinds` methods, in such a way to have a ready-to-use dictionary with also the kinds, automatically generated:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'cell': {'value': [[3.5, 0.0, 0.0], [0.0, 3.5, 0.0], [0.0, 0.0, 3.5]]},\n",
" 'pbc': {'value': [True, True, True]},\n",
" 'positions': {'value': [[0.0, 0.0, 0.0], [1.5, 1.5, 1.5]]},\n",
" 'symbols': {'value': ['Li', 'Li']},\n",
" 'mass': {'value': [6.941, 6.941]},\n",
" 'charge': {'value': [0.0, 0.0]},\n",
" 'kinds': {'value': ['Li0', 'Li0']}}"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_properties = structure.to_dict(generate_kinds= True, kinds_exclude=['mass'],kinds_thresholds={\"charge\":1.5})\n",
"new_properties"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"structure_with_kinds = StructureData(properties=new_properties)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Kinds(parent=<StructureData: uuid: 89aa0c75-d80f-4c9f-bfce-d7079852ead0 (unstored)>, domain='intra-site', value=['Li0', 'Li0'])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"structure_with_kinds.properties.kinds"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<StructureData: uuid: 89aa0c75-d80f-4c9f-bfce-d7079852ead0 (pk: 13824)>"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"structure_with_kinds.store()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"loaded_structure_kinds = orm.load_node(structure_with_kinds.pk)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Kinds(parent=<StructureData: uuid: 89aa0c75-d80f-4c9f-bfce-d7079852ead0 (pk: 13824)>, domain='intra-site', value=['Li0', 'Li0'])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"loaded_structure_kinds.properties.kinds"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 1af101e

Please sign in to comment.