1+ /*
2+ For more information, please see: http://software.sci.utah.edu
3+
4+ The MIT License
5+
6+ Copyright (c) 2015 Scientific Computing and Imaging Institute,
7+ University of Utah.
8+
9+
10+ Permission is hereby granted, free of charge, to any person obtaining a
11+ copy of this software and associated documentation files (the "Software"),
12+ to deal in the Software without restriction, including without limitation
13+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+ and/or sell copies of the Software, and to permit persons to whom the
15+ Software is furnished to do so, subject to the following conditions:
16+
17+ The above copyright notice and this permission notice shall be included
18+ in all copies or substantial portions of the Software.
19+
20+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26+ DEALINGS IN THE SOFTWARE.
27+ */
28+
29+ // / @todo Documentation Core/Python/PythonInterpreter.cc
30+
31+ #ifdef BUILD_WITH_PYTHON
32+ #ifdef _MSC_VER
33+ // #pragma warning( push )
34+ #pragma warning( disable: 4244 )
35+ #endif
36+
37+ #include < Core/Python/PythonDatatypeConverter.h>
38+ #include < Core/Datatypes/SparseRowMatrix.h>
39+ #include < Core/Datatypes/DenseMatrix.h>
40+ #include < Core/Datatypes/String.h>
41+ #include < Core/Matlab/matlabarray.h>
42+ #include < Core/Matlab/matlabconverter.h>
43+
44+ using namespace SCIRun ;
45+ using namespace SCIRun ::Core::Python;
46+ using namespace SCIRun ::Core::Datatypes;
47+ using namespace SCIRun ::MatlabIO;
48+
49+ namespace
50+ {
51+ template <class T >
52+ boost::python::list toPythonList (const DenseMatrixGeneric<T>& dense)
53+ {
54+ boost::python::list list;
55+ for (int i = 0 ; i < dense.nrows (); ++i)
56+ {
57+ boost::python::list row;
58+ for (int j = 0 ; j < dense.ncols (); ++j)
59+ row.append (dense (i, j));
60+ list.append (row);
61+ }
62+ return list;
63+ }
64+
65+ template <class T >
66+ boost::python::list toPythonList (const SparseRowMatrixGeneric<T>& sparse)
67+ {
68+ boost::python::list rows, columns, values;
69+
70+ for (int i = 0 ; i < sparse.nonZeros (); ++i)
71+ {
72+ values.append (sparse.valuePtr ()[i]);
73+ }
74+ for (int i = 0 ; i < sparse.nonZeros (); ++i)
75+ {
76+ columns.append (sparse.innerIndexPtr ()[i]);
77+ }
78+ for (int i = 0 ; i < sparse.outerSize (); ++i)
79+ {
80+ rows.append (sparse.outerIndexPtr ()[i]);
81+ }
82+
83+ boost::python::list list;
84+ list.append (rows);
85+ list.append (columns);
86+ list.append (values);
87+ return list;
88+ }
89+ }
90+
91+ boost::python::object SCIRun::Core::Python::convertFieldToPython (FieldHandle field)
92+ {
93+ matlabarray ma;
94+ matlabconverter mc (nullptr );
95+ mc.converttostructmatrix ();
96+ mc.sciFieldTOmlArray (field, ma);
97+ boost::python::dict matlabStructure;
98+
99+ for (const auto & fieldName : ma.getfieldnames ())
100+ {
101+ auto subField = ma.getfield (0 , fieldName);
102+ switch (subField.gettype ())
103+ {
104+ case matfilebase::miUINT8:
105+ {
106+ auto str = subField.getstring ();
107+ matlabStructure[fieldName] = str;
108+ break ;
109+ }
110+ case matfilebase::miDOUBLE:
111+ {
112+ std::vector<double > v;
113+ subField.getnumericarray (v);
114+ if (1 != subField.getm () && 1 != subField.getn ())
115+ matlabStructure[fieldName] = toPythonListOfLists (v, subField.getn (), subField.getm ());
116+ else
117+ matlabStructure[fieldName] = toPythonList (v);
118+ break ;
119+ }
120+ default :
121+ std::cout << " some other array: " << std::endl;
122+ break ;
123+ }
124+ }
125+ return matlabStructure;
126+ }
127+
128+ boost::python::object SCIRun::Core::Python::convertMatrixToPython (DenseMatrixHandle matrix)
129+ {
130+ if (matrix)
131+ return ::toPythonList (*matrix);
132+ return {};
133+ }
134+
135+ boost::python::object SCIRun::Core::Python::convertMatrixToPython (SparseRowMatrixHandle matrix)
136+ {
137+ if (matrix)
138+ return ::toPythonList (*matrix);
139+ return {};
140+ }
141+
142+ boost::python::object SCIRun::Core::Python::convertStringToPython (StringHandle str)
143+ {
144+ if (str)
145+ {
146+ boost::python::object obj (std::string (str->value ()));
147+ return obj;
148+ }
149+ return {};
150+ }
151+
152+
153+
154+
155+ #endif
0 commit comments