Skip to content

Commit

Permalink
autoSTRVECToArray fix candidate #2
Browse files Browse the repository at this point in the history
  • Loading branch information
hokiedsp committed Jul 4, 2024
1 parent 783628b commit 14ad15a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
31 changes: 17 additions & 14 deletions src/parselmouth/praat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,23 @@ py::object autoSTRVECToArray(autoSTRVEC &&vector) {
if (!vector.elements)
return py::none();


auto v = vector.get(); // _stringvector <char32>
// return py::array_t<py::object>(py::cast(std::vector<char32 *>(v.begin(), v.end()))); // fails with error: static assertion failed: Attempt to use a non-POD or unimplemented POD type as a numpy dtype

// candidate #1: still the same error if casted,
// std::vector<std::u32string_view> str_vec;
// std::transform(v.begin(), v.end(), std::back_inserter(str_vec), [](auto &s)
// { return std::u32string_view(s,std::char_traits<char32>::length(s)); });
// return str_vec;

py::list str_list(v.size);
for (int i=0;i<v.size;++i)
str_list[i] = v[i+1];
return py::array(str_list);
auto v = vector.get();

// original - fails with error: static assertion failed: Attempt to use a non-POD or unimplemented POD type as a numpy dtype
// return py::array_t<py::object>(py::cast(std::vector<char32 *>(v.begin(), v.end())));

// candidate 1 - py::list->py::array - seems redundant
// py::list str_list(v.size);
// for (int i=0;i<v.size;++i)
// str_list[i] = v[i+1];
// return py::array(str_list);

// candidate 2 - most comparable to the original?
py::array str_array(py::dtype('O'), {v.size}, {});
auto view = static_cast<py::object *>(str_array.mutable_data());
for (int i = 0; i < v.size; ++i)
view[i] = py::cast(v[i + 1]);
return str_array;
}

class PraatEnvironment {
Expand Down
4 changes: 2 additions & 2 deletions tests/test_praat.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ def test_call_return_string_vector():
strings_vector = parselmouth.praat.call(strings, "List all strings")
assert isinstance(strings_vector, np.ndarray)
assert strings_vector.shape == (5,)
# assert strings_vector.dtype == np.object_
assert strings_vector.dtype == np.dtype('<U5')
assert strings_vector.dtype == np.object_
# assert strings_vector.dtype == np.dtype('<U5') # <- for candidate 1
assert np.all(strings_vector == sentence.split(" "))


Expand Down

0 comments on commit 14ad15a

Please sign in to comment.