Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save petebankhead/1a0a91e2a6e4372d78277c9162685323 to your computer and use it in GitHub Desktop.

Select an option

Save petebankhead/1a0a91e2a6e4372d78277c9162685323 to your computer and use it in GitHub Desktop.
Script to update cell measurement names for a multichannel fluorescence image to incorporate actual stored channel names
/**
* Script to update cell measurement names for a multichannel fluorescence image
* to incorporate actual stored channel names, rather than the defaults of
* 'Channel 1', 'Channel 2' etc.
*
* @author Pete Bankhead
*/
import qupath.lib.scripting.QPEx
def server = QPEx.getCurrentImageData().getServer()
def cells = QPEx.getCellObjects()
// If true, just take the channel name before the first space
boolean splitAtSpace = true
// Get a map of channel names
// Here we take the channel name before the first space
def channelMap = [:]
for (int c = 0; c < server.nChannels(); c++) {
def channelName = server.getChannelName(c)
if (splitAtSpace)
channelName = channelName.split(' ')[0]
channelMap['Channel ' + (c+1)] = channelName
}
// Create a map of all measurement names, checking all cells
def measurementMap = [:]
for (cell in cells) {
// Loop through the names & update the map if we haven't seen one before
def ml = cell.getMeasurementList()
for (name in ml.getMeasurementNames()) {
if (measurementMap.containsKey(name))
continue
// Start by assuming the name won't change
measurementMap.put(name, name)
for (channel in channelMap.keySet()) {
if (name.contains(channel)) {
// Update the start of the measurement name
measurementMap.put(name, name.replace(channel, channelMap.get(channel)))
break
}
}
}
}
// Loop through the cells & update
for (cell in cells) {
// Put measurements into a temporary map
def ml = cell.getMeasurementList()
def newMeasurements = [:]
for (int m = 0; m < ml.size(); m++) {
def name = ml.getMeasurementName(m)
def value = ml.getMeasurementValue(m)
newMeasurements.put(measurementMap[name], value)
}
// Clear MeasurementList & replace its values
ml.clear()
for (entry in newMeasurements.entrySet()) {
ml.putMeasurement(entry.getKey(), entry.getValue())
}
ml.closeList()
}
QPEx.fireHierarchyUpdate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment