itom
Loading...
Searching...
No Matches
pythonDataObject.h
1/* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2020, Institut für Technische Optik (ITO),
5 Universität Stuttgart, Germany
6
7 This file is part of itom.
8
9 itom is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public Licence as published by
11 the Free Software Foundation; either version 2 of the Licence, or (at
12 your option) any later version.
13
14 itom is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17 General Public Licence for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with itom. If not, see <http://www.gnu.org/licenses/>.
21*********************************************************************** */
22
23#ifndef PYTHONDATAOBJECT
24#define PYTHONDATAOBJECT
25
26/* includes */
27#include <string>
28
29#ifndef Q_MOC_RUN
30 #define PY_ARRAY_UNIQUE_SYMBOL itom_ARRAY_API //see numpy help ::array api :: Miscellaneous :: Importing the api (this line must before including global.h)
31 #define NO_IMPORT_ARRAY
32
33 #include "pythonWrapper.h"
34#endif
35
36#include "../../DataObject/dataobj.h"
37//#include <qobject.h>
38
39namespace ito
40{
42{
43 public:
44 //-------------------------------------------------------------------------------------------------
45 // typedefs
46 //-------------------------------------------------------------------------------------------------
47 typedef struct
48 {
49 PyObject_HEAD
50 DataObject* dataObject;
51 PyObject* base;
52 }
54
55 typedef struct
56 {
57 const char *name;
58 int typeno;
59 }
61
62 #define PyDataObject_Check(op) PyObject_TypeCheck(op, &PythonDataObject::PyDataObjectType)
63
64 static inline void PyDataObject_SetBase( PyDataObject *op, PyObject *base )
65 {
66 PyObject* x = op->base;
67 Py_XINCREF(base);
68 op->base = base;
69 Py_XDECREF(x);
70 }
71
72 //-------------------------------------------------------------------------------------------------
73 // constructor, deconstructor, alloc, dellaoc
74 //-------------------------------------------------------------------------------------------------
75
76 static void PyDataObject_dealloc(PyDataObject *self);
77 static PyObject *PyDataObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
78 static int PyDataObject_init(PyDataObject *self, PyObject *args, PyObject *kwds);
79
80 //-------------------------------------------------------------------------------------------------
81 // general members
82 //-------------------------------------------------------------------------------------------------
83 static PyObject *PyDataObject_name(PyDataObject *self);
84
85 static PyObject* PyDataObject_repr(PyDataObject *self);
86
87 static PyObject* PyDataObject_data(PyDataObject *self);
88 static PyObject* PyDataObject_conj(PyDataObject *self);
89 static PyObject* PyDataObject_conjugate(PyDataObject *self);
90 static PyObject* PyDataObject_adj(PyDataObject *self);
91 static PyObject* PyDataObject_adjugate(PyDataObject *self);
92 static PyObject* PyDataObject_trans(PyDataObject *self);
93 static PyObject* PyDataObject_copy(PyDataObject *self, PyObject* args, PyObject *kwds);
94 static PyObject* PyDataObject_astype(PyDataObject *self, PyObject* args, PyObject* kwds);
95 static PyObject* PyDataObject_normalize(PyDataObject *self, PyObject* args, PyObject* kwds);
96 static PyObject* PyDataObject_size(PyDataObject *self, PyObject* args);
97 static PyObject* PyDataObject_makeContinuous(PyDataObject *self);
98 static PyObject* PyDataObject_locateROI(PyDataObject *self);
99 static PyObject* PyDataObject_adjustROI(PyDataObject *self, PyObject* args, PyObject *kwds);
100 static PyObject* PyDataObject_squeeze(PyDataObject *self, PyObject* args);
101
102 static PyObject* PyDataObject_mul(PyDataObject *self, PyObject *args);
103 static PyObject* PyDataObject_div(PyDataObject *self, PyObject *args);
104
105 static PyObject* PyDataObject_reshape(PyDataObject *self, PyObject *args, PyObject *kwds);
106
107 static PyObject* PyDataObject_createMask(PyDataObject *self, PyObject *args, PyObject* kwds);
108
109 // Get / Set metadata / objecttags
110 static PyObject* PyDataObj_SetAxisOffset(PyDataObject *self, PyObject *args);
111 static PyObject* PyDataObj_SetAxisScale(PyDataObject *self, PyObject *args);
112 static PyObject* PyDataObj_SetAxisDescription(PyDataObject *self, PyObject *args);
113 static PyObject* PyDataObj_SetAxisUnit(PyDataObject *self, PyObject *args);
114 static PyObject* PyDataObj_PhysToPix(PyDataObject *self, PyObject *args, PyObject *kwds);
115 static PyObject* PyDataObj_PixToPhys(PyDataObject *self, PyObject *args, PyObject *kwds);
116 static PyObject* PyDataObj_CopyMetaInfo(PyDataObject *self, PyObject *args, PyObject *kwds);
117
118 static PyObject* PyDataObj_SetTag(PyDataObject *self, PyObject *args);
119 static PyObject* PyDataObj_DeleteTag(PyDataObject *self, PyObject *args);
120 static PyObject* PyDataObj_TagExists(PyDataObject *self, PyObject *args);
121 static PyObject* PyDataObj_GetTagListSize(PyDataObject *self);
122 static PyObject* PyDataObj_AddToProtocol(PyDataObject *self, PyObject *args);
123
124 // end tags
125
126 static PyObject* PyDataObject_RichCompare(PyDataObject *self, PyObject *other, int cmp_op);
127
128 static PyGetSetDef PyDataObject_getseters[];
129 static PyObject* PyDataObj_GetDims(PyDataObject *self, void *closure);
130 static PyObject* PyDataObj_GetType(PyDataObject *self, void *closure);
131 static PyObject* PyDataObj_GetShape(PyDataObject *self, void *closure);
132 static PyObject* PyDataObj_GetContinuous(PyDataObject *self, void *closure);
133
134 static PyObject* PyDataObject_getTagDict(PyDataObject *self, void *closure);
135 static int PyDataObject_setTagDict(PyDataObject *self, PyObject *value, void *closure);
136
137 static PyObject* PyDataObject_getTags(PyDataObject *self, void *closure);
138 static int PyDataObject_setTags(PyDataObject *self, PyObject *value, void *closure);
139
140 static PyObject* PyDataObject_getAxisScales(PyDataObject *self, void *closure);
141 static int PyDataObject_setAxisScales(PyDataObject *self, PyObject *value, void *closure);
142
143 static PyObject* PyDataObject_getAxisOffsets(PyDataObject *self, void *closure);
144 static int PyDataObject_setAxisOffsets(PyDataObject *self, PyObject *value, void *closure);
145
146 static PyObject* PyDataObject_getAxisDescriptions(PyDataObject *self, void *closure);
147 static int PyDataObject_setAxisDescriptions(PyDataObject *self, PyObject *value, void *closure);
148
149 static PyObject* PyDataObject_getAxisUnits(PyDataObject *self, void *closure);
150 static int PyDataObject_setAxisUnits(PyDataObject *self, PyObject *value, void *closure);
151
152 static PyObject* PyDataObject_getValueUnit(PyDataObject *self, void *closure);
153 static int PyDataObject_setValueUnit(PyDataObject *self, PyObject *value, void *closure);
154
155 static PyObject* PyDataObject_getValueDescription(PyDataObject *self, void *closure);
156 static int PyDataObject_setValueDescription(PyDataObject *self, PyObject *value, void *closure);
157
158 static PyObject* PyDataObject_getValueScale(PyDataObject *self, void *closure);
159 static PyObject* PyDataObject_getValueOffset(PyDataObject *self, void *closure);
160
161 static PyObject* PyDataObject_getReal(PyDataObject *self, void *closure);
162 static int PyDataObject_setReal(PyDataObject *self, PyObject *value, void *closure);
163
164 static PyObject* PyDataObject_getImag(PyDataObject *self, void *closure);
165 static int PyDataObject_setImag(PyDataObject *self, PyObject *value, void *closure);
166
167 static PyObject* PyDataObject_abs(PyDataObject *self, void *closure);
168 static PyObject* PyDataObject_arg(PyDataObject *self, void *closure);
169
170 static int PyDataObject_setXYRotationalMatrix(PyDataObject *self, PyObject *value, void *closure);
171 static PyObject* PyDataObject_getXYRotationalMatrix(PyDataObject *self, void *closure);
172
173 static PyObject* PyDataObject_getValue(PyDataObject *self, void *closure);
174 static int PyDataObject_setValue(PyDataObject *self, PyObject *value, void *closure);
175
176 static PyObject* PyDataObject_transpose(PyDataObject *self, void *closure);
177
178 static PyObject* PyDataObj_Array_StructGet(PyDataObject *self);
179 static PyObject* PyDataObj_Array_Interface(PyDataObject *self);
180 static PyObject* PyDataObj_Array_(PyDataObject *self, PyObject *args);
181
182 static PyObject* PyDataObject_real(PyDataObject *self);
183 static PyObject* PyDataObject_imag(PyDataObject *self);
184
185 static PyObject* PyDataObj_ToGray(PyDataObject *self, PyObject *args, PyObject *kwds);
186 static PyObject* PyDataObj_ToNumpyColor(PyDataObject *self, PyObject *args, PyObject *kwds);
187 static PyObject* PyDataObj_SplitColor(PyDataObject *self, PyObject *args, PyObject *kwds);
188
189 static PyObject* PyDataObj_ToList(PyDataObject *self);
190 static PyObject* PyDataObj_At(ito::DataObject *dataObj, const unsigned int *idx);
191 static PyObject* PyDataObj_ToListRecursive(ito::DataObject *dataObj, unsigned int *currentIdx, int iterationIndex);
192
193
194 static void PyDataObj_Capsule_Destructor(PyObject* capsule); //called if capsule (dataObject exported by __array__struct_) is destroyed
195
196 static PyObject* PyDataObj_Reduce(PyDataObject *self, PyObject *args);
197 static PyObject* PyDataObj_SetState(PyDataObject *self, PyObject *args);
198 static PyObject* PyDataObj_lineCut(PyDataObject *self, PyObject *args);
199
200 //-------------------------------------------------------------------------------------------------
201 // number protocol
202 //
203 // python note: Binary and ternary functions must check the type of all their operands, and implement
204 // the necessary conversions (at least one of the operands is an instance of the defined type).
205 // If the operation is not defined for the given operands, binary and ternary functions must return
206 // Py_NotImplemented, if another error occurred they must return NULL and set an exception.
207 //-------------------------------------------------------------------------------------------------
208 static PyObject* PyDataObj_nbAdd(PyObject* o1, PyObject* o2);
209 static PyObject* PyDataObj_nbSubtract(PyObject* o1, PyObject* o2);
210 static PyObject* PyDataObj_nbMultiply(PyObject* o1, PyObject* o2);
211 static PyObject* PyDataObj_nbMatrixMultiply(PyObject* o1, PyObject* o2);
212 static PyObject* PyDataObj_nbDivide(PyObject* o1, PyObject* o2);
213 static PyObject* PyDataObj_nbRemainder(PyObject* o1, PyObject* o2);
214 static PyObject* PyDataObj_nbDivmod(PyObject* o1, PyObject* o2);
215 static PyObject* PyDataObj_nbPower(PyObject* o1, PyObject* o2, PyObject* o3);
216 static PyObject* PyDataObj_nbNegative(PyObject* o1);
217 static PyObject* PyDataObj_nbPositive(PyObject* o1);
218 static PyObject* PyDataObj_nbAbsolute(PyObject* o1);
219 static PyObject* PyDataObj_nbInvert(PyObject* o1);
220 static PyObject* PyDataObj_nbLshift(PyObject* o1, PyObject* o2);
221 static PyObject* PyDataObj_nbRshift(PyObject* o1, PyObject* o2);
222 static PyObject* PyDataObj_nbAnd(PyObject* o1, PyObject* o2);
223 static PyObject* PyDataObj_nbXor(PyObject* o1, PyObject* o2);
224 static PyObject* PyDataObj_nbOr(PyObject* o1, PyObject* o2);
225 static int PyDataObj_nbBool(PyDataObject *self);
226 static PyObject* PyDataObj_nbInplaceAdd(PyObject* o1, PyObject* o2);
227 static PyObject* PyDataObj_nbInplaceSubtract(PyObject* o1, PyObject* o2);
228 static PyObject* PyDataObj_nbInplaceMultiply(PyObject* o1, PyObject* o2);
229 static PyObject* PyDataObj_nbInplaceRemainder(PyObject* o1, PyObject* o2);
230 static PyObject* PyDataObj_nbInplacePower(PyObject* o1, PyObject* o2, PyObject* o3);
231 static PyObject* PyDataObj_nbInplaceLshift(PyObject* o1, PyObject* o2);
232 static PyObject* PyDataObj_nbInplaceRshift(PyObject* o1, PyObject* o2);
233 static PyObject* PyDataObj_nbInplaceAnd(PyObject* o1, PyObject* o2);
234 static PyObject* PyDataObj_nbInplaceXor(PyObject* o1, PyObject* o2);
235 static PyObject* PyDataObj_nbInplaceOr(PyObject* o1, PyObject* o2);
236 static PyObject* PyDataObj_nbInplaceTrueDivide(PyObject* o1, PyObject* o2);
237 static PyObject* PyDataObj_nbInplaceMatrixMultiply(PyObject* o1, PyObject* o2);
238
239 //-------------------------------------------------------------------------------------------------
240 // iterator protocol
241 //-------------------------------------------------------------------------------------------------
242 static PyObject* PyDataObj_getiter(PyDataObject* self); //getiterfunc tp_iter;
243
244 //-------------------------------------------------------------------------------------------------
245 // mapping protocol
246 //-------------------------------------------------------------------------------------------------
247 static int PyDataObj_mappingLength(PyDataObject* self);
248 static PyObject* PyDataObj_mappingGetElem(PyDataObject* self, PyObject* key);
249 static int PyDataObj_mappingSetElem(PyDataObject* self, PyObject* key, PyObject* value);
250
251 //-------------------------------------------------------------------------------------------------
252 // type structures
253 //-------------------------------------------------------------------------------------------------
254 static PyMemberDef PyDataObject_members[];
255 static PyMethodDef PyDataObject_methods[];
256 static PyTypeObject PyDataObjectType;
257 static PyModuleDef PyDataObjectModule;
258
259 static PyNumberMethods PyDataObject_numberProtocol;
260 static PyMappingMethods PyDataObject_mappingProtocol;
261
262 //-------------------------------------------------------------------------------------------------
263 // helper methods
264 //-------------------------------------------------------------------------------------------------
265 static RetVal PyDataObj_ParseCreateArgs(PyObject *args, PyObject *kwds, int &typeno, std::vector<unsigned int> &dims, unsigned char &continuous);
266
267 static PyDataObjectTypes PyDataObject_types[];
268 static int dObjTypeFromName(const char *name);
269 static const char* typeNumberToName(int typeno);
270 static int numDataTypes();
271
272 static PyDataObject* createEmptyPyDataObject();
273 static PyObject* createPyDataObjectFromArray(PyObject *npArray); //returns NULL with set Python exception if npArray could not be converted to data object
274
275 static bool checkPyDataObject(int number, PyObject* o1 = NULL, PyObject* o2 = NULL, PyObject* o3 = NULL);
276
277 static RetVal parseTypeNumber(int typeno, char &typekind, int &itemsize);
278 static int getDObjTypeOfNpArray(char typekind, int itemsize);
279 static int getCompatibleDObjTypeOfNpArray(char typekind, int itemsize);
280 static int getNpTypeFromDataObjectType(int type);
281 static std::string getNpDTypeStringFromNpDTypeEnum(const int type);
282
283 static ito::RetVal copyNpArrayValuesToDataObject(PyArrayObject *npNdArray, ito::DataObject *dataObject, ito::tDataType type);
284 static int PyDataObj_CreateFromShapeTypeData(PyDataObject *self, PyObject *args, PyObject *kwds); //helper method for PyDataObject_init
285 static int PyDataObj_CreateFromNpNdArrayAndType(PyDataObject *self, PyObject *args, PyObject *kwds, bool addNpOrgTags); //helper method for PyDataObject_init
286 static bool PyDataObj_CopyFromDatetimeNpNdArray(PyDataObject *self, PyArrayObject *dateTimeArray, int dims, const int* sizes);
287 static bool PyDataObj_CopyFromTimedeltaNpNdArray(PyDataObject *self, PyArrayObject *timeDeltaArray, int dims, const int* sizes);
288
289
290 //-------------------------------------------------------------------------------------------------
291 // static type methods
292 //-------------------------------------------------------------------------------------------------
293 //static PyObject* PyDataObj_StaticArange(PyDataObject *self, PyObject *args);
294 static PyObject* PyDataObj_StaticZeros(PyObject *self, PyObject *args, PyObject *kwds);
295 static PyObject* PyDataObj_StaticOnes(PyObject *self, PyObject *args, PyObject *kwds);
296 static PyObject* PyDataObj_StaticNans(PyObject *self, PyObject *args, PyObject *kwds);
297 static PyObject* PyDataObj_StaticRand(PyObject *self, PyObject *args, PyObject *kwds);
298 static PyObject* PyDataObj_StaticRandN(PyObject *self, PyObject *args, PyObject *kwds);
299 static PyObject* PyDataObj_StaticEye(PyObject *self, PyObject *args, PyObject *kwds);
300 static PyObject* PyDataObj_StaticFromNumpyColor(PyObject *self, PyObject *args, PyObject *kwds);
301 static PyObject* PyDataObj_dstack(PyObject *self, PyObject *args, PyObject *kwds);
302
303
304
305 //#################################################################################################
306 // ITERATOR METHODS
307 //#################################################################################################
308
309 typedef struct
310 {
311 PyObject_HEAD
314 Py_ssize_t len;
315 PyObject* base;
316 }
318
319 //-------------------------------------------------------------------------------------------------
320 // constructor, deconstructor, alloc, dellaoc
321 //-------------------------------------------------------------------------------------------------
322
323 static void PyDataObjectIter_dealloc(PyDataObjectIter *self);
324 static PyObject *PyDataObjectIter_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
325 static int PyDataObjectIter_init(PyDataObjectIter *self, PyObject *args, PyObject *kwds);
326
327 static PyObject* PyDataObjectIter_iternext(PyDataObjectIter* self);
328 static PyObject* PyDataObjectIter_len(PyDataObjectIter* self);
329
330 //-------------------------------------------------------------------------------------------------
331 // type structures
332 //-------------------------------------------------------------------------------------------------
333 static PyMethodDef PyDataObjectIter_methods[];
334 static PyTypeObject PyDataObjectIterType;
335
336};
337
338} //end namespace ito
339
340#endif
constant iterator through data object
Definition dataobj.h:456
dataObject contains a n-dimensional matrix
Definition dataobj.h:591
Definition pythonDataObject.h:42
Class for managing status values (like errors or warning)
Definition retVal.h:54
Definition apiFunctionsGraph.cpp:40
tDataType
Definition typeDefs.h:88
Definition pythonDataObject.h:48
Definition pythonDataObject.h:310
Definition pythonDataObject.h:56