Skip to content

Commit

Permalink
Merge pull request #56 from JuDFTteam/Release-0.4.9
Browse files Browse the repository at this point in the history
Release 0.4.9
  • Loading branch information
janssenhenning authored Jun 28, 2021
2 parents bc6d7f6 + 09e7a99 commit 1334d48
Show file tree
Hide file tree
Showing 22 changed files with 1,377 additions and 711 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.4.8
current_version = 0.4.9
commit = True
tag = True
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+)(?P<build>\d+))?
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
# No sphinx_rtd_theme installed
pass


autodoc_mock_imports = ['bokeh']

# -- Options for manual page output ---------------------------------------

Expand Down
42 changes: 21 additions & 21 deletions docs/source/devel_guide/plotting.rst
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
.. _devguideplotting:

Using the :py:class:`~masci_tools.vis.Plotter` class
++++++++++++++++++++++++++++++++++++++++++++++++++++
Using the :py:class:`~masci_tools.vis.parameters.Plotter` class
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Description
------------

The :py:class:`~masci_tools.vis.Plotter` class aims to provide a framework, which can be used to handle default values and collect common codeblocks needed for different plotting frameworks.
The :py:class:`~masci_tools.vis.parameters.Plotter` class aims to provide a framework, which can be used to handle default values and collect common codeblocks needed for different plotting frameworks.

The :py:class:`~masci_tools.vis.Plotter` class is a base class that should be subclassed for different Plotting backends. See :py:class:`~masci_tools.vis.matplotlib_plotter.MatplotlibPlotter` or :py:class:`~masci_tools.vis.bokeh_plotter.BokehPlotter` for examples. The Subclass provides a dictionary of all the keys that should be handled by the plotter class. The Plotter class provides a hierachy of overwriting these parameters (Higher numbers take precedence).
The :py:class:`~masci_tools.vis.parameters.Plotter` class is a base class that should be subclassed for different Plotting backends. See :py:class:`~masci_tools.vis.matplotlib_plotter.MatplotlibPlotter` or :py:class:`~masci_tools.vis.bokeh_plotter.BokehPlotter` for examples. The Subclass provides a dictionary of all the keys that should be handled by the plotter class. The Plotter class provides a hierachy of overwriting these parameters (Higher numbers take precedence).

1. Function defaults set with :py:meth:`~masci_tools.vis.Plotter.set_defaults()` with ``default_type='function'``
2. Global defaults set with :py:meth:`~masci_tools.vis.Plotter.set_defaults()`
3. Parameters set with :py:meth:`~masci_tools.vis.Plotter.set_parameters()`
1. Function defaults set with :py:meth:`~masci_tools.vis.parameters.Plotter.set_defaults()` with ``default_type='function'``
2. Global defaults set with :py:meth:`~masci_tools.vis.parameters.Plotter.set_defaults()`
3. Parameters set with :py:meth:`~masci_tools.vis.parameters.Plotter.set_parameters()`

The subclasses should then also provide the plotting backend specific useful code snippets. For example showing colorbars, legends, and so on ...

