1{ lib
2, stdenv
3, fetchPypi
4, python
5, buildPythonPackage
6, gfortran
7, hypothesis
8, pytestCheckHook
9, typing-extensions
10, blas
11, lapack
12, writeTextFile
13, cython
14, pythonAtLeast
15, pythonOlder
16}:
17
18assert (!blas.isILP64) && (!lapack.isILP64);
19
20let
21 cfg = writeTextFile {
22 name = "site.cfg";
23 text = lib.generators.toINI {} {
24 ${blas.implementation} = {
25 include_dirs = "${lib.getDev blas}/include:${lib.getDev lapack}/include";
26 library_dirs = "${blas}/lib:${lapack}/lib";
27 runtime_library_dirs = "${blas}/lib:${lapack}/lib";
28 libraries = "lapack,lapacke,blas,cblas";
29 };
30 lapack = {
31 include_dirs = "${lib.getDev lapack}/include";
32 library_dirs = "${lapack}/lib";
33 runtime_library_dirs = "${lapack}/lib";
34 };
35 blas = {
36 include_dirs = "${lib.getDev blas}/include";
37 library_dirs = "${blas}/lib";
38 runtime_library_dirs = "${blas}/lib";
39 };
40 };
41 };
42in buildPythonPackage rec {
43 pname = "numpy";
44 version = "1.25.2";
45 format = "setuptools";
46 disabled = pythonOlder "3.9" || pythonAtLeast "3.12";
47
48 src = fetchPypi {
49 inherit pname version;
50 extension = "tar.gz";
51 hash = "sha256-/WCOGcjXxVAh3/1Dv+VJL6uMwQXMiYb4E/jDwEizh2A=";
52 };
53
54 patches = [
55 # Disable `numpy/core/tests/test_umath.py::TestComplexFunctions::test_loss_of_precision[complex256]`
56 # on x86_64-darwin because it fails under Rosetta 2 due to issues with trig functions and
57 # 80-bit long double complex numbers.
58 ./disable-failing-long-double-test-Rosetta-2.patch
59 ]
60 # We patch cpython/distutils to fix https://bugs.python.org/issue1222585
61 # Patching of numpy.distutils is needed to prevent it from undoing the
62 # patch to distutils.
63 ++ lib.optionals python.hasDistutilsCxxPatch [
64 ./numpy-distutils-C++.patch
65 ];
66
67 postPatch = ''
68 # fails with multiple errors because we are not using the pinned setuptools version
69 # see https://github.com/numpy/numpy/blob/v1.25.0/pyproject.toml#L7
70 # error: option --single-version-externally-managed not recognized
71 # TypeError: dist must be a Distribution instance
72 rm numpy/core/tests/test_cython.py
73 '';
74
75 nativeBuildInputs = [ gfortran cython ];
76 buildInputs = [ blas lapack ];
77
78 # Causes `error: argument unused during compilation: '-fno-strict-overflow'` due to `-Werror`.
79 hardeningDisable = lib.optionals stdenv.cc.isClang [ "strictoverflow" ];
80
81 # we default openblas to build with 64 threads
82 # if a machine has more than 64 threads, it will segfault
83 # see https://github.com/xianyi/OpenBLAS/issues/2993
84 preConfigure = ''
85 sed -i 's/-faltivec//' numpy/distutils/system_info.py
86 export NPY_NUM_BUILD_JOBS=$NIX_BUILD_CORES
87 export OMP_NUM_THREADS=$((NIX_BUILD_CORES > 64 ? 64 : NIX_BUILD_CORES))
88 '';
89
90 preBuild = ''
91 ln -s ${cfg} site.cfg
92 '';
93
94 enableParallelBuilding = true;
95
96 nativeCheckInputs = [
97 pytestCheckHook
98 hypothesis
99 typing-extensions
100 ];
101
102 preCheck = ''
103 cd "$out"
104 '';
105
106 # https://github.com/numpy/numpy/blob/a277f6210739c11028f281b8495faf7da298dbef/numpy/_pytesttester.py#L180
107 pytestFlagsArray = [
108 "-m" "not\\ slow" # fast test suite
109 ];
110
111 # https://github.com/numpy/numpy/issues/24548
112 disabledTests = lib.optionals stdenv.isi686 [
113 "test_new_policy" # AssertionError: assert False
114 "test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
115 "test_float_remainder_overflow" # AssertionError: FloatingPointError not raised by divmod
116 "test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
117 ] ++ lib.optionals stdenv.isAarch32 [
118 "test_impossible_feature_enable" # AssertionError: Failed to generate error
119 "test_features" # AssertionError: Failure Detection
120 "test_new_policy" # AssertionError: assert False
121 "test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
122 "test_unary_spurious_fpexception"# AssertionError: Got warnings: [<warnings.WarningMessage object at 0xd1197430>]
123 "test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
124 "test_real" # AssertionError: selectedrealkind(16): expected 10 but got -1
125 "test_quad_precision" # AssertionError: selectedrealkind(32): expected 16 but got -1
126 "test_big_arrays" # ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger tha...
127 "test_multinomial_pvals_float32" # Failed: DID NOT RAISE <class 'ValueError'>
128 ] ++ lib.optionals stdenv.isAarch64 [
129 "test_big_arrays" # OOM on a 16G machine
130 ];
131
132 passthru = {
133 # just for backwards compatibility
134 blas = blas.provider;
135 blasImplementation = blas.implementation;
136 inherit cfg;
137 };
138
139 # Disable test
140 # - test_large_file_support: takes a long time and can cause the machine to run out of disk space
141 NOSE_EXCLUDE="test_large_file_support";
142
143 meta = {
144 description = "Scientific tools for Python";
145 homepage = "https://numpy.org/";
146 license = lib.licenses.bsd3;
147 maintainers = with lib.maintainers; [ fridh ];
148 };
149}