Renaming Resource Files

215 views
Skip to first unread message

Duncan Smith

unread,
Mar 30, 2021, 9:38:40 AM3/30/21
to xnat_discussion
Hello,

We currently have an existing project requiring some naming changes for purposes of anonymization and consistency. I've not had a problem doing this for subjects and experiments, but I can't seem to do this for the individual scan resource files.

The files in question are stored in resource folders attached to scans. I have tried various REST calls but I haven't had any luck. This page suggests existing resource files can be updated, but I'm unsure if that means updated with a new file or if something like the filename can be changed: https://wiki.xnat.org/display/XAPI/Image+Scan+Resource+API#ImageScanResourceAPI-UpdateAnExistingResourceFile.

Is there a way to do this, either through a REST call or perhaps through XNATpy? If not, my backup option is to download the files, rename them, and then upload them. This isn't a small project so that wouldn't be an ideal solution as it will need to be every file in the project, so I'd like to avoid that if possible.

Thanks,

Duncan

Moore, Charlie

unread,
Mar 30, 2021, 11:07:51 AM3/30/21
to xnat_di...@googlegroups.com
Hi Duncan,

I'm not aware of a way to do that in the REST API. If your data is very "regular" (by which I mean you don't have this problem https://issues.xnat.org/browse/XNAT-6707), I think the best course of action would be to rename all of the files directly on the file system, and then just refresh the catalogs via the REST API. This is definitely something to dry run on a dev server though before production.

Thanks,
Charlie

From: xnat_di...@googlegroups.com <xnat_di...@googlegroups.com> on behalf of Duncan Smith <duno...@gmail.com>
Sent: Tuesday, March 30, 2021 8:38 AM
To: xnat_discussion <xnat_di...@googlegroups.com>
Subject: [XNAT Discussion] Renaming Resource Files
 

* External Email - Caution *

--
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/613b53cb-dfc7-49c3-a50c-f6b7a101b2b7n%40googlegroups.com.

 


The materials in this message are private and may contain Protected Healthcare Information or other information of a sensitive nature. If you are not the intended recipient, be advised that any unauthorized use, disclosure, copying or the taking of any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error, please immediately notify the sender via telephone or return mail.

Herrick, Rick

unread,
Mar 30, 2021, 1:03:43 PM3/30/21
to xnat_di...@googlegroups.com

Yeah, today I learned that there’s not a way to do what you’re trying to do. I thought there was! But unfortunately I was wrong.

 

Charlie’s correct: you could fairly easily write a script that would find and rename all of the files under your various resource folders. And rather than refreshing each of the resource catalogs, you could use a tool like sed or awk to change the names in the catalog files as well. Each catalog file contains zero to n entries, either <cat:entry> or <cat:dcmEntry>, each of which references an individual file. In its current architecture, XNAT has no references to anything lower than the resource catalog file, so you can rename the files there and modify references in the catalog at the same time. Suppose you have a catalog file like this (I cut a lot of stuff out of here for readability, so make sure you look at the catalog files you’ll be working with!):

 

<?xml version="1.0" encoding="UTF-8"?>
<cat:Catalog ID="FOO" xmlns:cat="http://nrg.wustl.edu/catalog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <cat:entries>
        <cat:entry ID="item1.pdf" URI="item1.pdf" cachePath="item1.pdf" content="PDF" digest="cc8ac14fd94ba5979f27346949df6dbc" format="PDF" name="item1.pdf"/>
        <cat:entry ID="item2.txt" URI="item2.txt" cachePath="item2.txt" content="TEXT" digest="ded4c18106345a8b39de6f12943eb4aa" format="TXT" name="item2.txt"/>
    </cat:entries>
</cat:Catalog>

 

If you wanted to do something like change the naming from itemN.ext to fileN.ext, you could use a command like this:

 

$ sed -E --in-place=".bak" 's/"item([0-9]+)\.([a-z0-9]+)"/"file\1.\2"/g' FOO_catalog.xml

 

This holds even if you’re working with resources in a folder structure. Since XNAT doesn’t know anything about where those files are located relative to the resource, you just need to fix up the various paths as well. The paths below the resource folder are reflected in the ID, URI, and cachePath attributes of the <cat:entry> and <cat:dcmEntry> elements, but not in the name attribute.

 

<cat:entry ID="subfolder/qa-notes.docx" URI="subfolder/qa-notes.docx" cachePath="subfolder/qa-notes.docx" name="qa-notes.docx">

 

Once you’ve renamed the files and changed the references in the catalog, you can just start using the system with your new organization.

 

-- 

Rick Herrick

Sr. Programmer/Analyst

Neuroinformatics Research Group

Washington University School of Medicine

Phone: +1 (314) 273-1645

Duncan Smith

unread,
Apr 1, 2021, 11:47:41 AM4/1/21
to xnat_discussion
Hi both,

This is really useful information, it's much appreciated. I'll look into taking this route next week and see how I get on. From briefly looking at our archive based on what you've said, it makes sense what I need to do (and is preferable to downloading, renaming, and uploading).

Thanks again,

Duncan

Duncan Smith

unread,
Apr 13, 2021, 7:54:21 AM4/13/21
to xnat_discussion
Just to update, I have been able to add in the resource file renaming to a script I was developing and it is all working as you explained. This will be useful to know in the future if there are any tasks needing something similar.

Thanks both.

Duncan

Herrick, Rick

unread,
Apr 13, 2021, 10:44:18 AM4/13/21
to xnat_di...@googlegroups.com

Excellent, I’m glad that’s working for you. I did add an issue to our Jira for a new feature to allow renaming and moving files within a resource folder, but no timeline on when that might get done.

 

-- 

Rick Herrick

Sr. Programmer/Analyst

Neuroinformatics Research Group

Washington University School of Medicine

Phone: +1 (314) 273-1645

 

Célia Sage

unread,
Nov 28, 2022, 9:32:12 AM11/28/22
to xnat_discussion
Hello, 

I was wondering if this new feature had been added since this message.

Thanks,
Célia

Rick Herrick

unread,
Nov 28, 2022, 12:03:58 PM11/28/22
to xnat_di...@googlegroups.com
No, not yet. You can check the status of any development by looking at the linked issue, XNAT-6766 (note that our Jira address is moving from issues.xnat.org to radiologics.atlassian.net so the link address has changed but the new address is referenced from the old one).

Rick Herrick
Senior Software Developer


------ Original Message ------
From "Célia Sage" <celia....@gmail.com>
To "xnat_discussion" <xnat_di...@googlegroups.com>
Date 11/28/2022 7:55:40 AM
Reply all
Reply to author
Forward
0 new messages