Skip to content

Commit

Permalink
mark if x is the first index when building a volume from an array
Browse files Browse the repository at this point in the history
(#92)
  • Loading branch information
denisri committed Feb 7, 2023
1 parent 548f454 commit a294104
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
57 changes: 42 additions & 15 deletions pyaims/src/libpyaims/vector/numpyarrayfunc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,38 @@ using namespace std;
namespace
{

PyObject* transposeNumpyArray( PyObject* a, bool xyzorder )
PyObject* transposeNumpyArray( PyObject* a, bool xyzorder,
int current_x_first = -1 )
{
PyArrayObject *arr = (PyArrayObject *) a;
int i, ndim = PyArray_NDIM( arr );
if( ndim < 2 )
return a;
bool firstindexinc = xyzorder;
for( i=0; i<ndim - 1; ++i )
bool transpose = false;
if( current_x_first >= 0 ) // order is given
transpose = ( bool( current_x_first ) != xyzorder );
else
{
if( PyArray_STRIDES( arr )[i] < PyArray_STRIDES( arr )[i + 1] )
{
firstindexinc = true;
break;
}
else if( PyArray_STRIDES( arr )[i] > PyArray_STRIDES( arr )[i + 1] )
bool firstindexinc = xyzorder;
for( i=0; i<ndim - 1; ++i )
{
firstindexinc = false;
break;
if( PyArray_STRIDES( arr )[i] < PyArray_STRIDES( arr )[i + 1] )
{
firstindexinc = true;
break;
}
else if( PyArray_STRIDES( arr )[i] > PyArray_STRIDES( arr )[i + 1] )
{
firstindexinc = false;
break;
}
// otherwise equal, check next dim
}
// otherwise equal, check next dim
if( xyzorder != firstindexinc )
transpose = true;
}
if( xyzorder == firstindexinc )

if( !transpose )
return a;
PyArray_Dims adims;
vector<npy_intp> dims( ndim );
Expand Down Expand Up @@ -91,7 +101,16 @@ namespace aims
{
PyObject *arr = PyObject_GetAttrString( sipSelf, "_arrayext" );
if( arr )
sipRes = transposeNumpyArray( arr, xyzorder );
{
int current_x_first = -1;
if( PyObject_HasAttrString( sipSelf, "_array_x_is_first" ) )
{
PyObject *x = PyObject_GetAttrString( sipSelf, "_array_x_is_first" );
if( x )
current_x_first = int( bool( PyLong_AsLong( x ) ) );
}
sipRes = transposeNumpyArray( arr, xyzorder, current_x_first );
}
}
// else look if an array has already been built on the object
else if( PyObject_HasAttrString( sipSelf, "_arrayref" ) )
Expand All @@ -103,7 +122,15 @@ namespace aims
if( arr )
{
Py_INCREF( arr );
sipRes = transposeNumpyArray( arr, xyzorder );
int current_x_first = -1;
if( PyObject_HasAttrString( sipSelf, "_array_x_is_first" ) )
{
PyObject *x = PyObject_GetAttrString( sipSelf,
"_array_x_is_first" );
if( x )
current_x_first = int( bool( PyLong_AsLong( x ) ) );
}
sipRes = transposeNumpyArray( arr, xyzorder, current_x_first );
}
else
{
Expand Down
12 changes: 8 additions & 4 deletions pyaims/src/sip/volume.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -715,20 +715,20 @@ The header contains all meta-data.
int nd = PyArray_NDIM( arr );
if( nd < 4 )
nd = 4;
bool xyzorder = false;
std::vector<int> dims( nd, 1 );
int inc = 1, start = 0;
/*
// TODO: retreive exact strides and react accordingly
if( PyArray_NDIM( arr ) >= 2 )
{
if( PyArray_STRIDES( arr )[1] < PyArray_STRIDES( arr )[0] )
{
xyzorder = true;
// increments higher index first
inc = -1;
start = PyArray_NDIM( arr )-1;
// inc = -1;
// start = PyArray_NDIM( arr )-1;
}
}
*/

std::vector<size_t> strides( nd, 1 );

Expand All @@ -748,6 +748,10 @@ The header contains all meta-data.
is still living.
*/
PyObject_SetAttrString( (PyObject *) sipSelf, "_arrayext", a0 );
PyObject *xfirst = PyBool_FromLong( long( xyzorder ) );
Py_DECREF( xfirst );
PyObject_SetAttrString( (PyObject *) sipSelf, "_array_x_is_first",
xfirst );
}
else
{
Expand Down

0 comments on commit a294104

Please sign in to comment.