From e4c07e18353e6d147bb30b17c9c79edb21608a5e Mon Sep 17 00:00:00 2001 From: pglez82 Date: Sat, 15 Nov 2025 16:51:48 +0100 Subject: [PATCH] changing the way the file is loaded --- quapy/method/_bayesian.py | 4 ++++ quapy/method/confidence.py | 3 +-- setup.py | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/quapy/method/_bayesian.py b/quapy/method/_bayesian.py index c5feaf3..da65eed 100644 --- a/quapy/method/_bayesian.py +++ b/quapy/method/_bayesian.py @@ -2,6 +2,7 @@ Utility functions for `Bayesian quantification `_ methods. """ import numpy as np +import importlib.resources try: import jax @@ -82,6 +83,9 @@ def sample_posterior( +def load_stan_file(): + return importlib.resources.files('quapy.method').joinpath('stan/pq.stan').read_text(encoding='utf-8') + def pq_stan(stan_code, n_bins, pos_hist, neg_hist, test_hist, number_of_samples, num_warmup, stan_seed): """ Perform Bayesian prevalence estimation using a Stan model for probabilistic quantification. diff --git a/quapy/method/confidence.py b/quapy/method/confidence.py index dd9e05f..ab649c2 100644 --- a/quapy/method/confidence.py +++ b/quapy/method/confidence.py @@ -624,8 +624,7 @@ class PQ(AggregativeSoftQuantifier, BinaryAggregativeQuantifier): self.num_samples = num_samples self.region = region self.stan_seed = stan_seed - with open('quapy/method/stan/pq.stan', 'r') as f: - self.stan_code = str(f.read()) + self.stan_code = _bayesian.load_stan_file() def aggregation_fit(self, classif_predictions, labels): y_pred = classif_predictions[:, self.pos_label] diff --git a/setup.py b/setup.py index ba5f205..c058f9d 100644 --- a/setup.py +++ b/setup.py @@ -124,7 +124,7 @@ setup( # Similar to `install_requires` above, these must be valid existing # projects. extras_require={ # Optional - 'bayes': ['jax', 'jaxlib', 'numpyro'], + 'bayes': ['jax', 'jaxlib', 'numpyro', 'pystan'], 'neural': ['torch'], 'tests': ['certifi'], 'docs' : ['sphinx-rtd-theme', 'myst-parser'],