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

Fix StanMetaRegression estimator #108

Merged
merged 22 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
997f336
Fix StanMetaRegression estimator
JulioAPeraza Jun 14, 2022
28ef19d
add pystan and arviz to required dependencies
JulioAPeraza Jun 14, 2022
5071356
Revert "add pystan and arviz to required dependencies"
JulioAPeraza Jun 14, 2022
4e05e71
Keeping pystan and arviz optional
JulioAPeraza Jun 14, 2022
933d9e5
Skip PyStan on Python 3.6
JulioAPeraza Jun 14, 2022
887fc4c
Skip PyStan test for Python < 3.6
JulioAPeraza Jun 14, 2022
1b90603
Include warning to the documentation and raise an error in __init__
JulioAPeraza Jun 14, 2022
393e128
Merge branch 'neurostuff:master' into fix-pystan
JulioAPeraza Jan 24, 2023
bd11b07
Merge branch 'neurostuff:master' into fix-pystan
JulioAPeraza Jan 26, 2023
c1e278f
Update estimators.py
JulioAPeraza Jan 27, 2023
5ad53e1
Fix `black` issues
JulioAPeraza Feb 6, 2023
b7c21da
Merge branch 'master' into fix-pystan
JulioAPeraza Apr 3, 2024
a2297f6
Update test_stan_estimators.py
JulioAPeraza Apr 3, 2024
2c561a7
Merge branch 'fix-pystan' of https://github.com/JulioAPeraza/PyMARE i…
JulioAPeraza Apr 3, 2024
7acf129
Merge branch 'master' of github.com:neurostuff/PyMARE into fix-pystan
jdkent Apr 3, 2024
f99c8dd
declare array
jdkent Apr 3, 2024
6b421d3
try adding additional prereqs
jdkent Apr 3, 2024
5221332
add additional import statement to get error
jdkent Apr 4, 2024
328f16a
fix scipy issue
jdkent Apr 4, 2024
653bd17
add the naked import back in
jdkent Apr 4, 2024
454b23c
fix scipy version
jdkent Apr 4, 2024
e56e294
undo naked import
jdkent Apr 4, 2024
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
26 changes: 9 additions & 17 deletions pymare/estimators/estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from warnings import warn

import numpy as np
import stan
import wrapt
from scipy.optimize import Bounds, minimize

Expand Down Expand Up @@ -550,11 +551,6 @@ class StanMetaRegression(BaseEstimator):
when fitting the meta-regression model repeatedly to different data;
the separation of .compile() and .fit() steps allows one to compile
the model only once.

Warning
-------
With changes to Stan in version 3, which requires Python 3.7, this class no longer works for
Python 3.7+. We will try to fix it in the future.
"""

_result_cls = BayesianMetaRegressionResults
Expand Down Expand Up @@ -594,14 +590,7 @@ def compile(self):
theta ~ normal(0, tau2);
}
"""
try:
from pystan import StanModel
except ImportError:
raise ImportError(
"Please install pystan or, if using Python 3.7+, switch to Python 3.6."
)
JulioAPeraza marked this conversation as resolved.
Show resolved Hide resolved

self.model = StanModel(model_code=spec)
self.model = stan.build(spec, data=self.data)

def fit(self, y, v, X, groups=None):
"""Run the Stan sampler and return results.
Expand Down Expand Up @@ -645,9 +634,6 @@ def fit(self, y, v, X, groups=None):
"shape {}.".format(y.shape)
)

if self.model is None:
self.compile()

N = y.shape[0]
groups = groups or np.arange(1, N + 1, dtype=int)
K = len(np.unique(groups))
Expand All @@ -662,7 +648,13 @@ def fit(self, y, v, X, groups=None):
"sigma": v.ravel(),
}

self.result_ = self.model.sampling(data=data, **self.sampling_kwargs)
self.data = data

if self.model is None:
self.compile()

# self.result_ = self.model.sampling(data=data, **self.sampling_kwargs)
self.result_ = self.model.sample(**self.sampling_kwargs)
return self

def summary(self, ci=95):
Expand Down
6 changes: 1 addition & 5 deletions pymare/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
from inspect import getfullargspec
from warnings import warn

import arviz as az
import numpy as np
import pandas as pd
import scipy.stats as ss

try:
import arviz as az
except ImportError:
az = None

from pymare.stats import q_gen, q_profile


Expand Down
8 changes: 4 additions & 4 deletions pymare/tests/test_stan_estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from pymare.estimators import StanMetaRegression


@pytest.mark.skip(reason="StanMetaRegression won't work with Python 3.7+.")
# @pytest.mark.skip(reason="StanMetaRegression won't work with Python 3.7+.")
def test_stan_estimator(dataset):
"""Run smoke test for StanMetaRegression."""
# no ground truth here, so we use sanity checks and rough bounds
est = StanMetaRegression(iter=3000).fit_dataset(dataset)
est = StanMetaRegression(num_samples=3000).fit_dataset(dataset)
results = est.summary()
assert "BayesianMetaRegressionResults" == results.__class__.__name__
summary = results.summary(["beta", "tau2"])
Expand All @@ -18,9 +18,9 @@ def test_stan_estimator(dataset):
assert 3 < tau2 < 5


@pytest.mark.skip(reason="StanMetaRegression won't work with Python 3.7+.")
# @pytest.mark.skip(reason="StanMetaRegression won't work with Python 3.7+.")
def test_stan_2d_input_failure(dataset_2d):
"""Run smoke test for StanMetaRegression on 2D data."""
with pytest.raises(ValueError) as exc:
StanMetaRegression(iter=500).fit_dataset(dataset_2d)
StanMetaRegression(num_samples=500).fit_dataset(dataset_2d)
assert str(exc.value).startswith("The StanMetaRegression")