Skip to content

Commit

Permalink
Added more unit testing; split up unit testing files; added additiona…
Browse files Browse the repository at this point in the history
…l validation for several properties
  • Loading branch information
chrisiacovella committed Feb 15, 2025
1 parent 1e8fdcf commit 554a33c
Show file tree
Hide file tree
Showing 5 changed files with 489 additions and 274 deletions.
64 changes: 43 additions & 21 deletions modelforge-curate/modelforge/curate/examples/basic_usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"id": "a5bcd450-3d52-4a45-ab1a-f107e11df202",
"metadata": {},
"outputs": [],
Expand All @@ -155,6 +155,28 @@
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "711bec10-63ec-44a8-a368-f38bd3bb3577",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Positions(name='positions', value=array([[[1., 1., 1.],\n",
" [2., 2., 2.]]]), units=<Unit('nanometer')>, classification='per_atom', property_type='length', n_configs=1, n_atoms=2)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"positions"
]
},
{
"cell_type": "markdown",
"id": "144b4f9e-62b8-42b5-8e79-429e133574b4",
Expand All @@ -168,7 +190,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 12,
"id": "e03c8a4f-a49f-444f-8b3c-252d7373e77a",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -210,7 +232,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 13,
"id": "72291451-a62f-4780-b981-8fcd4127a9f1",
"metadata": {},
"outputs": [],
Expand All @@ -232,7 +254,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 14,
"id": "caf4f58d-502a-48ee-baec-e1fb99e094b0",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -265,7 +287,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 15,
"id": "56329f06-7440-44e9-82aa-4a1a38e1b874",
"metadata": {},
"outputs": [],
Expand All @@ -284,7 +306,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 16,
"id": "cf96ffd4-de45-427c-9937-19a3ab60bf41",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -322,7 +344,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 17,
"id": "38a2bed3-e521-49ae-a2ef-2ecd58d15b84",
"metadata": {},
"outputs": [
Expand All @@ -340,7 +362,7 @@
" 'meta_data': {}}"
]
},
"execution_count": 12,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -359,7 +381,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 18,
"id": "1020acc8-7b71-4ff9-819f-9964713c6314",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -398,7 +420,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 19,
"id": "1c990c88-4230-48b0-b622-a97030a3ea4a",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -591,7 +613,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 20,
"id": "bcb5c1dd-49dc-448a-a861-9a39c9b28c5d",
"metadata": {},
"outputs": [
Expand All @@ -601,7 +623,7 @@
"True"
]
},
"execution_count": 19,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -963,7 +985,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 30,
"id": "552fc18d-288d-40fd-8a7e-d17832989478",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -996,7 +1018,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 31,
"id": "f08f8aa5-f3ba-4cb9-91e9-e493513167f3",
"metadata": {},
"outputs": [],
Expand All @@ -1016,7 +1038,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 32,
"id": "82df5525-ce64-41d3-9d71-93b8fdfeb3a4",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1045,7 +1067,7 @@
"* meta_data: ([])"
]
},
"execution_count": 33,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1064,7 +1086,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 33,
"id": "7a8ce2cd-a380-43ea-9605-de6d0efe5b1f",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1131,14 +1153,14 @@
"outputs": [
{
"ename": "ValidationError",
"evalue": "1 validation error for Positions\n Value error, Unit angstrom ** 2 of positions are not compatible with the property type length.\n [type=value_error, input_value={'value': [[[1.0, 1.0, 1....<Unit('angstrom ** 2')>}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.8/v/value_error",
"evalue": "1 validation error for Positions\n Value error, Unit angstrom ** 2 of positions are not compatible with the property type length.\n [type=value_error, input_value={'value': [[[1.0, 1.0, 1....<Unit('angstrom ** 2')>}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.10/v/value_error",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValidationError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[35], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m pos \u001b[38;5;241m=\u001b[39m \u001b[43mPositions\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1.0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1.0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1.0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m2.0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2.0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2.0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3.0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3.0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3.0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munits\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43munit\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mangstrom\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43munit\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mangstrom\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/anaconda3/envs/modelforge311/lib/python3.11/site-packages/pydantic/main.py:193\u001b[0m, in \u001b[0;36mBaseModel.__init__\u001b[0;34m(self, **data)\u001b[0m\n\u001b[1;32m 191\u001b[0m \u001b[38;5;66;03m# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks\u001b[39;00m\n\u001b[1;32m 192\u001b[0m __tracebackhide__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m--> 193\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__pydantic_validator__\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalidate_python\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mself_instance\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mValidationError\u001b[0m: 1 validation error for Positions\n Value error, Unit angstrom ** 2 of positions are not compatible with the property type length.\n [type=value_error, input_value={'value': [[[1.0, 1.0, 1....<Unit('angstrom ** 2')>}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.8/v/value_error"
"File \u001b[0;32m/opt/anaconda3/envs/modelforge311/lib/python3.12/site-packages/pydantic/main.py:214\u001b[0m, in \u001b[0;36mBaseModel.__init__\u001b[0;34m(self, **data)\u001b[0m\n\u001b[1;32m 212\u001b[0m \u001b[38;5;66;03m# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks\u001b[39;00m\n\u001b[1;32m 213\u001b[0m __tracebackhide__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m--> 214\u001b[0m validated_self \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__pydantic_validator__\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalidate_python\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mself_instance\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 215\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m validated_self:\n\u001b[1;32m 216\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(\n\u001b[1;32m 217\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA custom validator is returning a value other than `self`.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 218\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mReturning anything other than `self` from a top level model validator isn\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt supported when validating via `__init__`.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 219\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSee the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m 220\u001b[0m stacklevel\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m,\n\u001b[1;32m 221\u001b[0m )\n",
"\u001b[0;31mValidationError\u001b[0m: 1 validation error for Positions\n Value error, Unit angstrom ** 2 of positions are not compatible with the property type length.\n [type=value_error, input_value={'value': [[[1.0, 1.0, 1....<Unit('angstrom ** 2')>}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.10/v/value_error"
]
}
],
Expand Down Expand Up @@ -1435,7 +1457,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.12.8"
}
},
"nbformat": 4,
Expand Down
12 changes: 10 additions & 2 deletions modelforge-curate/modelforge/curate/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,11 @@ class TotalCharge(PropertyBaseModel):
def _check_charge_shape(self) -> Self:
if self.value.shape[1] != 1:
raise ValueError(
f"Shape of charge should be [n_configs, 1], found {len(self.value.shape)}"
f"Shape of charge should be [n_configs, 1], found {self.value.shape}"
)
if len(self.value.shape) != 2:
raise ValueError(
f"Shape of charge should be 2d, found {len(self.value.shape)}"
)
return self

Expand Down Expand Up @@ -688,7 +692,11 @@ class Polarizability(PropertyBaseModel):
def _check_polarizability_shape(self) -> Self:
if self.value.shape[1] != 1:
raise ValueError(
f"Shape of polarizability should be [n_configs, 1], found {len(self.value.shape)}"
f"Shape of polarizability should be [n_configs, 1], found {self.value.shape}"
)
if len(self.value.shape) != 2:
raise ValueError(
f"Shape of polarizability should be 2d, found {len(self.value.shape)}"
)
return self

Expand Down
Loading

0 comments on commit 554a33c

Please sign in to comment.