Created
August 5, 2018 13:42
-
-
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
This file contains hidden or 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 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