https://github.com/basf/pytopas
Bruker's TOPAS X-ray diffraction calculations parser
https://github.com/basf/pytopas
materials-design materials-informatics materials-science x-ray-diffraction x-ray-images x-ray-physics x-ray-spectroscopy
Last synced: 10 days ago
JSON representation
Bruker's TOPAS X-ray diffraction calculations parser
- Host: GitHub
- URL: https://github.com/basf/pytopas
- Owner: basf
- License: bsd-3-clause
- Created: 2022-02-24T23:49:42.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-04-16T08:51:26.000Z (almost 2 years ago)
- Last Synced: 2024-05-14T00:06:05.275Z (almost 2 years ago)
- Topics: materials-design, materials-informatics, materials-science, x-ray-diffraction, x-ray-images, x-ray-physics, x-ray-spectroscopy
- Language: Python
- Homepage:
- Size: 1.1 MB
- Stars: 5
- Watchers: 2
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pytopas
This is an early version of Bruker's TOPAS macro language parser. This macro language is used by [commercial](https://www.bruker.com/de/products-and-solutions/diffractometers-and-x-ray-microscopes/x-ray-diffractometers/diffrac-suite-software.html) and [academic](http://www.topas-academic.net) TOPAS code. We have compared two canonical parsing approaches for Python, `lark` vs. `pyparsing`, and ended up with `pyparsing` being more convenient for debugging.
## Installation
`pip install .`
Install package with optional dependencies with `pip install -e .[lint,test,release]`
## Usage
Parse a TOPAS macro language node with:
```sh
echo "xdd { 42 }" | topas2json - | json2topas -
```
More specifically, parse TOPAS input and convert it to JSON with:
```python
import json
from pytopas import TOPASParser
src = "a(b,c)"
tree = TOPASParser.parse(src)
print(json.dumps(tree))
```
Convert parser's JSON-encoded TOPAS code back into the TOPAS input format:
```python
import json
from pytopas import TOPASParseTree
input_json = """
["topas",
["formula",
["func_call", "a",
["formula", ["p", {"n": ["parameter_name", "b"]}]],
["formula", ["p", {"n": ["parameter_name", "c"]}]]]]]
"""
serialized = json.loads(input_json)
src = TOPASParser.reconstruct(serialized)
print(src)
```
## CLI
After installing the package, two command line utilities will be available.
```
usage: topas2json [-h] [--ignore-warnings] file
Parse TOPAS input and output JSON
positional arguments:
file Path to TOPAS file or '-' for stdin input
options:
-h, --help show this help message and exit
--ignore-warnings Don't print parsing warnings
```
```
usage: json2topas [-h] file
Parse JSON input and output TOPAS
positional arguments:
file Path to JSON file or '-' for stdin input
options:
-h, --help show this help message and exit
```
## License
Author Sergey Korolev, Tilde Materials Informatics
Copyright 2023 BASF SE
BSD 3-Clause