{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Validation\n", "\n", "There are multiple ways to check if a file or dataframe are compliant with the BDF recommendations using the `validate()` function. The validate function take as an argument:\n", "\n", "- a filepath\n", "- a URL\n", "- a dataframe\n", "\n", "It compares the content of the object with the BDF and returns a report. " ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "# Import the package\n", "import bdf" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "# Read from a local file path and convert to bdf\n", "filepath = \"../../data/SINTEF__LiGrR2032__2024-04-30__25degC__Landt.csv\"\n", "df, meta = bdf.read(filepath)\n", "df = df.to_pandas()\n", "df.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "# Validate the dataframe and print the report\n", "report = bdf.validate(df, report=True)\n", "print(report)" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "# Validate the file prior to bdf conversion\n", "report = bdf.validate(filepath, report=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "# Validate all reference BDF files in examples/reference\n", "from pathlib import Path\n", "\n", "candidates = [Path('examples/reference'), Path('reference'), Path('../examples/reference')]\n", "reference_dir = next((p.resolve() for p in candidates if p.exists()), candidates[0].resolve())\n", "files = sorted(reference_dir.glob('*.csv'))\n", "print(f'Found {len(files)} reference files in {reference_dir}')\n", "\n", "results = []\n", "for path in files:\n", " report = bdf.validate(path, report=False, raise_on_error=False)\n", " results.append((path.name, bool(report['ok']), report.get('missing', []), report.get('extras', [])))\n", "\n", "for name, ok, missing, extras in results:\n", " status = 'PASS' if ok else 'FAIL'\n", " print(f'{status} | {name} | missing={len(missing)} | extras={len(extras)}')\n", "\n", "n_pass = sum(1 for _, ok, _, _ in results if ok)\n", "print(f'\\nSummary: {n_pass}/{len(results)} passed')\n", "if n_pass != len(results):\n", " failed = [name for name, ok, _, _ in results if not ok]\n", " print('Failed files:', failed)\n" ] } ], "metadata": { "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.10.10" } }, "nbformat": 4, "nbformat_minor": 5 }