alta.cpp 4.18 KB
Newer Older
1 2 3 4
// Boost includes
#include <boost/python.hpp>

// ALTA include
5 6
#include <core/common.h>
#include <core/ptr.h>
7
#include <core/function.h>
8
#include <core/plugins_manager.h>
9

10 11
// STL include
#include <iostream>
12

13 14 15
#define bp boost::python


16 17 18
/* The following code register ALTA's shared pointer as a valid shared ptr
 * to be used by boost::python .
 */
19 20
template <typename T> 
T* get_pointer(ptr<T> const& p) {
21 22 23
  return const_cast<T*>(p.get());
}

24 25 26 27 28 29 30 31
namespace boost {
	namespace python {
   		template <typename T>
    	struct pointee< ::ptr<T> > {
        	typedef T type;
    	};
	}
}
32 33


34 35 36 37 38 39
/* Wrapper to ALTA's vec class. This is only here to allow init with Python's
 * list.
 *
 * TODO: Make sure that the value passed to this vector are floatting point
 *       convertible.
 */
40 41 42 43
struct python_vec : public vec {
    python_vec() : vec() {}
	python_vec(const vec& x) : vec(x) {}
    python_vec(const bp::list& l) : vec(bp::len(l)) {
44 45 46 47 48
        for(size_t i=0; i<bp::len(l); ++i) {
            (*this)[i] = bp::extract<double>(l[i]);
        }
    }
};
49 50 51 52 53 54 55 56 57
std::ostream &operator<<(std::ostream &out, const python_vec &x) {
	out << "[";
	for(int i=0; i<x.size(); ++i) {
		if(i != 0) { out << ", "; }
		out << x[i]; 
	}
	return out << "]";
}

58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

/* This class is a wrapper to ALTA's arguments class to add Python specific
 * behaviour such as dictionnary initialization.
 */
struct python_arguments : public arguments {
	python_arguments() : arguments() {}
	python_arguments(bp::dict d) : arguments() {
		bp::list keys = d.keys();
		for(int i=0; i<bp::len(keys); ++i) {
			const std::string s_key = bp::extract<std::string>(keys[i]);
			const std::string s_val = bp::extract<std::string>(d[keys[i]]);
			this->update(s_key, s_val);
		}
	}
};


/* Create a data object from a plugin's name and the data filename. This 
 * function is here to accelerate the loading of data file.
 */
79 80 81 82 83 84
ptr<data> load_data(const std::string& plugin_name, const std::string& filename) {
	ptr<data> d = plugins_manager::get_data(plugin_name);
	d->load(filename);
	return d;
}

85 86 87 88 89 90 91 92 93 94 95 96 97

/* Creating functions for the plugins_manager calls
 * 
 * TODO: Throw python exceptions if the function is not correctly created.
 *       Those function should disapear when the return type of get_Function
 *       in the plugin_manager will be ptr<function>.
 */
ptr<function> get_function(const std::string& filename) {
    ptr<function> func(plugins_manager::get_function(filename));
    if(!func) {
    	std::cerr << "<<ERROR>> no function created" << std::endl;
    }
	return func;
98
}
99
ptr<function> get_function_from_args(const python_arguments& args) {
100 101 102 103 104
    ptr<function> func(plugins_manager::get_function(args));
    if(!func) {
    	std::cerr << "<<ERROR>> no function created" << std::endl;
    }
    return func;
105
}
106

107 108 109

/* Exporting the ALTA module 
 */
110 111
BOOST_PYTHON_MODULE(alta)
{
112 113
	// Argument class
	//
114
	bp::class_<python_arguments>("arguments")
115
		.def(bp::init<>())
116
		.def(bp::init<bp::dict>())
117 118
		.def("update", &arguments::update);

119 120 121 122 123 124 125

	// Vector class
	//
	// TODO: There is a conversion issue right now that prevents us from using vectors
	// within Python. This needs to be investiguated.
	bp::class_<python_vec>("vec")
		.def(bp::init<vec>())
126
		.def(bp::init<bp::list>())
127 128 129 130 131
		.def(bp::self_ns::str(bp::self_ns::self))
		/*.def(" __setitem__", &my_vec::operator[])*/;
	bp::implicitly_convertible<vec, python_vec>();
	bp::implicitly_convertible<python_vec, vec>();

132 133 134 135

	// Function interface
	//
	bp::class_<function, ptr<function>, boost::noncopyable>("function", bp::no_init)
136 137 138 139 140
		.def("value", &function::value)
		.def("load",  &function::load)
		.def("save",  &function::save);
	bp::def("get_function", get_function);
	bp::def("get_function", get_function_from_args);
141

142

143 144 145
	// Data interface
	//
	bp::class_<data, ptr<data>, boost::noncopyable>("data", bp::no_init)
146
		.def("size", &data::size)
147 148 149
		.def("get",  &data::get)
		.def("set",  &data::set)
		.def("load", static_cast< void(data::*)(const std::string&)>(&data::load))
150 151
		.def("save", &data::save);
	bp::def("get_data",  plugins_manager::get_data);
152 153
	bp::def("load_data", load_data);

154

155 156 157 158 159
	// Fitter interface
	//
	bp::class_<fitter, ptr<fitter>, boost::noncopyable>("fitter", bp::no_init)
		.def("fit_data", &fitter::fit_data);
	bp::def("get_fitter", plugins_manager::get_fitter);
160
}