HOW TO SEE IF I CAN GET TO THE FILE IN RESOURCES

44 views
Skip to first unread message

Albertina Ripanu

unread,
Apr 27, 2024, 7:44:57 PM4/27/24
to xnat_discussion
Hi! I am trying my best to understand how to work with XNAT and resources .
So basically, I have a project and a subject, and when I enter on the manage files, I have resources>NIFTI(3files). 
I do not have any experiment, so no scans neither. 
How can I call the files from my resources on python in order to see if the files exists?
Down you will see what it gets returned if i run the commands but I do not know how to work it out with resources.
If you have any advice of how to correctly store the NIFTI files , please let me know as I am trying to integrate in a software in order to be able to download from the xnat server, the NIFTI files . 
de=interface.select.project('proj').subjects('11').get()
        print(de)
>>>[]
de=interface.select.project('proj').get()
>>>[11]

Message has been deleted

Albertina Ripanu

unread,
Apr 27, 2024, 8:46:11 PM4/27/24
to xnat_discussion
 # Select the project
        project = interface.select.project(project_id)

        # Check if the project exists
        if project.exists():
            # Select the subject
            subject = project.subject(subject_id)

            # Check if the subject exists
            if subject.exists():
                # Select the resource
                resource = subject.resource(resource_name)

                # Check if the resource exists
                if resource is not None:
                    # Get the specific file from the resource
                    file = resource.file(file_name)

                    # Check if the file exists
                    if file.exists():
                        # Download the file using the get() method
                        file_path = file.get()
I did this but it downloads automatically to the file downloads on my own PC. How do I tell it the path where to be downloaded?

Jordi Huguet

unread,
May 28, 2024, 9:47:29 AM5/28/24
to xnat_di...@googlegroups.com
Hi/Buna Albertina, 

I did this but it downloads automatically to the file downloads on my own PC. How do I tell it the path where to be downloaded?
 
It is specified in the `get()` function docstring:

In [1]: f.get?
Signature: f.get(dest=None)
Docstring:
Downloads the file.
Parameters
----------
dest: string | None
    - If dest is None, then the user's Downloads directory is used
        as the default download location.
    - Else the file is downloaded at the requested location.
        Path should include the file name.
        eg: /path/to/file.txt
Returns
-------
string : the file location.
File:      ~/envs/pyxnat/lib/python3.10/site-packages/pyxnat/core/resources.py
Type:      method

See below an example that fetches a random file (through project > experiment > resource) and retrieves it,
```
import pyxnat

cfg = '.xnat.cfg'
intf = pyxnat.Interface(config=cfg)

random_expID = intf.select.project('sandbox').experiments().get()[0]
e = p.experiment(random_expID)
random_file  = e.resource('MY_RESOURCE').files()[0]

random_file.get(dest='/enter/here/your/local/file_path.nii.gz')
```

Finally, you can find inspirational code fragments/examples at the pyxnat project repo (https://github.com/pyxnat/pyxnat) as well as at the docs page (https://pyxnat.github.io/pyxnat/). 

Hope it helps. 

Jordi


--
You received this message because you are subscribed to the Google Groups "xnat_discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xnat_discussi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xnat_discussion/1524e3d7-efad-4c33-9256-e485644ae4d5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages