Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up a bit using new dependencies #288

Merged
merged 20 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
44 changes: 22 additions & 22 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
`Unreleased <https://github.com/pace-neutrons/Euphonic/compare/v1.2.1...HEAD>`_
----------
-------------------------------------------------------------------------------

- Requirements

Expand Down Expand Up @@ -49,7 +49,7 @@


`v1.2.1 <https://github.com/pace-neutrons/Euphonic/compare/v1.2.0...v1.2.1>`_
------
-----------------------------------------------------------------------------

- Improvements

Expand Down Expand Up @@ -82,7 +82,7 @@
- A deprecation in Numpy 1.25, which indirectly caused a test failure, has been addressed.

`v1.2.0 <https://github.com/pace-neutrons/Euphonic/compare/v1.1.0...v1.2.0>`_
------
-----------------------------------------------------------------------------

- Improvements:

Expand All @@ -105,7 +105,7 @@
and ``--brille-npts-density`` arguments.

`v1.1.0 <https://github.com/pace-neutrons/Euphonic/compare/v1.0.0...v1.1.0>`_
----------
-----------------------------------------------------------------------------

- New features:

Expand All @@ -121,7 +121,7 @@
interactions, see `#239 <https://github.com/pace-neutrons/Euphonic/issues/239>`_.

`v1.0.0 <https://github.com/pace-neutrons/Euphonic/compare/v0.6.5...v1.0.0>`_
----------
-----------------------------------------------------------------------------

- Changes:

Expand Down Expand Up @@ -163,7 +163,7 @@
by default. To recover previous behaviour set this to ``False``.

`v0.6.5 <https://github.com/pace-neutrons/Euphonic/compare/v0.6.4...v0.6.5>`_
------
-----------------------------------------------------------------------------

- New Features:

Expand Down Expand Up @@ -212,7 +212,7 @@
Previously this was interpreted as an astronomical unit by Pint.

`v0.6.4 <https://github.com/pace-neutrons/Euphonic/compare/v0.6.3...v0.6.4>`_
------
-----------------------------------------------------------------------------

- Improvements:

Expand Down Expand Up @@ -243,7 +243,7 @@
which will just emit a warning.

`v0.6.3 <https://github.com/pace-neutrons/Euphonic/compare/v0.6.2...v0.6.3>`_
------
-----------------------------------------------------------------------------

- New Features:

Expand Down Expand Up @@ -273,7 +273,7 @@
they refer to, and to match other arguments like ``vmin``, ``vmax``.

`v0.6.2 <https://github.com/pace-neutrons/Euphonic/compare/v0.6.1...v0.6.2>`_
------
-----------------------------------------------------------------------------

- Improvements:

Expand All @@ -289,7 +289,7 @@
factor is not present

`v0.6.1 <https://github.com/pace-neutrons/Euphonic/compare/v0.6.0...v0.6.1>`_
------
-----------------------------------------------------------------------------

- Bug fixes:

Expand All @@ -302,7 +302,7 @@
can now be correctly recovered by multiplying S(Q,w) by the energy bin width.

`v0.6.0 <https://github.com/pace-neutrons/Euphonic/compare/v0.5.2...0.6.0>`_
------
----------------------------------------------------------------------------

- Euphonic can now calculate neutron-weighted partial density of states, and
has new ``Spectra`` features to handle PDOS data:
Expand Down Expand Up @@ -361,7 +361,7 @@
``euphonic-optimise-dipole-parameter``.

`v0.5.2 <https://github.com/pace-neutrons/Euphonic/compare/v0.5.1...v0.5.2>`_
------
-----------------------------------------------------------------------------

- Improvements:

Expand All @@ -386,7 +386,7 @@
``ebins - 1`` bins were generated

`v0.5.1 <https://github.com/pace-neutrons/Euphonic/compare/v0.5.0...v0.5.1>`_
----------
-----------------------------------------------------------------------------

- New Features:

Expand Down Expand Up @@ -417,7 +417,7 @@
implementation: moved ``1/2`` factor and added explicit q-point weights

`v0.5.0 <https://github.com/pace-neutrons/Euphonic/compare/v0.4.0...v0.5.0>`_
----------
-----------------------------------------------------------------------------

- New Features:

Expand All @@ -436,7 +436,7 @@
``Spectrum1DCollection.from_castep_phonon_dos``
- **Adaptive broadening** is now available for DOS, which can obtain a
more representative DOS than standard fixed-width broadening. See
`the docs <https://euphonic.readthedocs.io/en/latest/dos.html#adaptive-broadening>`_
`the docs <https://euphonic.readthedocs.io/en/latest/dos.html#adaptive-broadening>`__
for details
- Adaptive broadening can be used in the ``euphonic-dos`` tool with the
``--adaptive`` argument
Expand Down Expand Up @@ -494,7 +494,7 @@
in implementation

