Skip to content

Commit

Permalink
define type alias for Array (#82)
Browse files Browse the repository at this point in the history
* define type alias for Array

* remove TypeAlias (not supported in python 3.9)

* use Union

* remove Array type alias from test coverage

* test on 3.12

* update tox

* add numpy matrix

* change matrix

* change matrix

* loosen numpy dep.

* add setuptools for tests

* change GHA

* change matrix

* change matrix

* change matrix

* change matrix

* change matrix

* change matrix

* change matrix

* change GHA and narrow down python version

* change GHA

* squeeze index array

* correct )

* change event mult

* change call to size

* change numpy dep + testing

* disable xp test

* remove torch tests (not compatible with numpy 2.0)

* ignore torch enabled lines for test coverage

* configure 2 test envs
  • Loading branch information
gschramm authored Jul 28, 2024
1 parent 1f83557 commit 187bb68
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 42 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/c_python_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest] # not building on mac-os because there is no OpenMP
python-version: ['3.9','3.11']
python-version: [3.9, 3.12]
test-env: [test-numpy-1, test-numpy-2]

steps:
- uses: actions/checkout@v4
Expand All @@ -47,7 +48,7 @@ jobs:
pdm show --version
- name: Install dependencies and package
run: |
pdm install -d -G test
pdm install -d -G ${{ matrix.test-env }}
- if: matrix.os == 'ubuntu-latest'
name: Run Tests
Expand Down
2 changes: 1 addition & 1 deletion examples/05_algorithms/03_run_mlem_projection_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

# %%
from __future__ import annotations
from array_api_strict._array_object import Array
from parallelproj import Array

import array_api_compat.numpy as xp

Expand Down
2 changes: 1 addition & 1 deletion examples/05_algorithms/04_run_osem_projection_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

# %%
from __future__ import annotations
from array_api_strict._array_object import Array
from parallelproj import Array

import array_api_compat.numpy as xp

Expand Down
2 changes: 1 addition & 1 deletion examples/05_algorithms/07_run_mlem_open_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

# %%
from __future__ import annotations
from array_api_strict._array_object import Array
from parallelproj import Array

import array_api_compat.numpy as xp

Expand Down
2 changes: 1 addition & 1 deletion examples/05_algorithms/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Sequence
import abc
from array_api_strict._array_object import Array
from parallelproj import Array

from types import ModuleType

Expand Down
2 changes: 1 addition & 1 deletion examples/06_listmode_algorithms/01_listmode_mlem.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

# %%
from __future__ import annotations
from array_api_strict._array_object import Array
from parallelproj import Array

import array_api_compat.numpy as xp

Expand Down
2 changes: 1 addition & 1 deletion examples/06_listmode_algorithms/02_listmode_osem.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

# %%
from __future__ import annotations
from array_api_strict._array_object import Array
from parallelproj import Array

import array_api_compat.numpy as xp

