-
Notifications
You must be signed in to change notification settings - Fork 42
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
Reading files into NArray #192
Comments
Hello. If the data is small, you can read a text file and convert the ruby array to Numo::NArray with Input file:
ruby script. require 'numo/narray'
na = nil
File.open("input.txt", "r") do |f|
a, b = f.gets.split("\n").map(&:to_i)
arr = f.gets.split(" ").map(&:to_i)
na = Numo::UInt8.cast(arr).reshape(a, b)
end
p na Of course, you can also use the CSV library. If the input is in binary format, use Numo::NArray is not flexible enough to change its size. Methods such as If you want to deal with really huge data, Apache Arrow may be useful. Currently, Apache Arrow is the fastest way to convert CSV files to Ruby data, with much better performance than the standard CSV library. |
Or do you need an alternative to numpy's fromfile? |
Thank you for your reply. Yes, I'm searching for a way to load huge data in rails/ruby application. Small data would also be used as it all depends on user input. User can upload any file type, it gets splitted and data is loaded in binary. I never used numpy but I think there would be a problem as I have to combine few files into one array/matrix. Thank you once again, I would appreciate if you could maybe think of something else you can recommend as I specified my goal. |
Also I can specify that files that user can upload won't be larger than 8-16 GB. Will ruby script you posted at the beginning be enough to achieve fast results? I guess that's not considered as "large" data. I tried loading data with built-in ruby array at my first approach to the algorithm and it wasn't enough so I guess I will have to use something else |
I see. |
Thank you for sharing knowledge. It's first time I'm working with this big data, so you helped a lot as I didn't know anything about it. I'm going to try your recommendations in the following days. |
Good luck with your work. VALUE nary;
uint8_t* nary_ptr; nary = rb_narray_new(numo_cUInt8, n_dims, shape);
nary_ptr = (uint8_t*)na_get_pointer_for_write(nary);
for (y = 0; y < height; y++) {
row_ptr = row_ptr_ptr[y];
memcpy(nary_ptr + y * width * n_ch, row_ptr, width * n_ch);
} return nary; |
Thank you for constantly bringing new ideas to the table. Overall the library works fine. Performance is ok for now, especially the loading part directly from binary data works great, better than I expected. I will try fromfile gem soon. I am not yet sure if matrix multiplication performance is going to be sufficient to bring the product to the market, but this part can be done in a different way. I appreciate your involvement, take care. |
@railsmk |
Hello,
I have asked a question at stackoverflow concerning reading files into Numo::NArray and dynamically inserting rows/data.
Would you care to show me the way or tell me if the thing I'm trying to do is even possible to accomplish with this library?
https://stackoverflow.com/questions/68282417/reading-files-into-ruby-numonarray
The text was updated successfully, but these errors were encountered: