Skip to content

Commit

Permalink
add notebook + notebook test
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Mar 11, 2022
1 parent d7552af commit d2dfa22
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 11 deletions.
33 changes: 22 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ name: tests

on:
push:
paths:
- '**.py' # only run workflow when source files changed
pull_request:
paths:
- '**.py'

jobs:
tests:
Expand All @@ -27,13 +23,28 @@ jobs:
#pip install nose #pytest # flake8
pip install pytest # flake8
pip install --quiet -r requirements.txt
# - name: Lint with flake8
# run: |
# # stop the build if there are Python syntax errors or undefined names
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
python -m pytest pyxtal_ff/test_all.py
#nosetests -s
notebooks:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8]

steps:
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install -U pip
#pip install nose #pytest # flake8
pip install pytest nbmake
pip install --quiet -r requirements.txt
- name: Test with pytest
run: |
pytest --nbmake ./examples
207 changes: 207 additions & 0 deletions examples/Si-LAMMPS/main.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"# Train a Model"
]
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [],
"source": [
"from pyxtal_ff import PyXtal_FF\n",
"import os\n",
"import urllib.request"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading the training and test data\n"
]
}
],
"source": [
"TrainData = \"training.json\"\n",
"TestData = \"test.json\"\n",
"\n",
"if not os.path.exists(TrainData):\n",
" if not os.path.exists('data'):\n",
" os.mkdir('data')\n",
" os.chdir('data')\n",
" print('Downloading the training and test data')\n",
"\n",
" url = \"https://raw.githubusercontent.com/materialsvirtuallab/mlearn/master/data/Si/\"\n",
"\n",
" urllib.request.urlretrieve(f\"{url}/{TrainData}\", TrainData)\n",
" urllib.request.urlretrieve(f\"{url}/{TestData}\", TestData)\n",
" os.chdir('..')"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [],
"source": [
"if True:\n",
" folder = 'Si-snap-zbl/'\n",
" descriptor = {'type': 'SNAP',\n",
" 'weights': {'Si': 1.0},\n",
" 'Rc': 5.0,\n",
" 'parameters': {'lmax': 3},\n",
" 'base_potential': {'inner': 1.5, 'outer': 2.0}, #zbl potential\n",
" 'ncpu': 1,\n",
" }\n",
"else:\n",
" # Not working so far\n",
" folder = 'Si-so3-zbl/'\n",
" descriptor = {'type': 'SO3',\n",
" 'weights': {'Si': 1.0},\n",
" 'Rc': 5.0,\n",
" 'parameters': {'lmax': 4, 'nmax': 3},\n",
" 'base_potential': {'inner': 1.5, 'outer': 2.0}, #zbl potential\n",
" 'ncpu': 1,\n",
" }"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [],
"source": [
"model = {'system' : ['Si'],\n",
" 'hiddenlayers': [12, 12],\n",
" 'path': folder,\n",
" #'restart': folder + '12-12-checkpoint.pth',\n",
" 'optimizer': {'method': 'lbfgs'},\n",
" 'force_coefficient': 2e-2,\n",
" 'stress_coefficient': 2e-3,\n",
" \"stress_group\": [\"Elastic\"],\n",
" 'alpha': 1e-6,\n",
" 'epoch': 1000,\n",
" }"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "code",
"execution_count": 11,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\n",
" ______ _ _ _ _______ _______ \n",
" (_____ \\ \\ \\ / / | | (_______|_______)\n",
" _____) ) _ \\ \\/ / |_ ____| | _____ _____ \n",
" | ____/ | | | ) (| _)/ _ | | | ___) | ___) \n",
" | | | |_| |/ /\\ \\ |_( ( | | |_______| | | | \n",
" |_| \\__ /_/ \\_\\___)_||_|_(_______)_| |_| \n",
" (____/ \n",
"\n",
"\n",
" A Python package for Machine Learning Interatomic Force Field\n",
" Developed by Zhu's group at University of Nevada Las Vegas\n",
" The source code is available at https://github.com/qzhu2017/FF-project\n",
"\n",
"\n",
"=========================== version 0.1.9 =============================\n",
"\n",
"Descriptor parameters:\n",
"type : SNAP\n",
"Rc : 5.0\n",
"cutoff : cosine\n",
"lmax : 3\n",
"rfac : 0.99363\n",
"\n",
"\n"
]
},
{
"ename": "UnboundLocalError",
"evalue": "local variable 'structure_dict' referenced before assignment",
"output_type": "error",
"traceback": [
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[1;31mUnboundLocalError\u001B[0m Traceback (most recent call last)",
"\u001B[1;32m~\\AppData\\Local\\Temp/ipykernel_16420/3131584846.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m 1\u001B[0m \u001B[0mff\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mPyXtal_FF\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mdescriptors\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mdescriptor\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mmodel\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mmodel\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 2\u001B[1;33m \u001B[0mff\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mrun\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mmode\u001B[0m\u001B[1;33m=\u001B[0m\u001B[1;34m'train'\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mTrainData\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mjoin\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"data\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mTrainData\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mTestData\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mjoin\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"data\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mTestData\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
"\u001B[1;32m~\\PycharmProjects\\PyXtal_FF\\pyxtal_ff\\__init__.py\u001B[0m in \u001B[0;36mrun\u001B[1;34m(self, mode, TrainData, TestData, mliap)\u001B[0m\n\u001B[0;32m 263\u001B[0m \u001B[1;32mif\u001B[0m \u001B[1;32mnot\u001B[0m \u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mexists\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m+\u001B[0m\u001B[1;34m'Train_db.dat'\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;32mand\u001B[0m \u001B[1;32mnot\u001B[0m \u001B[0mos\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mexists\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m+\u001B[0m\u001B[1;34m'Train_db.db'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 264\u001B[0m \u001B[0mtrainDB\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mDatabase\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mname\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m+\u001B[0m\u001B[1;34m'Train_db'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 265\u001B[1;33m \u001B[0mtrainDB\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mstore\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mTrainData\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m_descriptors\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;32mTrue\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m+\u001B[0m\u001B[1;34m'ase.db'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m 266\u001B[0m \u001B[1;32melse\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 267\u001B[0m \u001B[0mtrainDB\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mDatabase\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mname\u001B[0m\u001B[1;33m=\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mpath\u001B[0m\u001B[1;33m+\u001B[0m\u001B[1;34m'Train_db'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
"\u001B[1;32m~\\PycharmProjects\\PyXtal_FF\\pyxtal_ff\\utilities\\__init__.py\u001B[0m in \u001B[0;36mstore\u001B[1;34m(self, structure_file, function, storage, ase_db)\u001B[0m\n\u001B[0;32m 91\u001B[0m \u001B[1;31m# extract the structures and energy, forces, and stress information.\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 92\u001B[0m \u001B[1;32mif\u001B[0m \u001B[0mfmt\u001B[0m \u001B[1;33m==\u001B[0m \u001B[1;34m'json'\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 93\u001B[1;33m \u001B[0mdata\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mparse_json\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mstructure_file\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m 94\u001B[0m \u001B[1;32melif\u001B[0m \u001B[0mfmt\u001B[0m \u001B[1;33m==\u001B[0m \u001B[1;34m'vasp-out'\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 95\u001B[0m \u001B[0mdata\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mparse_OUTCAR_comp\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mstructure_file\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
"\u001B[1;32m~\\PycharmProjects\\PyXtal_FF\\pyxtal_ff\\utilities\\__init__.py\u001B[0m in \u001B[0;36mparse_json\u001B[1;34m(path, N, Random)\u001B[0m\n\u001B[0;32m 337\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 338\u001B[0m \u001B[1;32mif\u001B[0m \u001B[0mN\u001B[0m \u001B[1;32mis\u001B[0m \u001B[1;32mNone\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 339\u001B[1;33m \u001B[0mN\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mlen\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mstructure_dict\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m 340\u001B[0m \u001B[1;32melif\u001B[0m \u001B[0mRandom\u001B[0m \u001B[1;32mand\u001B[0m \u001B[0mN\u001B[0m \u001B[1;33m<\u001B[0m \u001B[0mlen\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mstructure_dict\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m 341\u001B[0m \u001B[0mstructure_dict\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0msample\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mstructure_dict\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mN\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
"\u001B[1;31mUnboundLocalError\u001B[0m: local variable 'structure_dict' referenced before assignment"
]
}
],
"source": [
"ff = PyXtal_FF(descriptors=descriptor, model=model)\n",
"ff.run(mode='train', TrainData=os.path.join(\"data\", TrainData), TestData=os.path.join(\"data\", TestData))"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit d2dfa22

Please sign in to comment.