Last active
July 27, 2019 09:28
-
-
Save petebankhead/8dfbaf2de91f6432b79c7678e2997d6a to your computer and use it in GitHub Desktop.
Import images & data from a QuPath v0.2.0-m2 project to v0.2.0-m3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Script to import images & data from a QuPath v0.2.0-m2 project to v0.2.0-m3. | |
* | |
* The script should be called from v0.2.0-m3, with a project already open. | |
* You'll be prompted to select a .qpproj file from v0.2.0-m2. | |
* Any compatible images will then be added to the m3 project, a new thumbnail | |
* created, and any data file transferred. | |
* | |
* Warning: Use with caution! | |
* Make sure any projects involved are backed up, and be on the lookout for | |
* errors or incompatibilities. In particular, files containing many images | |
* (read by Bio-Formats) can be problematic. | |
* Check everything! | |
*/ | |
import com.google.gson.Gson | |
import com.google.gson.JsonObject | |
import javafx.application.Platform | |
import qupath.lib.common.GeneralTools | |
import qupath.lib.gui.commands.ProjectImportImagesCommand | |
import qupath.lib.images.servers.ImageServer | |
import qupath.lib.images.servers.ImageServerBuilder | |
import qupath.lib.images.servers.ImageServerProvider | |
import qupath.lib.images.servers.bioformats.BioFormatsImageServer | |
import qupath.lib.images.servers.bioformats.BioFormatsServerBuilder | |
import qupath.lib.io.PathIO | |
import java.awt.image.BufferedImage | |
import java.nio.file.Paths | |
import static qupath.lib.gui.scripting.QPEx.* | |
def qupath = getQuPath() | |
def project = qupath.getProject() | |
if (project == null) { | |
print 'Please open a project! Entries will be imported from m2 to the current project.' | |
return | |
} | |
print 'Please select v0.2.0-m2 project file' | |
def fileM2 = qupath.getDialogHelper().promptForFile( | |
"Choose m2 project file", | |
null, | |
"Project file", | |
".qpproj" | |
) | |
if (fileM2 == null) | |
return | |
def dirDataM2 = new File(fileM2.getParentFile(), 'data') | |
// Keep a record of troubles | |
def problems = new StringBuilder() | |
// Loop through entries from M2 | |
def gson = new Gson() | |
JsonObject jsonM2 = gson.fromJson(fileM2.text, JsonObject.class) | |
def imagesM2 = jsonM2.get("images").getAsJsonArray() | |
for (temp in imagesM2) { | |
def entryM2 = gson.fromJson(temp, EntryM2.class) | |
print 'Importing ' + entryM2 | |
try { | |
// We assume the path is a URI, but some Bio-Formats paths contained a fragment (in M2) | |
// We need to extract the fragment & use it to identify the series | |
String pathM2 = entryM2.path | |
String[] args = new String[0] | |
def uriM2 = GeneralTools.toURI(pathM2) | |
ImageServer<BufferedImage> server | |
if (uriM2.getFragment()) { | |
// If we have a fragment, assume we have a Bio-Formats server - and build it | |
args = ['--series', uriM2.getFragment()] as String[] | |
uriM2 = GeneralTools.toPath(uriM2).toUri() | |
server = new BioFormatsImageServer(uriM2, args) | |
} else { | |
// If we don't have a fragment, build whatever server we can | |
server = ImageServerProvider.buildServer(pathM2, BufferedImage.class, args) | |
} | |
// Create a new entry in the m3 project | |
def builder = server.getBuilder() | |
if (!(builder instanceof ImageServerBuilder.DefaultImageServerBuilder)) { | |
throw new IOException("Cannot import builder " + builder) | |
} | |
def entry = project.addImage(builder) | |
// Set the name & thumbnail | |
entry.setImageName(entryM2.name) | |
def imgThumbnail = ProjectImportImagesCommand.getThumbnailRGB(server, null) | |
entry.setThumbnail(imgThumbnail) | |
// Open any data file we find & write in the m3 project | |
def fileDataM2 = Paths.get(dirDataM2.getAbsolutePath(), entryM2.uniqueName, 'data.qpdata').toFile() | |
if (fileDataM2.exists()) { | |
def imageDataM2 = PathIO.readImageData(fileDataM2, null, server, BufferedImage.class) | |
entry.saveImageData(imageDataM2) | |
} | |
server.close() | |
} catch (Exception e) { | |
print e | |
problems << System.lineSeparator() << entryM2.toString() | |
} | |
} | |
print 'Done!' | |
project.syncChanges() | |
Platform.runLater { | |
qupath.refreshProject() | |
} | |
if (problems.size() > 0) { | |
print 'Unable to import:' | |
print problems.toString() | |
} | |
class EntryM2 { | |
String path | |
String name | |
String uniqueName | |
public String toString() { | |
return path + '\t' + name + '\t' + uniqueName | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment