python3Packages.cnvkit: fix build error and failing tests (#431215)

This commit is contained in:
Gaétan Lepage
2025-08-07 11:44:26 +02:00
committed by GitHub
2 changed files with 85 additions and 55 deletions

View File

@@ -3,9 +3,9 @@
buildPythonPackage,
fetchFromGitHub,
fetchpatch,
python,
makeWrapper,
# dependencies
R,
biopython,
matplotlib,
numpy,
@@ -13,15 +13,15 @@
pomegranate,
pyfaidx,
pysam,
rPackages,
reportlab,
rPackages,
scikit-learn,
scipy,
R,
# tests
pytestCheckHook,
}:
}:
buildPythonPackage rec {
pname = "cnvkit";
version = "0.9.12";
@@ -47,11 +47,38 @@ buildPythonPackage rec {
"pomegranate"
];
# Numpy 2 compatibility
postPatch = ''
substituteInPlace skgenome/intersect.py \
--replace-fail "np.string_" "np.bytes_"
'';
nativeBuildInputs = [
makeWrapper
];
buildInputs = [
R
];
postPatch =
let
rscript = lib.getExe' R "Rscript";
in
# Numpy 2 compatibility
''
substituteInPlace skgenome/intersect.py \
--replace-fail "np.string_" "np.bytes_"
''
# Patch shebang lines in R scripts
+ ''
substituteInPlace cnvlib/segmentation/flasso.py \
--replace-fail "#!/usr/bin/env Rscript" "#!${rscript}"
substituteInPlace cnvlib/segmentation/cbs.py \
--replace-fail "#!/usr/bin/env Rscript" "#!${rscript}"
substituteInPlace cnvlib/segmentation/__init__.py \
--replace-fail 'rscript_path="Rscript"' 'rscript_path="${rscript}"'
substituteInPlace cnvlib/commands.py \
--replace-fail 'default="Rscript"' 'default="${rscript}"'
'';
dependencies = [
biopython
@@ -61,12 +88,42 @@ buildPythonPackage rec {
pomegranate
pyfaidx
pysam
rPackages.DNAcopy
reportlab
rPackages.DNAcopy
scikit-learn
scipy
];
# Make sure R can find the DNAcopy package
postInstall = ''
wrapProgram $out/bin/cnvkit.py \
--set R_LIBS_SITE "${rPackages.DNAcopy}/library" \
--set MPLCONFIGDIR "/tmp/matplotlib-config"
'';
installCheckPhase = ''
runHook preInstallCheck
${python.executable} -m pytest --deselect=test/test_commands.py::CommandTests::test_batch \
--deselect=test/test_commands.py::CommandTests::test_segment_hmm
cd test
# Set matplotlib config directory for the tests
export MPLCONFIGDIR="/tmp/matplotlib-config"
export HOME="/tmp"
mkdir -p "$MPLCONFIGDIR"
# Use the installed binary - it's already wrapped with R_LIBS_SITE
make cnvkit="$out/bin/cnvkit.py" || {
echo "Make tests failed"
exit 1
}
runHook postInstallCheck
'';
doInstallCheck = true;
pythonImportsCheck = [ "cnvlib" ];
nativeCheckInputs = [
@@ -74,13 +131,6 @@ buildPythonPackage rec {
R
];
disabledTests = [
# AttributeError: module 'pomegranate' has no attribute 'NormalDistribution'
# https://github.com/etal/cnvkit/issues/815
"test_batch"
"test_segment_hmm"
];
meta = {
homepage = "https://cnvkit.readthedocs.io";
description = "Python library and command-line software toolkit to infer and visualize copy number from high-throughput DNA sequencing data";

View File

@@ -3,20 +3,15 @@
stdenv,
buildPythonPackage,
fetchFromGitHub,
# build-system
fetchpatch,
pytestCheckHook,
setuptools,
# dependencies
apricot-select,
networkx,
numpy,
scikit-learn,
scipy,
torch,
# tests
pytestCheckHook,
}:
buildPythonPackage rec {
@@ -27,34 +22,10 @@ buildPythonPackage rec {
src = fetchFromGitHub {
repo = "pomegranate";
owner = "jmschrei";
# tag = "v${version}";
# No tag for 1.1.2
rev = "e9162731f4f109b7b17ecffde768734cacdb839b";
hash = "sha256-vVoAoZ+mph11ZfINT+yxRyk9rXv6FBDgxBz56P2K95Y=";
tag = "v${version}";
hash = "sha256-p2Gn0FXnsAHvRUeAqx4M1KH0+XvDl3fmUZZ7MiMvPSs=";
};
# _pickle.UnpicklingError: Weights only load failed.
# https://pytorch.org/docs/stable/generated/torch.load.html
postPatch = ''
substituteInPlace \
tests/distributions/test_bernoulli.py \
tests/distributions/test_categorical.py \
tests/distributions/test_exponential.py \
tests/distributions/test_gamma.py \
tests/distributions/test_independent_component.py \
tests/distributions/test_normal_diagonal.py \
tests/distributions/test_normal_full.py \
tests/distributions/test_poisson.py \
tests/distributions/test_student_t.py \
tests/distributions/test_uniform.py \
tests/test_bayes_classifier.py \
tests/test_gmm.py \
tests/test_kmeans.py \
--replace-fail \
'torch.load(".pytest.torch")' \
'torch.load(".pytest.torch", weights_only=False)'
'';
build-system = [ setuptools ];
dependencies = [
@@ -72,11 +43,20 @@ buildPythonPackage rec {
pytestCheckHook
];
disabledTestPaths = lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [
patches = [
# Fix tests for pytorch 2.6
(fetchpatch {
name = "python-2.6.patch";
url = "https://github.com/jmschrei/pomegranate/pull/1142/commits/9ff5d5e2c959b44e569937e777b26184d1752a7b.patch";
hash = "sha256-BXsVhkuL27QqK/n6Fa9oJCzrzNcL3EF6FblBeKXXSts=";
})
];
pytestFlagsArray = lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [
# AssertionError: Arrays are not almost equal to 6 decimals
"=tests/distributions/test_normal_full.py::test_fit"
"=tests/distributions/test_normal_full.py::test_from_summaries"
"=tests/distributions/test_normal_full.py::test_serialization"
"--deselect=tests/distributions/test_normal_full.py::test_fit"
"--deselect=tests/distributions/test_normal_full.py::test_from_summaries"
"--deselect=tests/distributions/test_normal_full.py::test_serialization"
];
disabledTests = [