Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add npy reader and writer #465

Merged
merged 1 commit into from Oct 30, 2017
Merged

add npy reader and writer #465

merged 1 commit into from Oct 30, 2017

Conversation

wolfv
Copy link
Member

@wolfv wolfv commented Oct 19, 2017

needs tests...

@wolfv
Copy link
Member Author

wolfv commented Oct 19, 2017

@llohse we used your code. can we remove the MIT license and only keep the BSD, with your copyright?

@llohse
Copy link

llohse commented Oct 20, 2017

@wolfv: Yes, you may remove the MIT license.
I would kindly ask for any significant improvements to be provided to the original project.

@llohse
Copy link

llohse commented Oct 20, 2017

Note that the newest version of libnpy has compile-time type-checking and byte-order determination.

@wolfv
Copy link
Member Author

wolfv commented Oct 20, 2017

ok, great, I modified the license in the header accordingly.
I would have contributed improvements back, but most of what is changed is style + the map_type (which is kind of runtime, but i guess the compiler will remove all branches (as the type is known at compile time) and it reduced the code size quite a bit, so this is rather a stylistic change.

Thanks for the great piece of work!

Wolf

@wolfv
Copy link
Member Author

wolfv commented Oct 20, 2017

hnggg... the github user interface offered me a merge of master ... probably we're gonna rebase anyways.

@JohanMabille
Copy link
Member

JohanMabille commented Oct 20, 2017

indeed I think you'll want to rebase, squash, remove the merge commit and force push once the lights are green.

@SylvainCorlay
Copy link
Member

@llohse awesome! libnpy is really nice.

By the way, we are working hard to support more and more features of numpy in xtensor (see the cheat sheet). Don't hesitate to hop on our gitter chat if you are interested.

@JohanMabille
Copy link
Member

JohanMabille commented Oct 23, 2017

unsigned long is 8-bytes long on linux whereas it is 32-bytes long on Windows. I think we should use types with explicit size such as uint_32 / uint64_t in the tests to avoid that kind of errors.

: m_shape(shape), m_fortran_order(fortran_order), m_typestring(typestring)
{
// Allocate memory
m_word_size = atoi(&typestring[2]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem: The "typestring" can have a much more general form where the 3rd byte is not the width.

size_t m_word_size;
size_t m_n_bytes;
std::string m_typestring;
char* m_buffer;
Copy link
Member Author

@wolfv wolfv Oct 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed there is a memory leak here.

I see two options:
a) adding a boolean (pointer_moved, or has_been_cast) that indicates wether this object has been cast, and the pointer is owned by the xadaptor (which will take care of the destruction). If not, the destructor of npy_file will free the memory.
b) using a smart pointer (maybe unique, or shared) and hope that the xadaptor can handle them ;)

What do you think @SylvainCorlay?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the npy file could have two actual C++ casting operators

  • one that return an xarray_container<owning_buffer_adapter, L, std::vector<std::size_t>> && which can only be called when the npy file is an rvalue. It takes ownership of the pointer and prevents the npy file destructor to actually delete the buffer with a moved_from flag or setting the pointer in the class to nullptr.

  • one to xarray_adaptor<nonowning_buffer_adapter, L, std::vector<std::size_t>> which can only be called when the npy file is an lvalue. It does not take ownership of the pointer. When assigning it to a container, the generalized copy constructor is called and a copy occurs.

So basically

auto operator xarray_container<owning_buffer_adapter, L, std::vector<std::size_t>() &&

and

auto operator xarray_adaptor<const_nonowning_buffer_adapter, L, std::vector<std::size_t>() const &

and

auto operator xarray_adaptor<nonowning_buffer_adapter, L, std::vector<std::size_t>() &

if ((L == layout_type::column_major && !m_fortran_order) ||
(L == layout_type::row_major && m_fortran_order))
{
throw std::runtime_error("fortran order and xarray layout type do not match up.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Layout mismatch between npy file and requested layout.

@SylvainCorlay
Copy link
Member

You can rebase on master now that the xbuffer_adaptor thingy is in.

@wolfv
Copy link
Member Author

wolfv commented Oct 27, 2017

@SylvainCorlay if you could look at the casters ... this way it seems to somewhat work :)

Copy link
Member

@SylvainCorlay SylvainCorlay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The codacy warning about the npy file missing copy constructors seems legitimate.

We should probably have the all the copy semantics operator (assignments and copy constructors plus move versions setting the pointer to nullptr).

{
auto cast_elems = cast_impl<T, L>(check_type);
m_buffer = nullptr;
return std::move(xadapt(std::move(std::get<0>(cast_elems)), std::get<1>(cast_elems),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that there is no need to call std::move on xadapt since xadapt should already return an rvalue.

@SylvainCorlay SylvainCorlay merged commit 45b5ce3 into xtensor-stack:master Oct 30, 2017
@wolfv wolfv deleted the xnpy branch November 1, 2017 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants