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

Reorganisation into wpandas and lpandas #280

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6980b13
wpandas refactor
williamjameshandley Mar 22, 2023
23faf4a
Updated modules
williamjameshandley Mar 30, 2023
d4bf52c
Updated documentation
williamjameshandley Mar 30, 2023
43bc825
Merge branch 'master' into wpandas_reorg
williamjameshandley Apr 9, 2023
076cdd4
Tests now pass
williamjameshandley Apr 9, 2023
44f90d5
documentation update weighted_pandas -> wpandas
williamjameshandley Apr 9, 2023
a5bc9e7
Removed :show-inheritance: from wpandas to match master behaviour
williamjameshandley Apr 9, 2023
f8c0ac5
Sphinx actually fixed now
williamjameshandley Apr 9, 2023
167b23e
missing brackets after super()
AdamOrmondroyd Apr 10, 2023
0ea16a0
Merge branch 'master' into wpandas_reorg
williamjameshandley Apr 11, 2023
fbf4a7a
Moved anesthetic.samples to anesthetic.core
williamjameshandley Apr 11, 2023
6fe2183
Merge branch 'master' into wpandas_reorg
AdamOrmondroyd Apr 11, 2023
ca022cb
version bump
AdamOrmondroyd Apr 11, 2023
e9fba58
Refactored wpandas
williamjameshandley Apr 12, 2023
354f665
Merge branch 'wpandas_reorg' of github.com:handley-lab/anesthetic int…
williamjameshandley Apr 12, 2023
8f37ed7
remove show-inheritance from anesthetic.core to see if this fixes docs
AdamOrmondroyd Apr 17, 2023
5611c58
attempt to correct wpandas.utils docs
AdamOrmondroyd Apr 17, 2023
1f8c516
put utils docs in right place
AdamOrmondroyd Apr 17, 2023
8e7548a
just get rid of wpandas utils
AdamOrmondroyd Apr 17, 2023
53f486d
separate wpandas.core module
AdamOrmondroyd Apr 17, 2023
fe08bf1
reinstate :show-inheritance:
AdamOrmondroyd Apr 17, 2023
a2c6d03
run sphinx-apidoc on anesthetic, wpandas and lpandas separately, then…
AdamOrmondroyd Apr 21, 2023
536a56f
add wpandas.core.groupby.rst and wpandas.core.util.rst
AdamOrmondroyd Apr 21, 2023
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
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
anesthetic: nested sampling post-processing
===========================================
:Authors: Will Handley and Lukas Hergt
:Version: 2.0.0-beta.27
:Version: 2.0.0-beta.28
:Homepage: https://github.com/handley-lab/anesthetic
:Documentation: http://anesthetic.readthedocs.io/

Expand Down Expand Up @@ -130,8 +130,12 @@ and view the documentation by opening ``docs/build/html/index.html`` in a browse

.. code:: bash

sphinx-apidoc -fM -t docs/templates/ -o docs/source/ wpandas/
sphinx-apidoc -fM -t docs/templates/ -o docs/source/ lpandas/
sphinx-apidoc -fM -t docs/templates/ -o docs/source/ anesthetic/

and modify/revert ``docs/source/modules.rst`` accordingly with any new modules.

Citation
--------

Expand Down
37 changes: 2 additions & 35 deletions anesthetic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,12 @@
import anesthetic.samples
import anesthetic.plot
import anesthetic.read.chain
import anesthetic._version

import pandas
import pandas.plotting._core
import pandas.plotting._misc
from anesthetic._format import _DataFrameFormatter
from anesthetic._version import __version__ # noqa: F401


def _anesthetic_override(_get_plot_backend):
"""Override the default backend.

When _get_plot_backend asks for 'matplotlib' it will be directed to
'anesthetic.plotting._matplotlib'. This is necessary since any users of
:class:`anesthetic.weighted_pandas.WeightedSamples` should not be using the
original backend.
"""
def wrapper(backend=None):
if backend == 'matplotlib':
return _get_plot_backend('anesthetic.plotting._matplotlib')
return _get_plot_backend(backend)
return wrapper


# Override the two places where _get_plot_backend is defined
pandas.plotting._core._get_plot_backend = \
_anesthetic_override(pandas.plotting._core._get_plot_backend)
pandas.plotting._misc._get_plot_backend = \
_anesthetic_override(pandas.plotting._misc._get_plot_backend)

# Set anesthetic.plotting._matplotlib as the actual backend
pandas.options.plotting.backend = 'anesthetic.plotting._matplotlib'

pandas.io.formats.format.DataFrameFormatter = _DataFrameFormatter
pandas.options.display.max_colwidth = 14

