Guest User

Untitled

a guest
Jun 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.63 KB | None | 0 0
  1. guiscript=true
  2.  
  3. import javafx.collections.FXCollections
  4. import javafx.geometry.Insets
  5. import javafx.geometry.Pos
  6. import javafx.scene.Scene
  7. import javafx.scene.control.Button
  8. import javafx.scene.control.ChoiceBox
  9. import javafx.scene.layout.GridPane
  10. import javafx.scene.layout.StackPane
  11. import javafx.scene.text.Font
  12. import javafx.scene.text.FontWeight
  13. import javafx.scene.text.Text
  14. import javafx.stage.Stage
  15. import qupath.lib.gui.prefs.PathPrefs
  16. import qupath.lib.gui.QuPathGUI.Modes
  17. import qupath.lib.gui.QuPathGUI
  18. import qupath.lib.images.ImageData
  19. import qupath.lib.images.servers.ImageServer
  20. import qupath.lib.images.servers.ImageServerProvider
  21. import qupath.lib.io.PathIO
  22. import qupath.lib.objects.PathObject
  23. import qupath.lib.scripting.QP
  24. import qupath.lib.common.GeneralTools
  25.  
  26. import groovy.io.FileType
  27. import qupath.lib.scripting.QPEx
  28.  
  29. import javax.script.ScriptContext
  30. import javax.script.ScriptEngine
  31. import javax.script.ScriptEngineManager
  32. import javax.script.SimpleBindings
  33. import javax.script.SimpleScriptContext
  34. import java.awt.image.BufferedImage
  35.  
  36. /** Pre-analysis script for selecting annotations to run a script on
  37. *
  38. *
  39. * @author Caleb Grenko
  40. *
  41. **/
  42.  
  43. Preanalysis.run()
  44.  
  45. class Preanalysis {
  46.  
  47. static void run() {
  48. def selections = getSelections()
  49. print("When this appears, you should have just hit 'Done'")
  50. def script = getScript()
  51. print(script)
  52. runScriptOnSelections(script, selections)
  53. }
  54. /**
  55. * Checks for selections. If no annotations are selected, gives option of either launching selection viewer
  56. * or using the existing annotations. However, it disables the annotation button if there are no annotations.
  57. * After the choice is made, it selects the objects which you chose.
  58. *
  59. */
  60. static void getSelections() {
  61. print("Getting selections")
  62.  
  63. def selections = QP.getSelectedObjects().findAll() { it.getDisplayedName() == "PathAnnotationObject" }.toSet()
  64. def hasSelections = (!selections.isEmpty())
  65.  
  66. GridPane grid = new GridPane()
  67. grid.setAlignment(Pos.CENTER)
  68. grid.setHgap(50)
  69. grid.setVgap(15)
  70. grid.setPadding(new Insets(15, 10, 15, 10))
  71.  
  72. //Initial prompt
  73. Text text = new Text("Select which \nobjects to use")
  74. text.setFont(Font.font("Arial", FontWeight.LIGHT, 14))
  75. StackPane stack = new StackPane()
  76. stack.getChildren().add(text)
  77. grid.add(stack, 0, 1, 2, 1)
  78.  
  79. //Open region selector option
  80. Button openSelectorButton = new Button("Launch region selector")
  81. openSelectorButton.setOnAction {
  82. runRegionSelector()
  83. }
  84. openSelectorButton.setPrefWidth(150)
  85. grid.add(openSelectorButton, 2, 1)
  86.  
  87. //Use currently selected objects
  88. Button useSelectionsButton = new Button("Use current selections")
  89.  
  90. if (!hasSelections) {
  91. useSelectionsButton.setDisable(true)
  92. }
  93. useSelectionsButton.setOnAction {
  94. useSelectionsButton.getScene().getWindow().hide()
  95. QP.selectAnnotations()
  96. }
  97. useSelectionsButton.setPrefWidth(150)
  98. grid.add(useSelectionsButton, 2, 2)
  99.  
  100. //Use all annotations
  101. Button useAnnotationsButton = new Button("Use all annotations")
  102.  
  103. if (QP.getAnnotationObjects().isEmpty()) {
  104. useAnnotationsButton.setDisable(true)
  105. }
  106. useAnnotationsButton.setOnAction {
  107. useAnnotationsButton.getScene().getWindow().hide()
  108. QP.selectAnnotations()
  109. }
  110. useAnnotationsButton.setPrefWidth(150)
  111. grid.add(useAnnotationsButton, 2, 3)
  112.  
  113. //Cancel the process
  114. Button cancelButton = new Button("Close")
  115. cancelButton.setCancelButton(true)
  116. cancelButton.setOnAction {
  117. cancelButton.getScene().getWindow().hide()
  118. }
  119. cancelButton.setPrefWidth(150)
  120. grid.add(cancelButton, 2, 4)
  121.  
  122. def stage = new Stage()
  123. stage.setTitle('Select Objects To Use')
  124. stage.setResizable(false)
  125. stage.setScene(new Scene(grid, 300, 200))
  126. stage.initOwner(QuPathGUI.getInstance().getStage())
  127. stage.showAndWait()
  128. }
  129.  
  130. /**
  131. * Allows user to repeatedly draw regions. When the "Done" button is pressed, it selects the regions just drawn.
  132. *
  133. * @return List of regions that the user selects
  134. *
  135. * This works by toggling the "returnToMoveMode" preference and then selecting the Rectangle mode. When the "Done"
  136. * button is pressed, it toggles back to the previous setting and changes back to the move marker. When the window is
  137. * opened, a list is created of all pre-existing annotations. After the user is done, another list is created and the
  138. * contents of the old list are removed from the new list. The remainder of the objects in the new list are selected.
  139. *
  140. * TODO: On drag release, add to empty list for more efficiency
  141. *
  142. */
  143. static LinkedList<PathObject> runRegionSelector() {
  144. def oldObjects = new LinkedList<PathObject>()
  145. def qupath = QuPathGUI.getInstance()
  146. def previousPref = PathPrefs.getReturnToMoveMode()
  147. def hierarchy = qupath.getImageData().getHierarchy()
  148.  
  149. //Region selection scene
  150. GridPane selectionGrid = new GridPane()
  151. selectionGrid.setAlignment(Pos.CENTER)
  152. selectionGrid.setHgap(30)
  153. selectionGrid.setVgap(10)
  154. selectionGrid.setPadding(new Insets(25, 10, 25, 10))
  155. Text selectText = new Text("Hit 'Done!' when finished creating regions")
  156. selectText.setFont(Font.font("Arial", FontWeight.LIGHT, 14))
  157. StackPane selectionStack = new StackPane()
  158. selectionStack.getChildren().add(selectText)
  159. selectionGrid.add(selectionStack, 0, 1, 2, 1)
  160.  
  161. //Done Button: resets user preferences for ReturnToMoveMode
  162. Button doneButton = new Button()
  163. doneButton.setText("Done!")
  164.  
  165. doneButton.setOnAction() {
  166. PathPrefs.setReturnToMoveMode(previousPref)
  167. qupath.setMode(Modes.MOVE)
  168. def newObjectsList = qupath.getImageData().getHierarchy().getFlattenedObjectList(new LinkedList<PathObject>())
  169. newObjectsList.removeAll(oldObjects)
  170. doneButton.getScene().getWindow().hide()
  171. hierarchy.getSelectionModel().selectObjects(newObjectsList)
  172. }
  173. selectionGrid.add(doneButton, 3, 3)
  174.  
  175. def stage = new Stage()
  176. stage.setTitle('Select regions')
  177.  
  178. stage.setOnShown {
  179.  
  180. oldObjects = hierarchy.getFlattenedObjectList(new LinkedList<PathObject>())
  181. PathPrefs.setReturnToMoveMode(false)
  182. qupath.setMode((Modes.RECTANGLE))
  183. }
  184.  
  185. stage.setOnCloseRequest {
  186. PathPrefs.setReturnToMoveMode(previousPref)
  187. qupath.setMode(Modes.MOVE)
  188. }
  189.  
  190. stage.setScene(new Scene(selectionGrid, 300, 100))
  191. stage.setResizable(false)
  192. stage.setAlwaysOnTop(true)
  193.  
  194. stage.initOwner(qupath.getStage())
  195. stage.showAndWait()
  196. return QP.getSelectedObjects()
  197. }
  198.  
  199. static String getScript() {
  200. //Fetches image data, sets to Brightfield (other) and estimates stains if not set already
  201. def imageData = QP.getCurrentImageData()
  202. print("Current image type: " + imageData.getImageType())
  203. //Gets directory of projects (assumes parent folder for current project)
  204. def generalProjectDirectory = getProjectBaseDirectory().substring(0, getProjectBaseDirectory().lastIndexOf("\\"))
  205.  
  206. GridPane grid = new GridPane()
  207. grid.setAlignment(Pos.CENTER)
  208. grid.setHgap(0)
  209. grid.setVgap(15)
  210. grid.setPadding(new Insets(15, 10, 15, 10))
  211.  
  212. Text text = new Text("Select script to run on selected regions")
  213. text.setFont(Font.font("Arial", FontWeight.LIGHT, 14))
  214. StackPane stack = new StackPane()
  215. stack.getChildren().add(text)
  216. grid.add(stack, 0, 0,)
  217.  
  218.  
  219.  
  220.  
  221. // Create a File object
  222. def folder = new File("${generalProjectDirectory}\\Analysis\\Scripts")
  223.  
  224. // If it doesn't exist
  225. if( !folder.exists() ) {
  226. // Create all folders up-to and including B
  227. folder.mkdirs()
  228. }
  229.  
  230. def list = []
  231.  
  232. folder.eachFileRecurse(FileType.FILES) { file ->
  233. list << file
  234. }
  235.  
  236. def listNames = new ArrayList<String>()
  237.  
  238. def name
  239. for (item in list) {
  240. name = item.toString()
  241. listNames.add(name.substring(name.lastIndexOf('\\') + 1, name.lastIndexOf('.')))
  242. }
  243.  
  244. ChoiceBox scripts = new ChoiceBox(FXCollections.observableArrayList(listNames))
  245. grid.add(scripts, 0, 1)
  246.  
  247. Button ok = new Button("Ok")
  248. ok.setOnAction {
  249. ok.getScene().getWindow().hide()
  250. }
  251.  
  252. grid.add(ok, 1, 1)
  253. def stage = new Stage()
  254. stage.setTitle('Select script')
  255. stage.setResizable(false)
  256. stage.setScene(new Scene(grid, 300, 100))
  257. stage.initOwner(QuPathGUI.getInstance().getStage())
  258. stage.showAndWait()
  259. return list.get(scripts.getSelectionModel().getSelectedIndex())
  260. }
  261.  
  262. /** Executes a given script on a specified collection of regions
  263. *
  264. * @param scriptPath the analysis script to be used on selected regions
  265. * @param selections a collection of PathAnnotationObject where the script will be executed
  266. *
  267. * TODO: Get it to actually work
  268. *
  269. */
  270. static void runScriptOnSelections(String scriptPath, Collection<PathObject> selections) {
  271. ScriptEngineManager manager = new ScriptEngineManager()
  272. ScriptEngine engine = manager.getEngineByExtension("groovy")
  273. String script = GeneralTools.readFileAsString(scriptPath)
  274.  
  275. engine.eval(script)
  276.  
  277. }
  278. }
Add Comment
Please, Sign In to add comment