Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📝 Add a faq for custome validator (#1249)
- Loading branch information
Showing
4 changed files
with
173 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ faq/notebooks | |
faq/setup | ||
faq/import-schema | ||
faq/reference-field | ||
faq/validator | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Custom validators" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!lamin init --storage test-validator --schema bionty" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import lamindb as ln\n", | ||
"import lnschema_bionty as lb\n", | ||
"from lamin_utils import logger\n", | ||
"\n", | ||
"lb.settings.organism = \"human\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's use an AnnData as the dataset to validate:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"adata = ln.dev.datasets.anndata_human_immune_cells(populate_registries=True)\n", | ||
"adata" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Define validation criteria for an `AnnData`:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"validators = {\n", | ||
" \"var\": {\"index\": lb.Gene.ensembl_gene_id},\n", | ||
" \"obs\": {\n", | ||
" \"donor\": ln.ULabel.name,\n", | ||
" \"tissue\": lb.Tissue.name,\n", | ||
" \"cell_type\": lb.CellType.name,\n", | ||
" \"assay\": lb.ExperimentalFactor.name,\n", | ||
" },\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Run bulk validation:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"features = ln.Feature.lookup()\n", | ||
"\n", | ||
"for slot, slot_validators in validators.items():\n", | ||
" for name, validator in slot_validators.items():\n", | ||
" # registry model\n", | ||
" model = validator.field.model\n", | ||
"\n", | ||
" # validate index\n", | ||
" if name == \"index\":\n", | ||
" logger.print(f\"validating {slot}.{name}:\")\n", | ||
" index = adata.__getattribute__(slot).index\n", | ||
" validated = model.validate(index, validator)\n", | ||
" if validated.sum() == len(index):\n", | ||
" logger.print(\"🎉 PASS\")\n", | ||
"\n", | ||
" # validate columns\n", | ||
" else:\n", | ||
" logger.print(f\"\\nvalidating {slot}.{name}:\")\n", | ||
" # check if the column name exist\n", | ||
" if name not in adata.__getattribute__(slot).columns:\n", | ||
" logger.warning(f\"{slot}.{name} field is missing\")\n", | ||
" else:\n", | ||
" # check if a feature is registered for the column\n", | ||
" if not hasattr(features, name):\n", | ||
" logger.warning(f\"feature '{name}' is not registered\")\n", | ||
" # validate categorical labels in a column\n", | ||
" else:\n", | ||
" labels = adata.__getattribute__(slot)[name]\n", | ||
" validated = model.validate(labels, validator)\n", | ||
" if validated.sum() == len(labels):\n", | ||
" logger.print(\"🎉 PASS\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!lamin delete --force test-validator" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "py39", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.16" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters