Skip to content

Commit

Permalink
Changes for pandas 2.1 (#335)
Browse files Browse the repository at this point in the history
* pandas have removed teardown method, use plt.close instead

* something funky going on with loc and masks, first pass

* version bump

* restrict pandas version to <2.2.0

* add extra space

* stick to ~=2.1 for now

* update workflows to use 3.9 as minimum requirement, and 3.11 for building docs etc

* autoupdate docs

* correct python version in minimum dependencies name

* cast filled in labels to same type as given labels

* test removing autoclass from PlotAccessor

* Revert "test removing autoclass from PlotAccessor"

This reverts commit 9468521.

* try removing examples from PlotAccessor docstring

* remove the accidentally added doc `show-inheritance` stuff for select modules

* revert the cutting out of the examples section from the PlotAccessor docs

* preload pandas for autodoc for examples in docstrings

* move PlotAccessor stuff from samples into weighted_pandas, where it should have arguably been in the first place

* also used anesthetic plotting for labelled_pandas

* change mask instead

* use .to_numpy() on other mask

* some ilocs in test_plot.py due to label deprecation

* return restriction to >=pandas2.0

* ['A'] instead of .iloc[0] so no line break needed

* erroneous version to re trigger

* Restored correct version number

* erroneous version to re trigger

* Restored correct version number

* remove anesthetic PlotAccessor from labelled_pandas, so now just a patch update

---------

Co-authored-by: lukashergt <[email protected]>
Co-authored-by: Will Handley <[email protected]>
  • Loading branch information
3 people authored Sep 29, 2023
1 parent 96fad22 commit 79e4164
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 41 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.8'
python-version: '3.11'
- name: Upgrade pip and install doc requirements
run: |
python -m pip install --upgrade pip
Expand All @@ -56,7 +56,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-version: ['3.8', '3.9', '3.10', '3.11']
python-version: ['3.9', '3.10', '3.11']
extras: [true, false]
include:
- os: macos-latest
Expand Down Expand Up @@ -94,7 +94,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-version: ['3.8', '3.9', '3.10', '3.11']
python-version: ['3.9', '3.10', '3.11']
include:
- os: macos-latest
python-version: '3.11'
Expand Down Expand Up @@ -126,10 +126,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: 3.11
- name: Install pypa/build
run: python -m pip install build --user
- name: Build a binary wheel and a source tarball
Expand All @@ -146,10 +146,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: 3.9

- name: Install dependencies
run: |
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: 3.11
- name: Install pypa/build
run: python -m pip install build --user
- name: Build a binary wheel and a source tarball
Expand All @@ -53,10 +53,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
2 changes: 1 addition & 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.3.4
:Version: 2.3.5
:Homepage: https://github.com/handley-lab/anesthetic
:Documentation: http://anesthetic.readthedocs.io/

Expand Down
2 changes: 1 addition & 1 deletion anesthetic/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.3.4'
__version__ = '2.3.5'
3 changes: 2 additions & 1 deletion anesthetic/labelled_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def get_labels_map(self, axis=0, fill=True):
labels_map = index.to_frame().droplevel(labs)[labs]
if fill:
replacement = labels_map.loc[labels_map == ''].index
labels_map.loc[labels_map == ''] = replacement
labels_map.loc[labels_map == ''] = replacement.astype(
labels_map.loc[labels_map != ''].dtype)
return labels_map
else:
return index.to_series()
Expand Down
7 changes: 0 additions & 7 deletions anesthetic/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@
from anesthetic.gui.plot import RunPlotter
from anesthetic.weighted_pandas import WeightedDataFrame, WeightedSeries
from anesthetic.labelled_pandas import LabelledDataFrame, LabelledSeries
from pandas.core.accessor import CachedAccessor
from anesthetic.plot import (make_1d_axes, make_2d_axes,
AxesSeries, AxesDataFrame)
from anesthetic.utils import adjust_docstrings
import anesthetic.weighted_pandas
from anesthetic.plotting import PlotAccessor
anesthetic.weighted_pandas._WeightedObject.plot =\
CachedAccessor("plot", PlotAccessor)


class WeightedLabelledDataFrame(WeightedDataFrame, LabelledDataFrame):
Expand Down Expand Up @@ -172,8 +167,6 @@ def __init__(self, *args, **kwargs):
def _constructor(self):
return Samples

plot = CachedAccessor("plot", PlotAccessor)

def plot_1d(self, axes=None, *args, **kwargs):
"""Create an array of 1D plots.
Expand Down
7 changes: 6 additions & 1 deletion anesthetic/weighted_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from anesthetic.utils import (compress_weights, neff, quantile,
temporary_seed, adjust_docstrings)
from pandas.core.dtypes.missing import notna
from pandas.core.accessor import CachedAccessor
from anesthetic.plotting import PlotAccessor


class WeightedGroupBy(GroupBy):
Expand Down Expand Up @@ -141,6 +143,9 @@ def __init__(self, *args, **kwargs):
if weights is not None:
self.set_weights(weights, inplace=True)

plot = CachedAccessor("plot", PlotAccessor)
""":meta private:"""

def isweighted(self, axis=0):
"""Determine if weights are actually present."""
return 'weights' in self._get_axis(axis).names
Expand Down Expand Up @@ -207,7 +212,7 @@ def set_weights(self, weights, axis=0, inplace=False, level=None):
def _rand(self, axis=0):
"""Random number for consistent compression."""
seed = hash_pandas_object(self._get_axis(axis)).sum() % 2**32
with temporary_seed(seed):
with temporary_seed(int(seed)):
return np.random.rand(self.shape[axis])

def reset_index(self, level=None, drop=False, inplace=False,
Expand Down
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def get_version(short=False):
plot_include_source = True
plot_html_show_source_link = False
plot_html_show_formats = False
plot_pre_code = "import numpy as np; from matplotlib import pyplot as plt; import pandas as pd"


# -- Options for HTML output -------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ requires-python = ">=3.8"
dependencies = [
"scipy",
"numpy",
"pandas~=2.0.0",
"pandas>=2.0.0",
"matplotlib>=3.6.1,<3.8.0",
]
classifiers = [
Expand Down
5 changes: 3 additions & 2 deletions tests/test_gui.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import matplotlib.pyplot as plt
import anesthetic.examples._matplotlib_agg # noqa: F401
from anesthetic import read_chains
import pytest
import pandas._testing as tm
from utils import skipif_no_h5py


@pytest.fixture(autouse=True)
def close_figures_on_teardown():
tm.close()
yield
plt.close("all")


@pytest.mark.parametrize('root', ["./tests/example_data/pc",
Expand Down
12 changes: 6 additions & 6 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
scatter_plot_2d, quantile_plot_interval,
basic_cmap, AxesSeries, AxesDataFrame)
from numpy.testing import assert_array_equal
import pandas._testing as tm

from matplotlib.axes import SubplotBase
from matplotlib.contour import ContourSet
Expand All @@ -25,7 +24,8 @@

@pytest.fixture(autouse=True)
def close_figures_on_teardown():
tm.close()
yield
plt.close("all")


def test_AxesObjects():
Expand Down Expand Up @@ -97,18 +97,18 @@ def test_make_1d_axes():

# Check ncol argument
fig, axes = make_1d_axes(paramnames, ncol=2)
nrows, ncol = axes[0].get_subplotspec().get_gridspec().get_geometry()
nrows, ncol = axes.iloc[0].get_subplotspec().get_gridspec().get_geometry()
assert ncol == 2

# Check gridspec argument
grid = gs.GridSpec(2, 2, width_ratios=[3, 1], height_ratios=[3, 1])
g00 = grid[0, 0]
fig, axes = make_1d_axes(paramnames, subplot_spec=g00)
assert g00 is axes[0].get_subplotspec().get_topmost_subplotspec()
assert g00 is axes.iloc[0].get_subplotspec().get_topmost_subplotspec()

# Check gridspec kwargs
fig, axes = make_1d_axes(paramnames, gridspec_kw=dict(wspace=0.1))
ws = axes[0].get_subplotspec().get_gridspec().get_subplot_params().wspace
ws = axes['A'].get_subplotspec().get_gridspec().get_subplot_params().wspace
assert ws == 0.1

# Check figure kwargs
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_make_2d_axes_inputs_outputs():

# Check gridspec kwargs
fig, axes = make_1d_axes(paramnames_x, gridspec_kw=dict(wspace=0.1))
ws = axes[0].get_subplotspec().get_gridspec().get_subplot_params().wspace
ws = axes['A'].get_subplotspec().get_gridspec().get_subplot_params().wspace
assert ws == 0.1

# Check figure kwargs
Expand Down
5 changes: 3 additions & 2 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
import numpy as np
from numpy.testing import assert_array_equal, assert_array_almost_equal
import matplotlib.pyplot as plt
from anesthetic.testing import assert_frame_equal
from anesthetic import MCMCSamples, NestedSamples
from anesthetic import read_chains
Expand All @@ -12,14 +13,14 @@
from anesthetic.read.multinest import read_multinest
from anesthetic.read.ultranest import read_ultranest
from anesthetic.read.nestedfit import read_nestedfit
import pandas._testing as tm
from anesthetic.read.hdf import HDFStore, read_hdf
from utils import pytables_mark_xfail, h5py_mark_xfail, getdist_mark_skip


@pytest.fixture(autouse=True)
def close_figures_on_teardown():
tm.close()
yield
plt.close("all")


def test_read_getdist():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
from numpy.testing import (assert_array_equal, assert_array_almost_equal,
assert_array_less, assert_allclose)
from pandas.testing import assert_frame_equal
import pandas._testing as tm
from matplotlib.colors import to_hex
from scipy.stats import ks_2samp, kstest, norm
from utils import skipif_no_fastkde, astropy_mark_xfail, fastkde_mark_skip


@pytest.fixture(autouse=True)
def close_figures_on_teardown():
tm.close()
yield
plt.close("all")


def test_build_samples():
Expand Down Expand Up @@ -1478,7 +1478,7 @@ def test_groupby_stats():
assert np.all(chains.corrwith(mcmc).get_weights() == [w1, w2])

for chain in [1, 2]:
mask = mcmc.chain == chain
mask = (mcmc.chain == chain).to_numpy()
assert_allclose(mcmc.loc[mask, params].mean(),
chains.mean().loc[chain])
assert_allclose(mcmc.loc[mask, params].std(),
Expand Down Expand Up @@ -1520,7 +1520,7 @@ def test_groupby_stats():
for col in params:
if 'chain' not in col:
for chain in [1, 2]:
mask = mcmc.chain == chain
mask = (mcmc.chain == chain).to_numpy()
assert_allclose(mcmc.loc[mask, col].mean(),
chains[col].mean().loc[chain])
assert_allclose(mcmc.loc[mask, col].std(),
Expand Down
5 changes: 3 additions & 2 deletions tests/test_scripts.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import matplotlib.pyplot as plt
from anesthetic.scripts import gui
from bin.utils import unit_incremented
import pytest
import pandas._testing as tm


@pytest.fixture(autouse=True)
def close_figures_on_teardown():
tm.close()
yield
plt.close("all")


def test_gui():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_weighted_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from pandas.plotting._matplotlib.misc import (
scatter_matrix as orig_scatter_matrix
)
import pandas._testing as tm


@pytest.fixture(autouse=True)
def close_figures_on_teardown():
tm.close()
yield
plt.close("all")


@pytest.fixture
Expand Down

0 comments on commit 79e4164

Please sign in to comment.