-
Notifications
You must be signed in to change notification settings - Fork 3k
Add nifti support #7815
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
Add nifti support #7815
Conversation
lhoestq
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow this is awesome ! the code looks all good to me
I am a bit unsure what we need to add to the document_dataset.mdx and document_load.mdx. I should probably create a dataset on the hub first to create this guide instead of copy+pasting from PDF.
imo you could get some inspiration from the PDF docs indeed but showcase how it works for an actual dataset, and ideally what are the main usage of Nifti1Image in general and also in a training setting (convert to PIL.Image or torch tensor for example)
| return test_case | ||
|
|
||
|
|
||
| def require_nibabel(test_case): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't forget to add nibabel to setup.py in the test dependencies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done,thanks
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
|
Btw I couldn't resist but share your PR with the community online on twitter already, I hope this is fine ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, docs are updated, code in the docs is tested as well and works. Happy for another review round.
EDIT: I created a proper nifti dataset on the hub: https://huggingface.co/datasets/TobiasPitters/NIfTI-SIRF-exercises-geometry, but thought it's not good practice to reference personal (yet public) datasets in the docs.
| return test_case | ||
|
|
||
|
|
||
| def require_nibabel(test_case): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done,thanks
Wow, that was quick! Thanks, already liked your comment, I appreciate it! |
lhoestq
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm !
|
NIfTI support is out in Btw do you know a good NIfTI vizualizer in HTML/JS or using python ? We could add something like cc @cfahlgren1 @georgiachanning for viz |
|
Hi, I have a quick question while testing the new NIfTI support. I cloned the latest from datasets import load_dataset
dataset = load_dataset(
"TobiasPitters/NIfTI-SIRF-exercises-geometry",
split="train"
)
dataset[0]['nifti'].get_fdata()[0].shapeHowever, I’m getting the following error: When I manually place the NIfTI file in that local path, it works fine. Interestingly, after adding the |
Thanks for the report, I can confirm this. It's a problem with the dataset. Can you try this: from datasets import load_dataset
import nibabel as nib
dataset = load_dataset(
"TobiasPitters/test-nifti-unzipped",
split="train" # Load as single Dataset, not DatasetDict
)
print("length dataset:", len(dataset))
for item in dataset:
assert isinstance(item["nifti"], nib.nifti1.Nifti1Image)If your interested in |
I would suggest https://github.com/rii-mango/Papaya, just tested it and it looks quite good. How would it work to add that to the dataset-viewer? And I assume you'd like to have the EDIT: do we have anything like this already for other features? Couldn't find anything. I mean we can do this in different ways, simply inlining papaya or building custom components (like e.g. SHAP is doing). If we decide for the latter, this means that we'll need to build js components in datasets, so we'll need a bundler, etc. but this provides the highest flexibility. If that's of interest, I can take a look into this. |
|
Hi, thanks for your help earlier. I tested the dataset you shared (TobiasPitters/test-nifti-unzipped), and it works perfectly — all NIfTI files load correctly and get_fdata() returns valid arrays. However, when I upload my own dataset to the Hugging Face Hub using my code, it doesn’t work properly. The NIfTI files seem not to decode correctly. can you check it? |
Are you using zipped Nifti files? It seems like there is an issue with that. I found that this creates problems, that in @lhoestq : what do you suggest here? We could probably do something like this in the if path.startswith("gzip:"):
path = path.split("::")[-1]Though I would need to test if this is actually OS agnostic. |
|
I think the issue with gzip can be fixed using the same code as in Image() imo: - try:
- repo_id = string_to_dict(source_url, pattern)["repo_id"]
- token = token_per_repo_id.get(repo_id)
- except ValueError:
- token = None
+ source_url_fields = string_to_dict(source_url, pattern)
+ token = (
+ token_per_repo_id.get(source_url_fields["repo_id"]) if source_url_fields is not None else None
+ ) |
Can you pls try with this branch: This should fix the existing problems with NifTI |
|
I checked and it looks like the fix-embed-storage-nifti branch has already been merged into the main. And it worked fine. thanks. |
Add support for NIfTI.
supports #7804
This PR follows #7325 very closely
I am a bit unsure what we need to add to the
document_dataset.mdxanddocument_load.mdx. I should probably create a dataset on the hub first to create this guide instead of copy+pasting from PDF.Open todos:
[ ] updatedocument_dataset.mdxanddocument_load.mdxEDIT:
I tested with two datasets I created on the hub:
for zipped (file extension
.nii.gzand unzipped.nii) files and both seem to work fine. Also tested loading locally and that seems to work as well.Here is the scriptsthat I ran against the hub: