Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
60f205f
WIP on doctests
janbridley Jan 15, 2025
474d8eb
Add sphinx-copybutton and notfound
janbridley Jan 15, 2025
1b8d934
messy wip on sybil
janbridley Jan 15, 2025
6ec1619
Further wip
janbridley Jan 15, 2025
f22e876
Remove .doctrees
janbridley Jan 15, 2025
90cd64a
Remove CIFTEST
janbridley Jan 15, 2025
8ed9575
Swap to pytest-doctestplus
janbridley Jan 15, 2025
6511b22
Remove unused files
janbridley Jan 15, 2025
186148b
Clean up settings
janbridley Jan 15, 2025
9cbd851
Fix read_cell_params interaction with cast_values
janbridley Jan 15, 2025
445f199
Clean up conf.py
janbridley Jan 15, 2025
a3c17f2
Update filepath for docstrings
janbridley Jan 15, 2025
3e18f34
Clean up pyproject.toml
janbridley Jan 15, 2025
1c89cfa
Fix formatting for doctests
janbridley Jan 15, 2025
2640240
Finalize indentation for quickstart.rst
janbridley Jan 15, 2025
9b8a429
Update requirements.in files
janbridley Jan 15, 2025
ae30d9c
Update doc/requirements.txt
janbridley Jan 15, 2025
a4ed0ae
Update requirements files
janbridley Jan 15, 2025
49efea4
Fix return None example
janbridley Jan 15, 2025
38fcb7b
Lint
janbridley Jan 15, 2025
eb9ec19
Fix pytest-doctestplus version
janbridley Jan 15, 2025
22ceb58
Update `cell` to return box-like
janbridley Jan 16, 2025
6837f40
Clarify docs for CifFile.cell
janbridley Jan 16, 2025
0dfb7d4
Final cleanup of freud tests
janbridley Jan 16, 2025
170c986
Install freud on ubuntu-latest tests
janbridley Jan 16, 2025
d2b5fc4
Add missing doctest: +SKIP
janbridley Jan 16, 2025
bfa08da
Use pytest importorskip
janbridley Jan 16, 2025
959f3df
Remove freud from ci
janbridley Jan 16, 2025
a049245
Fix doctest-requires block
janbridley Jan 16, 2025
7ae4ff4
Standardize spelling of angstrom
janbridley Jan 16, 2025
7331d04
Rename cell to box
janbridley Jan 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
WIP on doctests
  • Loading branch information
janbridley committed Jan 15, 2025
commit 60f205f36b488e69a12004e0625a17b421d7b4e5
18 changes: 18 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import doctest

from sybil import Sybil
from sybil.parsers.codeblock import PythonCodeBlockParser, CodeBlockParser
from sybil.parsers.doctest import DocTestParser
from sybil.parsers.rest import DocTestDirectiveParser

DOCTEST_OPTIONFLAGS = (
doctest.NORMALIZE_WHITESPACE | doctest.IGNORE_EXCEPTION_DETAIL | doctest.ELLIPSIS
)

