Migrate pkg_resources to importlib_resources (#2352)

This commit is contained in:
Cuong Duong 2023-01-25 16:00:03 +11:00 committed by GitHub
parent 0bf05baf3c
commit 3c5321681a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 15 deletions

View file

@ -9,8 +9,7 @@ from abc import abstractmethod, ABC
from typing import Tuple from typing import Tuple
from collections import OrderedDict from collections import OrderedDict
from enum import Enum from enum import Enum
from pathlib import Path import importlib_resources
import pkg_resources
import platform import platform
import logging import logging
@ -59,11 +58,9 @@ class CmdStanPyBackend(IStanBackend):
def __init__(self): def __init__(self):
import cmdstanpy import cmdstanpy
# this must be set before super.__init__() for load_model to work on Windows # this must be set before super.__init__() for load_model to work on Windows
local_cmdstan = pkg_resources.resource_filename( local_cmdstan = importlib_resources.files("prophet") / "stan_model" / f"cmdstan-{self.CMDSTAN_VERSION}"
"prophet", f"stan_model/cmdstan-{self.CMDSTAN_VERSION}" if local_cmdstan.exists():
) cmdstanpy.set_cmdstan_path(str(local_cmdstan))
if Path(local_cmdstan).exists():
cmdstanpy.set_cmdstan_path(local_cmdstan)
super().__init__() super().__init__()
@staticmethod @staticmethod
@ -72,11 +69,8 @@ class CmdStanPyBackend(IStanBackend):
def load_model(self): def load_model(self):
import cmdstanpy import cmdstanpy
model_file = pkg_resources.resource_filename( model_file = importlib_resources.files("prophet") / "stan_model" / "prophet_model.bin"
'prophet', return cmdstanpy.CmdStanModel(exe_file=str(model_file))
'stan_model/prophet_model.bin',
)
return cmdstanpy.CmdStanModel(exe_file=model_file)
def fit(self, stan_init, stan_data, **kwargs): def fit(self, stan_init, stan_data, **kwargs):
if 'inits' not in kwargs and 'init' in kwargs: if 'inits' not in kwargs and 'init' in kwargs:

View file

@ -22,6 +22,7 @@ dependencies = [
"holidays>=0.14.2", "holidays>=0.14.2",
"python-dateutil>=2.8.0", "python-dateutil>=2.8.0",
"tqdm>=4.36.1", "tqdm>=4.36.1",
"importlib_resources",
] ]
authors = [ authors = [
{name = "Sean J. Taylor", email = "sjtz@pm.me"}, {name = "Sean J. Taylor", email = "sjtz@pm.me"},
@ -56,6 +57,6 @@ parallel = [
] ]
[project.urls] [project.urls]
homepage = "https://facebook.github.io/prophet/" Homepage = "https://facebook.github.io/prophet/"
documentation = "https://facebook.github.io/prophet/" Documentation = "https://facebook.github.io/prophet/"
repository = "https://github.com/facebook/prophet" Repository = "https://github.com/facebook/prophet"