Skip to content

Commit

Permalink
Fix errors (only two errors left)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmatteo committed Jul 24, 2024
1 parent 090fca1 commit d56b647
Show file tree
Hide file tree
Showing 27 changed files with 507 additions and 560 deletions.
37 changes: 22 additions & 15 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ jobs:
python: '3.11'

runs-on: ubuntu-latest
env:
PMG_MAPI_KEY: ${{ secrets.PMG_MAPI_KEY }}
# This is used in the flow scripts to generate the graph with graphviz.
READTHEDOCS: 1

steps:
- name: Check out Abipy repo
uses: actions/checkout@v4
Expand All @@ -54,26 +59,28 @@ jobs:
mpirun -n 1 abinit --build
pip install --editable .
mkdir -p $HOME/.abinit/abipy/
cp abipy/data/managers/travis_manager.yml $HOME/.abinit/abipy/manager.yml
cp abipy/data/managers/simple_scheduler.yml $HOME/.abinit/abipy/scheduler.yml
cp abipy/data/managers/gh_manager.yml $HOME/.abinit/abipy/manager.yml
cp abipy/data/managers/gh_scheduler.yml $HOME/.abinit/abipy/scheduler.yml
- name: Build docs with Sphinx
run: |
conda activate abipy
cd docs
cd docs
source install.sh
make
#- name: Upload artifact
# uses: actions/upload-pages-artifact@v2
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
path: site/ # Important. Set to your output dir in MkDocs

#deploy:
# environment:
# name: github-pages
# url: ${{ steps.deployment.outputs.page_url }}
# runs-on: ubuntu-latest
# needs: build
# steps:
# - name: Deploy to GitHub Pages
# id: deployment
# uses: actions/deploy-pages@v2
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v3
8 changes: 3 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ jobs:
config:
- os: ubuntu-latest
python: '3.11'
#python: '>3.9'

runs-on: ${{ matrix.config.os }}

#env:
# PMG_MAPI_KEY: ${{ secrets.PMG_MAPI_KEY }}
env:
PMG_MAPI_KEY: ${{ secrets.PMG_MAPI_KEY }}

steps:
- name: Check out Abipy repo
Expand All @@ -57,7 +55,7 @@ jobs:
run: |
conda activate abipy
pip install -r requirements-tests.txt
pytest -v .
pytest -v .
- name: integration_tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ AbiPy is released under the GNU GPL license. For more details see the LICENSE fi

.. |download-with-anaconda| image:: https://anaconda.org/abinit/abipy/badges/installer/conda.svg
:alt: Download with Anaconda
:target: https://conda.anaconda.org/abinit
:target: https://anaconda.org/conda-forge/abinit

.. |launch-binder| image:: https://mybinder.org/badge.svg
:alt: Launch binder
Expand Down
228 changes: 104 additions & 124 deletions abipy/core/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from abipy.core.mixins import NotebookWriter


MP_DEFAULT_ENDPOINT = "https://materialsproject.org/rest/v2"

MP_KEYS_FOR_DATAFRAME = (
"pretty_formula", "e_above_hull", "energy_per_atom",
Expand All @@ -27,132 +26,113 @@
)


def get_mprester(api_key=None, endpoint=None) -> MyMPRester:
def get_mprester():
"""
Args:
api_key (str): A String API key for accessing the MaterialsProject
REST interface. Please apply on the Materials Project website for one.
If this is None, the code will check if there is a `PMG_MAPI_KEY` in
your .pmgrc.yaml. If so, it will use that environment
This makes easier for heavy users to simply add
this environment variable to their setups and MPRester can
then be called without any arguments.
endpoint (str): Url of endpoint to access the MaterialsProject REST interface.
Defaults to the standard Materials Project REST address, but
can be changed to other urls implementing a similar interface.
"""
if api_key is None:
try:
from pymatgen.core import SETTINGS
except ImportError:
from pymatgen import SETTINGS

api_key = SETTINGS.get("PMG_MAPI_KEY")
if api_key is None:
raise RuntimeError("Cannot find PMG_MAPI_KEY in pymatgen settings. Add it to $HOME/.pmgrc.yaml")

if endpoint is None: endpoint = MP_DEFAULT_ENDPOINT
return MyMPRester(api_key=api_key, endpoint=endpoint)


class MyMPRester(MPRester):
"""
Subclass Materials project Rester.
See :cite:`Jain2013,Ong2015`.
.. rubric:: Inheritance Diagram
.. inheritance-diagram:: MyMPRester
"""
Error = MPRestError

def get_phasediagram_results(self, elements) -> PhaseDiagramResults:
"""
Contact the materials project database, fetch entries and build :class:``PhaseDiagramResults`` instance.
Args:
elements: List of chemical elements.
"""
entries = self.get_entries_in_chemsys(elements, inc_structure="final")
return PhaseDiagramResults(entries)


class PhaseDiagramResults:
"""
Simplified interface to phase-diagram pymatgen API.
Inspired to:
https://anaconda.org/matsci/plotting-and-analyzing-a-phase-diagram-using-the-materials-api/notebook
See also: :cite:`Ong2008,Ong2010`
"""
def __init__(self, entries):
self.entries = entries
# Convert pymatgen structure to Abipy.
from abipy.core.structure import Structure
for e in entries:
e.structure.__class__ = Structure

self.structures = [e.structure for e in entries]
self.mpids = [e.entry_id for e in entries]

# Create phase diagram.
from pymatgen.analysis.phase_diagram import PhaseDiagram
self.phasediagram = PhaseDiagram(self.entries)