Expand Down
23 changes: 16 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ authors = [
{name = "Georg Schramm", email = "[email protected]"},
]
dependencies = [
"numpy~=1.23",
"numpy>=1.23",
"scipy~=1.0",
"array-api-compat~=1.7",
"array-api-strict~=1.0",
"matplotlib~=3.8",
]
requires-python = ">=3.9"
requires-python = ">=3.9,<3.13"
readme = "README.md"
license = {text = "MIT"}
classifiers = [
Expand All @@ -31,13 +30,20 @@ build-backend = "pdm.backend"
distribution = true

[tool.pdm.dev-dependencies]
test = [
test-numpy-1 = [
"pytest>=8.1.1",
"pytest-cov>=4.1.0",
"torch~=2.0",
"array-api-strict~=1.0",
"numpy<2",
"torch~=2.0"
]
numpy122 = [
test-numpy-2 = [
"pytest>=8.1.1",
"pytest-cov>=4.1.0",
"array-api-strict~=1.0",
"numpy>=2"
]

[tool.coverage.report]
exclude_lines = [
"def __repr__",
Expand All @@ -61,6 +67,9 @@ exclude_lines = [
"if cupy_enabled",
"lib_parallelproj_c_fname",
"empty_cuda_cache",
"__str__"
"__str__",
"Array =",
"elif not cupy_enabled and torch_enabled",
"elif cupy_enabled and not torch_enabled"
]

4 changes: 3 additions & 1 deletion src/parallelproj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
lib_parallelproj_c_fname,
lib_parallelproj_cuda_fname,
cuda_kernel_file,
Array,
)

from .operators import (
Expand Down Expand Up @@ -95,9 +96,10 @@
"SinogramSpatialAxisOrder",
"RegularPolygonPETLORDescriptor",
"EqualBlockPETLORDescriptor",
"Array",
]

if os.getenv('PARALLELPROJ_SILENT_IMPORT') is None:
if os.getenv("PARALLELPROJ_SILENT_IMPORT") is None:
print(
f"""
- - - - - - - - - - - -
Expand Down
26 changes: 23 additions & 3 deletions src/parallelproj/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import numpy as np
import array_api_compat
import numpy.ctypeslib as npct
from array_api_strict._array_object import Array

from types import ModuleType
from typing import Union


# check if cuda is present
cuda_present = shutil.which("nvidia-smi") is not None
Expand All @@ -30,9 +31,26 @@
# check if cupy is available
torch_enabled = importlib.util.find_spec("torch") is not None

# define type for cupy or numpy array
if cupy_enabled:
if cupy_enabled and torch_enabled:
import array_api_compat.cupy as cp
import array_api_compat.torch as torch

# type alias for array
Array = Union[np.ndarray, cp.ndarray, torch.Tensor]
elif cupy_enabled and not torch_enabled:
import array_api_compat.cupy as cp

# type alias for array
Array = Union[np.ndarray, cp.ndarray]
elif not cupy_enabled and torch_enabled:
import array_api_compat.torch as torch

# type alias for array
Array = Union[np.ndarray, torch.Tensor]
else:
# type alias for array
Array = np.ndarray


# numpy ctypes lib array definitions
ar_1d_single = npct.ndpointer(dtype=ctypes.c_float, ndim=1, flags="C")
Expand Down Expand Up @@ -1434,10 +1452,12 @@ def count_event_multiplicity(events: Array) -> Array:
tmp = np.unique(events, axis=0, return_counts=True, return_inverse=True)

mu = xp.asarray(tmp[2][tmp[1]], device=dev)
mu = xp.reshape(mu, (array_api_compat.size(mu),))

return mu



def to_numpy_array(x: Array) -> np.ndarray:
"""convert an array to a numpy array
Expand Down
2 changes: 1 addition & 1 deletion src/parallelproj/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
import array_api_compat
from array_api_compat import device
from array_api_strict._array_object import Array
from parallelproj import Array
from collections.abc import Sequence

import parallelproj
Expand Down
4 changes: 2 additions & 2 deletions src/parallelproj/pet_lors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import abc
import enum
import array_api_compat.numpy as np
from array_api_strict._array_object import Array
from parallelproj import Array
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Line3DCollection

Expand Down Expand Up @@ -107,7 +107,7 @@ def __init__(
self._scanner = scanner
self._all_block_pairs = all_block_pairs
self._num_lorendpoints_per_block = self.scanner.modules[0].num_lor_endpoints
self._num_lors_per_block_pair = self._num_lorendpoints_per_block ** 2
self._num_lors_per_block_pair = self._num_lorendpoints_per_block**2

@property
def all_block_pairs(self) -> Array:
Expand Down
2 changes: 1 addition & 1 deletion src/parallelproj/pet_scanners.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import abc
from array_api_strict._array_object import Array
from parallelproj import Array
import matplotlib.pyplot as plt

from types import ModuleType
Expand Down
28 changes: 14 additions & 14 deletions src/parallelproj/projectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import array_api_compat.numpy as np
from array_api_strict._array_object import Array
from parallelproj import Array
import array_api_compat
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
Expand Down Expand Up @@ -885,21 +885,21 @@ def convert_sinogram_to_listmode(

num_events_ss = int(self.xp.sum(ss))

event_start_coords[
event_offset : (event_offset + num_events_ss), :
] = self.xp.take(xstart, event_sino_inds, axis=0)
event_end_coords[
event_offset : (event_offset + num_events_ss), :
] = self.xp.take(xend, event_sino_inds, axis=0)
event_start_coords[event_offset : (event_offset + num_events_ss), :] = (
self.xp.take(xstart, event_sino_inds, axis=0)
)
event_end_coords[event_offset : (event_offset + num_events_ss), :] = (
self.xp.take(xend, event_sino_inds, axis=0)
)

if self.tof:
event_tofbins[
event_offset : (event_offset + num_events_ss)
] = self.xp.full(
num_events_ss,
it - num_tofbins // 2,
device=self._dev,
dtype=self.xp.int16,
event_tofbins[event_offset : (event_offset + num_events_ss)] = (
self.xp.full(
num_events_ss,
it - num_tofbins // 2,
device=self._dev,
dtype=self.xp.int16,
)
)

event_offset += num_events_ss
Expand Down
2 changes: 0 additions & 2 deletions tests/test_projectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ def test_lmprojector(
xstart, xend, img_dim, voxel_size, img_origin
)

assert lm_proj.xp == xp

assert xp.all(lm_proj.event_start_coordinates == xstart)
assert xp.all(lm_proj.event_end_coordinates == xend)

Expand Down
10 changes: 7 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
[tox]
minversion = 4.0
envlist = py{39,312}-np{122,126}
envlist = py{39,312}-numpy{123,20}

[testenv]
groups = test
deps =
np122: numpy>=1.22,<1.23, setuptools
np126: numpy>=1.26,<1.27
setuptools
pytest>=8.1.1
pytest-cov>=4.1.0
array-api-strict~=1.0
numpy123: numpy==1.23
numpy20: numpy>=2.0
passenv = MPLBACKEND, PARALLELPROJ_*
commands =
pytest tests -v --cov-report term-missing --cov "{envsitepackagesdir}/parallelproj" --cov-fail-under=100
Expand Down

0 comments on commit 187bb68

Please sign in to comment.