Skip to content
Draft
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ed84626
initial changes on datacontainer and image data
paskino Apr 8, 2025
c090068
torch test working
paskino Apr 8, 2025
dc7c6f0
updates to the pixelwise_binary
paskino Apr 8, 2025
07b00cc
sapyb
paskino Apr 8, 2025
1f1fbd0
fix ImageData
paskino Apr 8, 2025
e8b7a92
add array_api_compat to recipe
paskino Apr 8, 2025
0bba4b0
fix GHA
paskino Apr 8, 2025
6827b4a
Add parametrize to unittest
paskino Apr 9, 2025
b0b32d8
added tests adn bugfixes
paskino Apr 9, 2025
4d54f3b
work in progress
paskino Apr 11, 2025
5db9a1f
modified test
paskino Jun 2, 2025
101175e
convert numpy ComplexWarning to Exception
paskino Jun 2, 2025
c53115b
add function to squeeze an array on multiple dimensions with array_ap…
paskino Jun 3, 2025
8283b87
added cil array_api_ compat
paskino Jun 4, 2025
b695c5b
fix test_fill_dimension_AcquisitionData
paskino Jun 4, 2025
a6c6e56
fixed DataContainer unittests
paskino Jun 4, 2025
4ed3ffb
Merge remote-tracking branch 'origin' into pytorch-hackathon-datacont…
paskino Jun 4, 2025
c9977e9
removed prints
paskino Jun 4, 2025
cbe1f3b
temporarily disable some tests if torch is not installed
paskino Jun 4, 2025
1a84659
skip tests with torch but executes with numpy
paskino Jun 4, 2025
b4b3cda
prevent AttributeError: module 'numpy' has no attribute 'exceptions'
paskino Jun 4, 2025
97fbe35
update the type of the data according to the parameter requested
paskino Jun 4, 2025
fdbdb3a
WIP added allclose
paskino Jun 9, 2025
ba6194b
WIP for TotalVariation
paskino Jun 16, 2025
0129293
WIP
paskino Jun 16, 2025
50c2ffa
removed pysnooper
paskino Jun 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions Wrappers/Python/cil/framework/acquisition_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from .labels import AcquisitionDimension, Backend
from .data_container import DataContainer
from .partitioner import Partitioner

import array_api_compat
from array_api_compat import array_namespace # https://data-apis.org/array-api-compat/

class AcquisitionData(DataContainer, Partitioner):
"""
Expand Down Expand Up @@ -80,20 +81,18 @@ def __init__(self,
raise ValueError("Deprecated: 'dimension_labels' cannot be set with 'allocate()'. Use 'geometry.set_labels()' to modify the geometry before using allocate.")

if array is None:
if dtype is None:
dtype = geometry.dtype
array = numpy.empty(geometry.shape, dtype)

xp = array_api_compat.numpy
dtype = kwargs.get('dtype', xp.float32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you intend to change the default behaviour from dtype = geometry.dtype to xp.float32?

array = xp.empty(geometry.shape, dtype=dtype)
elif issubclass(type(array) , DataContainer):
array = array.as_array()

elif issubclass(type(array) , numpy.ndarray):
# remove singleton dimensions
array = numpy.squeeze(array)

else:
raise TypeError('array must be a CIL type DataContainer or numpy.ndarray got {}'.format(type(array)))

# xp = array_namespace(array)
# idx = numpy.where(numpy.asarray(array.shape) == 1)
# # remove singleton dimensions
# for axis in idx:
# array = xp.squeeze(array, axis=axis)
array = array.squeeze()
if array.shape != geometry.shape:
raise ValueError('Shape mismatch got {} expected {}'.format(array.shape, geometry.shape))

Expand Down
5 changes: 3 additions & 2 deletions Wrappers/Python/cil/framework/acquisition_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2173,8 +2173,9 @@ def allocate(self, value=0, **kwargs):
:type dtype: numpy type, default numpy.float32
'''
dtype = kwargs.get('dtype', self.dtype)

out = AcquisitionData(geometry=self.copy(),
ng = self.copy()
ng.dtype = dtype
out = AcquisitionData(geometry=ng,
dtype=dtype,
suppress_warning=True)

Expand Down
74 changes: 74 additions & 0 deletions Wrappers/Python/cil/framework/array_api_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Updates to array API compatibility layer for CIL
from array_api_compat import array_namespace

def expand_dims(array, axis):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'''Expand dimensions of an array along specified axes.

Parameters
----------
array : array-like
The input array to expand.
axis : int or tuple of int
The axis or axes along which to expand the dimensions.

Returns
-------
array-like
The array with expanded dimensions. It may be a new array or the same array with expanded dimensions.

Raises:
--------
IndexError If provided an invalid axis position, an IndexError should be raised.

Notes:
This function recursively expands the dimensions of the input array along the specified axes if a list or tuple of ints is provided.
'''
xp = array_namespace(array)

if isinstance(axis, int):
return xp.expand_dims(array, axis=axis)
if len(axis) == 1:
return xp.expand_dims(array, axis=axis[0])
axis = list(axis)
ax = axis.pop(0)
if len(axis) == 1:
axis = axis[0]
return expand_dims(xp.expand_dims(array, axis=ax), axis=axis)

def squeeze(array, axis=None):
'''squeezes the array, removing all singleton dimensions recursively

Parameters
----------
array : array-like
The array to squeeze
axis : int or tuple of int, optional
The axis or axes to squeeze. If None, all singleton dimensions are removed.

Returns
-------
array-like
The squeezed array with all singleton dimensions removed. If the input array has no singleton dimensions, it is returned unchanged.
'''
xp = array_namespace(array)
# find and remove singleton dimensions
if axis is None:
s = xp.nonzero(xp.asarray(array.shape) == 1)[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is probably better to do this in pure python, particularly since this function might not be in certain lazy libraries due to being data dependent:

s = tuple(i for i, ax in enumerate(array.shape) if ax==1)

axis = s.tolist()
if len(axis) == 1:
axis = axis[0]
elif len(axis) == 0:
# nothing to do
return array
if isinstance(axis, int):
return xp.squeeze(array, axis=axis)
if len(axis) == 1:
return xp.squeeze(array, axis=axis[0])

# process from the largest axis to the smallest
axis = list(axis)
axis.sort(reverse=True)
ax = axis.pop(0)
if len(axis) == 1:
axis = axis[0]
return squeeze(xp.squeeze(array, axis=ax), axis=axis)
Loading
Loading