`v0.4.0 <https://github.com/pace-neutrons/Euphonic/compare/v0.3.2...v0.4.0>`_
----------
-----------------------------------------------------------------------------

- There have been some major changes and improvements to spectra, plotting
and command line tools, including:
Expand Down Expand Up @@ -549,7 +549,7 @@
`#108 <https://github.com/pace-neutrons/Euphonic/issues/108>`_

`v0.3.2 <https://github.com/pace-neutrons/Euphonic/compare/v0.3.1...v0.3.2>`_
----------
-----------------------------------------------------------------------------

- New Features:

Expand Down Expand Up @@ -594,7 +594,7 @@
there is only one q-point (which is gamma) and ``splitting=True``

`v0.3.1 <https://github.com/pace-neutrons/Euphonic/compare/v0.3.0...v0.3.1>`_
----------
-----------------------------------------------------------------------------

- New Features:

Expand Down Expand Up @@ -625,7 +625,7 @@
`#77 <https://github.com/pace-neutrons/Euphonic/issues/77>`_

`v0.3.0 <https://github.com/pace-neutrons/Euphonic/compare/v0.2.2...v0.3.0>`_
----------
-----------------------------------------------------------------------------

- Breaking Changes:

Expand Down Expand Up @@ -665,22 +665,22 @@
in C, `#45 <https://github.com/pace-neutrons/Euphonic/issues/45>`_

`v0.2.2 <https://github.com/pace-neutrons/Euphonic/compare/v0.2.1...v0.2.2>`_
------
-----------------------------------------------------------------------------

- Bug fixes:

- Add MANIFEST.in for PyPI distribution

`v0.2.1 <https://github.com/pace-neutrons/Euphonic/compare/v0.2.0...v0.2.1>`_
------
-----------------------------------------------------------------------------

- Bug fixes:

- Cannot easily upload C header files to PyPI without an accompanying source
file, so refactor C files to avoid this

`v0.2.0 <https://github.com/pace-neutrons/Euphonic/compare/v0.1-dev3...v0.2.0>`_
------
--------------------------------------------------------------------------------

- There are several breaking changes:

Expand Down
1 change: 1 addition & 0 deletions doc/source/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Python API
Force Constants <force-constants>
Phonon Frequencies and Eigenvectors <qpoint-phonon-modes>
Phonon Frequencies Only <qpoint-frequencies>
Crystal Structure <crystal>
Density of States <dos>
Structure Factors <structure-factor>
Scattering Intensities <scattering-intensities>
Expand Down
2 changes: 1 addition & 1 deletion doc/source/spectra.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ apply this resolution function to a Spectrum1D we package the polynomial into a
def tosca_resolution(energy):
poly_in_wavenumber = Polynomial([2.5, 0.005, 1e-7])
return poly_in_wavenumber(energy.to('1/cm').magnitude) * ureg('1/cm')
dos_tosca = dos.broaden(tosca_resolution, shape='gauss', width_convention='STD')
dos_tosca = dos.broaden(tosca_resolution, shape='gauss', width_convention='std')

Plotting
--------
Expand Down
86 changes: 47 additions & 39 deletions euphonic/broadening.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
"""
Functions for broadening spectra
"""
from typing import Callable, Union
from typing import Callable, Literal, Union
import warnings

import numpy as np
from numpy.polynomial import Chebyshev
from pint import Quantity
from scipy.optimize import nnls
from scipy.stats import norm
from scipy.signal import convolve

from euphonic import ureg, Quantity
from euphonic import ureg


def variable_width_broadening(bins: Quantity,
x: Quantity,
width_function: Callable[[Quantity], Quantity],
weights: Union[np.ndarray, Quantity],
width_lower_limit: Quantity = None,
width_convention: str = 'fwhm',
adaptive_error: float = 1e-2,
shape: str = 'gauss',
fit: str = 'cheby-log') -> Quantity:
ErrorFit = Literal['cheby-log', 'cubic']
KernelShape = Literal['gauss', 'lorentz']