__version__ = anesthetic._version.__version__
Samples = anesthetic.samples.Samples
MCMCSamples = anesthetic.samples.MCMCSamples
NestedSamples = anesthetic.samples.NestedSamples
make_2d_axes = anesthetic.plot.make_2d_axes
make_1d_axes = anesthetic.plot.make_1d_axes

read_chains = anesthetic.read.chain.read_chains
2 changes: 1 addition & 1 deletion anesthetic/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.0b27'
__version__ = '2.0.0b28'
3 changes: 2 additions & 1 deletion anesthetic/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.transforms import Affine2D
from anesthetic.utils import nest_level
from anesthetic.utils import (sample_compression_1d, quantile,
from anesthetic.utils import (sample_compression_1d,
triangular_sample_compression_2d,
iso_probability_contours,
match_contour_to_contourf)
from wpandas.utils import quantile
from anesthetic.boundary import cut_and_normalise_gaussian


Expand Down
19 changes: 19 additions & 0 deletions anesthetic/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,22 @@
.. autoclass:: anesthetic.plotting.PlotAccessor
"""
from anesthetic.plotting._core import PlotAccessor # noqa: 401
from anesthetic.plotting._matplotlib.core import ScatterPlot2d
from anesthetic.plotting._matplotlib.hist import (
Kde1dPlot,
FastKde1dPlot,
Kde2dPlot,
FastKde2dPlot,
Hist1dPlot,
Hist2dPlot,
)

import wpandas.plotting._matplotlib

wpandas.plotting._matplotlib.PLOT_CLASSES['hist_1d'] = Hist1dPlot
wpandas.plotting._matplotlib.PLOT_CLASSES['kde_1d'] = Kde1dPlot
wpandas.plotting._matplotlib.PLOT_CLASSES['fastkde_1d'] = FastKde1dPlot
wpandas.plotting._matplotlib.PLOT_CLASSES['hist_2d'] = Hist2dPlot
wpandas.plotting._matplotlib.PLOT_CLASSES['kde_2d'] = Kde2dPlot
wpandas.plotting._matplotlib.PLOT_CLASSES['fastkde_2d'] = FastKde2dPlot
wpandas.plotting._matplotlib.PLOT_CLASSES['scatter_2d'] = ScatterPlot2d
61 changes: 22 additions & 39 deletions anesthetic/plotting/_matplotlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,46 @@
"""anesthetic override of matplotlib backend for weighted samples."""
from pandas.plotting._matplotlib import ( # noqa: F401
PLOT_CLASSES,
__name__,
__all__,
plot,
register,
deregister
)
from anesthetic.plotting._matplotlib.boxplot import ( # noqa: F401
from wpandas.plotting._matplotlib import ( # noqa: F401
PLOT_CLASSES,
__name__,
__all__,
plot,
register,
deregister,
bootstrap_plot,
scatter_matrix,
andrews_curves,
autocorrelation_plot,
lag_plot,
parallel_coordinates,
radviz,
table,
BoxPlot,
boxplot,
boxplot_frame,
)
from pandas.plotting._matplotlib import boxplot_frame_groupby # noqa: F401
from anesthetic.plotting._matplotlib.core import ( # noqa: F401
AreaPlot,
BarhPlot,
BarPlot,
HexBinPlot,
LinePlot,
PiePlot,
ScatterPlot,
boxplot_frame_groupby,
KdePlot,
HistPlot,
hist_frame,
hist_series,
)
from anesthetic.plotting._matplotlib.core import ( # noqa: F401
ScatterPlot2d,
)
from anesthetic.plotting._matplotlib.hist import ( # noqa: F401
KdePlot,
Kde1dPlot,
FastKde1dPlot,
Kde2dPlot,
FastKde2dPlot,
HistPlot,
Hist1dPlot,
Hist2dPlot,
hist_frame,
hist_series,
)
from anesthetic.plotting._matplotlib.misc import ( # noqa: F401
bootstrap_plot,
scatter_matrix,
)
from pandas.plotting._matplotlib import ( # noqa: F401
andrews_curves,
autocorrelation_plot,
lag_plot,
parallel_coordinates,
radviz,
table,
)

PLOT_CLASSES["line"] = LinePlot
PLOT_CLASSES["bar"] = BarPlot
PLOT_CLASSES["barh"] = BarhPlot
PLOT_CLASSES["box"] = BoxPlot
PLOT_CLASSES["hist"] = HistPlot
PLOT_CLASSES["kde"] = KdePlot
PLOT_CLASSES["area"] = AreaPlot
PLOT_CLASSES["pie"] = PiePlot
PLOT_CLASSES["scatter"] = ScatterPlot
PLOT_CLASSES["hexbin"] = HexBinPlot

PLOT_CLASSES['hist_1d'] = Hist1dPlot
PLOT_CLASSES['kde_1d'] = Kde1dPlot
Expand Down
124 changes: 2 additions & 122 deletions anesthetic/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,10 @@
from typing import Literal
import numpy as np
from matplotlib import pyplot as plt
from pandas.core.dtypes.generic import ABCMultiIndex
import pandas.core.common as com
from pandas.plotting._matplotlib.core import (ScatterPlot as _ScatterPlot,
HexBinPlot as _HexBinPlot,
LinePlot as _LinePlot,
BarPlot as _BarPlot,
BarhPlot as _BarhPlot,
AreaPlot as _AreaPlot,
PiePlot as _PiePlot,
MPLPlot, PlanePlot)
from pandas.plotting._matplotlib.core import PlanePlot
from pandas.plotting._matplotlib.groupby import create_iter_data_given_by
from pandas.io.formats.printing import pprint_thing
from anesthetic.weighted_pandas import _WeightedObject
from anesthetic.plot import scatter_plot_2d


def _get_weights(kwargs, data):
if isinstance(data, _WeightedObject):
kwargs['weights'] = data.get_weights()


class _WeightedMPLPlot(MPLPlot):

_default_rot = None

def __init__(self, data, *args, **kwargs):
_get_weights(kwargs, data)
super().__init__(data, *args, **kwargs)

def _get_index_name(self):
if isinstance(self.data, _WeightedObject):
if isinstance(self.data.drop_weights().index, ABCMultiIndex):
name = self.data.drop_weights().index.names
if com.any_not_none(*name):
name = ",".join([pprint_thing(x) for x in name])
else:
name = None
else:
name = self.data.drop_weights().index.name
if name is not None:
name = pprint_thing(name)

# GH 45145, override the default axis label if one is provided.
index_name = self._get_custom_index_name()
if index_name is not None:
name = pprint_thing(index_name)

return name
else:
return super()._get_index_name()

def _get_xticks(self, convert_period: bool = False):
if isinstance(self.data, _WeightedObject):
return self.data.drop_weights().index._mpl_repr()
else:
return super()._get_xticks(convert_period)


def _compress_weights(kwargs, data):
if isinstance(data, _WeightedObject):
return data.compress(kwargs.pop('ncompress', True))
else:
return data


class _CompressedMPLPlot(MPLPlot):
def __init__(self, data, *args, **kwargs):
data = _compress_weights(kwargs, data)
super().__init__(data, *args, **kwargs)


class ScatterPlot(_CompressedMPLPlot, _ScatterPlot):
# noqa: disable=D101
def __init__(self, data, x, y, s=None, c=None, **kwargs) -> None:
if isinstance(data, _WeightedObject):
kwargs['alpha'] = kwargs.get('alpha', 0.5)
super().__init__(data, x, y, s, c, **kwargs)
from wpandas.plotting._matplotlib.core import _CompressedMPLPlot


class _PlanePlot2d(PlanePlot):
Expand Down Expand Up @@ -115,50 +42,3 @@ def _kind(self) -> Literal["scatter_2d"]:
@classmethod
def _plot(cls, ax, x, y, **kwds):
return scatter_plot_2d(ax, x, y, **kwds)


class HexBinPlot(_HexBinPlot):
# noqa: disable=D101
def __init__(self, data, x, y, C=None, **kwargs) -> None:
if isinstance(data, _WeightedObject):
C = '__weights'
data[C] = data.get_weights()
kwargs['reduce_C_function'] = np.sum
super().__init__(data, x, y, C=C, **kwargs)


class LinePlot(_LinePlot, _WeightedMPLPlot):
# noqa: disable=D101
pass


class PiePlot(_PiePlot):
# noqa: disable=D101
def __init__(self, data, kind=None, **kwargs) -> None:
if isinstance(data, _WeightedObject):
labels = data.drop_weights().index._mpl_repr()
kwargs['labels'] = kwargs.get('labels', labels)
super().__init__(data, kind=kind, **kwargs)


class BarPlot(_BarPlot, _WeightedMPLPlot):
# noqa: disable=D101
def _decorate_ticks(self, ax, name, ticklabels, start_edge, end_edge):
super()._decorate_ticks(ax, name, ticklabels, start_edge, end_edge)
if isinstance(self.data, _WeightedObject):
if self.xticks is None:
ax.set_xticklabels(self._get_xticks())


class BarhPlot(_BarhPlot, _WeightedMPLPlot):
# noqa: disable=D101
def _decorate_ticks(self, ax, name, ticklabels, start_edge, end_edge):
super()._decorate_ticks(ax, name, ticklabels, start_edge, end_edge)
if isinstance(self.data, _WeightedObject):
if self.yticks is None:
ax.set_yticklabels(self._get_xticks())


class AreaPlot(_AreaPlot, _WeightedMPLPlot):
# noqa: disable=D101
pass
Loading