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

Problem reading multiple files using NSIDReader #69

Open
badarirao opened this issue Jan 9, 2022 · 5 comments
Open

Problem reading multiple files using NSIDReader #69

badarirao opened this issue Jan 9, 2022 · 5 comments

Comments

@badarirao
Copy link

Hello, I am able to successfully read one hdf5 file using NSIDReader and store it in a variable.
However, when I read another file and store it in another variable, somehow the 1st variable data is also replaced by the new file. Could you please help me with this?

example:
dset1 = NSIDReader("data1.h5").read()
--> dset1 consists of data from data1.h5
dset2 = NSIDReader("data2.h5").read()
--> now both dset1 and dset consists of data from data2.h5.

How do I prevent dset1 from being overwritten?

I have tried the NSIDReader from both SciFiReaders and pyNSID modules, with the same result.

@badarirao
Copy link
Author

Found a temparory solution for this: Make copy of each element in the list.
dset1 = NSIDReader("data1.h5").read()
dset1copy = []
for data in dset1:
dset1copy.append(data.copy())
dset2 = NSIDReader("data2.h5").read()
dset2copy = []
for data in dset2:
dset2copy.append(data.copy())

This seems to ensure the two data remain different.

@ramav87
Copy link
Contributor

ramav87 commented Jan 14, 2022

Thanks for letting us know. The expected usage of readers is one reader per file. In your case you've instantiated a single object and (I believe) the same object is being used to read a new file resulting in it being overwritten. It would be better to write it as follows:

reader1 = NSIDReader("data1.h5")
dset1 = reader1.read()

reader2 = NSIDReader("data2.h5")
dset2 = reader2.read()

This also is the better way because it allows you to close files.

@ramav87 ramav87 reopened this Jan 14, 2022
@ramav87 ramav87 closed this as completed Jan 14, 2022
@badarirao
Copy link
Author

@ramav87 I just checked your solution, but the situation does not change. Strangely, the memory address of the two objects seems to be different, but the data is overwritten. Only copy() seems to work.

@ramav87
Copy link
Contributor

ramav87 commented Jan 21, 2022

I think it might be an issue with your specific interptretor. I tested this on Google Colab and it is functional:

https://colab.research.google.com/drive/1n3iOGdUOff1t1Jbmq84GaIV-JK03lweP?usp=sharing

Please take a look and let me know.

@ramav87 ramav87 reopened this Jan 21, 2022
@badarirao
Copy link
Author

@ramav87 Okay, I checked in Google Colab, and your code seems to be working. Upon deeper inspection, I found that this is the case even in my local machine. The problem is with its metadata, which is being overwritten. So, the metadata of dset1 is overwritten by that of dset2, whereas the actual data is not changed.

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