| 41 | namespace lbann { |
| 42 | |
| 43 | python_reader::python_reader(std::string module, |
| 44 | std::string module_dir, |
| 45 | std::string sample_function, |
| 46 | std::string num_samples_function, |
| 47 | std::string sample_dims_function, |
| 48 | bool shuffle) |
| 49 | : generic_data_reader(shuffle) |
| 50 | { |
| 51 | |
| 52 | // Make sure Python is running and acquire GIL |
| 53 | python::global_interpreter_lock gil; |
| 54 | |
| 55 | // Import Python module for data |
| 56 | if (!module_dir.empty()) { |
| 57 | auto path = PySys_GetObject("path"); // Borrowed reference |
| 58 | PyList_Append(path, python::object(module_dir)); |
| 59 | python::check_error(); |
| 60 | } |
| 61 | python::object data_module = PyImport_ImportModule(module.c_str()); |
| 62 | |
| 63 | // Get number of samples |
| 64 | python::object num_func = |
| 65 | PyObject_GetAttrString(data_module, num_samples_function.c_str()); |
| 66 | python::object num = PyObject_CallObject(num_func, nullptr); |
| 67 | m_num_samples = PyLong_AsLong(num); |
| 68 | python::check_error(); |
| 69 | |
| 70 | // Get sample dimensions |
| 71 | python::object dims_func = |
| 72 | PyObject_GetAttrString(data_module, sample_dims_function.c_str()); |
| 73 | python::object dims = PyObject_CallObject(dims_func, nullptr); |
| 74 | dims = PyObject_GetIter(dims); |
| 75 | for (auto d = PyIter_Next(dims); d != nullptr; d = PyIter_Next(dims)) { |
| 76 | m_sample_dims.push_back(PyLong_AsLong(d)); |
| 77 | Py_DECREF(d); |
| 78 | } |
| 79 | python::check_error(); |
| 80 | |
| 81 | // Get sample access function |
| 82 | m_sample_function = |
| 83 | PyObject_GetAttrString(data_module, sample_function.c_str()); |
| 84 | } |
| 85 | |
| 86 | python_reader::~python_reader() |
| 87 | { |
nothing calls this directly
no test coverage detected