xtal2png
xtal2png: A Python package for representing crystal structure as PNG files - Published in JOSS (2022)
Science Score: 95.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 1 DOI reference(s) in JOSS metadata -
✓Academic publication links
Links to: joss.theoj.org, zenodo.org -
✓Committers with academic emails
1 of 5 committers (20.0%) from academic institutions -
○Institutional organization owner
-
✓JOSS paper metadata
Published in Journal of Open Source Software
Keywords
Keywords from Contributors
Repository
Encode/decode a crystal structure to/from a grayscale PNG image for direct use with image-based machine learning models such as Palette.
Basic Info
- Host: GitHub
- Owner: sparks-baird
- License: mit
- Language: Python
- Default Branch: main
- Homepage: https://xtal2png.readthedocs.io/
- Size: 21.2 MB
Statistics
- Stars: 37
- Watchers: 3
- Forks: 3
- Open Issues: 29
- Releases: 19
Topics
Metadata Files
README.md
<!-- These are examples of badges you might also want to add to your README. Update the URLs accordingly.
-->
⚠️ Manuscript and results using a generative model coming soon ⚠️
xtal2png 
Encode/decode a crystal structure to/from a grayscale PNG image for direct use with image-based machine learning models such as Google's Imagen.
The latest advances in machine learning are often in natural language such as with LSTMs and transformers or image processing such as with GANs, VAEs, and guided diffusion models. Encoding/decoding crystal structures via grayscale PNG images is akin to making/reading a QR code for crystal structures. This allows you, as a materials informatics practitioner, to get streamlined results for new state-of-the-art image-based machine learning models applied to crystal structure. Let's take Google's text-to-image diffusion model, Imagen (unofficial), which can also be used as an image-to-image model. Rather than dig into the code spending hours, days, or weeks modifying, debugging, and playing GitHub phone tag with the developers before you can (maybe) get preliminary results, xtal2png lets you get those results using the default instructions on the repository.
After getting preliminary results, you get to decide whether it's worth it to you to take on the higher-cost/higher-expertise task of modifying the codebase and using a more customized approach. Or, you can stick with the results of xtal2png. It's up to you!
Getting Started
Installation
bash
conda create -n xtal2png -c conda-forge xtal2png m3gnet
conda activate xtal2png
NOTE:
m3gnetis an optional dependency that performs surrogate DFT relaxation.
Example
Here, we use the top-level
XtalConverter
class with and without optional relaxation via
m3gnet.
```python
example_structures is a list of pymatgen.core.structure.Structure objects
from xtal2png import XtalConverter, example_structures
xc = XtalConverter(relaxondecode=False) data = xc.xtal2png(examplestructures, show=True, save=True) decodedstructures = xc.png2xtal(data, save=False) len(decoded_structures) 2
xc = XtalConverter(relaxondecode=True) data = xc.xtal2png(examplestructures, show=True, save=True) relaxeddecodedstructures = xc.png2xtal(data, save=False) len(relaxeddecoded_structures) 2
```
Output
python
print(example_structures[0], decoded_structures[0], relaxed_decoded_structures[0])
| Original |
|---|
| ```python Structure Summary Lattice abc : 5.033788 11.523021 10.74117 angles : 90.0 90.0 90.0 volume : 623.0356027127609 A : 5.033788 0.0 3.0823061808931787e-16 B : 1.8530431062799525e-15 11.523021 7.055815392078867e-16 C : 0.0 0.0 10.74117 PeriodicSite: Zn2+ (0.9120, 5.7699, 9.1255) [0.1812, 0.5007, 0.8496] PeriodicSite: Zn2+ (4.1218, 5.7531, 1.6156) [0.8188, 0.4993, 0.1504] ... ``` |
| Decoded |
|---|
| ```python Structure Summary Lattice abc : 5.0250980392156865 11.533333333333331 10.8 angles : 90.0 90.0 90.0 volume : 625.9262117647058 A : 5.0250980392156865 0.0 0.0 B : 0.0 11.533333333333331 0.0 C : 0.0 0.0 10.8 PeriodicSite: Zn (0.9016, 5.7780, 3.8012) [0.1794, 0.5010, 0.3520] PeriodicSite: Zn (4.1235, 5.7554, 6.9988) [0.8206, 0.4990, 0.6480] ... ``` |
| Relaxed Decoded |
|---|
| ```python Structure Summary Lattice abc : 5.026834307381214 11.578854613685237 10.724087971087924 angles : 90.0 90.0 90.0 volume : 624.1953646135236 A : 5.026834307381214 0.0 0.0 B : 0.0 11.578854613685237 0.0 C : 0.0 0.0 10.724087971087924 PeriodicSite: Zn (0.9050, 5.7978, 3.7547) [0.1800, 0.5007, 0.3501] PeriodicSite: Zn (4.1218, 5.7810, 6.9693) [0.8200, 0.4993, 0.6499] ... ``` |
The before and after structures match within an expected tolerance; note the round-off error due to encoding numerical data as RGB images which has a coarse resolution of approximately 1/255 = 0.00392. Note also that the decoded version lacks charge states. The QR-code-like intermediate PNG image is also provided in original size and a scaled version for a better viewing experience:
| 64x64 pixels | Scaled for Better Viewing (tool credit) | Legend |
| ----------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
|
|
|
|
Additional examples can be found in the docs.
Limitations and Design Considerations
There are some limitations and design considerations for xtal2png. Here, we cover round-off error, image dimensions, contextual features, and customization.
Round-off
While the round-off
error is a necessary evil for encoding to a PNG file format, the unrounded NumPy arrays
can be used directly instead if supported by the image model of interest via
structures_to_arrays and arrays_to_structures.
Image dimensions
We choose a
$64\times64$ representation by default which supports up to 52 sites within a unit cell.
The maximum number of sites max_sites can be adjusted which changes the size of the
representation. A square representation is used for greater compatibility with the
common limitation of image-based models supporting only square image arrays. The choice
of the default sidelength as a base-2 number (i.e. $2^6$) reflects common conventions of
low-resolution images for image-based machine learning tasks.
Contextual features
While the
distance matrix does not directly contribute to the reconstruction in the current
implementation of xtal2png, it serves a number of purposes. First, similar to the unit
cell volume and space group information, it can provide additional guidance to the
algorithm. A corresponding example would be the role of background vs. foreground in
classification of wolves vs. huskies; oftentimes classification algorithms will pay
attention to the background (such as presence of snow) in predicting the animal class.
Likewise, providing contextual information such as volume, space group, and a distance
matrix is additional information that can help the models to capture the essence of
particular crystal structures. In a future implementation, we plan to reconstruct
Euclidean coordinates from the distance matrices and homogenize (e.g. via weighted
averaging) the explicit fractional coordinates with the reconstructed coordinates.
Customization
See the docs for the full list of customizable parameters that XtalConverter takes.
Installation
PyPI (pip) installation
Create and activate a new conda environment named xtal2png (-n) with python==3.9.* or your preferred Python version, then install xtal2png via pip.
bash
conda create -n xtal2png python==3.9.*
conda activate xtal2png
pip install xtal2png
Editable installation
In order to set up the necessary environment:
- clone and enter the repository via:
bash
git clone https://github.com/sparks-baird/xtal2png.git
cd xtal2png
- create and activate a new conda environment (optional, but recommended)
bash
conda env create --name xtal2png python==3.9.*
conda activate xtal2png
- perform an editable (
-e) installation in the current directory (.):
bash
pip install -e .
NOTE: Some changes, e.g. in
setup.cfg, might require you to runpip install -e .again.
Optional and needed only once after git clone:
- install several pre-commit git hooks with:
bash
pre-commit install
# You might also want to run `pre-commit autoupdate`
and checkout the configuration under .pre-commit-config.yaml.
The -n, --no-verify flag of git commit can be used to deactivate pre-commit hooks temporarily.
- install nbstripout git hooks to remove the output cells of committed notebooks with:
bash
nbstripout --install --attributes notebooks/.gitattributes
This is useful to avoid large diffs due to plots in your notebooks.
A simple nbstripout --uninstall will revert these changes.
Then take a look into the scripts and notebooks folders.
Command Line Interface (CLI)
Make sure to install the package first per the installation instructions above. Here is how to access the help for the CLI and a few examples to get you started.
Help
You can see the usage information of the xtal2png CLI script via:
bash
xtal2png --help
```bash Usage: xtal2png [OPTIONS]
xtal2png command line interface.
Options: --version Show version. -p, --path PATH Crystallographic information file (CIF) filepath (extension must be .cif or .CIF) or path to directory containing .cif files or processed PNG filepath or path to directory containing processed .png files (extension must be .png or .PNG). Assumes CIFs if --encode flag is used. Assumes PNGs if --decode flag is used. -s, --save-dir PATH Encode CIF files as PNG images. --encode Encode CIF files as PNG images. --decode Decode PNG images to CIF files. -v, --verbose TEXT Set loglevel to INFO. -vv, --very-verbose TEXT Set loglevel to INFO. --help Show this message and exit. ```
Examples
To encode a single CIF file located at src/xtal2png/utils/Zn2B2PbO6.cif as a PNG and save the PNG to the tmp directory:
bash
xtal2png --encode --path src/xtal2png/utils/Zn2B2PbO6.cif --save-dir tmp
To encode all CIF files contained in the src/xtal2png/utils directory as a PNG and
save corresponding PNGs to the tmp directory:
bash
xtal2png --encode --path src/xtal2png/utils --save-dir tmp
To decode a single structure-encoded PNG file located at
data/preprocessed/Zn8B8Pb4O24,volume=623,uid=b62a.png as a CIF file and save the CIF
file to the tmp directory:
bash
xtal2png --decode --path data/preprocessed/Zn8B8Pb4O24,volume=623,uid=b62a.png --save-dir tmp
To decode all structure-encoded PNG file contained in the data/preprocessed directory as CIFs and save the CIFs to the tmp directory:
bash
xtal2png --decode --path data/preprocessed --save-dir tmp
Note that the save directory (e.g. tmp) including any parents (e.g. ab/cd/tmp) will
be created automatically if the directory does not already exist.
Project Organization
├── AUTHORS.md <- List of developers and maintainers.
├── CHANGELOG.md <- Changelog to keep track of new features and fixes.
├── CONTRIBUTING.md <- Guidelines for contributing to this project.
├── Dockerfile <- Build a docker container with `docker build .`.
├── LICENSE.txt <- License as chosen on the command-line.
├── README.md <- The top-level README for developers.
├── configs <- Directory for configurations of model & application.
├── data
│ ├── external <- Data from third party sources.
│ ├── interim <- Intermediate data that has been transformed.
│ ├── preprocessed <- The final, canonical data sets for modeling.
│ └── raw <- The original, immutable data dump.
├── docs <- Directory for Sphinx documentation in rst or md.
├── environment.yml <- The conda environment file for reproducibility.
├── models <- Trained and serialized models, model predictions,
│ or model summaries.
├── notebooks <- Jupyter notebooks. Naming convention is a number (for
│ ordering), the creator's initials and a description,
│ e.g. `1.0-fw-initial-data-exploration`.
├── pyproject.toml <- Build configuration. Don't change! Use `pip install -e .`
│ to install for development or to build `tox -e build`.
├── references <- Data dictionaries, manuals, and all other materials.
├── reports <- Generated analysis as HTML, PDF, LaTeX, etc.
│ └── figures <- Generated plots and figures for reports.
├── scripts <- Analysis and production scripts which import the
│ actual PYTHON_PKG, e.g. train_model.
├── setup.cfg <- Declarative configuration of your project.
├── setup.py <- [DEPRECATED] Use `python setup.py develop` to install for
│ development or `python setup.py bdist_wheel` to build.
├── src
│ └── xtal2png <- Actual Python package where the main functionality goes.
├── tests <- Unit tests which can be run with `pytest`.
├── .coveragerc <- Configuration for coverage reports of unit tests.
├── .isort.cfg <- Configuration for git hook that sorts imports.
└── .pre-commit-config.yaml <- Configuration of pre-commit git hooks.
Note on PyScaffold
This project has been set up using PyScaffold 4.2.1 and the dsproject extension 0.7.1.
To create the same starting point for this repository, as of 2022-06-01 on Windows you will need the development versions of PyScaffold and extensions, however this will not be necessary once certain bugfixes have been introduced in the next stable releases:
bash
pip install git+https://github.com/pyscaffold/pyscaffold.git git+https://github.com/pyscaffold/pyscaffoldext-dsproject.git git+https://github.com/pyscaffold/pyscaffoldext-markdown.git
The following pyscaffold command creates a starting point for this repository:
bash
putup xtal2png --github-actions --markdown --dsproj
Alternatively, you can edit a file interactively and update and uncomment relevant lines, which saves some of the additional setup:
bash
putup --interactive xtal2png
Attributions
- @michaeldalverson for iterating through various representations during extensive work with crystal GANs. The base representation for
xtal2png(see #output) closely follows a recent iteration (2022-06-13), taking the first layer ($1\times64\times64$) of the $4\times64\times64$ representation and replacing a buffer column/row of zeros with unit cell volume.
Owner
- Name: Sparks/Baird Materials Informatics
- Login: sparks-baird
- Kind: organization
- Email: sterling.baird@utah.edu
- Location: United States of America
- Repositories: 63
- Profile: https://github.com/sparks-baird
Sterling Baird and Taylor Sparks Materials Informatics Projects
JOSS Publication
xtal2png: A Python package for representing crystal structure as PNG files
Authors
Laboratory of Molecular Simulation (LSMO), Institut des Sciences et Ingénierie Chimique, École Polytechnique Fédérale de Lausanne, Switzerland
Materials Science & Engineering, University of Utah, USA, Computer Science, University of Southern California, USA
Tags
materials informatics crystal structure computer vision image-based predictionsGitHub Events
Total
- Watch event: 3
Last Year
- Watch event: 3
Committers
Last synced: 5 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| sgbaird | s****d@i****m | 499 |
| Kevin Maik Jablonka | k****a@e****h | 59 |
| Hasan Sayeed | h****3@g****m | 6 |
| dependabot[bot] | 4****] | 3 |
| github-actions[bot] | 4****] | 2 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 4 months ago
All Time
- Total issues: 46
- Total pull requests: 64
- Average time to close issues: 16 days
- Average time to close pull requests: about 16 hours
- Total issue authors: 3
- Total pull request authors: 4
- Average comments per issue: 2.61
- Average comments per pull request: 0.39
- Merged pull requests: 60
- Bot issues: 0
- Bot pull requests: 2
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- sgbaird (38)
- PeterKraus (5)
- kjappelbaum (3)
Pull Request Authors
- sgbaird (49)
- kjappelbaum (10)
- dependabot[bot] (2)
- hasan-sayeed (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 3
-
Total downloads:
- pypi 50 last-month
-
Total dependent packages: 1
(may contain duplicates) -
Total dependent repositories: 1
(may contain duplicates) - Total versions: 58
- Total maintainers: 1
proxy.golang.org: github.com/sparks-baird/xtal2png
- Documentation: https://pkg.go.dev/github.com/sparks-baird/xtal2png#section-documentation
- License: mit
-
Latest release: v0.9.6
published over 2 years ago
Rankings
pypi.org: xtal2png
Encode and decode crystal structures via portable networks graphics (PNG) files.
- Homepage: https://github.com/sparks-baird/xtal2png/
- Documentation: https://xtal2png.readthedocs.io
- License: MIT
-
Latest release: 0.9.4
published over 3 years ago
Rankings
Maintainers (1)
conda-forge.org: xtal2png
- Homepage: https://github.com/sparks-baird/xtal2png/
- License: MIT
-
Latest release: 0.7.0
published over 3 years ago
Rankings
Dependencies
- BobAnkh/auto-generate-changelog master composite
- actions/checkout v2 composite
- actions/checkout v2 composite
- actions/download-artifact v2 composite
- actions/setup-python v2 composite
- actions/upload-artifact v2 composite
- coverallsapp/github-action master composite
- actions/checkout v3 composite
- github/codeql-action/analyze v2 composite
- github/codeql-action/autobuild v2 composite
- github/codeql-action/init v2 composite
- actions/checkout v2 composite
- actions/upload-artifact v1 composite
- openjournals/openjournals-draft-action master composite
- condaforge/mambaforge latest build
- ipykernel *
- myst-parser *
- nbsphinx *
- nbsphinx-link *
- sphinx >=3.2.1
- sphinx_copybutton *
- sphinx_rtd_theme *
- importlib_metadata ==4.11.4
- kaleido *
- numpy ==1.22.4
- pillow ==9.3.0
- plotly *
- pymatgen ==2022.5.19