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

Writing strided numpy arrays #1929

Open
MortenHannemose opened this issue Nov 28, 2024 · 1 comment
Open

Writing strided numpy arrays #1929

MortenHannemose opened this issue Nov 28, 2024 · 1 comment

Comments

@MortenHannemose
Copy link

There seems to be an issue when writing numpy arrays with non-standard strides. Consider the following example of me writing a multi-channel image

import numpy as np
shape = (4,4)

channels_np = np.zeros((*shape, 10), dtype=np.float32)
channels_np += np.arange(10)
channels = {}
for i in range(channels_np.shape[-1]):
    channels[f"chan{i}"] = channels_np[:, :, i]

print("chan1 before writing\n", channels["chan1"])
header = {"compression" : OpenEXR.RLE_COMPRESSION, "type" : OpenEXR.scanlineimage}
with OpenEXR.File(header, channels) as outfile:
    outfile.write("tmp.exr")

with OpenEXR.File("tmp.exr") as infile:
    print("chan1 read from file\n", infile.channels()["chan1"].pixels)

This prints

chan1 before writing
 [[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]
chan1 read from file
 [[1. 2. 3. 4.]
 [5. 6. 7. 8.]
 [9. 0. 1. 2.]
 [3. 4. 5. 6.]]

If I .copy() the arrays before adding them to the channels dict, this goes away, but it took me a long time to track down the error.
Ideally it should perform this copy internally if necessary, or at least warn the user that the strides are not supported.

@cary-ilm
Copy link
Member

cary-ilm commented Dec 1, 2024

Apologies for the lack of notice in the documentation, there is indeed an assumption that the numpy array has standard stride. I'll look into what it would take to relax that requirement, and/or update the documentation.

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

No branches or pull requests

2 participants