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

Can't update existing attributes in edit mode #48

Closed
garrettmflynn opened this issue Apr 7, 2023 · 7 comments
Closed

Can't update existing attributes in edit mode #48

garrettmflynn opened this issue Apr 7, 2023 · 7 comments

Comments

@garrettmflynn
Copy link

When opening an existing file in edit ('a') mode, any attempt to update an existing attribute (or other aspects of the file, to my knowledge) results in internal warnings that keep the attribute unmodified. Is this the intended / known behavior of the library?

Here's a minimal example that will trigger these warnings (code, demo):

import h5wasm from "https://cdn.jsdelivr.net/npm/[email protected]/dist/esm/hdf5_hl.js";
const { FS } = await h5wasm.ready;

let new_file = new h5wasm.File("myfile.h5", "w");

const attrToUpdate = "new_attr"

new_file.create_attribute(attrToUpdate, "something wicked this way comes");
new_file.close()

let editable = new h5wasm.File("myfile.h5", "a");
editable.create_attribute(attrToUpdate, "something new this way comes");
editable.close()

let readable = new h5wasm.File("myfile.h5", "r");
console.log(`Got updated ${attrToUpdate} on hdf5 file:`, readable.attrs[attrToUpdate].value)

This may be related to #14, though this PR specifically flagged their need to update datasets—not other parts of the file.

Currently, webnwb loads all file contents into a JS object that tracks changes and is recompiled into a separate HDF5 file when the user decides to save them—though it would be significantly more elegant to place updates into the existing h5wasm File instance (loaded in 'a' mode) when that decision occurs and close the file instance when all edits are complete.

@bmaranville
Copy link
Member

You're right - this is not a feature that has been implemented at all in h5wasm. When an h5wasm.File object is opened in "append" mode, that means you can add new datasets or attributes or groups, but no attempt has been made to allow you to edit existing objects.

This would take some research and work - it seems like if you're not changing the datatype or shape, you can just overwrite an attribute's data, but in the example you've given above the dtype is different between the old attribute value and the new. Looking at the source code for h5py for setting an attribute when it already exists, it seems they do a little dance where they create a temporary attribute from the new data, then delete the existing attribute, then rename the temporary to the old name. It's still easier than adding dataset editing like in #14

Have you considered using pyodide and h5py in your code? It takes a while to start up (~5 seconds on a newish computer) but after that you have the full h5py API in javascript. For advanced manipulations of HDF5 objects it might be worth the startup cost to get all that functionality.

@bmaranville
Copy link
Member

It looks like it would be pretty easy to add a method

function delete_attribute(attr_name: string): number

to h5wasm, then in your code you could overwrite attrs by delete_attribute followed by create_attribute?

@garrettmflynn
Copy link
Author

garrettmflynn commented Apr 10, 2023

Thank you for your detailed response! Being able to delete properties would be really wonderful. Are there additional complexities with deleting groups, datasets, etc. in this way?

The library I’m trying to replicate in JavaScript (PyNWB) is dependent on h5py and I’ve tried using that library in Pyodide. However, I’d had some issues with enumeration and toJs resolution. This may, however, be due to my lack of familiarity with Pyodide—and I'll admit the exploration was quite quick. For a full rundown of what I'd learned from it, see Issue 4 on WebNWB.

@bmaranville
Copy link
Member

Yeah, I have to admit that making detailed interconnects between python code and javascript code in pyodide still seems pretty complicated to me. I have some projects that mostly stay on the python side, or mostly on the javascript side, and that's easier than trying to mesh the two.

I added a delete_attribute method to the main branch of h5wasm, so it should be in the next release - how important is it to your use case to also be able to delete groups and datasets?

@garrettmflynn
Copy link
Author

garrettmflynn commented Apr 10, 2023 via email

@bmaranville
Copy link
Member

Dataset.delete_attribute and Group.delete_attribute are implemented in the newest v0.4.11, published to npm and github just now. (also able to create resizable datasets and overwrite sections of data, and resize datasets)

@garrettmflynn
Copy link
Author

garrettmflynn commented Apr 19, 2023 via email

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