-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ideaconsult/pydantic2
Pydantic2 migration & multidimensional matrices for nexus
- Loading branch information
Showing
18 changed files
with
32,328 additions
and
11,114 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 |
---|---|---|
@@ -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 |
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,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 | ||
} |
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,3 @@ | ||
import pyambit | ||
|
||
print("test") |
Oops, something went wrong.