From 4df9ed01d3baeef55783a54b3f81d9d0a87fa6f5 Mon Sep 17 00:00:00 2001 From: TomaSajt <62384384+TomaSajt@users.noreply.github.com> Date: Wed, 12 Jul 2023 13:33:05 +0200 Subject: [PATCH] python310Packages.cmdstanpy: init at 1.1.0 --- .../python-modules/cmdstanpy/default.nix | 82 +++++++++++++++++++ .../cmdstanpy/use-nix-cmdstan-path.patch | 25 ++++++ pkgs/top-level/python-packages.nix | 2 + 3 files changed, 109 insertions(+) create mode 100644 pkgs/development/python-modules/cmdstanpy/default.nix create mode 100644 pkgs/development/python-modules/cmdstanpy/use-nix-cmdstan-path.patch diff --git a/pkgs/development/python-modules/cmdstanpy/default.nix b/pkgs/development/python-modules/cmdstanpy/default.nix new file mode 100644 index 000000000000..f8960fadb3aa --- /dev/null +++ b/pkgs/development/python-modules/cmdstanpy/default.nix @@ -0,0 +1,82 @@ +{ lib +, buildPythonPackage +, fetchFromGitHub +, substituteAll + +, cmdstan + +, pandas +, numpy +, tqdm +, xarray + +, pytestCheckHook +}: + +buildPythonPackage rec { + pname = "cmdstanpy"; + version = "1.1.0"; + format = "setuptools"; + + src = fetchFromGitHub { + owner = "stan-dev"; + repo = "cmdstanpy"; + rev = "refs/tags/v${version}"; + hash = "sha256-9kAd3rbSctWEhAzB6RiQlbg5/uVxGIghYLus8hWzBFQ="; + }; + + patches = [ + (substituteAll { + src = ./use-nix-cmdstan-path.patch; + cmdstan = "${cmdstan}/opt/cmdstan"; + }) + ]; + + postPatch = '' + # conftest.py would have used git to clean up, which is unnecessary here + rm test/conftest.py + ''; + + propagatedBuildInputs = [ + pandas + numpy + tqdm + ]; + + passthru.optional-dependencies = { + all = [ xarray ]; + }; + + preCheck = '' + export HOME=$(mktemp -d) + ''; + + nativeCheckInputs = [ + pytestCheckHook + ] ++ passthru.optional-dependencies.all; + + disabledTestPaths = [ + # No need to test these when using Nix + "test/test_install_cmdstan.py" + "test/test_cxx_installation.py" + ]; + + disabledTests = [ + "test_lp_good" # Fails for some reason + "test_serialization" # Pickle class mismatch errors + # These tests use the flag -DSTAN_THREADS which doesn't work in cmdstan (missing file) + "test_multi_proc_threads" + "test_compile_force" + ]; + + pythonImportsCheck = [ "cmdstanpy" ]; + + meta = { + homepage = "https://github.com/stan-dev/cmdstanpy"; + description = "A lightweight interface to Stan for Python users"; + changelog = "https://github.com/stan-dev/cmdstanpy/releases/tag/v${version}"; + license = lib.licenses.bsd3; + platforms = lib.platforms.linux; + maintainers = with lib.maintainers; [ tomasajt ]; + }; +} diff --git a/pkgs/development/python-modules/cmdstanpy/use-nix-cmdstan-path.patch b/pkgs/development/python-modules/cmdstanpy/use-nix-cmdstan-path.patch new file mode 100644 index 000000000000..5f8df43c799f --- /dev/null +++ b/pkgs/development/python-modules/cmdstanpy/use-nix-cmdstan-path.patch @@ -0,0 +1,25 @@ +diff --git a/cmdstanpy/utils/cmdstan.py b/cmdstanpy/utils/cmdstan.py +index 227d97a..27c3ccc 100644 +--- a/cmdstanpy/utils/cmdstan.py ++++ b/cmdstanpy/utils/cmdstan.py +@@ -163,19 +163,7 @@ def cmdstan_path() -> str: + if 'CMDSTAN' in os.environ and len(os.environ['CMDSTAN']) > 0: + cmdstan = os.environ['CMDSTAN'] + else: +- cmdstan_dir = os.path.expanduser(os.path.join('~', _DOT_CMDSTAN)) +- if not os.path.exists(cmdstan_dir): +- raise ValueError( +- 'No CmdStan installation found, run command "install_cmdstan"' +- 'or (re)activate your conda environment!' +- ) +- latest_cmdstan = get_latest_cmdstan(cmdstan_dir) +- if latest_cmdstan is None: +- raise ValueError( +- 'No CmdStan installation found, run command "install_cmdstan"' +- 'or (re)activate your conda environment!' +- ) +- cmdstan = os.path.join(cmdstan_dir, latest_cmdstan) ++ cmdstan = '@cmdstan@' + os.environ['CMDSTAN'] = cmdstan + validate_cmdstan_path(cmdstan) + return os.path.normpath(cmdstan) diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix index 40e90c95567b..7e0b27d81b4e 100644 --- a/pkgs/top-level/python-packages.nix +++ b/pkgs/top-level/python-packages.nix @@ -2001,6 +2001,8 @@ self: super: with self; { cmdline = callPackage ../development/python-modules/cmdline { }; + cmdstanpy = callPackage ../development/python-modules/cmdstanpy { }; + cmigemo = callPackage ../development/python-modules/cmigemo { inherit (pkgs) cmigemo; };