def variable_width_broadening(

Check notice on line 21 in euphonic/broadening.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

euphonic/broadening.py#L21

Too many arguments (9/5)
bins: Quantity,
x: Quantity,
width_function: Callable[[Quantity], Quantity],
weights: Union[np.ndarray, Quantity],
width_lower_limit: Quantity = None,
width_convention: Literal['fwhm', 'std'] = 'fwhm',
adaptive_error: float = 1e-2,
shape: KernelShape = 'gauss',
fit: ErrorFit = 'cheby-log'
) -> Quantity:
r"""Apply x-dependent Gaussian broadening to 1-D data series

Typically this is an energy-dependent instrumental resolution function.
Expand Down Expand Up @@ -49,14 +56,14 @@
(default) the bin width will be used. To disable any lower limit, set
to 0 or lower.
width_convention
Either 'std' or 'fwhm', to indicate if polynomial function yields
standard deviation (sigma) or full-width half-maximum.
Indicate if polynomial function yields standard deviation (sigma) or
full-width half-maximum.
adaptive_error
Acceptable error for gaussian approximations, defined
as the absolute difference between the areas of the true and
approximate gaussians.
shape
Select 'gauss' or 'lorentz' kernel.
Select broadening kernel function.
fit
Select parametrisation of kernel width spacing to adaptive_error.
'cheby-log' is recommended: for shape 'gauss', 'cubic' is also
Expand All @@ -76,15 +83,10 @@

widths = sigma_function(x)

# With newer versions of Numpy/Pint we could dispense with most of the unit
# and magnitude shuffling as the numpy functions are handled more cleanly.

if width_lower_limit is None:
width_lower_limit = np.diff(bins.magnitude).max() * bins.units
width_lower_limit = np.diff(bins).max()

widths = np.maximum(widths.magnitude,
width_lower_limit.to(widths.units).magnitude
) * widths.units
widths = np.maximum(widths, width_lower_limit)

if isinstance(weights, np.ndarray):
weights = weights * ureg('dimensionless')
Expand All @@ -99,13 +101,15 @@
fit=fit) * weights_unit


def width_interpolated_broadening(bins: Quantity,
x: Quantity,
widths: Quantity,
weights: np.ndarray,
adaptive_error: float,
shape: str = 'gauss',
fit: str = 'cheby-log') -> Quantity:
def width_interpolated_broadening(

Check notice on line 104 in euphonic/broadening.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

euphonic/broadening.py#L104

Too many arguments (7/5)
bins: Quantity,
x: Quantity,
widths: Quantity,
weights: np.ndarray,
adaptive_error: float,
shape: KernelShape = 'gauss',
fit: ErrorFit = 'cheby-log'
) -> Quantity:
"""
Uses a fast, approximate method to broaden a spectrum
with a variable-width kernel. Exact Gaussians are calculated
Expand All @@ -131,8 +135,8 @@
as the absolute difference between the areas of the true and
approximate gaussians.
shape
Select 'gauss' or 'lorentz' kernel. Widths will correspond to sigma or
gamma parameters respectively.
Select kernel shape. Widths will correspond to sigma or gamma
parameters respectively.
fit
Select parametrisation of kernel width spacing to adaptive_error.
'cheby-log' is recommended: for shape 'gauss', 'cubic' is also
Expand All @@ -159,7 +163,9 @@
return gamma / (2 * np.pi * (x**2 + (gamma / 2)**2))


def _get_spacing(error, shape='gauss', fit='cheby-log'):
def _get_spacing(error,
shape: KernelShape = 'gauss',
fit: ErrorFit = 'cheby-log'):
"""
Determine suitable spacing value for mode_width given accepted error level

Expand Down Expand Up @@ -198,13 +204,14 @@
f'"Lorentz" shapes.')


def _width_interpolated_broadening(bins: np.ndarray,
x: np.ndarray,
widths: np.ndarray,
weights: np.ndarray,
adaptive_error: float,
shape='gauss',
fit='cheby-log') -> np.ndarray:
def _width_interpolated_broadening(

Check notice on line 207 in euphonic/broadening.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

euphonic/broadening.py#L207

Too many arguments (6/5)

Check notice on line 207 in euphonic/broadening.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

euphonic/broadening.py#L207

Too many local variables (27/15)
bins: np.ndarray,
x: np.ndarray,
widths: np.ndarray,
weights: np.ndarray,
adaptive_error: float,
shape: KernelShape = 'gauss',
fit: ErrorFit = 'cheby-log') -> np.ndarray:
"""
Broadens a spectrum using a variable-width kernel, taking the
same arguments as `variable_width` but expects arrays with
Expand Down Expand Up @@ -277,7 +284,8 @@
return spectrum


def find_coeffs(spacing: float, shape='gauss') -> np.ndarray:
def find_coeffs(spacing: float,
shape: KernelShape = 'gauss') -> np.ndarray:
""""
Function that, for a given spacing value, gives the coefficients of the
polynomial which describes the relationship between kernel width and the
Expand Down
Loading