Expand All @@ -21,12 +21,12 @@ For a list of these functions you can look at the respective documentation (:py:
Writing a plotting function
----------------------------

In the following we will go through a few examples of how to write a simple plotting function using the :py:class:`~masci_tools.vis.Plotter` class. We will be focusing on the :py:class:`~masci_tools.vis.matplotlib_plotter.MatplotlibPlotter`, but all of this is very similar for other plotting backends.
In the following we will go through a few examples of how to write a simple plotting function using the :py:class:`~masci_tools.vis.parameters.Plotter` class. We will be focusing on the :py:class:`~masci_tools.vis.matplotlib_plotter.MatplotlibPlotter`, but all of this is very similar for other plotting backends.

Local instance
^^^^^^^^^^^^^^^

Even though the :py:class:`~masci_tools.vis.Plotter` class is meant to be used globally or on the module level, it can also be useful locally for simplifying simple plotting scripts. Here we have a example of a function producing a single plot with the given data for the x and y coordinates.
Even though the :py:class:`~masci_tools.vis.parameters.Plotter` class is meant to be used globally or on the module level, it can also be useful locally for simplifying simple plotting scripts. Here we have a example of a function producing a single plot with the given data for the x and y coordinates.

.. code-block:: python
Expand Down Expand Up @@ -69,9 +69,9 @@ Even though the :py:class:`~masci_tools.vis.Plotter` class is meant to be used g
Global/Module level instance
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The local instance already gives us reusable code snippets to avoid common pitfalls when doing matplotlib/bokeh plots. But when instantiating the :py:class:`~masci_tools.vis.Plotter` class locally we have no way of letting the user modify the global defaults.
The local instance already gives us reusable code snippets to avoid common pitfalls when doing matplotlib/bokeh plots. But when instantiating the :py:class:`~masci_tools.vis.parameters.Plotter` class locally we have no way of letting the user modify the global defaults.

However, when handling global state we need to be careful to not leave the instance of the :py:class:`~masci_tools.vis.Plotter` class in an inconsistent state. If an error is thrown inside the plotting routine the parameters would stay set and may lead to very unexpected results. For this reason every plotting function using a global or module level instance of these plotters should be decorated with the :py:func:`~masci_tools.vis.ensure_plotter_consistency()` decorator. This does two things:
However, when handling global state we need to be careful to not leave the instance of the :py:class:`~masci_tools.vis.parameters.Plotter` class in an inconsistent state. If an error is thrown inside the plotting routine the parameters would stay set and may lead to very unexpected results. For this reason every plotting function using a global or module level instance of these plotters should be decorated with the :py:func:`~masci_tools.vis.parameters.ensure_plotter_consistency()` decorator. This does two things:

1. If an error occurs in the decorated function the parameters will be reset before the error is raised
2. It makes sure that nothing inside the plotting routine changed the user defined defaults
Expand Down Expand Up @@ -122,15 +122,15 @@ Let us take the previous example and convert it to use a global instance
plot_with_defaults(x, y, limits={'x': (0,1)})
plot_with_defaults(x, y)
The :py:meth:`masci_tools.vis.Plotter.set_defaults()` method is exposed in the two main modules for plotting :py:mod:`masci_tools.vis.plot_methods` :py:mod:`masci_tools.vis.bokeh_plots` as the functions :py:func:`masci_tools.vis.plot_methods.set_mpl_plot_defaults()` and :py:func:`masci_tools.vis.bokeh_plots.set_bokeh_plot_defaults()` specific to the plotter instance that is used in these modules.
The :py:meth:`masci_tools.vis.parameters.Plotter.set_defaults()` method is exposed in the two main modules for plotting :py:mod:`masci_tools.vis.plot_methods` :py:mod:`masci_tools.vis.bokeh_plots` as the functions :py:func:`masci_tools.vis.plot_methods.set_mpl_plot_defaults()` and :py:func:`masci_tools.vis.bokeh_plots.set_bokeh_plot_defaults()` specific to the plotter instance that is used in these modules.

Function defaults
^^^^^^^^^^^^^^^^^^

Some functions may want to set function specific defaults, that make sense inside the function, but may not be useful globally. The following example sets the default ``linewidth`` for our function to ``6``.

.. note::
Function defaults are also reset by the :py:func:`~masci_tools.vis.ensure_plotter_consistency()` decorator, when the plotting function terminates successfully or in an error
Function defaults are also reset by the :py:func:`~masci_tools.vis.parameters.ensure_plotter_consistency()` decorator, when the plotting function terminates successfully or in an error

.. code-block:: python
Expand Down Expand Up @@ -183,12 +183,12 @@ Some functions may want to set function specific defaults, that make sense insid
Passing keyword arguments directly to plot calls
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The plotter classes have a restricted set of keys that they recognize as valid parameters. This set is of course not complete, since there is a vast number of parameters you can set for all plotting backends. In our previous examples unknown keys will immediately lead to an error in the call to :py:meth:`~masci_tools.vis.Plotter.set_parameters()`. To enable this functionality we can provide the ``continue_on_error=True`` as an argument to this method.
The plotter classes have a restricted set of keys that they recognize as valid parameters. This set is of course not complete, since there is a vast number of parameters you can set for all plotting backends. In our previous examples unknown keys will immediately lead to an error in the call to :py:meth:`~masci_tools.vis.parameters.Plotter.set_parameters()`. To enable this functionality we can provide the ``continue_on_error=True`` as an argument to this method.

Then the unknown keys are ignored and are returned in a dictionary. Additionally you can explicitly bypass the plotter object if you provide arguments in a dictionary with the name ``extra_kwargs`` it will be ignored, unpacked and returned along with the unknown keys

.. warning::
Be careful with the this feature and especially the ``extra_kwargs``, since there is no check for name clashes with this argument. You might also run into situations, where arguments of different names collide with arguments provided by the :py:class:`~masci_tools.vis.Plotter`
Be careful with the this feature and especially the ``extra_kwargs``, since there is no check for name clashes with this argument. You might also run into situations, where arguments of different names collide with arguments provided by the :py:class:`~masci_tools.vis.parameters.Plotter`

.. code-block:: python
Expand Down Expand Up @@ -237,15 +237,15 @@ Then the unknown keys are ignored and are returned in a dictionary. Additionally
Multiple plotting calls
^^^^^^^^^^^^^^^^^^^^^^^^

The plotter classes also provide support for multiple plotting calls with different data sets in a single plotting function. To enable this feature we need to set two properties on the :py:class:`masci_tools.vis.Plotter`; ``single_plot`` to `False`` and ``num_plots`` to the number of plot calls made in this function. The plot specific parameters can then be specified in two ways. Shown behind the two ways is the way to set the color of the second data set to ``red``.
The plotter classes also provide support for multiple plotting calls with different data sets in a single plotting function. To enable this feature we need to set two properties on the :py:class:`masci_tools.vis.parameters.Plotter`; ``single_plot`` to `False`` and ``num_plots`` to the number of plot calls made in this function. The plot specific parameters can then be specified in two ways. Shown behind the two ways is the way to set the color of the second data set to ``red``.

1. List of values (``None`` for unspecified values) ``[None,'red']``
2. Dict with integer indices for the specified values ``{1: 'red'}``

Unspecified values are replaced with the previously set defaults.

.. note::
The ``num_plots`` and ``single_plot`` properties are also reset by the :py:func:`~masci_tools.vis.ensure_plotter_consistency()`
The ``num_plots`` and ``single_plot`` properties are also reset by the :py:func:`~masci_tools.vis.parameters.ensure_plotter_consistency()`

.. code-block:: python
Expand Down Expand Up @@ -305,12 +305,12 @@ Custom function specific parameters

You might have situations, where you want to have some function specific parameters, that should pull from the previously set defaults or even a custom default value.

The :py:meth:`~masci_tools.vis.Plotter.add_parameter()` method is implemented exactly for this purpose. It creates a new key to be handled by the plotter class and with the arguments ``default_from`` or ``default_value`` we can specify what the defaults should be. ``default_value`` sets a specific value, ``default_from`` specifies a key from the plotter class from which to take the default value.
The :py:meth:`~masci_tools.vis.parameters.Plotter.add_parameter()` method is implemented exactly for this purpose. It creates a new key to be handled by the plotter class and with the arguments ``default_from`` or ``default_value`` we can specify what the defaults should be. ``default_value`` sets a specific value, ``default_from`` specifies a key from the plotter class from which to take the default value.

The :py:meth:`~masci_tools.vis.matplotlib_plotter.MatplotlibPlotter.plot_kwargs()` method then can take keyword arguments to replace the arguments to take with your custom parameters

.. note::
These added parameters live on the function defaults and parameters level, meaning they will be removed by the :py:func:`~masci_tools.vis.ensure_plotter_consistency()` decorator after the function finishes
These added parameters live on the function defaults and parameters level, meaning they will be removed by the :py:func:`~masci_tools.vis.parameters.ensure_plotter_consistency()` decorator after the function finishes

.. code-block:: python
Expand Down Expand Up @@ -367,9 +367,9 @@ The :py:meth:`~masci_tools.vis.matplotlib_plotter.MatplotlibPlotter.plot_kwargs(
Nested plotting functions
^^^^^^^^^^^^^^^^^^^^^^^^^^

More complex plotting routines might want to call other plotting routines to simplify their structure. However, this has a side-effect when working with the :py:class:`~masci_tools.vis.Plotter` class and the :py:func:`~masci_tools.vis.ensure_plotter_consistency()` decorator. Since the decorator resets the parameters and function defaults after a plotting function has been called you lose everything that you might have modified in the enclosing plotting function.
More complex plotting routines might want to call other plotting routines to simplify their structure. However, this has a side-effect when working with the :py:class:`~masci_tools.vis.parameters.Plotter` class and the :py:func:`~masci_tools.vis.parameters.ensure_plotter_consistency()` decorator. Since the decorator resets the parameters and function defaults after a plotting function has been called you lose everything that you might have modified in the enclosing plotting function.

If you do need access to these parameters after calling a nested plotting function the :py:func:`~masci_tools.vis.NestedPlotParameters()` contextmanager is implemented. It defines a local scope, in which a plotting function can change the parameters and function defaults. After exiting the local scope the parameters and function defaults are always in the same state as when the ``with`` block was entered (Even if an error is raised). The nested plotting function will also start with the state that was set before.
If you do need access to these parameters after calling a nested plotting function the :py:func:`~masci_tools.vis.parameters.NestedPlotParameters()` contextmanager is implemented. It defines a local scope, in which a plotting function can change the parameters and function defaults. After exiting the local scope the parameters and function defaults are always in the same state as when the ``with`` block was entered (Even if an error is raised). The nested plotting function will also start with the state that was set before.

Usage is shown here:

Expand Down
2 changes: 1 addition & 1 deletion docs/source/module_guide/code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ KKR specific Plotting
General Plotting
-----------------

.. automodule:: masci_tools.vis
.. automodule:: masci_tools.vis.parameters
:members:

Matplotlib
Expand Down
2 changes: 1 addition & 1 deletion masci_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
'''
import logging

__version__ = '0.4.8'
__version__ = '0.4.9'

logging.getLogger(__name__).addHandler(logging.NullHandler())
2 changes: 1 addition & 1 deletion masci_tools/tools/greensfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def intersite_shells(hdffile, refAtom, return_greensf=True, show=False):
"""
elements = listElements(hdffile)

distances = [np.linalg.norm(elem.atomDiff) for elem in elements]
distances = [round(np.linalg.norm(elem.atomDiff), 12) for elem in elements]

#sort the elements according to shells
index_sorted = sorted(range(len(elements)), key=lambda k: distances[k])
Expand Down
Loading

0 comments on commit 1334d48

Please sign in to comment.