From 9ec3a0ad57c938e89ef7c7bdc2ace2cbb91c447c Mon Sep 17 00:00:00 2001 From: Aleksandros Sobczyk Date: Fri, 11 Mar 2022 22:57:50 +0100 Subject: [PATCH] Fix test and import for macos (#3) * Fix imports for macos * Bump version 1.0.2 * fix actions --- .github/workflows/python-package.yml | 2 +- README.md | 39 ++++++++++++++++++---------- pylspack/linalg_kernels.py | 22 +++++++++------- setup.py | 2 +- src/csr_kernels.h | 7 ++--- test/__init__.py | 0 test/test_csrrk.py | 2 +- test/test_csrsqn.py | 6 ++--- test/test_gemm.py | 6 ++--- test/test_leverage_scores.py | 4 +-- test/test_rmdsc.py | 2 +- test/test_rmsqn.py | 6 ++--- test/test_scale.py | 4 +-- test/test_set_randn.py | 2 +- test/test_set_value.py | 2 +- 15 files changed, 61 insertions(+), 45 deletions(-) delete mode 100644 test/__init__.py diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 04010b0..5bd1c8f 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -96,4 +96,4 @@ jobs: yapf --quiet --style "{based_on_style: pep8, blank_line_before_nested_class_or_def: true, indent_dictionary_value: true, dedent_closing_brackets: true, column_limit: 99}" --recursive . - name: Test with pytest run: | - export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:"$(pip show pylspack | grep 'Location:' | awk '{print $2}')/pylspack/" && python3 -m pytest -svvv test + cd test && python3 -m pytest -svvv . diff --git a/README.md b/README.md index c09ace0..4268bae 100644 --- a/README.md +++ b/README.md @@ -17,19 +17,29 @@ As already noted, the implementation is designed for existing data structures of The C++ API **can** be used as a standalone package, but it has not been tested. ### Citation -The corresponding publication https://doi.org/10.1137/20m1314471 can be cited as follows: +If you use this software in academic work, please consider citing the corresponding publications: +- https://doi.org/10.1137/20m1314471 ``` -@article{Sobczyk2021, - doi = {10.1137/20m1314471}, - url = {https://doi.org/10.1137/20m1314471}, - year = {2021}, - publisher = {Society for Industrial {\&} Applied Mathematics ({SIAM})}, - volume = {42}, - number = {3}, - pages = {1199--1228}, - author = {Aleksandros Sobczyk and Efstratios Gallopoulos}, - title = {Estimating Leverage Scores via Rank Revealing Methods and Randomization}, - journal = {{SIAM} Journal on Matrix Analysis and Applications} +@article{sobczyk2021estimating, + title={Estimating leverage scores via rank revealing methods and randomization}, + author={Sobczyk, Aleksandros and Gallopoulos, Efstratios}, + journal={SIAM Journal on Matrix Analysis and Applications}, + volume={42}, + number={3}, + pages={1199--1228}, + year={2021}, + doi={10.1137/20m1314471}, + url={https://doi.org/10.1137/20m1314471}, + publisher={SIAM} +} +``` +- https://doi.org/10.48550/arxiv.2203.02798 +``` +@article{sobczyk2022pylspack, + title={pylspack: Parallel algorithms and data structures for sketching, column subset selection, regression and leverage scores}, + author={Sobczyk, Aleksandros and Gallopoulos, Efstratios}, + journal={arXiv preprint arXiv:2203.02798}, + year={2022} } ``` @@ -92,11 +102,12 @@ pip install git+https://github.com/IBM/pylspack To run the tests: ```bash +python3 -m pip install -r test_requirements.txt +cd test +python3 -m pytest -svvv . # If you get an error about liblinalg_kernels.so, do the following: # PYLSPACK_LOCATION="$(pip show pylspack | grep Location: | awk '{print $2}')/pylspack/" # export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${PYLSPACK_LOCATION} -pip install -r test_requirements.txt -pytest -svvv test ``` ## Contributing diff --git a/pylspack/linalg_kernels.py b/pylspack/linalg_kernels.py index 68db3bf..4602eda 100644 --- a/pylspack/linalg_kernels.py +++ b/pylspack/linalg_kernels.py @@ -7,13 +7,17 @@ from scipy.sparse import csr_matrix libdir = os.path.dirname(os.path.realpath(__file__)) -try: - libfile = glob.glob('{}/liblinalg_kernels*.so'.format(libdir))[0] - ext_lib = CDLL(os.path.join(libdir, libfile)) -except Exception as e: - print('Warning: could not find {}/liblinalg_kernels*.so'.format(libdir)) - print('Caught exception: {}. Trying to load from LD_LIBRARY_PATH...'.format(e)) - ext_lib = CDLL('liblinalg_kernels.so') +libfile = glob.glob(f'{libdir}/liblinalg_kernels*') +if libfile: + ext_lib = CDLL(os.path.join(libdir, libfile[0])) +else: + print(f'Warning: could not find {libdir}/liblinalg_kernels*') + try: + print('Trying to fild liblinalg_kernels.so from LD_LIBRARY_PATH...') + ext_lib = CDLL('liblinalg_kernels.so') + except Exception: + print('Trying to fild liblinalg_kernels.dylib from LD_LIBRARY_PATH...') + ext_lib = CDLL('liblinalg_kernels.dylib') # arg types ext_lib.csrcgs.argtypes = [ @@ -48,12 +52,12 @@ def assert_shape(a: int, b: int) -> None: def assert_dtype(A: np.ndarray, dtype: str) -> None: - if A.dtype != dtype: + if A.dtype != dtype: # type: ignore raise TypeError('unsupported dtype: {}.'.format(A.dtype)) def assert_contiguous_type(A: np.ndarray, contiguous_type: str) -> None: - if A.flags[contiguous_type] is False: + if A.flags[contiguous_type] is False: # type: ignore raise TypeError('array is not {} as expected.'.format(contiguous_type)) diff --git a/setup.py b/setup.py index d72d0c6..7be2fe4 100644 --- a/setup.py +++ b/setup.py @@ -47,7 +47,7 @@ def read_readme(fname): setup( name='pylspack', - version='1.0.1', + version='1.0.2', description='Python package for leverage scores computations.', author='Sobczyk Aleksandros', author_email='obc@zurich.ibm.com', diff --git a/src/csr_kernels.h b/src/csr_kernels.h index 643c637..37eb2b1 100644 --- a/src/csr_kernels.h +++ b/src/csr_kernels.h @@ -186,6 +186,7 @@ extern "C" { double *_C; int i, j, k, up, lo; + double *G = new double[m]; #pragma omp parallel private(_C, i, j, k, up, lo) { double A_ki; @@ -197,11 +198,11 @@ extern "C" { row_limits.second = block_size * ( thread_id + 1 ); row_limits.second = std::min( row_limits.second, m ); const int n_rows = row_limits.second - row_limits.first; - double *_G = new double[n_rows]; std::random_device rd{}; std::mt19937_64 gen{rd()}; std::normal_distribution dist; + double *_G = &( G[ row_limits.first ] ); for ( k = 0; k < n; ++k ) { lo = A_indptr[k]; up = A_indptr[k + 1]; @@ -224,9 +225,9 @@ extern "C" { } } } - - delete[] _G; } + + delete[] G; double scale_factor = static_cast( 1 ) / sqrt( static_cast( m ) ); scale( m, d, C, scale_factor ); } diff --git a/test/__init__.py b/test/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/test/test_csrrk.py b/test/test_csrrk.py index dd7b833..f4cdb98 100644 --- a/test/test_csrrk.py +++ b/test/test_csrrk.py @@ -2,7 +2,7 @@ from scipy.sparse import csr_matrix import pytest from pylspack.linalg_kernels import csrrk -from .utils import ( +from utils import ( eps_machine, min_size, max_size, alpha_beta_pairs_generic, get_random_matrices, set_arrays_elements_to_value ) diff --git a/test/test_csrsqn.py b/test/test_csrsqn.py index c28af97..2733e3b 100644 --- a/test/test_csrsqn.py +++ b/test/test_csrsqn.py @@ -2,11 +2,11 @@ from scipy.sparse import csr_matrix import pytest from pylspack.linalg_kernels import csrsqn -from .utils import ( +from utils import ( eps_machine, alpha_beta_pairs_generic, get_random_matrices, set_arrays_elements_to_value ) -from .utils import A_shapes_generic as A_shapes -from .utils import B_shapes_generic as B_shapes +from utils import A_shapes_generic as A_shapes +from utils import B_shapes_generic as B_shapes def execute_and_check(alpha: float, A: csr_matrix, B: np.ndarray, beta: float, C: np.ndarray): diff --git a/test/test_gemm.py b/test/test_gemm.py index ee04b9c..747c149 100644 --- a/test/test_gemm.py +++ b/test/test_gemm.py @@ -1,11 +1,11 @@ import numpy as np import pytest from pylspack.linalg_kernels import gemm -from .utils import ( +from utils import ( eps_machine, alpha_beta_pairs_generic, get_random_matrices, set_arrays_elements_to_value ) -from .utils import A_shapes_generic as A_shapes -from .utils import B_shapes_generic as B_shapes +from utils import A_shapes_generic as A_shapes +from utils import B_shapes_generic as B_shapes def execute_and_check(alpha: float, A: np.ndarray, B: np.ndarray, beta: float, C: np.ndarray): diff --git a/test/test_leverage_scores.py b/test/test_leverage_scores.py index 3945d9e..e870cf7 100644 --- a/test/test_leverage_scores.py +++ b/test/test_leverage_scores.py @@ -6,7 +6,7 @@ sample_columns, ls_via_inv_gram, ls_via_sketched_svd, ls_hrn_exact, ls_hrn_approx, get_rank_from_vector ) -from .utils import eps_machine +from utils import eps_machine density = [0.1, 0.3, 1] matrices = [ @@ -19,7 +19,7 @@ def test_get_rank_from_vector(): s = np.zeros((10, )) assert get_rank_from_vector(s, rcond=0.5) == 0 - s = np.arange(10, 0, -1) + s = np.arange(10, 0, -1) # type: ignore assert get_rank_from_vector(s, rcond=0.65) == 4 assert get_rank_from_vector(s, rcond=0.05) == 10 assert get_rank_from_vector(s, rcond=0) == 10 diff --git a/test/test_rmdsc.py b/test/test_rmdsc.py index fe3f4a7..cec9e53 100644 --- a/test/test_rmdsc.py +++ b/test/test_rmdsc.py @@ -1,7 +1,7 @@ import numpy as np import pytest from pylspack.linalg_kernels import rmdsc -from .utils import eps_machine, min_size, max_size, set_arrays_elements_to_value +from utils import eps_machine, min_size, max_size, set_arrays_elements_to_value B_shapes = [(1, 1), (3, 1), (1, 3), (3, 3), (17, 5), (17, 17), (237, 631), (631, 237), (237, 237)] D_shapes = [(1, 1), (1, 1), (3, 3), (3, 3), (5, 5), (17, 17), (631, 631), (237, 237), (237, 237)] diff --git a/test/test_rmsqn.py b/test/test_rmsqn.py index b45e4b4..d5a4bc6 100644 --- a/test/test_rmsqn.py +++ b/test/test_rmsqn.py @@ -1,11 +1,11 @@ import numpy as np import pytest from pylspack.linalg_kernels import rmsqn -from .utils import ( +from utils import ( eps_machine, alpha_beta_pairs_generic, get_random_matrices, set_arrays_elements_to_value ) -from .utils import A_shapes_generic as A_shapes -from .utils import B_shapes_generic as B_shapes +from utils import A_shapes_generic as A_shapes +from utils import B_shapes_generic as B_shapes def execute_and_check(alpha: float, A: np.ndarray, B: np.ndarray, beta: float, C: np.ndarray): diff --git a/test/test_scale.py b/test/test_scale.py index e57d43d..692299c 100644 --- a/test/test_scale.py +++ b/test/test_scale.py @@ -1,8 +1,8 @@ import numpy as np import pytest from pylspack.linalg_kernels import scale -from .utils import eps_machine -from .utils import A_shapes_generic as A_shapes +from utils import eps_machine +from utils import A_shapes_generic as A_shapes def execute_and_check(A: np.ndarray, alpha: float): diff --git a/test/test_set_randn.py b/test/test_set_randn.py index 101ac29..1affd80 100644 --- a/test/test_set_randn.py +++ b/test/test_set_randn.py @@ -1,7 +1,7 @@ import numpy as np import pytest from pylspack.linalg_kernels import set_randn -from .utils import A_shapes_generic as A_shapes +from utils import A_shapes_generic as A_shapes def execute_and_check(A: np.ndarray): diff --git a/test/test_set_value.py b/test/test_set_value.py index 653d08d..345fbd9 100644 --- a/test/test_set_value.py +++ b/test/test_set_value.py @@ -1,7 +1,7 @@ import numpy as np import pytest from pylspack.linalg_kernels import set_value -from .utils import A_shapes_generic as A_shapes +from utils import A_shapes_generic as A_shapes def execute_and_check(A: np.ndarray, alpha: float):