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

loop over write chunks added for raw encoding in writer #106

Merged
merged 1 commit into from
Dec 31, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions nrrd/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,24 @@ def _write_data(data, fh, header, compression_level=None, index_order='F'):
# Convert the data into a string
raw_data = data.tostring(order=index_order)

# Write the raw data directly to the file
fh.write(raw_data)
# Write the data in chunks (see _WRITE_CHUNKSIZE declaration for more information why)
# Obtain the length of the data since we will be using it repeatedly, more efficient
start_index = 0
raw_data_len = len(raw_data)
# Loop through the data and write it by chunk
while start_index < raw_data_len:
# End index is start index plus the chunk size
# Set to the string length to read the remaining chunk at the end
end_index = min(start_index + _WRITE_CHUNKSIZE, raw_data_len)

# Write the compressed data
fh.write(raw_data[start_index:end_index])

start_index = end_index

# Finish writing the data
fh.flush()

elif header['encoding'].lower() in ['ascii', 'text', 'txt']:
# savetxt only works for 1D and 2D arrays, so reshape any > 2 dim arrays into one long 1D array
if data.ndim > 2:
Expand Down