pytest_collect_file = Sybil(
parsers=[
DocTestDirectiveParser(optionflags=DOCTEST_OPTIONFLAGS)
],
pattern='*.rst',
# fixtures=['tempdir']
).pytest()
9 changes: 9 additions & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Configuration file for the Sphinx documentation builder."""

import datetime
import doctest

CURRENT_YEAR = datetime.date.today().year
# For the full list of built-in configuration values, see the documentation:
Expand All @@ -22,11 +23,19 @@
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"sphinx.ext.doctest",
"sphinx.ext.intersphinx",
"sphinx.ext.napoleon",
# "pytest_doctestplus.sphinx.doctestplus",
"autodocsumm",
]

# Configure sphinx-build doctest
# doctest_default_flags = (
# doctest.NORMALIZE_WHITESPACE | doctest.IGNORE_EXCEPTION_DETAIL | doctest.ELLIPSIS
# )


templates_path = ["_templates"]
exclude_patterns = ["build", "Thumbs.db", ".DS_Store"]
intersphinx_mapping = {
Expand Down
81 changes: 44 additions & 37 deletions doc/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ Now, let's read extract the key-value pairs from our cif file. This subset of da
usually contains information to reconstruct the system's unit cell, and provides
information regarding the origin of the data.

.. code-block:: python
.. invisible-code-block: python

from parsnip import CifFile
filename = "my_file.cif"
cif = CifFile(filename)
print(cif.pairs)
... {
from parsnip import CifFile
filename = "doc/source/example_file.cif"

cif = CifFile(filename)


.. doctest:: python

>>> from parsnip import CifFile
>>> filename = "doc/source/example_file.cif" # TODO: fix filename
>>> cif = CifFile(filename)

>>> cif.pairs
{
... '_journal_year': '1999',
... '_journal_page_first': '0',
... '_journal_page_last': '123',
Expand All @@ -41,35 +50,33 @@ A `dict`-like getter syntax is provided to key-value pairs. Single keys function
as a python dict, while lists of keys return lists of values. Keys not present in the
:attr:`~.pairs` dict instead return :code:`None`.

.. code-block:: python
.. doctest:: python

cif["_journal_year"]
... "1999"
>>> cif["_journal_year"]
'1999'

cif["_not_in_pairs"]
>>> cif["_not_in_pairs"]
... None

# Multiple keys can be accessed simultaneously!
cif[["_cell_length_a", "_cell_length_b", "_cell_length_c"]]

... ["3.6", "3.6", "3.6"]
>>> # Multiple keys can be accessed simultaneously!
>>> cif[["_cell_length_a", "_cell_length_b", "_cell_length_c"]]
['3.6', '3.6', '3.6']

Note that all data is stored and returned as strings by default. It is not generally
feasible to determine whether a piece of data should be processed, as conversions may
be lossy. Setting the :attr:`~.cast_values` property to :code:`True` reprocesses the
data, converting to float or int where possible. Note that once data is reprocessed,
a new CifFile object must be created to restore the original string data

.. code-block:: python

cf.cast_values = True # Reprocesses our `pairs` dict
.. doctest:: python

cif["_journal_year"]
... 1999
>>> cif.cast_values = True # Reprocess our `pairs` dict

cif[["_cell_length_a", "_cell_length_b", "_cell_length_c"]]
>>> cif["_journal_year"]
1999

... [3.6, 3.6, 3.6]
>>> cif[["_cell_length_a", "_cell_length_b", "_cell_length_c"]]
[3.6, 3.6, 3.6]


Reading Tables
Expand All @@ -93,14 +100,14 @@ list of such arrays, although the :attr:`~.get_from_loops` method is often more
convenient.


.. code-block:: python
.. doctest:: python


len(cif.loops)
... 2
>>> len(cif.loops)
2

cif.loops[0]
... array(
>>> cif.loops[0]
array(
... [[('Cu1', '0.0000000000', '0.0000000000', '0.0000000000', 'Cu', 'a')]],
... dtype=[
... ('_atom_site_label', '<U12'),
Expand All @@ -112,18 +119,18 @@ convenient.
... ]
... )

cif.loops[0]["_atom_site_label"]
... array([['Cu1']], dtype='<U12')
>>> cif.loops[0]["_atom_site_label"]
array([['Cu1']], dtype='<U12')


# (Unstructured) slices of tables can be easily accessed!
xyz = cif.get_from_loops(["_atom_site_fract_x", "_atom_site_fract_y", "_atom_site_fract_z"])
>>> # (Unstructured) slices of tables can be easily accessed!
>>> xyz = cif.get_from_loops(["_atom_site_fract_x", "_atom_site_fract_y", "_atom_site_fract_z"])

print(xyz)
... array([['0.0000000000', '0.0000000000', '0.0000000000']], dtype='<U12')
>>> xyz
array([['0.0000000000', '0.0000000000', '0.0000000000']], dtype='<U12')

print(xyz.astype(float))
... array([[0.0, 0.0, 0.0]], dtype=np.float64)
>>> xyz.astype(float)
array([[0.0, 0.0, 0.0]], dtype=np.float64)



Expand All @@ -149,10 +156,10 @@ file), set :code:`fractional=False`

.. _`freud`: https://freud.readthedocs.io/en/latest/modules/data.html#freud.data.UnitCell

.. code-block:: python
.. doctest:: python

pos = cif.build_unit_cell(fractional=True)
... array([[0. , 0. , 0. ],
>>> pos = cif.build_unit_cell(fractional=True)
array([[0. , 0. , 0. ],
... [0. , 0.5, 0.5],
... [0.5, 0. , 0.5],
... [0.5, 0.5, 0. ]])
2 changes: 1 addition & 1 deletion parsnip/parsnip.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def __getitem__(self, index: str | Iterable[str]):
if isinstance(index, Iterable) and not isinstance(index, str):
return [self.pairs.get(k, None) for k in index]

return self.pairs[index]
return self.pairs.get(index, None)

def read_cell_params(self, degrees: bool = True, mmcif: bool = False):
r"""Read the `unit cell parameters`_ (lengths and angles) from a CIF file.
Expand Down
10 changes: 7 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ Issues = "https://github.com/glotzerlab/parsnip/issues"
optional-dependencies = {tests = { file = ["tests/requirements.in"] }, doc = { file = ["doc/requirements.txt"] }}

[tool.pytest.ini_options]
testpaths = ["tests", "parsnip", "doc"]
# testpaths = ["doc", "tests", "parsnip"]
testpaths = ["doc"]
console_output_style = "progress"
addopts = ["--doctest-modules", "--doctest-glob='*.rst'"]
doctest_optionflags = ["NORMALIZE_WHITESPACE", "ELLIPSIS", "NUMBER"]
# addopts = ["--doctest-plus", "--doctest-glob='*.rst'"]
addopts = ["--doctest-glob='*.rst'"]
# doctest_rst = true
# doctest_optionflags = ["NORMALIZE_WHITESPACE", "ELLIPSIS", "FLOAT_CMP", "REPORT_CDIFF"]
doctest_optionflags = ["NORMALIZE_WHITESPACE", "FLOAT_CMP", "REPORT_NDIFF"]

[tool.ruff]
include = ["*.py", "*.ipynb"]
Expand Down