def plot(self, show_unstable=True, show=True):
"""
Plot phase diagram.
Args:
show_unstable (float): Whether unstable phases will be plotted as
well as red crosses. If a number > 0 is entered, all phases with
ehull < show_unstable will be shown.
show: True to show plot.
Return: plotter object.
"""
from pymatgen.analysis.phase_diagram import PDPlotter
plotter = PDPlotter(self.phasediagram, show_unstable=show_unstable)
if show:
plotter.show()
return plotter

@lazy_property
def dataframe(self) -> pd.DataFrame:
"""Pandas dataframe with the most important results."""
rows = []
for e in self.entries:
d = e.structure.get_dict4pandas(with_spglib=True)
decomp, ehull = self.phasediagram.get_decomp_and_e_above_hull(e)

rows.append(OrderedDict([
("Materials ID", e.entry_id),
("spglib_symb", d["spglib_symb"]), ("spglib_num", d["spglib_num"]),
("Composition", e.composition.reduced_formula),
("Ehull", ehull), # ("Equilibrium_reaction_energy", pda.get_equilibrium_reaction_energy(e)),
("Decomposition", " + ".join(["%.2f %s" % (v, k.composition.formula) for k, v in decomp.items()])),
]))

import pandas as pd
return pd.DataFrame(rows, columns=list(rows[0].keys()) if rows else None)

def print_dataframes(self, with_spglib=False, file=sys.stdout, verbose=0) -> None:
"""
Print pandas dataframe to file `file`.
Args:
with_spglib: True to compute spacegroup with spglib.
file: Output stream.
verbose: Verbosity level.
"""
print_dataframe(self.dataframe, file=file)
if verbose:
from abipy.core.structure import dataframes_from_structures
dfs = dataframes_from_structures(self.structures, index=self.mpids, with_spglib=with_spglib)
print_dataframe(dfs.lattice, title="Lattice parameters:", file=file)
if verbose > 1:
print_dataframe(dfs.coords, title="Atomic positions (columns give the site index):", file=file)
rester = MPRester()
#print(f"{type(rester)=}")
return rester


#class MyMPRester(MPRester):
# """
# Subclass Materials project Rester.
# See :cite:`Jain2013,Ong2015`.
#
# .. rubric:: Inheritance Diagram
# .. inheritance-diagram:: MyMPRester
# """
# Error = MPRestError
#
# def get_phasediagram_results(self, elements) -> PhaseDiagramResults:
# """
# Contact the materials project database, fetch entries and build :class:``PhaseDiagramResults`` instance.
#
# Args:
# elements: List of chemical elements.
# """
# entries = self.get_entries_in_chemsys(elements, inc_structure="final")
# return PhaseDiagramResults(entries)


#class PhaseDiagramResults:
# """
# Simplified interface to phase-diagram pymatgen API.
#
# Inspired to:
#
# https://anaconda.org/matsci/plotting-and-analyzing-a-phase-diagram-using-the-materials-api/notebook
#
# See also: :cite:`Ong2008,Ong2010`
# """
# def __init__(self, entries):
# self.entries = entries
# # Convert pymatgen structure to Abipy.
# from abipy.core.structure import Structure
# for e in entries:
# e.structure.__class__ = Structure
#
# self.structures = [e.structure for e in entries]
# self.mpids = [e.entry_id for e in entries]
#
# # Create phase diagram.
# from pymatgen.analysis.phase_diagram import PhaseDiagram
# self.phasediagram = PhaseDiagram(self.entries)
#
# def plot(self, show_unstable=True, show=True):
# """
# Plot phase diagram.
#
# Args:
# show_unstable (float): Whether unstable phases will be plotted as
# well as red crosses. If a number > 0 is entered, all phases with
# ehull < show_unstable will be shown.
# show: True to show plot.
#
# Return: plotter object.
# """
# from pymatgen.analysis.phase_diagram import PDPlotter
# plotter = PDPlotter(self.phasediagram, show_unstable=show_unstable)
# if show:
# plotter.show()
# return plotter
#
# @lazy_property
# def dataframe(self) -> pd.DataFrame:
# """Pandas dataframe with the most important results."""
# rows = []
# for e in self.entries:
# d = e.structure.get_dict4pandas(with_spglib=True)
# decomp, ehull = self.phasediagram.get_decomp_and_e_above_hull(e)
#
# rows.append(OrderedDict([
# ("Materials ID", e.entry_id),
# ("spglib_symb", d["spglib_symb"]), ("spglib_num", d["spglib_num"]),
# ("Composition", e.composition.reduced_formula),
# ("Ehull", ehull), # ("Equilibrium_reaction_energy", pda.get_equilibrium_reaction_energy(e)),
# ("Decomposition", " + ".join(["%.2f %s" % (v, k.composition.formula) for k, v in decomp.items()])),
# ]))
#
# import pandas as pd
# return pd.DataFrame(rows, columns=list(rows[0].keys()) if rows else None)
#
# def print_dataframes(self, with_spglib=False, file=sys.stdout, verbose=0) -> None:
# """
# Print pandas dataframe to file `file`.
#
# Args:
# with_spglib: True to compute spacegroup with spglib.
# file: Output stream.
# verbose: Verbosity level.
# """
# print_dataframe(self.dataframe, file=file)
# if verbose:
# from abipy.core.structure import dataframes_from_structures
# dfs = dataframes_from_structures(self.structures, index=self.mpids, with_spglib=with_spglib)
# print_dataframe(dfs.lattice, title="Lattice parameters:", file=file)
# if verbose > 1:
# print_dataframe(dfs.coords, title="Atomic positions (columns give the site index):", file=file)


class DatabaseStructures(NotebookWriter):
Expand Down
Loading

0 comments on commit d56b647

Please sign in to comment.