4545#include " ../organizer/uiOrganizer.h"
4646#include " ../organizer/userOrganizer.h"
4747
48+ #include " numpy/ndarrayobject.h" // Ensure to include the necessary NumPy header
49+
4850#include < qcoreapplication.h>
4951
5052#include < qdir.h>
@@ -4781,9 +4783,7 @@ PyObject* PythonItom::PyLoadMatlabMat(PyObject* /*pSelf*/, PyObject* pArgs)
47814783 }
47824784
47834785 // Arguments must be: filename -> string
4784-
47854786 PyObject* filename = NULL ; // borrowed reference
4786-
47874787 if (!PyArg_ParseTuple (pArgs, " U" , &filename))
47884788 {
47894789 Py_XDECREF (scipyIoModule);
@@ -4792,21 +4792,23 @@ PyObject* PythonItom::PyLoadMatlabMat(PyObject* /*pSelf*/, PyObject* pArgs)
47924792
47934793 PyObject* kwdDict = PyDict_New ();
47944794 PyObject* argTuple = PyTuple_New (1 );
4795- // PyTuple_SetItem(argTuple, 0, PyUnicode_FromString(filename));
47964795 Py_INCREF (filename);
47974796 PyTuple_SetItem (argTuple, 0 , filename); // steals a reference
47984797 PyDict_SetItemString (kwdDict, " squeeze_me" , Py_True);
4798+
4799+ // Get callable loadmat function
47994800 PyObject* loadmatobj = PyUnicode_FromString (" loadmat" );
48004801 PyObject* callable = PyObject_GetAttr (scipyIoModule, loadmatobj);
48014802 Py_DECREF (loadmatobj);
4803+
4804+ // Call loadmat
48024805 resultLoadMat = PyObject_Call (callable, argTuple, kwdDict);
48034806 Py_DECREF (kwdDict);
48044807 Py_DECREF (argTuple);
48054808
48064809 if (resultLoadMat)
48074810 {
4808- // parse every element of dictionary and check if it is a numpy.ndarray. If so, transforms
4809- // it to c-style contiguous form
4811+ // Check if the result is a dictionary
48104812 if (PyDict_Check (resultLoadMat))
48114813 {
48124814 PyObject* key = NULL ;
@@ -4816,76 +4818,89 @@ PyObject* PythonItom::PyLoadMatlabMat(PyObject* /*pSelf*/, PyObject* pArgs)
48164818 PyObject* importMatlabMatAsDataObjectObj =
48174819 PyUnicode_FromString (" importMatlabMatAsDataObject" );
48184820
4821+ // Iterate through dictionary items
48194822 while (PyDict_Next (
48204823 resultLoadMat, &pos, &key, &value)) // borrowed reference to key and value
48214824 {
4822- if (PyArray_Check (value))
4825+ if (PyArray_Check (value)) // Check if the value is a NumPy array
48234826 {
4824- if (PyArray_SIZE ((PyArrayObject*)value) ==
4825- 1 ) // this is either a single value or a matlab-struct
4826- {
4827- PyObject* item = PyArray_ToList ((PyArrayObject*)value); // new ref
4827+ PyArrayObject* array =
4828+ reinterpret_cast <PyArrayObject*>(value); // Cast to PyArrayObject
48284829
4829- if (item && (PyLong_Check (item) || PyFloat_Check (item))) // keep it
4830+ // Check if it's a single element or a struct
4831+ if (PyArray_SIZE (array) == 1 )
4832+ {
4833+ PyObject* item = PyArray_ToList (array); // new ref
4834+ if (item &&
4835+ (PyLong_Check (item) || PyFloat_Check (item))) // Keep it if it's a scalar
48304836 {
48314837 PyDict_SetItem (resultLoadMat, key, item);
48324838 }
4833- else if (value && PyArray_HASFIELDS ((PyArrayObject*)value))
4839+ else if (PyArray_HASFIELDS (
4840+ array)) // Use HASFIELDS macro to check for fields
48344841 {
4835- // it may be that this is a struct which has been generated earlier from
4836- // a npDataObject or dataObject
4837- PyArray_Descr* descr = PyArray_DESCR ((PyArrayObject*)value);
4842+ // It may be that this is a struct
4843+ PyArray_Descr* descr = PyArray_DESCR (array); // Get the descriptor
48384844
4839- if (descr->fields != NULL ) // fields is a dictionary with "fieldname" =>
4840- // type-description for this field
4845+ // Instead of checking 'fields', let's manually check for a specific
4846+ // field
4847+ if (descr) // Check if descriptor exists
48414848 {
4842- if (PyDict_Contains (descr->fields , itomMetaInfoObj))
4849+ // Get the field names if available
4850+ PyObject* fields = PyObject_GetAttrString (
4851+ reinterpret_cast <PyObject*>(descr), " fields" );
4852+ if (fields &&
4853+ PyDict_Check (fields)) // Check if fields is a dictionary
48434854 {
4844- PythonEngine* pyEngine = qobject_cast<PythonEngine*>(
4845- AppManagement::getPythonEngine ());
4846- if (pyEngine)
4855+ // Now check if itomMetaInfoObj exists in the fields
4856+ if (PyDict_Contains (fields, itomMetaInfoObj) == 1 )
48474857 {
4848- PyObject* result = PyObject_CallMethodObjArgs (
4849- pyEngine->m_itomFunctions ,
4850- importMatlabMatAsDataObjectObj,
4851- value,
4852- NULL ); // new reference
4853-
4854- if (result == NULL || PyErr_Occurred ())
4858+ PythonEngine* pyEngine = qobject_cast<PythonEngine*>(
4859+ AppManagement::getPythonEngine ());
4860+ if (pyEngine)
48554861 {
4862+ PyObject* result = PyObject_CallMethodObjArgs (
4863+ pyEngine->m_itomFunctions ,
4864+ importMatlabMatAsDataObjectObj,
4865+ array,
4866+ NULL ); // new reference
4867+
4868+ if (result == NULL || PyErr_Occurred ())
4869+ {
4870+ Py_XDECREF (result);
4871+ Py_XDECREF (scipyIoModule);
4872+ Py_XDECREF (itomMetaInfoObj);
4873+ Py_XDECREF (importMatlabMatAsDataObjectObj);
4874+ PyErr_PrintEx (0 );
4875+ PyErr_SetString (
4876+ PyExc_RuntimeError,
4877+ " Error while parsing imported dataObject or "
4878+ " npDataObject." );
4879+ return NULL ;
4880+ }
4881+ PyDict_SetItem (resultLoadMat, key, result);
48564882 Py_XDECREF (result);
4883+ }
4884+ else
4885+ {
48574886 Py_XDECREF (scipyIoModule);
48584887 Py_XDECREF (itomMetaInfoObj);
48594888 Py_XDECREF (importMatlabMatAsDataObjectObj);
4860- PyErr_PrintEx (0 );
48614889 PyErr_SetString (
4862- PyExc_RuntimeError,
4863- " Error while parsing imported dataObject or "
4864- " npDataObject." );
4890+ PyExc_RuntimeError, " Python Engine not available" );
48654891 return NULL ;
48664892 }
4867- PyDict_SetItem (resultLoadMat, key, result);
4868- Py_XDECREF (result);
4869- }
4870- else
4871- {
4872- Py_XDECREF (scipyIoModule);
4873- Py_XDECREF (itomMetaInfoObj);
4874- Py_XDECREF (importMatlabMatAsDataObjectObj);
4875- PyErr_SetString (
4876- PyExc_RuntimeError, " Python Engine not available" );
4877- return NULL ;
48784893 }
48794894 }
4895+ Py_XDECREF (fields);
48804896 }
48814897 }
4882-
48834898 Py_XDECREF (item);
48844899 }
4885- else // this should be an ordinary numpy. array
4900+ else // Ordinary numpy array
48864901 {
4887- PyObject* newArr = (PyObject*) PyArray_GETCONTIGUOUS (
4888- (PyArrayObject*)value ); // should be new reference
4902+ PyObject* newArr =
4903+ (PyObject*) PyArray_GETCONTIGUOUS (array ); // should be new reference
48894904 PyDict_SetItem (resultLoadMat, key, newArr);
48904905 Py_DECREF (newArr);
48914906 }
@@ -4898,7 +4913,6 @@ PyObject* PythonItom::PyLoadMatlabMat(PyObject* /*pSelf*/, PyObject* pArgs)
48984913 }
48994914
49004915 Py_XDECREF (scipyIoModule);
4901-
49024916 return resultLoadMat;
49034917}
49044918
0 commit comments