comparison PyTypeInterface.cpp @ 51:c1e4f706ca9a

Fix numpy version incompatibility issues and updated some example plugins.
author fazekasgy
date Thu, 08 Oct 2009 08:47:28 +0000
parents af9c4cee95a8
children 74703a562ce3
comparison
equal deleted inserted replaced
50:3868da185d73 51:c1e4f706ca9a
43 (EXCEPT FOR TEMPORARY PYTHON OBJECTS)! */ 43 (EXCEPT FOR TEMPORARY PYTHON OBJECTS)! */
44 44
45 PyTypeInterface::PyTypeInterface() : 45 PyTypeInterface::PyTypeInterface() :
46 m_strict(false), 46 m_strict(false),
47 m_error(false), 47 m_error(false),
48 m_numpyInstalled(false),
48 error(m_error) // const public reference for easy access 49 error(m_error) // const public reference for easy access
49 { 50 {
50 } 51 }
51 52
52 PyTypeInterface::~PyTypeInterface() 53 PyTypeInterface::~PyTypeInterface()
62 //TODO: check for limits here (same on most systems) 63 //TODO: check for limits here (same on most systems)
63 return (float) PyFloat_AS_DOUBLE(pyValue); 64 return (float) PyFloat_AS_DOUBLE(pyValue);
64 65
65 if (pyValue == NULL) 66 if (pyValue == NULL)
66 { 67 {
67 setValueError("Error while converting float object.",m_strict); 68 setValueError("Error while converting object " + PyValue_Get_TypeName(pyValue) + " to float. ",m_strict);
68 return 0.0; 69 return 0.0;
69 } 70 }
70 71
71 // in strict mode we will not try harder 72 // in strict mode we will not try harder
72 if (m_strict) { 73 if (m_strict) {
73 setValueError("Strict conversion error: object is not float.",m_strict); 74 setValueError("Strict conversion error: object" + PyValue_Get_TypeName(pyValue) +" is not float.",m_strict);
74 return 0.0; 75 return 0.0;
75 } 76 }
76 77
77 // convert other objects supporting the number protocol 78 // convert other objects supporting the number protocol
78 if (PyNumber_Check(pyValue)) 79 if (PyNumber_Check(pyValue))
558 ListElement = (string) PyString_AsString(PyObject_Str(pyString)); 559 ListElement = (string) PyString_AsString(PyObject_Str(pyString));
559 Output.push_back(ListElement); 560 Output.push_back(ListElement);
560 } 561 }
561 return Output; 562 return Output;
562 } 563 }
563 #ifdef _DEBUG 564
564 cerr << "PyTypeInterface::PyValue_To_StringVector: Warning: Value is not list of strings." << endl; 565 // #ifdef _DEBUG
565 #endif 566 // cerr << "PyTypeInterface::PyValue_To_StringVector: Warning: Value is not list of strings." << endl;
567 // #endif
566 568
567 /// Assume a single value that can be casted as string 569 /// Assume a single value that can be casted as string
568 /// this allows to write e.g. Feature.label = 5.2 instead of ['5.2'] 570 /// this allows to write e.g. Feature.label = 5.2 instead of ['5.2']
569 Output.push_back(PyValue_To_String(pyList)); 571 Output.push_back(PyValue_To_String(pyList));
570 if (m_error) { 572 if (m_error) {
581 std::vector<float> 583 std::vector<float>
582 PyTypeInterface::PyValue_To_FloatVector (PyObject *pyValue) const 584 PyTypeInterface::PyValue_To_FloatVector (PyObject *pyValue) const
583 { 585 {
584 586
585 #ifdef HAVE_NUMPY 587 #ifdef HAVE_NUMPY
588 if (m_numpyInstalled)
589 {
586 // there are four types of values we may receive from a numpy process: 590 // there are four types of values we may receive from a numpy process:
587 // * a python scalar, 591 // * a python scalar,
588 // * an array scalar, (e.g. numpy.float32) 592 // * an array scalar, (e.g. numpy.float32)
589 // * an array with nd = 0 (0D array) 593 // * an array with nd = 0 (0D array)
590 // * an array with nd > 0 594 // * an array with nd > 0
602 } 606 }
603 607
604 /// numpy array 608 /// numpy array
605 if (PyArray_CheckExact(pyValue)) 609 if (PyArray_CheckExact(pyValue))
606 return PyArray_To_FloatVector(pyValue); 610 return PyArray_To_FloatVector(pyValue);
607 611 }
608 #endif 612 #endif
609 613
610 /// python list of floats (backward compatible) 614 /// python list of floats (backward compatible)
611 if (PyList_Check(pyValue)) { 615 if (PyList_Check(pyValue)) {
612 return PyList_To_FloatVector(pyValue); 616 return PyList_To_FloatVector(pyValue);
619 Output.push_back(PyValue_To_Float(pyValue)); 623 Output.push_back(PyValue_To_Float(pyValue));
620 if (m_error) { 624 if (m_error) {
621 std::string msg = "Value is not list or array of floats nor can be casted as float. "; 625 std::string msg = "Value is not list or array of floats nor can be casted as float. ";
622 setValueError(msg,m_strict); 626 setValueError(msg,m_strict);
623 #ifdef _DEBUG 627 #ifdef _DEBUG
624 cerr << "PyTypeInterface::PyValue_To_FloatVector failed." << msg << endl; 628 cerr << "PyTypeInterface::PyValue_To_FloatVector failed. " << msg << endl;
625 #endif 629 #endif
626 } 630 }
627 return Output; 631 return Output;
628 } 632 }
629 633
672 Output.push_back(ListElement); 676 Output.push_back(ListElement);
673 } 677 }
674 return Output; 678 return Output;
675 } 679 }
676 680
677 #ifdef HAVE_NUMPY 681 // if numpy is not installed this will not be called,
682 // therefor we do not check again
683 #ifdef HAVE_NUMPY
678 std::vector<float> 684 std::vector<float>
679 PyTypeInterface::PyArray_To_FloatVector (PyObject *pyValue) const 685 PyTypeInterface::PyArray_To_FloatVector (PyObject *pyValue) const
680 { 686 {
681 std::vector<float> Output; 687 std::vector<float> Output;
682 688
742 } 748 }
743 } 749 }
744 #endif 750 #endif
745 751
746 752
747 /// FeatureSet (an integer map of OutputLists) 753 /// FeatureSet (an integer map of FeatureLists)
748 Vamp::Plugin::FeatureSet 754 Vamp::Plugin::FeatureSet
749 PyTypeInterface::PyValue_To_FeatureSet(PyObject* pyValue) const 755 PyTypeInterface::PyValue_To_FeatureSet(PyObject* pyValue) const
750 { 756 {
751 Vamp::Plugin::FeatureSet rFeatureSet; 757 Vamp::Plugin::FeatureSet rFeatureSet;
752 758
796 } 802 }
797 if (!m_errorQueue.empty()) m_error = true; 803 if (!m_errorQueue.empty()) m_error = true;
798 return rFeatureSet; 804 return rFeatureSet;
799 } 805 }
800 806
801 /// accept no return values 807 /// accept None return values
802 if (pyValue == Py_None) return rFeatureSet; 808 if (pyValue == Py_None) return rFeatureSet;
803 809
804 /// give up 810 /// give up
805 std::string msg = "Unsupported return type. Expected list or vampy.FeatureSet(). "; 811 std::string msg = "Unsupported return type. Expected list or vampy.FeatureSet(). ";
806 setValueError(msg,m_strict); 812 setValueError(msg,m_strict);