Skip to content

Commit 9d11439

Browse files
committed
Merge branch 'master' into modules
2 parents cc8ca57 + 5d0402c commit 9d11439

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1515
-217
lines changed

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ IF(BUILD_TESTING)
857857
IF(BUILD_WITH_PYTHON)
858858
SET_PROPERTY(TARGET Core_Python_Tests PROPERTY FOLDER "Core/Tests")
859859
SET_PROPERTY(TARGET Engine_Python_Tests PROPERTY FOLDER "Dataflow/Engine/Tests")
860+
SET_PROPERTY(TARGET Modules_Python_Tests PROPERTY FOLDER "Modules/Python")
860861
ENDIF()
861862
ENDIF()
862863

@@ -868,6 +869,8 @@ SET_PROPERTY(TARGET xml2 PROPERTY FOLDER "Externals")
868869
IF(BUILD_WITH_PYTHON)
869870
SET_PROPERTY(TARGET Core_Python PROPERTY FOLDER "Core")
870871
SET_PROPERTY(TARGET SCIRunPythonAPI PROPERTY FOLDER "Dataflow/Engine")
872+
SET_PROPERTY(TARGET Modules_Python PROPERTY FOLDER "Modules/Python")
873+
SET_PROPERTY(TARGET Interface_Modules_Python PROPERTY FOLDER "Interface/Modules")
871874
ENDIF()
872875

873876
########################################################################

src/Core/Algorithms/Base/AlgorithmBase.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ double AlgorithmParameter::toDouble() const
134134
return v ? *v : toInt();
135135
}
136136

137+
std::vector<double> AlgorithmParameter::toDoubleVector() const
138+
{
139+
const std::vector<double>* v = boost::get<std::vector<double>>(&value_);
140+
return v ? *v : std::vector<double>();
141+
}
142+
137143
std::string AlgorithmParameter::toString() const
138144
{
139145
const std::string* v = boost::get<std::string>(&value_);

src/Core/Algorithms/Base/Variable.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ namespace Algorithms {
5454
std::string,
5555
bool,
5656
AlgoOption,
57+
std::vector<double>,
5758
List
5859
> Value;
5960

6061
Variable() {}
6162
Variable(const Name& name, const Value& value);
62-
Variable(const Name& name, const Datatypes::DatatypeHandle& data) : name_(name), data_(data) {}
63+
enum DatatypeVariableDummyEnum { DATATYPE_VARIABLE };
64+
Variable(const Name& name, const Datatypes::DatatypeHandle& data, DatatypeVariableDummyEnum) : name_(name), data_(data) {}
6365
virtual ~Variable() {}
6466

6567
const Name& name() const { return name_; }
@@ -72,6 +74,7 @@ namespace Algorithms {
7274
std::string toString() const;
7375
boost::filesystem::path toFilename() const;
7476
bool toBool() const;
77+
std::vector<double> toDoubleVector() const;
7578
List toVector() const;
7679
AlgoOption toOption() const;
7780

src/Core/Python/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929

3030
SET(Core_Python_SRCS
3131
PythonInterpreter.cc
32+
PythonDatatypeConverter.cc
3233
)
3334

3435
SET(Core_Python_HEADERS
3536
PythonInterpreter.h
37+
PythonDatatypeConverter.h
3638
share.h
3739
)
3840

@@ -62,7 +64,9 @@ TARGET_LINK_LIBRARIES(Core_Python
6264
${SCI_PYTHON_LIBRARY}
6365
${SCI_BOOST_LIBRARY}
6466
SCIRunPythonAPI
65-
Core_Application
67+
Core_Matlab
68+
Core_Datatypes
69+
Core_Datatypes_Legacy_Field
6670
)
6771

6872
SET_TARGET_PROPERTIES(Core_Python
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
#ifdef BUILD_WITH_PYTHON
30+
#ifndef CORE_PYTHON_PYTHONDATATYPECONVERTER_H
31+
#define CORE_PYTHON_PYTHONDATATYPECONVERTER_H
32+
33+
#include <boost/python.hpp>
34+
#include <boost/python/stl_iterator.hpp>
35+
#include <boost/shared_ptr.hpp>
36+
#include <vector>
37+
#include <Core/Datatypes/DatatypeFwd.h>
38+
39+
40+
#include <Core/Python/share.h>
41+
42+
namespace SCIRun
43+
{
44+
namespace Core
45+
{
46+
namespace Python
47+
{
48+
template <class T>
49+
boost::python::list toPythonList(const std::vector<T>& vec)
50+
{
51+
boost::python::list list;
52+
for (const auto& v : vec)
53+
{
54+
list.append(v);
55+
}
56+
return list;
57+
}
58+
59+
template <class T>
60+
boost::python::list toPythonListOfLists(const std::vector<T>& vec, int dim1, int dim2)
61+
{
62+
boost::python::list list;
63+
auto iter = vec.begin();
64+
for (int i = 0; i < dim1; ++i)
65+
{
66+
boost::python::list row;
67+
for (int j = 0; j < dim2; ++j)
68+
row.append(*iter++);
69+
list.append(row);
70+
}
71+
return list;
72+
}
73+
74+
template< typename T >
75+
std::vector< T > to_std_vector(const boost::python::object& iterable)
76+
{
77+
return std::vector< T >(boost::python::stl_input_iterator< T >(iterable),
78+
boost::python::stl_input_iterator< T >());
79+
}
80+
81+
82+
SCISHARE boost::python::object convertFieldToPython(FieldHandle field);
83+
SCISHARE boost::python::object convertMatrixToPython(Datatypes::DenseMatrixHandle matrix);
84+
SCISHARE boost::python::object convertMatrixToPython(Datatypes::SparseRowMatrixHandle matrix);
85+
SCISHARE boost::python::object convertStringToPython(Datatypes::StringHandle str);
86+
}
87+
}
88+
}
89+
90+
#endif
91+
#endif

0 commit comments

Comments
 (0)