Skip to content

Commit

Permalink
Merge pull request #1 from ideaconsult/pydantic2
Browse files Browse the repository at this point in the history
Pydantic2 migration & multidimensional matrices for nexus
  • Loading branch information
kerberizer authored Aug 20, 2024
2 parents 2e50efb + 0c54f54 commit 655ba11
Show file tree
Hide file tree
Showing 18 changed files with 32,328 additions and 11,114 deletions.
40 changes: 40 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
repos:
- repo: https://github.com/python-poetry/poetry
rev: 1.8.3
hooks:
- id: poetry-check
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-docstring-first
- id: check-json
- id: check-merge-conflict
- id: debug-statements
- id: detect-private-key
- id: end-of-file-fixer
- id: name-tests-test
- id: pretty-format-json
args: [--autofix, --no-ensure-ascii]
- id: trailing-whitespace
- repo: https://github.com/facebook/usort
rev: v1.0.8
hooks:
- id: usort
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.8.0
hooks:
- id: black
args: [--preview]
- repo: https://github.com/pycqa/flake8
rev: 7.1.1
hooks:
- id: flake8
args: [--exit-zero]
verbose: true
additional_dependencies:
- flake8-bugbear == 24.4.26
- repo: https://github.com/adrienverge/yamllint
rev: v1.35.1
hooks:
- id: yamllint
98 changes: 98 additions & 0 deletions examples/demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from pyambit.datamodel import Substances, Study \n",
"import nexusformat.nexus.tree as nx\n",
"import os.path\n",
"import tempfile\n",
"# to_nexus is not added without this import\n",
"from pyambit import nexus_writer\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def query(url = \"https://apps.ideaconsult.net/gracious/substance/\" ,params = {\"max\" : 1}):\n",
" substances = None\n",
" headers = {'Accept': 'application/json'}\n",
" result = requests.get(url,params=params,headers=headers)\n",
" if result.status_code==200:\n",
" response = result.json()\n",
" substances = Substances.model_construct(**response)\n",
" for substance in substances.substance:\n",
" url_study = \"{}/study\".format(substance.URI)\n",
" study = requests.get(url_study,headers=headers)\n",
" if study.status_code==200:\n",
" response_study = study.json()\n",
" substance.study = Study.model_construct(**response_study).study\n",
"\n",
" return substances\n",
"\n",
"def write_studies_nexus(substances):\n",
" for substance in substances.substance:\n",
" for study in substance.study:\n",
" file = os.path.join(tempfile.gettempdir(), \"study_{}.nxs\".format(study.uuid))\n",
" nxroot = nx.NXroot()\n",
" try:\n",
" study.to_nexus(nxroot)\n",
" nxroot.save(file, mode=\"w\")\n",
" except Exception as err:\n",
" #print(\"error\",file,str(err))\n",
" print(file)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"try:\n",
" substances = query(params = {\"max\" : 10}) \n",
" _json = substances.model_dump(exclude_none=True)\n",
" new_substances = Substances.model_construct(**_json)\n",
" #test roundtrip\n",
" assert substances == new_substances\n",
"\n",
" file = os.path.join(tempfile.gettempdir(), \"remote.json\")\n",
" print(file)\n",
" with open(file, 'w', encoding='utf-8') as file:\n",
" file.write(substances.model_dump_json(exclude_none=True))\n",
" write_studies_nexus(substances)\n",
"except Exception as x:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
3 changes: 3 additions & 0 deletions examples/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pyambit

print("test")
Loading

0 comments on commit 655ba11

Please sign in to comment.