Skip to content

Instantly share code, notes, and snippets.

@lacan
Created August 24, 2023 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacan/c3036ff064cb90b0823cd9f9dac1db83 to your computer and use it in GitHub Desktop.
Save lacan/c3036ff064cb90b0823cd9f9dac1db83 to your computer and use it in GitHub Desktop.
[Fix Unmapped Image Entry URIs] Fix overly long times to reach "Update URIs" when the server address changed #groovy #qupath #project #uri #server
/*
* Update QuPath URIs
* After the migration of our EPFL servers from svfasX to sv-nas1.rcp, some projects fail to open in a timely manner,
* especially under Windows when the full path to the server is used (instead of a networked drive).
* The reason seems to be that QuPath is waiting for a response from the server, and the timeouts are long, so it takes a
* really long time before the "Update URIs" window pops up
*
* This script aims to assist in fixing that by at least changing the host name (svfasX) to a valid host (sv-nas1.rcp)
* that way, QuPath will immediately either find or fail to find the file, restoring normal behavior
*
* In case this script breaks something, it creates a *new* .qpproj file in the same location instead of overwriting the original project.
*
* If you are feeling confident and want to overwrite your original project file, set the overwrite variable to "true"
*/
def overwrite = false // At your own risk
// Previous host name, that we want to change
def previousHostName = "svfas6"
// New host name to change the location of the data
def newHostName = "sv-nas1.rcp"
def qpProjectFile = Dialogs.promptForFile("QuPath Projejct file", null, null)
def projectRoot = new File( qpProjectFile.getParent() )
def qpProject = ProjectIO.loadProject( qpProjectFile, BufferedImage.class )
qpProject.getImageList().each { entry ->
def uris = entry.getUris()
def replacements = new HashMap<URI, URI>()
uris.each{
def host = it.getHost()
if (host == previousHostName ) {
host = newHostName
println "Changing entry $entry"
}
def newUri = new URI( it.getScheme(),
it.getUserInfo(),
host,
it.getPort(),
it.getPath(),
it.getQuery(),
it.getFragment()
)
replacements.put( it, newUri )
}
replacements.each{ k, v ->
println " Original: $k"
println " -----> Modified: $v"
}
entry.updateURIs( replacements )
}
// Write the project
if( overwrite ) {
qpProject.syncChanges()
} else {
qpProject.writeProject( new File( projectRoot, "project_modified.qpproj" ) )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment