1 // SWIG file Sample.i 2 3 %ignore OT::Sample::storeToTemporaryFile; 4 %ignore OT::Sample::streamToRFormat; 5 6 %{ 7 #include "openturns/PythonWrappingFunctions.hxx" 8 #include "openturns/SampleImplementation.hxx" 9 #include "openturns/Sample.hxx" 10 %} 11 12 %include Sample_doc.i 13 14 %template(SampleImplementationTypedInterfaceObject) OT::TypedInterfaceObject<OT::SampleImplementation>; 15 %template(SampleCollection) OT::Collection<OT::Sample>; 16 17 #define OT_TYPECHECK_NUMERICALSAMPLE 5 18 19 %typemap(in) const Sample & ($1_basetype temp) { 20 if (! SWIG_IsOK(SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, SWIG_POINTER_NO_NULL))) { 21 try { 22 temp = OT::convert<OT::_PySequence_, OT::Sample>($input); 23 $1 = &temp; catch(OT::InvalidArgumentException &)24 } catch (OT::InvalidArgumentException &) { 25 SWIG_exception(SWIG_TypeError, "Object passed as argument is not convertible to a Sample"); 26 } 27 } 28 } 29 30 %typemap(typecheck,precedence=OT_TYPECHECK_NUMERICALSAMPLE) const Sample & { 31 $1 = SWIG_IsOK(SWIG_ConvertPtr($input, NULL, $1_descriptor, SWIG_POINTER_NO_NULL)) 32 || OT::isAPythonBufferOf<OT::Scalar, 2>($input) 33 || OT::isAPythonSequenceOf<OT::_PySequence_>($input); 34 } 35 36 %apply const Sample & { const OT::Sample & }; 37 38 %typemap(in) const UnsignedIntegerCollection & ($1_basetype temp) { 39 if (! SWIG_IsOK(SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, SWIG_POINTER_NO_NULL))) { 40 try { 41 temp = OT::convert<OT::_PySequence_, OT::Collection<OT::UnsignedInteger> >($input); 42 $1 = &temp; catch(OT::InvalidArgumentException &)43 } catch (OT::InvalidArgumentException &) { 44 SWIG_exception(SWIG_TypeError, "Object passed as argument is not convertible to a collection of UnsignedInteger"); 45 } 46 } 47 } 48 49 %apply const UnsignedIntegerCollection & { const OT::Sample::UnsignedIntegerCollection &, const OT::SampleImplementation::UnsignedIntegerCollection & }; 50 51 %rename(__contains__) OT::Sample::contains; 52 %rename(__baseaddress__) OT::Sample::data; 53 %rename(__elementsize__) OT::Sample::elementSize; 54 55 %include openturns/SampleImplementation.hxx 56 %include openturns/Sample.hxx 57 58 59 %typemap(in) const SampleCollection & ($1_basetype temp) { 60 if (! SWIG_IsOK(SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, SWIG_POINTER_NO_NULL))) { 61 try { 62 temp = OT::convert<OT::_PySequence_, OT::Collection<OT::Sample> >($input); 63 $1 = &temp; catch(OT::InvalidArgumentException &)64 } catch (OT::InvalidArgumentException &) { 65 SWIG_exception(SWIG_TypeError, "Object passed as argument is not convertible to a collection of Sample"); 66 } 67 } 68 } 69 70 %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) const SampleCollection & { 71 $1 = SWIG_IsOK(SWIG_ConvertPtr($input, NULL, $1_descriptor, SWIG_POINTER_NO_NULL)) 72 || OT::canConvertCollectionObjectFromPySequence< OT::Sample >($input); 73 } 74 75 %apply const SampleCollection & { const OT::ProcessSample::SampleCollection &}; 76 77 %pythoncode %{ 78 # This code has been added to conform to Numpy ndarray interface 79 # that tries to reuse the data stored in the Sample (zero copy) 80 # see http://docs.scipy.org/doc/numpy/reference/arrays.interface.html#arrays-interface 81 # for details. 82 # See python doc http://docs.python.org/reference/datamodel.html?highlight=getattribute#object.__getattribute__ 83 # for details on how to write such a method. 84 def Sample___getattribute__(self, name): 85 """Implement attribute accesses.""" 86 if name == '__array_interface__': 87 self.__dict__['__array_interface__'] = {'shape': (self.getSize(), self.getDimension()), 88 'typestr': "|f" + str(self.__elementsize__()), 89 'data': (int(self.__baseaddress__()), True), 90 'version': 3, 91 } 92 return super(Sample, self).__getattribute__(name) 93 Sample.__getattribute__ = Sample___getattribute__ 94 95 96 def Sample__repr_html_(self): 97 """Get HTML representation.""" 98 html = '<TABLE>' 99 desc = self.getDescription() 100 ell_threshold = openturns.common.ResourceMap.GetAsUnsignedInteger("Sample-PrintEllipsisThreshold") 101 ell_size = openturns.common.ResourceMap.GetAsUnsignedInteger("Sample-PrintEllipsisSize") 102 size = self.getSize() 103 dim = self.getDimension() 104 ellipsis = size * dim > ell_threshold 105 if not desc.isBlank(): 106 if ellipsis and dim > 2 * ell_size: 107 html += '<TR><TH></TH><TH>' 108 html += '</TH><TH>'.join(desc[0:ell_size]) 109 html += '</TH><TH COLSPAN="%d">...</TH><TH>' % (dim - 2 * ell_size) 110 html += '</TH><TH>'.join(desc[-ell_size:]) 111 html += '</TH></TR>\n' 112 else: 113 html += '<TR><TD></TD><TH>' + '</TH><TH>'.join(desc) + '</TH></TR>\n' 114 for i in range(size): 115 if ellipsis and size > 2 * ell_size: 116 if i == ell_size: 117 html += '<TR><TD COLSPAN="%d">...</TD></TR>\n' % (dim + 1) 118 continue 119 else: 120 if i > ell_size and i < size - ell_size: 121 continue 122 # Write row 123 fmt = "%.7g" 124 if ellipsis and dim > 2 * ell_size: 125 html += '<TR><TH>' + str(i) 126 if dim > 0: 127 html += '</TH><TD>' 128 html += '</TD><TD>'.join([fmt % x for x in self[i, 0:ell_size]]) 129 html += '<TD COLSPAN="%d">...</TD><TD>' % (dim - 2 * ell_size) 130 html += '</TD><TD>'.join([fmt % x for x in self[i, -ell_size:]]) 131 html += '</TD></TR>\n' 132 else: 133 html += '<TR><TH>' + str(i) 134 if dim > 0: 135 html += '</TH><TD>' + '</TD><TD>'.join([fmt % x for x in self[i]]) 136 html += '</TD></TR>\n' 137 html += '</TABLE>' 138 return html 139 140 Sample._repr_html_ = Sample__repr_html_ 141 %} 142 143 namespace OT { 144 %extend Sample { 145 146 Point __getitem__(SignedInteger index) const 147 { 148 OT::UnsignedInteger size = self->getSize(); 149 if (index < 0) 150 index += self->getSize(); 151 if (index < 0) 152 throw OT::OutOfBoundException(HERE) << "index should be in [-" << size << ", " << size - 1 << "]." ; 153 return self->at(index); 154 } 155 156 void __setitem__ (SignedInteger index, 157 const Point & val) 158 { 159 OT::UnsignedInteger size = self->getSize(); 160 if (index < 0) 161 index += self->getSize(); 162 if (index < 0) 163 throw OT::OutOfBoundException(HERE) << "index should be in [-" << size << ", " << size - 1 << "]." ; 164 // CopyOnWrite only if index is ok 165 self->copyOnWrite(); 166 self->at(index) = val; 167 } 168 169 UnsignedInteger __len__() const 170 { 171 return self->getSize(); 172 } 173 174 175 PyObject * __getitem__(PyObject * args) const 176 { 177 if (!PyTuple_Check(args)) 178 { 179 if (PySlice_Check(args)) 180 { 181 // case 0.1: [slice] => Sample 182 Py_ssize_t start = 0; 183 Py_ssize_t stop = 0; 184 Py_ssize_t step = 0; 185 Py_ssize_t size = 0; 186 PySlice_GetIndicesEx(OT::SliceCast(args), self->getSize(), &start, &stop, &step, &size); 187 OT::Sample result(size, self->getDimension()); 188 for (Py_ssize_t i = 0; i < size; ++ i) 189 result.at(i) = self->at(start + i * step); 190 result.setDescription(self->getDescription()); 191 return SWIG_NewPointerObj((new OT::Sample(static_cast< const OT::Sample& >(result))), SWIGTYPE_p_OT__Sample, SWIG_POINTER_OWN | 0); 192 } 193 else if (PySequence_Check(args)) 194 { 195 // case 0.2: [sequence] => Sample 196 OT::ScopedPyObjectPointer seq(PySequence_Fast(args, "")); 197 const Py_ssize_t size = PySequence_Fast_GET_SIZE(seq.get()); 198 OT::Sample result(size, self->getDimension()); 199 for (Py_ssize_t i = 0; i < size; ++ i) 200 { 201 PyObject * elt = PySequence_Fast_GET_ITEM(seq.get(), i); 202 long index = 0; 203 if (PyInt_Check(elt)) 204 index = PyInt_AsLong(elt); 205 else if (PyObject_HasAttrString(elt, "__int__")) 206 { 207 OT::ScopedPyObjectPointer intValue(PyObject_CallMethod(elt, const_cast<char *>("__int__"), const_cast<char *>("()"))); 208 if (intValue.isNull()) 209 OT::handleException(); 210 index = PyInt_AsLong(intValue.get()); 211 } 212 else 213 throw OT::InvalidArgumentException(HERE) << "Indexing list expects int type"; 214 if (index < 0) 215 index += self->getSize(); 216 if (index < 0) 217 throw OT::OutOfBoundException(HERE) << "index should be in [-" << size << ", " << size - 1 << "]." ; 218 result.at(i) = self->at(index); 219 } 220 result.setDescription(self->getDescription()); 221 return SWIG_NewPointerObj((new OT::Sample(static_cast< const OT::Sample& >(result))), SWIGTYPE_p_OT__Sample, SWIG_POINTER_OWN | 0); 222 } 223 else if (PyObject_HasAttrString(args, "__int__")) 224 { 225 // case 0.3: [numpy.int64] => Point 226 OT::ScopedPyObjectPointer intValue(PyObject_CallMethod(args, const_cast<char *>("__int__"), const_cast<char *>("()"))); 227 if (intValue.isNull()) 228 OT::handleException(); 229 long index = PyInt_AsLong(intValue.get()); 230 if (index < 0) 231 index += self->getSize(); 232 if (index < 0) 233 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getSize() << ", " << self->getSize() - 1 << "]." ; 234 OT::Point result(self->at(index)); 235 return SWIG_NewPointerObj((new OT::Point(static_cast< const OT::Point& >(result))), SWIGTYPE_p_OT__Point, SWIG_POINTER_OWN | 0); 236 } 237 } 238 239 PyObject * obj1 = 0; 240 PyObject * obj2 = 0; 241 if (!PyArg_ParseTuple(args,(char *)"OO:Sample___getitem__", &obj1, &obj2)) SWIG_fail; 242 243 if (OT::isAPython< OT::_PyInt_ >(obj1)) 244 { 245 long index1 = 0; 246 int ecode1 = SWIG_AsVal_long(obj1, &index1); 247 if (!SWIG_IsOK(ecode1)) 248 SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "Sample___getitem__" "', argument " "2"" of type '" "OT::UnsignedInteger""'"); 249 if (index1 < 0) 250 index1 += self->getSize(); 251 if (index1 < 0) 252 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getSize() << ", " << self->getSize() - 1 << "]." ; 253 254 if (OT::isAPython< OT::_PyInt_ >(obj2)) 255 { 256 // case 1.1: [int/int] => float 257 long index2 = 0; 258 int ecode2 = SWIG_AsVal_long(obj2, &index2); 259 if (!SWIG_IsOK(ecode2)) { 260 SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Sample___getitem__" "', argument " "3"" of type '" "OT::UnsignedInteger""'"); 261 } 262 if (index2 < 0) { 263 index2 += self->getDimension(); 264 } 265 if (index2 < 0) { 266 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 267 } 268 return OT::convert< OT::Scalar, OT::_PyFloat_>(self->at(index1, index2)); 269 } 270 else if (PySlice_Check(obj2)) 271 { 272 // case 1.2: [int/slice] => Point 273 Py_ssize_t start2 = 0; 274 Py_ssize_t stop2 = 0; 275 Py_ssize_t step2 = 0; 276 Py_ssize_t size2 = 0; 277 PySlice_GetIndicesEx(OT::SliceCast(obj2), self->getDimension(), &start2, &stop2, &step2, &size2); 278 279 OT::Point result(size2); 280 for (Py_ssize_t j = 0; j < size2; ++ j) 281 result.at(j) = self->at(index1, start2 + j * step2); 282 return SWIG_NewPointerObj((new OT::Point(static_cast< const OT::Point& >(result))), SWIGTYPE_p_OT__Point, SWIG_POINTER_OWN | 0); 283 } 284 else if (PySequence_Check(obj2)) 285 { 286 // case 1.3: [int/sequence] => Point 287 OT::ScopedPyObjectPointer seq2(PySequence_Fast(obj2, "")); 288 const Py_ssize_t size2 = PySequence_Fast_GET_SIZE(seq2.get()); 289 OT::Point result(size2); 290 for (Py_ssize_t j = 0; j < size2; ++ j) 291 { 292 PyObject * elt = PySequence_Fast_GET_ITEM(seq2.get(), j); 293 if (PyInt_Check(elt)) 294 { 295 long index2 = PyInt_AsLong(elt); 296 if (index2 < 0) 297 index2 += self->getDimension(); 298 if (index2 < 0) 299 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 300 result[j] = self->at(index1, index2); 301 } 302 else 303 SWIG_exception(SWIG_TypeError, "Indexing list expects int type"); 304 } 305 return SWIG_NewPointerObj((new OT::Point(static_cast< const OT::Point& >(result))), SWIGTYPE_p_OT__Point, SWIG_POINTER_OWN | 0); 306 } 307 } 308 else if (PySlice_Check(obj1)) 309 { 310 Py_ssize_t start1 = 0; 311 Py_ssize_t stop1 = 0; 312 Py_ssize_t step1 = 0; 313 Py_ssize_t size1 = 0; 314 PySlice_GetIndicesEx(OT::SliceCast(obj1), self->getSize(), &start1, &stop1, &step1, &size1); 315 if (OT::isAPython< OT::_PyInt_ >(obj2)) 316 { 317 // case 2.1: [slice/int] => Sample 318 long index2 = 0; 319 int ecode2 = SWIG_AsVal_long(obj2, &index2); 320 if (!SWIG_IsOK(ecode2)) 321 SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Sample___getitem__" "', argument " "3"" of type '" "OT::UnsignedInteger""'"); 322 if (index2 < 0) 323 index2 += self->getDimension(); 324 if (index2 < 0) 325 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 326 327 OT::Sample result(size1, 1); 328 for (Py_ssize_t i = 0; i < size1; ++ i) 329 result.at(i, 0) = self->at(start1 + i * step1, index2); 330 result.setDescription(OT::Description(1, self->getDescription()[index2])); 331 return SWIG_NewPointerObj((new OT::Sample(static_cast< const OT::Sample& >(result))), SWIGTYPE_p_OT__Sample, SWIG_POINTER_OWN | 0); 332 } 333 else if (PySlice_Check(obj2)) 334 { 335 // case 2.2: [slice/slice] => Sample 336 Py_ssize_t start2 = 0; 337 Py_ssize_t stop2 = 0; 338 Py_ssize_t step2 = 0; 339 Py_ssize_t size2 = 0; 340 PySlice_GetIndicesEx(OT::SliceCast(obj2), self->getDimension(), &start2, &stop2, &step2, &size2); 341 OT::Sample result(size1, size2); 342 for (Py_ssize_t i = 0; i < size1; ++ i) 343 for (Py_ssize_t j = 0; j < size2; ++ j) 344 result.at(i, j) = self->at(start1 + i * step1, start2 + j * step2); 345 OT::Description entireDescription(self->getDescription()); 346 OT::Description description(size2); 347 for (Py_ssize_t j = 0; j < size2; ++ j) 348 description[j] = entireDescription[start2 + j*step2]; 349 result.setDescription(description); 350 return SWIG_NewPointerObj((new OT::Sample(static_cast< const OT::Sample& >(result))), SWIGTYPE_p_OT__Sample, SWIG_POINTER_OWN | 0); 351 } 352 else if (PySequence_Check(obj2)) 353 { 354 // case 2.3: [slice/sequence] => Sample 355 OT::ScopedPyObjectPointer seq2(PySequence_Fast(obj2, "")); 356 Py_ssize_t size2 = PySequence_Fast_GET_SIZE(seq2.get()); 357 OT::Sample result(size1, size2); 358 OT::Indices indices2(size2); 359 for (Py_ssize_t j = 0; j < size2; ++ j) 360 { 361 PyObject * elt = PySequence_Fast_GET_ITEM(seq2.get(), j); 362 if (PyInt_Check(elt)) 363 { 364 long index2 = PyInt_AsLong(elt); 365 if (index2 < 0) 366 index2 += self->getDimension(); 367 if (index2 < 0) 368 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 369 indices2[j] = index2; 370 } 371 else 372 SWIG_exception(SWIG_TypeError, "Indexing list expects int type"); 373 } 374 for (Py_ssize_t i = 0; i < size1; ++ i) 375 for (Py_ssize_t j = 0; j < size2; ++ j) 376 result.at(i, j) = self->at(start1 + i * step1, indices2[j]); 377 OT::Description description(self->getDescription()); 378 OT::Description marginalDescription(size2); 379 for (Py_ssize_t j = 0; j < size2; ++ j) 380 marginalDescription[j] = description[indices2[j]]; 381 result.setDescription(marginalDescription); 382 return SWIG_NewPointerObj((new OT::Sample(static_cast< const OT::Sample& >(result))), SWIGTYPE_p_OT__Sample, SWIG_POINTER_OWN | 0); 383 } 384 } 385 else if (PySequence_Check(obj1)) 386 { 387 OT::ScopedPyObjectPointer seq1(PySequence_Fast(obj1, "")); 388 const Py_ssize_t size1 = PySequence_Fast_GET_SIZE(seq1.get()); 389 OT::Indices indices1(size1); 390 for (Py_ssize_t i = 0; i < size1; ++ i) 391 { 392 PyObject * elt = PySequence_Fast_GET_ITEM(seq1.get(), i); 393 if (PyInt_Check(elt)) 394 { 395 long index1 = PyInt_AsLong(elt); 396 if (index1 < 0) 397 index1 += self->getSize(); 398 if (index1 < 0) 399 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getSize() << ", " << self->getSize() - 1 << "]." ; 400 indices1[i] = index1; 401 } 402 else 403 SWIG_exception(SWIG_TypeError, "Indexing list expects int type"); 404 } 405 406 if (OT::isAPython< OT::_PyInt_ >(obj2)) 407 { 408 // case 3.1: [sequence/int] => Sample 409 long index2 = 0; 410 int ecode2 = SWIG_AsVal_long(obj2, &index2); 411 if (!SWIG_IsOK(ecode2)) 412 SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Sample___getitem__" "', argument " "3"" of type '" "OT::UnsignedInteger""'"); 413 if (index2 < 0) 414 index2 += self->getDimension(); 415 if (index2 < 0) 416 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 417 OT::Sample result(size1, 1); 418 for (Py_ssize_t i = 0; i < size1; ++ i) 419 result.at(i, 0) = self->at(indices1[i], index2); 420 result.setDescription(OT::Description(1, self->getDescription()[index2])); 421 return SWIG_NewPointerObj((new OT::Sample(static_cast< const OT::Sample& >(result))), SWIGTYPE_p_OT__Sample, SWIG_POINTER_OWN | 0); 422 } 423 else if (PySlice_Check(obj2)) 424 { 425 // case 3.2: [sequence/slice] => Sample 426 Py_ssize_t start2 = 0; 427 Py_ssize_t stop2 = 0; 428 Py_ssize_t step2 = 0; 429 Py_ssize_t size2 = 0; 430 PySlice_GetIndicesEx(OT::SliceCast(obj2), self->getDimension(), &start2, &stop2, &step2, &size2); 431 OT::Sample result(size1, size2); 432 for (Py_ssize_t i = 0; i < size1; ++ i) 433 for (Py_ssize_t j = 0; j < size2; ++ j) 434 result.at(i, j) = self->at(indices1[i], start2 + j * step2); 435 OT::Description description(self->getDescription()); 436 OT::Description marginalDescription(size2); 437 for (Py_ssize_t j = 0; j < size2; ++ j) 438 marginalDescription[j] = description[start2 + j*step2]; 439 result.setDescription(marginalDescription); 440 return SWIG_NewPointerObj((new OT::Sample(static_cast< const OT::Sample& >(result))), SWIGTYPE_p_OT__Sample, SWIG_POINTER_OWN | 0); 441 } 442 else if (PySequence_Check(obj2)) 443 { 444 // case 3.3: [sequence/sequence] => Sample 445 OT::ScopedPyObjectPointer seq2(PySequence_Fast(obj2, "")); 446 const Py_ssize_t size2 = PySequence_Fast_GET_SIZE(seq2.get()); 447 OT::Indices indices2(size2); 448 for (Py_ssize_t j = 0; j < size2; ++ j) 449 { 450 PyObject * elt = PySequence_Fast_GET_ITEM(seq2.get(), j); 451 if (PyInt_Check(elt)) 452 { 453 long index2 = PyInt_AsLong(elt); 454 if (index2 < 0) 455 index2 += self->getDimension(); 456 if (index2 < 0) 457 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 458 indices2[j] = index2; 459 } 460 else 461 SWIG_exception(SWIG_TypeError, "Indexing list expects int type"); 462 } 463 OT::Sample result(size1, size2); 464 for (Py_ssize_t i = 0; i < size1; ++ i) 465 for (Py_ssize_t j = 0; j < size2; ++ j) 466 result.at(i, j) = self->at(indices1[i], indices2[j]); 467 OT::Description description(self->getDescription()); 468 OT::Description marginalDescription(size2); 469 for (Py_ssize_t j = 0; j < size2; ++ j) 470 marginalDescription[j] = description[indices2[j]]; 471 result.setDescription(marginalDescription); 472 return SWIG_NewPointerObj((new OT::Sample(static_cast< const OT::Sample& >(result))), SWIGTYPE_p_OT__Sample, SWIG_POINTER_OWN | 0); 473 } 474 } 475 else 476 SWIG_exception(SWIG_TypeError, "Sample.__getitem__ expects int, slice or sequence arguments"); 477 fail: 478 return NULL; 479 } 480 481 482 483 void __setitem__(PyObject * args, PyObject * valObj) 484 { 485 if (!PyTuple_Check(args)) 486 { 487 if (PySlice_Check(args)) 488 { 489 // case 0.1: [slice] <= Sample 490 Py_ssize_t start = 0; 491 Py_ssize_t stop = 0; 492 Py_ssize_t step = 0; 493 Py_ssize_t size = 0; 494 PySlice_GetIndicesEx(OT::SliceCast(args), self->getSize(), &start, &stop, &step, &size); 495 OT::Sample temp; 496 OT::Sample *val = 0; 497 if (! SWIG_IsOK(SWIG_ConvertPtr(valObj, (void **) &val, SWIGTYPE_p_OT__Sample, SWIG_POINTER_NO_NULL))) { 498 temp = OT::convert< OT::_PySequence_, OT::Sample >(valObj); 499 val = &temp; 500 } 501 assert(val); 502 OT::Sample result(size, self->getDimension()); 503 for (Py_ssize_t i = 0; i < size; ++ i) 504 self->at(start + i*step) = val->at(i); 505 } 506 else if (PySequence_Check(args)) 507 { 508 // case 0.2: [sequence] <= Sample 509 OT::ScopedPyObjectPointer seq(PySequence_Fast(args, "")); 510 const Py_ssize_t size = PySequence_Fast_GET_SIZE(seq.get()); 511 OT::Sample temp; 512 OT::Sample *val = 0; 513 if (! SWIG_IsOK(SWIG_ConvertPtr(valObj, (void **) &val, SWIGTYPE_p_OT__Sample, SWIG_POINTER_NO_NULL))) { 514 temp = OT::convert< OT::_PySequence_, OT::Sample >(valObj); 515 val = &temp; 516 } 517 assert(val); 518 OT::Sample result(size, self->getDimension()); 519 for (Py_ssize_t i = 0; i < size; ++ i) 520 { 521 PyObject * elt = PySequence_Fast_GET_ITEM(seq.get(), i); 522 long index = 0; 523 if (PyInt_Check(elt)) 524 index = PyInt_AsLong(elt); 525 else if (PyObject_HasAttrString(elt, "__int__")) 526 { 527 OT::ScopedPyObjectPointer intValue(PyObject_CallMethod(elt, const_cast<char *>("__int__"), const_cast<char *>("()"))); 528 if (intValue.isNull()) 529 OT::handleException(); 530 index = PyInt_AsLong(intValue.get()); 531 } 532 else 533 throw OT::InvalidArgumentException(HERE) << "Indexing list expects int type"; 534 if (index < 0) 535 index += self->getSize(); 536 if (index < 0) 537 throw OT::OutOfBoundException(HERE) << "index should be in [-" << size << ", " << size - 1 << "]." ; 538 self->at(index) = val->at(i); 539 } 540 } 541 else if (PyObject_HasAttrString(args, "__int__")) 542 { 543 // case 0.3: [numpy.int64] <= Point 544 OT::ScopedPyObjectPointer intValue(PyObject_CallMethod(args, const_cast<char *>("__int__"), const_cast<char *>("()"))); 545 if (intValue.isNull()) 546 OT::handleException(); 547 long index = PyInt_AsLong(intValue.get()); 548 if (index < 0) 549 index += self->getSize(); 550 if (index < 0) 551 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getSize() << ", " << self->getSize() - 1 << "]." ; 552 OT::Point temp; 553 OT::Point *val = 0; 554 if (! SWIG_IsOK(SWIG_ConvertPtr(valObj, (void **) &val, SWIGTYPE_p_OT__Point, SWIG_POINTER_NO_NULL))) { 555 temp = OT::convert<OT::_PySequence_, OT::Point>(valObj); 556 val = &temp; 557 } 558 assert(val); 559 self->at(index) = temp; 560 } 561 return; 562 } 563 564 PyObject * obj1 = 0; 565 PyObject * obj2 = 0; 566 if (!PyArg_ParseTuple(args,(char *)"OO:Sample___getitem__", &obj1, &obj2)) SWIG_fail; 567 568 if (OT::isAPython< OT::_PyInt_ >(obj1)) 569 { 570 long index1 = 0; 571 int ecode1 = SWIG_AsVal_long(obj1, &index1); 572 if (!SWIG_IsOK(ecode1)) 573 SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "Sample___getitem__" "', argument " "2"" of type '" "OT::UnsignedInteger""'"); 574 if (index1 < 0) 575 index1 += self->getSize(); 576 if (index1 < 0) 577 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getSize() << ", " << self->getSize() - 1 << "]." ; 578 579 if (OT::isAPython< OT::_PyInt_ >(obj2)) 580 { 581 // case 1.1: [int/int] <= float 582 long index2 = 0; 583 int ecode2 = SWIG_AsVal_long(obj2, &index2); 584 if (!SWIG_IsOK(ecode2)) { 585 SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Sample___getitem__" "', argument " "3"" of type '" "OT::UnsignedInteger""'"); 586 } 587 if (index2 < 0) { 588 index2 += self->getDimension(); 589 } 590 if (index2 < 0) { 591 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 592 } 593 594 const OT::Scalar val = OT::checkAndConvert<OT::_PyFloat_, OT::Scalar>(valObj); 595 self->at(index1, index2) = val; 596 } 597 else if (PySlice_Check(obj2)) 598 { 599 // case 1.2: [int/slice] <= Point 600 Py_ssize_t start2 = 0; 601 Py_ssize_t stop2 = 0; 602 Py_ssize_t step2 = 0; 603 Py_ssize_t size2 = 0; 604 PySlice_GetIndicesEx(OT::SliceCast(obj2), self->getDimension(), &start2, &stop2, &step2, &size2); 605 606 OT::Point temp; 607 OT::Point *val = 0; 608 if (! SWIG_IsOK(SWIG_ConvertPtr(valObj, (void **) &val, SWIGTYPE_p_OT__Point, SWIG_POINTER_NO_NULL))) { 609 temp = OT::convert<OT::_PySequence_, OT::Point>(valObj); 610 val = &temp; 611 } 612 613 for (Py_ssize_t j = 0; j < size2; ++ j) 614 self->at(index1, start2 + j * step2) = val->at(j); 615 } 616 else if (PySequence_Check(obj2)) 617 { 618 // case 1.3: [int/sequence] <= Point 619 OT::ScopedPyObjectPointer seq2(PySequence_Fast(obj2, "")); 620 const Py_ssize_t size2 = PySequence_Fast_GET_SIZE(seq2.get()); 621 622 OT::Indices indices2(size2); 623 for (Py_ssize_t j = 0; j < size2; ++ j) 624 { 625 PyObject * elt = PySequence_Fast_GET_ITEM(seq2.get(), j); 626 if (PyInt_Check(elt)) 627 { 628 long index2 = PyInt_AsLong(elt); 629 if (index2 < 0) 630 index2 += self->getDimension(); 631 if (index2 < 0) 632 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 633 indices2[j] = index2; 634 } 635 else 636 SWIG_exception(SWIG_TypeError, "Indexing list expects int type"); 637 } 638 639 OT::Point temp; 640 OT::Point *val = 0; 641 if (! SWIG_IsOK(SWIG_ConvertPtr(valObj, (void **) &val, SWIGTYPE_p_OT__Point, SWIG_POINTER_NO_NULL))) { 642 temp = OT::convert<OT::_PySequence_, OT::Point>(valObj); 643 val = &temp; 644 } 645 646 for (Py_ssize_t j = 0; j < size2; ++ j) 647 self->at(index1, indices2[j]) = val->at(j); 648 } 649 } 650 else if (PySlice_Check(obj1)) 651 { 652 Py_ssize_t start1 = 0; 653 Py_ssize_t stop1 = 0; 654 Py_ssize_t step1 = 0; 655 Py_ssize_t size1 = 0; 656 PySlice_GetIndicesEx(OT::SliceCast(obj1), self->getSize(), &start1, &stop1, &step1, &size1); 657 658 OT::Sample temp; 659 OT::Sample *val = 0; 660 if (! SWIG_IsOK(SWIG_ConvertPtr(valObj, (void **) &val, SWIGTYPE_p_OT__Sample, SWIG_POINTER_NO_NULL))) { 661 temp = OT::convert<OT::_PySequence_, OT::Sample>(valObj); 662 val = &temp; 663 } 664 assert(val); 665 666 if (OT::isAPython< OT::_PyInt_ >(obj2)) 667 { 668 // case 2.1: [slice/int] <= Sample 669 long index2 = 0; 670 int ecode2 = SWIG_AsVal_long(obj2, &index2); 671 if (!SWIG_IsOK(ecode2)) 672 SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Sample___getitem__" "', argument " "3"" of type '" "OT::UnsignedInteger""'"); 673 if (index2 < 0) 674 index2 += self->getDimension(); 675 if (index2 < 0) 676 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 677 678 for (Py_ssize_t i = 0; i < size1; ++ i) 679 self->at(start1 + i * step1, index2) = val->at(i, 0); 680 } 681 else if (PySlice_Check(obj2)) 682 { 683 // case 2.2: [slice/slice] <= Sample 684 Py_ssize_t start2 = 0; 685 Py_ssize_t stop2 = 0; 686 Py_ssize_t step2 = 0; 687 Py_ssize_t size2 = 0; 688 PySlice_GetIndicesEx(OT::SliceCast(obj2), self->getDimension(), &start2, &stop2, &step2, &size2); 689 for (Py_ssize_t i = 0; i < size1; ++ i) 690 for (Py_ssize_t j = 0; j < size2; ++ j) 691 self->at(start1 + i * step1, start2 + j * step2) = val->at(i, j); 692 } 693 else if (PySequence_Check(obj2)) 694 { 695 // case 2.3: [slice/sequence] <= Sample 696 OT::ScopedPyObjectPointer seq2(PySequence_Fast(obj2, "")); 697 Py_ssize_t size2 = PySequence_Fast_GET_SIZE(seq2.get()); 698 OT::Sample result(size1, size2); 699 OT::Indices indices2(size2); 700 for (Py_ssize_t j = 0; j < size2; ++ j) 701 { 702 PyObject * elt = PySequence_Fast_GET_ITEM(seq2.get(), j); 703 if (PyInt_Check(elt)) 704 { 705 long index2 = PyInt_AsLong(elt); 706 if (index2 < 0) 707 index2 += self->getDimension(); 708 if (index2 < 0) 709 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 710 indices2[j] = index2; 711 } 712 else 713 SWIG_exception(SWIG_TypeError, "Indexing list expects int type"); 714 } 715 for (Py_ssize_t i = 0; i < size1; ++ i) 716 for (Py_ssize_t j = 0; j < size2; ++ j) 717 self->at(start1 + i * step1, indices2[j]) = val->at(i, j); 718 } 719 } 720 else if (PySequence_Check(obj1)) 721 { 722 OT::ScopedPyObjectPointer seq1(PySequence_Fast(obj1, "")); 723 const Py_ssize_t size1 = PySequence_Fast_GET_SIZE(seq1.get()); 724 OT::Indices indices1(size1); 725 for (Py_ssize_t i = 0; i < size1; ++ i) 726 { 727 PyObject * elt = PySequence_Fast_GET_ITEM(seq1.get(), i); 728 if (PyInt_Check(elt)) 729 { 730 long index1 = PyInt_AsLong(elt); 731 if (index1 < 0) 732 index1 += self->getSize(); 733 if (index1 < 0) 734 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getSize() << ", " << self->getSize() - 1 << "]." ; 735 indices1[i] = index1; 736 } 737 else 738 SWIG_exception(SWIG_TypeError, "Indexing list expects int type"); 739 } 740 741 OT::Sample temp; 742 OT::Sample *val = 0; 743 if (! SWIG_IsOK(SWIG_ConvertPtr(valObj, (void **) &val, SWIGTYPE_p_OT__Sample, SWIG_POINTER_NO_NULL))) { 744 temp = OT::convert<OT::_PySequence_, OT::Sample>(valObj); 745 val = &temp; 746 } 747 assert(val); 748 749 if (OT::isAPython< OT::_PyInt_ >(obj2)) 750 { 751 // case 3.1: [sequence/int] <= Sample 752 long index2 = 0; 753 int ecode2 = SWIG_AsVal_long(obj2, &index2); 754 if (!SWIG_IsOK(ecode2)) 755 SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "Sample___getitem__" "', argument " "3"" of type '" "OT::UnsignedInteger""'"); 756 if (index2 < 0) 757 index2 += self->getDimension(); 758 if (index2 < 0) 759 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 760 for (Py_ssize_t i = 0; i < size1; ++ i) 761 self->at(indices1[i], index2) = val->at(i, 0); 762 } 763 else if (PySlice_Check(obj2)) 764 { 765 // case 3.2: [sequence/slice] <= Sample 766 Py_ssize_t start2 = 0; 767 Py_ssize_t stop2 = 0; 768 Py_ssize_t step2 = 0; 769 Py_ssize_t size2 = 0; 770 PySlice_GetIndicesEx(OT::SliceCast(obj2), self->getDimension(), &start2, &stop2, &step2, &size2); 771 for (Py_ssize_t i = 0; i < size1; ++ i) 772 for (Py_ssize_t j = 0; j < size2; ++ j) 773 self->at(indices1[i], start2 + j * step2) = val->at(i, j); 774 } 775 else if (PySequence_Check(obj2)) 776 { 777 // case 3.3: [sequence/sequence] <= Sample 778 OT::ScopedPyObjectPointer seq2(PySequence_Fast(obj2, "")); 779 const Py_ssize_t size2 = PySequence_Fast_GET_SIZE(seq2.get()); 780 OT::Indices indices2(size2); 781 for (Py_ssize_t j = 0; j < size2; ++ j) 782 { 783 PyObject * elt = PySequence_Fast_GET_ITEM(seq2.get(), j); 784 if (PyInt_Check(elt)) 785 { 786 long index2 = PyInt_AsLong(elt); 787 if (index2 < 0) 788 index2 += self->getDimension(); 789 if (index2 < 0) 790 throw OT::OutOfBoundException(HERE) << "index should be in [-" << self->getDimension() << ", " << self->getDimension() - 1 << "]." ; 791 indices2[j] = index2; 792 } 793 else 794 SWIG_exception(SWIG_TypeError, "Indexing list expects int type"); 795 } 796 for (Py_ssize_t i = 0; i < size1; ++ i) 797 for (Py_ssize_t j = 0; j < size2; ++ j) 798 self->at(indices1[i], indices2[j]) = val->at(i, j); 799 } 800 } 801 else 802 SWIG_exception(SWIG_TypeError, "Sample.__setitem__ expects int, slice or sequence arguments"); 803 fail: 804 return; 805 } 806 807 Sample(const Sample & other) 808 { 809 return new OT::Sample( other ); 810 } 811 812 Sample(PyObject * pyObj) 813 { 814 return new OT::Sample( OT::convert< OT::_PySequence_, OT::Sample>(pyObj) ); 815 } 816 817 Bool __eq__(const Sample & other) { return (*self) == other; } 818 819 #if SWIG_VERSION < 0x030011 820 Sample __truediv__(const Scalar & u) { return (*self) / u; } 821 822 Sample __truediv__(const Point & v) { return (*self) / v; } 823 #endif 824 825 Sample __rmul__(Scalar s) 826 { 827 return s * (*self); 828 } 829 830 } // %extend 831 } // namespace OT 832 833