From cabaea3a5432f1b6054c2e97c29375f423ac9124 Mon Sep 17 00:00:00 2001 From: Adam Fekete Date: Wed, 27 Jan 2021 17:45:30 +0100 Subject: [PATCH 1/3] using args and kwargs fot the quickview method --- examples/4 - Interface for pymatgen.ipynb | 74 ++++++++++++++++++++++- jupyter_jsmol/pymatgen.py | 11 +++- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/examples/4 - Interface for pymatgen.ipynb b/examples/4 - Interface for pymatgen.ipynb index 96a9591..d2cc776 100644 --- a/examples/4 - Interface for pymatgen.ipynb +++ b/examples/4 - Interface for pymatgen.ipynb @@ -71,6 +71,78 @@ "quick_view(struct, conventional=True, supercell=[2,2,2])" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Using extra paramaters" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "All positional arguments will be passed to jsmol's load command. You can use jsmol's load command to build supercell." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "quick_view(struct, \"{2 2 2}\", conventional=True)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Any extra kw arguments will be passed to th pymatgens CifWriter constructor." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "quick_view(struct, \"{2 2 2}\", conventional=True, symprec=0.01)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "quick_view(struct, \"packed\", conventional=True, symprec=0.01)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "quick_view(struct, \"{2 2 2}\", \"packed\", conventional=True, symprec=0.01)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "?quick_view" + ] + }, { "cell_type": "code", "execution_count": null, @@ -95,7 +167,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.8" + "version": "3.8.5" } }, "nbformat": 4, diff --git a/jupyter_jsmol/pymatgen.py b/jupyter_jsmol/pymatgen.py index 6705da8..7ab54d3 100644 --- a/jupyter_jsmol/pymatgen.py +++ b/jupyter_jsmol/pymatgen.py @@ -4,19 +4,24 @@ try: from pymatgen import Structure from pymatgen.symmetry.analyzer import SpacegroupAnalyzer + from pymatgen.io.cif import CifWriter + pymatgen_loaded = True except ImportError: raise RuntimeError('To use quick_view, you need to have pymatgen installed.') from jupyter_jsmol import JsmolView -def quick_view(structure: Structure, conventional: bool = False, supercell: list = None) -> JsmolView: +def quick_view(structure: Structure, *args, conventional: bool = False, supercell: list = None, **kwargs) -> JsmolView: """A function to visualize pymatgen Structure objects in jupyter notebook using jupyter_jsmol package. Args: structure: pymatgen Structure object. conventional: use conventional cell. Defaults to False. - transform: can be used to make supercells with pymatgen.Structure.make_supercell method. + supercell: can be used to make supercells with pymatgen.Structure.make_supercell method. + *args: Extra arguments for JSmol's load command. Eg. "{2 2 2}", "packed" + **kwargs: Kwargs passthru to CifWriter methods. E.g., This allows + the passing of parameters like symprec=0.01 for generation of symmetric cifs. Returns: A jupyter widget object. @@ -29,4 +34,4 @@ def quick_view(structure: Structure, conventional: bool = False, supercell: list if supercell: s.make_supercell(supercell) - return JsmolView.from_str(s.to('cif')) \ No newline at end of file + return JsmolView.from_str(str(CifWriter(s, **kwargs)), *args) \ No newline at end of file From bcef46823fa4a7daf3ecec1be01c56c0345de485 Mon Sep 17 00:00:00 2001 From: Adam Fekete Date: Wed, 27 Jan 2021 19:23:05 +0100 Subject: [PATCH 2/3] bumping up version --- js/package.json | 2 +- jupyter_jsmol/_frontend.py | 2 +- jupyter_jsmol/_version.py | 2 +- setup.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/js/package.json b/js/package.json index 170d0ba..6d33216 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "jupyter-jsmol", - "version": "0.2.5", + "version": "0.2.6", "author": { "name": "Adam Fekete", "email": "adam@fekete.co.uk" diff --git a/jupyter_jsmol/_frontend.py b/jupyter_jsmol/_frontend.py index 1da7dfd..fd95dd4 100644 --- a/jupyter_jsmol/_frontend.py +++ b/jupyter_jsmol/_frontend.py @@ -9,4 +9,4 @@ """ module_name = "jupyter-jsmol" -module_version = "^0.2.5" +module_version = "^0.2.6" diff --git a/jupyter_jsmol/_version.py b/jupyter_jsmol/_version.py index 4f0c45a..301ba82 100644 --- a/jupyter_jsmol/_version.py +++ b/jupyter_jsmol/_version.py @@ -4,5 +4,5 @@ # Copyright (c) Adam Fekete. # Distributed under the terms of the Modified BSD License. -version_info = (0, 2, 5) +version_info = (0, 2, 6) __version__ = ".".join(map(str, version_info)) diff --git a/setup.py b/setup.py index efd4e25..dc1d134 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,7 @@ description='JSmol viewer widget for Jupyter', long_description=long_description, long_description_content_type="text/markdown", - version='0.2.5', + version='0.2.6', author='Adam Fekete', author_email='adam@fekete.co.uk', url='https://github.com/fekad/jupyter-jsmol', From 7951435af3066169efdeb8fa9b28b77cff1af4ea Mon Sep 17 00:00:00 2001 From: Adam Fekete Date: Thu, 28 Jan 2021 01:09:56 +0100 Subject: [PATCH 3/3] cleanup (kwargs for SpacegroupAnalyzer, supercell) --- examples/4 - Interface for pymatgen.ipynb | 59 ++++------------------- jupyter_jsmol/pymatgen.py | 34 +++++++++---- 2 files changed, 34 insertions(+), 59 deletions(-) diff --git a/examples/4 - Interface for pymatgen.ipynb b/examples/4 - Interface for pymatgen.ipynb index d2cc776..d302cfe 100644 --- a/examples/4 - Interface for pymatgen.ipynb +++ b/examples/4 - Interface for pymatgen.ipynb @@ -38,7 +38,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [], "source": [ "quick_view(struct)" @@ -47,7 +49,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "scrolled": false + }, "outputs": [], "source": [ "quick_view(struct, conventional=True)" @@ -75,32 +79,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Using extra paramaters" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "All positional arguments will be passed to jsmol's load command. You can use jsmol's load command to build supercell." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "scrolled": false - }, - "outputs": [], - "source": [ - "quick_view(struct, \"{2 2 2}\", conventional=True)" + "## Using extra parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Any extra kw arguments will be passed to th pymatgens CifWriter constructor." + "All positional arguments will be passed to jsmol's load command." ] }, { @@ -111,27 +97,7 @@ }, "outputs": [], "source": [ - "quick_view(struct, \"{2 2 2}\", conventional=True, symprec=0.01)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "scrolled": false - }, - "outputs": [], - "source": [ - "quick_view(struct, \"packed\", conventional=True, symprec=0.01)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "quick_view(struct, \"{2 2 2}\", \"packed\", conventional=True, symprec=0.01)" + "quick_view(struct, \"packed\", conventional=True)" ] }, { @@ -142,13 +108,6 @@ "source": [ "?quick_view" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/jupyter_jsmol/pymatgen.py b/jupyter_jsmol/pymatgen.py index 7ab54d3..e34a0f2 100644 --- a/jupyter_jsmol/pymatgen.py +++ b/jupyter_jsmol/pymatgen.py @@ -8,20 +8,32 @@ pymatgen_loaded = True except ImportError: - raise RuntimeError('To use quick_view, you need to have pymatgen installed.') + raise RuntimeError("To use quick_view, you need to have pymatgen installed.") from jupyter_jsmol import JsmolView -def quick_view(structure: Structure, *args, conventional: bool = False, supercell: list = None, **kwargs) -> JsmolView: + +def quick_view( + structure: Structure, + *args, + conventional: bool = False, + supercell: list = [1, 1, 1], + symprec: float = 0.01, + angle_tolerance: float = 5.0 +) -> JsmolView: """A function to visualize pymatgen Structure objects in jupyter notebook using jupyter_jsmol package. Args: structure: pymatgen Structure object. + *args: Extra arguments for JSmol's load command. Eg. "{2 2 2}", "packed" conventional: use conventional cell. Defaults to False. supercell: can be used to make supercells with pymatgen.Structure.make_supercell method. - *args: Extra arguments for JSmol's load command. Eg. "{2 2 2}", "packed" - **kwargs: Kwargs passthru to CifWriter methods. E.g., This allows - the passing of parameters like symprec=0.01 for generation of symmetric cifs. + symprec: If not none, finds the symmetry of the structure + and writes the cif with symmetry information. Passes symprec + to the SpacegroupAnalyzer. + angle_tolerance: Angle tolerance for symmetry finding. Passes + angle_tolerance to the SpacegroupAnalyzer. Used only if symprec + is not None. Returns: A jupyter widget object. @@ -29,9 +41,13 @@ def quick_view(structure: Structure, *args, conventional: bool = False, supercel s = structure.copy() if conventional: - s = SpacegroupAnalyzer(s).get_conventional_standard_structure() + spga = SpacegroupAnalyzer(s, symprec=symprec, angle_tolerance=angle_tolerance) + s = spga.get_conventional_standard_structure() + + cif = CifWriter( + s, symprec=symprec, angle_tolerance=angle_tolerance, refine_struct=False + ) - if supercell: - s.make_supercell(supercell) + supercell_str = "{" + " ".join(map(str, supercell)) + "}" - return JsmolView.from_str(str(CifWriter(s, **kwargs)), *args) \ No newline at end of file + return JsmolView.from_str(str(cif), supercell_str, *args)