1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
| package plugins.nchenouard.particletracking;
import icy.file.FileUtil;
import icy.gui.frame.IcyFrame;
import icy.gui.frame.IcyInternalFrame;
import icy.gui.frame.progress.AnnounceFrame;
import icy.gui.frame.progress.ProgressFrame;
import icy.image.ImageUtil;
import icy.main.Icy;
import icy.plugin.abstract_.PluginActionable;
import icy.resource.ResourceUtil;
import icy.sequence.Sequence;
import icy.swimmingPool.SwimmingObject;
import icy.system.SystemUtil;
import icy.util.XMLUtil;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import plugins.fab.trackmanager.TrackGroup;
import plugins.fab.trackmanager.TrackManager;
import plugins.fab.trackmanager.TrackSegment;
import plugins.nchenouard.particletracking.MHTracker.HMMMHTracker;
import plugins.nchenouard.particletracking.filtering.IMM2D;
import plugins.nchenouard.particletracking.filtering.IMM3D;
import plugins.nchenouard.particletracking.filtering.KF2dDirected;
import plugins.nchenouard.particletracking.filtering.KF2dRandomWalk;
import plugins.nchenouard.particletracking.filtering.KF3dDirected;
import plugins.nchenouard.particletracking.filtering.KF3dRandomWalk;
import plugins.nchenouard.particletracking.filtering.Predictor;
import plugins.nchenouard.particletracking.filtering.Predictor2D;
import plugins.nchenouard.particletracking.filtering.Predictor3D;
import plugins.nchenouard.particletracking.gui.TrackingMainPanel;
import plugins.nchenouard.particletracking.legacytracker.StoppableTracker;
import plugins.nchenouard.particletracking.legacytracker.Tracker;
import plugins.nchenouard.particletracking.legacytracker.associationMethod.InstantaneousTracker;
import plugins.nchenouard.particletracking.legacytracker.associationMethod.SolveMLAssociation;
import plugins.nchenouard.particletracking.legacytracker.associationMethod.Track;
import plugins.nchenouard.particletracking.legacytracker.gui.PanelTracking;
import plugins.nchenouard.particletracking.simplifiedMHT.SimplifiedMHTPanel;
import plugins.nchenouard.spot.Detection;
import plugins.nchenouard.spot.DetectionResult;
import plugins.nchenouard.spot.Spot;
/**
* Particle tracking plugin for ICY that uses the multiple hypothesis tracking algorithm
* described in:
* Nicolas Chenouard, Isabelle Bloch, Jean-Christophe Olivo-Marin,
* "Multiple Hypothesis Tracking for Cluttered Biological Image Sequences," IEEE Transactions on Pattern Analysis and Machine Intelligence, vol. 35, no. 11, pp. 2736-3750, Nov., 2013
* Pubmed link: http://www.ncbi.nlm.nih.gov/pubmed/23689865
*
* Part of the Spot Tracking plugin for ICY: http://icy.bioimageanalysis.org/plugin/Spot_Tracking
*
* @author Nicolas Chenouard (nicolas.chenouard@gmail.com)
* @version 3.1
* @date 2013-11-13
* @license gpl v3.0
*/
public class SpotTrackingPlugin extends PluginActionable implements ActionListener
{
public static String MHT_PLUGIN_CONFIGURATION = "MHTconfiguration";
private enum GUIStateEnum {ADVANCED, SIMPLE, LEGACY};
GUIStateEnum currentGUISate = GUIStateEnum.SIMPLE;
IcyFrame mainFrame;
JMenuBar menuBar;
JMenu guiMenu, fileMenu, infoMenu;
JMenuItem menuItem;
JPanel mainPanel;
final String SIMPLEGUI = "Simple interface";
final String ADVANCEDGUI = "Avanced interface";
final String LEGACYGUI = "Legacy plugin";
TrackingMainPanel advancedGUIPanel;
SimplifiedMHTPanel simpleGUIPanel;
PanelTracking legacyGUIPanel;
LegacyTrackerManager legacyTrackerManager;
MHTrackerManager mhTrackerManager;
MHTparameterSet mhtParameterSet = null;
boolean defaultParameterSet = true;
Thread computationThread = null;
ProgressFrame trackingAnnounce;
public static boolean optimizationLibraryLoaded = false;
boolean useMultithreading = true;
boolean useLPSolve = true;
public SpotTrackingPlugin()
{
if (!optimizationLibraryLoaded)
{
try
{
if (!loadLibrary("lpsolve55"))
{
System.err.println("Library cannot be loaded");
throw (new Exception("lpsolve55 could not be loaded"));
}
if (extractLibrary("lpsolve55j") == null)
{
System.err.println("Library cannot be extracted");
throw (new Exception("lpsolve55 could not be loaded"));
}
}
catch (Exception e)
{
e.printStackTrace();
optimizationLibraryLoaded = false;
JOptionPane.showMessageDialog(Icy.getMainInterface().getMainFrame(),
"The lpsolve55 optimization library has failed to load.\n" +
"The plugin is now using a slower library.\n" +
"Please, consider switching to another platform (eg. windows) for improved performance,\nor contact the developper.",
"Library loading error",
JOptionPane.ERROR_MESSAGE);
return;
}
optimizationLibraryLoaded = true;
}
}
public File extractLibrary(String libName)
{
try
{
// get mapped library name
String mappedlibName = System.mapLibraryName(libName);
// get base resource path for native library
final String basePath = getResourceLibraryPath() + ResourceUtil.separator;
// search for library in resource
URL libUrl = getResource(basePath + mappedlibName);
// not found ?
if (libUrl == null)
{
// jnilib extension may not work, try with "dylib" extension instead
if (mappedlibName.endsWith(".jnilib"))
{
mappedlibName = mappedlibName.substring(0, mappedlibName.length() - 7) + ".dylib";
libUrl = getResource(basePath + mappedlibName);
}
}
// resource not found --> error
if (libUrl == null)
throw new IOException("Couldn't find resource " + basePath + mappedlibName);
// extract resource
return extractResource(SystemUtil.getTempLibraryDirectory() + FileUtil.separator + mappedlibName, libUrl);
}
catch (IOException e)
{
System.err.println("Error while loading packed library " + libName + ": " + e);
}
return null;
}
@Override
public void run() {
mhTrackerManager = new MHTrackerManager();
mainPanel = new JPanel();
mainFrame = new IcyFrame("Spot Tracking", true, true, true, true);
mainFrame.getContentPane().add(mainPanel);
// set up the tracking plugin panels
mainPanel.setLayout(new BorderLayout());
int iconSize = 100;
ImageIcon detectionIcon = ResourceUtil.getImageIcon( ImageUtil.load( getResourceAsStream( "plugins/nchenouard/particletracking/simplifiedMHT/detectionIcon.png" ) ), iconSize );
ImageIcon mhtIcon = ResourceUtil.getImageIcon( ImageUtil.load( getResourceAsStream( "plugins/nchenouard/particletracking/simplifiedMHT/MHTIcon.png" ) ), iconSize );
ImageIcon outputIcon = ResourceUtil.getImageIcon( ImageUtil.load( getResourceAsStream( "plugins/nchenouard/particletracking/simplifiedMHT/trackPoolIcon.png" ) ), iconSize );
ImageIcon workingIcon = ResourceUtil.getImageIcon( ImageUtil.load( getResourceAsStream( "plugins/nchenouard/particletracking/simplifiedMHT/workingIcon.png" ) ), iconSize );
simpleGUIPanel = new SimplifiedMHTPanel(detectionIcon, mhtIcon, outputIcon, workingIcon);
mainPanel.add(simpleGUIPanel, BorderLayout.CENTER);
simpleGUIPanel.loadParametersButton.addActionListener(this);
simpleGUIPanel.runTrackingButton.addActionListener(mhTrackerManager);
simpleGUIPanel.detectionChooser.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (simpleGUIPanel.detectionChooser.getSelectedDetectionResult() != null && mhtParameterSet != null)
mhtParameterSet.detectionResults = simpleGUIPanel.detectionChooser.getSelectedDetectionResult();
}
});
simpleGUIPanel.trackGroupNameTF.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
mhtParameterSet.trackGroupName = simpleGUIPanel.trackGroupNameTF.getText();
}
@Override
public void keyPressed(KeyEvent e) {
}
});
simpleGUIPanel.estimateParametersButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0)
{
estimateTrackingParameters();
}
});
advancedGUIPanel = new TrackingMainPanel();
advancedGUIPanel.startTrackingButton.addActionListener(mhTrackerManager);
advancedGUIPanel.configFilePanel.exportConfigurationFileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
mhtParameterSet = advancedGUIPanel.getParameterSet();
exportConfiguration();
}
});
advancedGUIPanel.configFilePanel.loadConfigurationFileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
loadMHTParameters();
}
});
// advancedGUIPanel.configFilePanel.runMultipleConfigurationFileButton.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e)
// {
// }
// });
refreshMainPanel(currentGUISate);
// legacy tracker
legacyGUIPanel = new PanelTracking();
legacyTrackerManager = new LegacyTrackerManager(legacyGUIPanel);
// set up the menu bar
menuBar = new JMenuBar();
mainFrame.setJMenuBar(menuBar);
fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem saveConfigurationItem = new JMenuItem("Save parameters");
fileMenu.add(saveConfigurationItem);
saveConfigurationItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exportConfiguration();
}
});
JMenuItem loadConfigurationItem = new JMenuItem("Load parameters");
fileMenu.add(loadConfigurationItem);
loadConfigurationItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loadMHTParameters();
}
});
JMenuItem configurationItem = new JMenuItem("Plugin configuration");
fileMenu.add(configurationItem);
configurationItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String title = "Plugin configuration";
boolean resizable = false;
boolean closable = true;
boolean maximizable = false;
boolean iconifiable = true;
final IcyInternalFrame configFrame = new IcyInternalFrame(title, resizable, closable, maximizable, iconifiable);
JPanel configFramePanel = new JPanel();
configFrame.setContentPane(configFramePanel);
configFramePanel.setLayout(new GridLayout(3, 1));
final JCheckBox multithreadingBox = new JCheckBox("Multithreaded computation.");
multithreadingBox.setSelected(useMultithreading);
final JCheckBox lpSolveBox = new JCheckBox("Optimization library in C (faster).");
lpSolveBox.setSelected(useLPSolve);
final JButton closeButton = new JButton("Apply");
configFramePanel.add(multithreadingBox);
configFramePanel.add(lpSolveBox);
configFramePanel.add(closeButton);
closeButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
useMultithreading = multithreadingBox.isSelected();
useLPSolve = lpSolveBox.isSelected();
configFrame.close(false);
}});
configFrame.pack();
configFrame.setVisible(true);
Icy.getMainInterface().addToDesktopPane(configFrame);
}
});
guiMenu = new JMenu("Interface");
menuBar.add(guiMenu);
JMenuItem simplifiedGUIItem = new JMenuItem(SIMPLEGUI);
guiMenu.add(simplifiedGUIItem);
simplifiedGUIItem.addActionListener(this);
JMenuItem advancedGUIItem = new JMenuItem(ADVANCEDGUI);
guiMenu.add(advancedGUIItem);
advancedGUIItem.addActionListener(this);
JMenuItem legacyInterfaceItem = new JMenuItem(LEGACYGUI);
guiMenu.add(legacyInterfaceItem);
legacyInterfaceItem.addActionListener(this);
infoMenu = new JMenu("Info");
menuBar.add(infoMenu);
JMenuItem manualItem = new JMenuItem("Manual");
infoMenu.add(manualItem);
manualItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
IcyFrame manualFrame = new IcyFrame("Spot Tracking Plugin Manual");
manualFrame.setResizable(true);
manualFrame.setClosable(true);
JEditorPane descriptionPane = new JEditorPane();
descriptionPane.setEditable(false);
descriptionPane.setContentType("text/html");
descriptionPane.setText( "<html><p>For a full description, please read the <a href=http://icy.bioimageanalysis.org/plugin/Spot_Tracking#documentation>online manual</a> on ICY website:<br> http://icy.bioimageanalysis.org/plugin/Spot_Tracking#documentation"
+ "<p><b>Quick description:</b>"
+"<br><html>The usual way of using the software is to follow indications given by the interface from top to bottom.<br> Tool tip text on each graphical element gives useful information about the use." +
"<br>In general, the following sequence of operations will be performed: <ol><li>Select a set of locations in space and over time (detections) for the spots to track.<br> This can be done with the Spot Detector plugin. (Section 1)</li> <li>Estimate parameters for the tracking steps or load a set of existing parameters from a local file. (Section 2)</li><li>Specify a unique name for the tracking results. (Section 3)</li><li>Run the track construction process. (Section 4)</li><li>Analyze and save tracking results with the Track Manager plugin.</li><li>Save the tracking parameters via the File menu of the plugin for future re-use.</li></ol>"
+ "<br>Exhaustive control of the tracking parameters is accessible by switching to the advanced interface.<br> (Menu Interface/Advanced Interface).</html>");
manualFrame.setContentPane(descriptionPane);
manualFrame.pack();
addIcyFrame(manualFrame);
manualFrame.setVisible(true);
}
});
JMenuItem aboutItem = new JMenuItem("About");
infoMenu.add(aboutItem);
aboutItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
IcyFrame aboutFrame = new IcyFrame("About Spot Tracking Plugin");
aboutFrame.setResizable(true);
aboutFrame.setClosable(true);
JEditorPane descriptionPane = new JEditorPane();
descriptionPane.setEditable(false);
descriptionPane.setContentType("text/html");
descriptionPane.setText("<html><p>The Spot Tracking Plugin is a essentially an implementation of the methods described in the article:" +
"<br>Nicolas Chenouard, Isabelle Bloch, Jean-Christophe Olivo-Marin,<br> <b>Multiple Hypothesis Tracking for Cluttered Biological Image Sequences</b>, <br>IEEE Transactions on Pattern Analysis and Machine Intelligence, vol. 35, no. 11, pp. 2736-3750, Nov., 2013" +
"<br>Pubmed link: http://www.ncbi.nlm.nih.gov/pubmed/23689865" +
"<br><p>By using the software for data analysis you agree to properly cite and reference this work<br> in any communication regarding results obtained by using it." +
"<br><p>Author: Nicolas Chenouard. Institut Pasteur, Paris, France." +
"<br>Version 3.1, 2013-11-13.</html>");
aboutFrame.setContentPane(descriptionPane);
aboutFrame.pack();
addIcyFrame(aboutFrame);
aboutFrame.setVisible(true);
}
});
mainFrame.pack();
mainFrame.addToDesktopPane();
mainFrame.center();
mainFrame.setVisible(true);
mainFrame.requestFocus();
defaultParameterSet = true;
simpleGUIPanel.setUsingDefaultParameters(defaultParameterSet);
}
private void refreshMHTParameterSetFromGUI()
{
if (advancedGUIPanel != null)
{
MHTparameterSet parameters = advancedGUIPanel.getParameterSet();
if (parameters != null)
{
mhtParameterSet = parameters;
simpleGUIPanel.setParameter(mhtParameterSet);
defaultParameterSet = false;
simpleGUIPanel.setUsingDefaultParameters(defaultParameterSet);
}
}
}
private void setMHTParameterSetToGUI()
{
if (advancedGUIPanel != null && mhtParameterSet != null)
{
advancedGUIPanel.setParameterset(mhtParameterSet);
simpleGUIPanel.setParameter(mhtParameterSet);
defaultParameterSet = false;
}
}
private void refreshMainPanel(final GUIStateEnum guiState)
{
mainPanel.removeAll();
switch(guiState)
{
case ADVANCED:
setMHTParameterSetToGUI(); // set the current parameters to the advanced GUI
mainPanel.add(advancedGUIPanel, BorderLayout.CENTER);
break;
case LEGACY:
mainPanel.add(legacyGUIPanel, BorderLayout.CENTER);
break;
case SIMPLE:
refreshMHTParameterSetFromGUI(); // load parameters from advanced GUI
mainPanel.add(simpleGUIPanel, BorderLayout.CENTER);
break;
default:
break;
}
mainFrame.pack();
mainFrame.updateUI();
currentGUISate = guiState;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JMenuItem)
{
if (((JMenuItem)e.getSource()).getText().equals(SIMPLEGUI))
refreshMainPanel(GUIStateEnum.SIMPLE); // switch to simple GUI
if (((JMenuItem)e.getSource()).getText().equals(ADVANCEDGUI))
refreshMainPanel(GUIStateEnum.ADVANCED); // switch to advanced GUI
if (((JMenuItem)e.getSource()).getText().equals(LEGACYGUI))
refreshMainPanel(GUIStateEnum.LEGACY); // switch to legacy GUI
}
if (e.getSource() == simpleGUIPanel.loadParametersButton)
loadMHTParameters();
}
public void loadMHTParameters()
{
MHTparameterSet parameters = loadConfiguration();
if (parameters != null)
{
mhtParameterSet = parameters;
advancedGUIPanel.setParameterset(mhtParameterSet);
}
}
private void exportConfigurationToFile(File file) {
Document document = XMLUtil.createDocument( true );
Element configurationElement = document.createElement(MHT_PLUGIN_CONFIGURATION);
XMLUtil.getRootElement( document ).appendChild(configurationElement);
if (mhtParameterSet != null)
{
mhtParameterSet.saveToXML(configurationElement);
}
XMLUtil.saveDocument( document , file );
}
public void exportConfiguration() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return "XML file filter";
}
@Override
public boolean accept(File file) {
if (file.isDirectory())
return true;
return file.getName().endsWith(".xml");
}
});
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setName("Output xml file selection");
int returnVal = fileChooser.showOpenDialog(mainPanel);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if(!file.getName().endsWith(".xml"))
file = new File(file.getAbsolutePath() + ".xml");
exportConfigurationToFile(file);
}
}
public MHTparameterSet loadConfiguration() {
MHTparameterSet parameterSet = null;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return "XML file filter";
}
@Override
public boolean accept(File file) {
if (file.isDirectory())
return true;
return file.getName().endsWith(".xml");
}
});
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setName("Input xml file selection");
int returnVal = fileChooser.showOpenDialog(mainPanel);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
parameterSet = loadConfigurationFromFile(file);
}
return parameterSet;
}
private MHTparameterSet loadConfigurationFromFile(File file)
{
Document document = XMLUtil.loadDocument( file );
Element root = XMLUtil.getRootElement( document );
if ( root == null )
{
throw new IllegalArgumentException( "can't find: <root> tag." );
}
Element configurationElement = XMLUtil.getElements( root , MHT_PLUGIN_CONFIGURATION ).get( 0 );
if ( configurationElement == null )
{
throw new IllegalArgumentException( "can't find: <root><" + MHT_PLUGIN_CONFIGURATION + "> tag." ) ;
}
MHTparameterSet parameterSet = MHTparameterSet.loadFromXML(configurationElement);
return parameterSet;
}
public static void sendTracksToPool(final TrackGroup trackGroup,
final Sequence sequence) {
SwingUtilities.invokeLater(new Runnable()
{
public void run() {
// Add the given trackGroup
SwimmingObject result = new SwimmingObject(trackGroup);
Icy.getMainInterface().getSwimmingPool().add(result);
TrackManager manager = new TrackManager();
if (sequence != null)
manager.setDisplaySequence(sequence );
new AnnounceFrame("Tracking results exported to Track manager plugin");
}
});
}
double squaredDistance(Spot s1, Spot s2)
{
return (s1.mass_center.x - s2.mass_center.x)*(s1.mass_center.x - s2.mass_center.x) + (s1.mass_center.y - s2.mass_center.y)*(s1.mass_center.y - s2.mass_center.y) + (s1.mass_center.z - s2.mass_center.z)*(s1.mass_center.z - s2.mass_center.z);
}
void estimateTrackingParameters()
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ParameterEstimationFrame estimationFrame = new ParameterEstimationFrame();
addIcyFrame(estimationFrame);
estimationFrame.setVisible(true);
}
});
}
class MHTrackerManager implements ActionListener
{
boolean isRunning = false;
boolean stopTracking = false;
boolean stopped = false;
HMMMHTracker runningTracker;
private Lock stopLock = new ReentrantLock();
private Condition stopCondition = stopLock.newCondition();
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == advancedGUIPanel.startTrackingButton || e.getSource() == simpleGUIPanel.runTrackingButton)
{
if (e.getSource() == advancedGUIPanel.startTrackingButton)
mhtParameterSet = advancedGUIPanel.getParameterSet();
startSoppableMHTracking();
}
}
public void startSoppableMHTracking()
{
if (isRunning)
{
stopLock.lock();
if (runningTracker != null)
runningTracker.stopComputing();
stopTracking = true;
try {
while (isRunning)
stopCondition.await();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
finally
{
stopLock.unlock();
}
stopTracking = false;
}
else
{
enableTracking(false);
startMHTracking();
}
}
private void enableTracking(final boolean isEnableTracking)
{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
simpleGUIPanel.changeTrackingState(!isEnableTracking);
advancedGUIPanel.changeTrackingState(!isEnableTracking);
}});
}
public HMMMHTracker buildMHTracker(DetectionResult dr)
{
HMMMHTracker tracker = null;
int dim = 2;
double volume = 256*256;
if (dr.getSequence() != null)
{
if (dr.getSequence().getSizeZ() > 1)
dim = 3;
volume = dr.getSequence().getSizeX()*dr.getSequence().getSizeY()*dr.getSequence().getSizeZ();
}
else
{
// check detections to see if they all have the same z coordinate
// double minX = Double.MAX_VALUE;
// double minY = Double.MAX_VALUE;
// double minZ = Double.MAX_VALUE;
double minX = 0; // assume the sequence coordinates start at 0
double minY = 0;
double minZ = 0;
double maxX = 0;
double maxY = 0;
double maxZ = 0;
for (int t = dr.getFirstFrameTime(); t <= dr.getLastFrameTime(); t++)
{
for (Spot s:dr.getDetectionsAtT(t))
{
minX = Math.min(minX, s.mass_center.x);
minY = Math.min(minY, s.mass_center.y);
minZ = Math.min(minZ, s.mass_center.z);
maxX = Math.max(maxX, s.mass_center.x);
maxY = Math.max(maxY, s.mass_center.y);
maxZ = Math.max(maxZ, s.mass_center.z);
}
}
if (maxZ > minZ)
{
dim = 3;
volume = (maxX - minX +1)*(maxY - minY +1)*(maxZ - minZ +1);
}
else
{
dim = 2;
volume = (maxX - minX +1)*(maxY - minY +1);
}
}
try {
tracker = advancedGUIPanel.buildTracker(dim, volume, useLPSolve && optimizationLibraryLoaded, useMultithreading);
} catch (Exception e) {
e.printStackTrace();
}
return tracker;
}
private void startMHTracking()
{
final DetectionResult dr = mhtParameterSet.detectionResults;// advancedGUIPanel.getSelectedDetectionResult();
if (dr == null)
{
enableTracking(true);
new AnnounceFrame("Tracking requires to specify a set of detections. You can use the Spot Detector plugin to create some.", 10);
return;
}
if (dr.getNumberOfDetection() < 1)
{
enableTracking(true);
new AnnounceFrame("A non empty set of detections needs to be used");
return;
}
final HMMMHTracker mhtracker = buildMHTracker(dr);
computationThread = new Thread(){
@Override
public void run()
{
trackingAnnounce.setLength(dr.getLastFrameTime());
for (int t = dr.getFirstFrameTime(); t <= dr.getLastFrameTime(); t++)
{
if (!stopTracking)
{
trackingAnnounce.setMessage("Track extraction at frame " + t);
trackingAnnounce.setPosition(t);
mhtracker.track(t, dr.getDetectionsAtT(t));
}
else
{
stopped = true;
break;
}
}
trackingAnnounce.close();
ArrayList<TrackSegment> tracks = mhtracker.getCompleteTracks();
TrackGroup tg = new TrackGroup(dr.getSequence());
tg.setDescription(mhtParameterSet.trackGroupName);
for (TrackSegment ts:tracks)
tg.addTrackSegment(ts);
sendTracksToPool(tg, tg.getSequence());
isRunning = false;
enableTracking(true);
if (stopped)
{
stopLock.lock();
isRunning = false;
stopCondition.signalAll();
stopLock.unlock();
}
computationThread = null;
runningTracker = null;
}
};
trackingAnnounce = new ProgressFrame("Tracking progress started");
computationThread.start();
}
}
class LegacyTrackerManager implements ActionListener
{
private StoppableTracker runningTracker = null;
private boolean stopTracking = false;
private boolean isRunning = false;
private Thread runningThread = null;
private Lock stopLock = new ReentrantLock();
private Condition stopCondition = stopLock.newCondition();
PanelTracking panelTracking;
public LegacyTrackerManager(PanelTracking legacyGUIPanel)
{
legacyGUIPanel.trackingStartButton.addActionListener(this);
this.panelTracking = legacyGUIPanel;
}
private void enableTracking(final boolean isEnableTracking)
{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
panelTracking.changeTrackingState(!isEnableTracking);
}});
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == panelTracking.trackingStartButton)
{
if (isRunning)
{
stopLock.lock();
if (runningTracker != null)
runningTracker.stopComputing();
stopTracking = true;
try {
while (isRunning)
stopCondition.await();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
finally
{
stopLock.unlock();
}
stopTracking = false;
}
else
{
DetectionResult detections = panelTracking.getDetectionResults();
if (detections != null) {
enableTracking(false);
trackingRun(detections, detections.getSequence());
} else
new AnnounceFrame("Please first select a detection set.");
}
}
}
private synchronized void trackingRun(final DetectionResult detections, final Sequence sequence) {
if (detections != null) {
isRunning = true;
Thread trackThread = new Thread() {
public void run() {
int dim = 2;
if (sequence.getSizeZ() > 1)
dim = 3;
Tracker tracker = panelTracking.buildTracker(dim);
if (tracker instanceof StoppableTracker)
runningTracker = (StoppableTracker)tracker;
int firstT = detections.getFirstFrameTime();
int lastT = detections.getLastFrameTime();
String message = "Tracking at frame ";
ProgressFrame announceFrame = new ProgressFrame("");
announceFrame.setLength(lastT - firstT + 1);
boolean stopped = false;
for (int t = firstT; t <= lastT; t++) {
if (!stopTracking)
{
announceFrame.setPosition(t);
announceFrame.setMessage(message+" "+t);
tracker.track(t, detections.getDetectionsAtT(t));
}
else
{
stopped = true;
break;
}
}
announceFrame.close();
if (tracker instanceof InstantaneousTracker) {
TrackGroup trackGroup = new TrackGroup(sequence);
trackGroup.setDescription(panelTracking.getTrackGroupName());
for (Track track : ((InstantaneousTracker) tracker)
.getTracks()) {
TrackSegment ts = convertTrack2TrackSegment(track,
null);
if (ts != null && ts.getDetectionList().size() > 1)
trackGroup.addTrackSegment(ts);
}
sendTracksToPool(trackGroup, sequence);
new AnnounceFrame("Tracks exported as "+trackGroup.getDescription()+" in TrackEditor");
}
isRunning = false;
enableTracking(true);
if (stopped)
{
stopLock.lock();
isRunning = false;
stopCondition.signalAll();
stopLock.unlock();
}
runningThread = null;
runningTracker = null;
}
};
runningThread = trackThread;
trackThread.start();
}
}
public TrackSegment convertTrack2TrackSegment(Track track, Object detectionEditor) {
int firstIndex = track.getFirstIndex();
int lastIndex = track.getLastIndex();
for (int i = lastIndex; i > firstIndex; i--)
{
if (track.isPredictionAtFrame(i))
lastIndex = i-1;
else
break;
}
if (firstIndex <= lastIndex)
{
ArrayList<Detection> detections = new ArrayList<Detection>(lastIndex - firstIndex + 1);
for (int i = firstIndex; i <= lastIndex; i++)
{
Spot s = track.getSpotAtFrame(i);
Detection detect = new DetectionSpotTrack(s, i);
if (track.isPredictionAtFrame(i))
detect.setDetectionType(Detection.DETECTIONTYPE_VIRTUAL_DETECTION);
detections.add(detect);
}
return new TrackSegment(detections);
}
else
return null;
}
}
class ParameterEstimationFrame extends IcyFrame
{
public ParameterEstimationFrame()
{
super("Parameters estimation", true, true, true, true);
JPanel mainPanel = new JPanel();
this.setContentPane(mainPanel);
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridy = 0;
mainPanel.add(new JLabel("Target motion"), gc);
gc.gridy++;
final JRadioButton diffusiveButton = new JRadioButton("is mainly diffusive (default).");
mainPanel.add(diffusiveButton, gc);
gc.gridy++;
final JRadioButton directedButton = new JRadioButton("is mainly directed.");
mainPanel.add(directedButton, gc);
gc.gridy++;
final JRadioButton directedDiffusiveButton = new JRadioButton("is both diffusive and directed.");
mainPanel.add(directedDiffusiveButton, gc);
gc.gridy++;
ButtonGroup motionGroup = new ButtonGroup();
motionGroup.add(diffusiveButton);
motionGroup.add(directedButton);
motionGroup.add(directedDiffusiveButton);
diffusiveButton.setSelected(true);
mainPanel.add(new JLabel("Parameters for target motion"), gc);
gc.gridy++;
final JRadioButton noUpdateMotionButton = new JRadioButton("are kept to their initial values (default).");
mainPanel.add(noUpdateMotionButton, gc);
gc.gridy++;
final JRadioButton updateMotionButton = new JRadioButton("are re-estimated online.");
mainPanel.add(updateMotionButton, gc);
gc.gridy++;
ButtonGroup updateGroup = new ButtonGroup();
updateGroup.add(noUpdateMotionButton);
updateGroup.add(updateMotionButton);
noUpdateMotionButton.setSelected(true);
JButton estimateParametersButton = new JButton("Run parameter estimation procedure");
mainPanel.add(estimateParametersButton, gc);
estimateParametersButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0)
{
estimateParameters(directedButton.isSelected(), !directedDiffusiveButton.isSelected(), updateMotionButton.isSelected());
close();
}
});
this.pack();
}
public void estimateParameters(final boolean isDirectedMotion, final boolean isSingleMotion, final boolean isUpdateMotion)
{
computationThread = new Thread()
{
@Override
public void run()
{
mhtParameterSet.isDirectedMotion = isDirectedMotion;
mhtParameterSet.isSingleMotion = isSingleMotion;
mhtParameterSet.isUpdateMotion = isUpdateMotion;
// first get an estimation of parameters based on analysis of distance between detections
if (mhtParameterSet.detectionResults == null)
return;
// fill the detection distance list between subsequent detections
int maxNumDetections = 10000;
int numDetections = 0;
int numT = 0;
for (int t = mhtParameterSet.detectionResults.getFirstFrameTime(); t < mhtParameterSet.detectionResults.getLastFrameTime(); t++)
{
numDetections += mhtParameterSet.detectionResults.getDetectionsAtT(t).size();
numT++;
if (numDetections > maxNumDetections)
break;
}
int t = mhtParameterSet.detectionResults.getFirstFrameTime();
double[] distTabXY = new double[numDetections];
double[] distTabZ = new double[numDetections];
int cntDist = 0;
while (t < mhtParameterSet.detectionResults.getFirstFrameTime() + numT)
{
Vector<Spot> dList1 = mhtParameterSet.detectionResults.getDetectionsAtT(t);
Vector<Spot> dList2 = mhtParameterSet.detectionResults.getDetectionsAtT(t + 1);
if (!dList1.isEmpty() && !dList2.isEmpty())
{
for (Spot s1:dList1)
{
double minDist = Double.MAX_VALUE;
Spot minSpot = null;
for (Spot s2:dList2)
{
double d = squaredDistance(s1, s2);
if (d < minDist)
{
minDist = d;
minSpot = s2;
}
}
if (minDist < Double.MAX_VALUE)
{
///distTab[cntDist] = minDist;
distTabXY[cntDist] = (s1.mass_center.x - minSpot.mass_center.x)*(s1.mass_center.x - minSpot.mass_center.x) + (s1.mass_center.y - minSpot.mass_center.y)*(s1.mass_center.y - minSpot.mass_center.y);
distTabZ[cntDist] += (s1.mass_center.z - minSpot.mass_center.z)*(s1.mass_center.z - minSpot.mass_center.z);
cntDist++;
}
}
}
t++;
}
// get a first estimation of the diffusion as the underestimated median of the squared distances
Arrays.sort(distTabXY);
Arrays.sort(distTabZ);
double varDiffXY = distTabXY[(int)(distTabXY.length*0.45)];
double varDiffZ = distTabZ[(int)(distTabXY.length*0.45)];
// then run the legacy tracker to get a refined estimation
int maxConsecutivePred = 1;
int gateFactor = 3;
boolean gateLikelihood = true;
Predictor predictor = null;
if (isSingleMotion)
{
if (varDiffZ > 0)
{
if (isDirectedMotion)
predictor = new KF3dDirected();
else
predictor = new KF3dRandomWalk();
predictor.setTrackingCovariances(new double[]{varDiffXY, varDiffXY, varDiffZ});
}
else
{
if (isDirectedMotion)
predictor = new KF2dDirected();
else
predictor = new KF2dRandomWalk();
predictor.setTrackingCovariances(new double[]{varDiffXY, varDiffXY});
}
}
else
{
if (varDiffZ > 0)
{
ArrayList<Predictor3D> predictors = new ArrayList<Predictor3D>();
predictors.add(new KF3dRandomWalk());
predictors.add(new KF3dDirected());
predictors.get(0).setTrackingCovariances(new double[]{varDiffXY, varDiffXY, varDiffZ});
predictors.get(1).setTrackingCovariances(new double[]{varDiffXY, varDiffXY, varDiffZ});
if (mhtParameterSet.useMostLikelyModel)
predictor = new IMM3D(IMM3D.LikelihoodTypes.IMM_MAX_LIKELIHOOD, new Double(mhtParameterSet.immInertia), predictors);
else
predictor = new IMM3D(IMM3D.LikelihoodTypes.IMM_WEIGHTED_LIKELIHOOD, mhtParameterSet.immInertia, predictors);
}
else
{
ArrayList<Predictor2D> predictors = new ArrayList<Predictor2D>();
predictors.add(new KF2dRandomWalk());
predictors.add(new KF2dDirected());
predictors.get(0).setTrackingCovariances(new double[]{varDiffXY, varDiffXY});
predictors.get(1).setTrackingCovariances(new double[]{varDiffXY, varDiffXY});
if (mhtParameterSet.useMostLikelyModel)
predictor = new IMM2D(IMM2D.LikelihoodTypes.IMM_MAX_LIKELIHOOD, new Double(mhtParameterSet.immInertia), predictors);
else
predictor = new IMM2D(IMM2D.LikelihoodTypes.IMM_WEIGHTED_LIKELIHOOD, mhtParameterSet.immInertia, predictors);
}
}
SolveMLAssociation assignementSolver = new SolveMLAssociation(true);
InstantaneousTracker tracker = new InstantaneousTracker(assignementSolver, predictor, gateLikelihood, gateFactor, maxConsecutivePred);
for (int tt = mhtParameterSet.detectionResults.getFirstFrameTime(); tt < mhtParameterSet.detectionResults.getLastFrameTime(); tt++)
tracker.track(tt, mhtParameterSet.detectionResults.getDetectionsAtT(tt));
ArrayList<Track> tracks = tracker.getTracks();
// consider tracks with length < 3 to be wrong
ArrayList<Track> trueTracks = new ArrayList<Track>();
ArrayList<Track> falseTracks = new ArrayList<Track>();
for (Track tr:tracks)
{
if (tr.getLastIndex() - tr.getFirstIndex() > 2)
trueTracks.add(tr);
else
falseTracks.add(tr);
}
if (trueTracks.isEmpty())
return;
// compute track length
int sumTrackLength = 0;
for (Track tr:trueTracks)
sumTrackLength += (tr.getLastIndex() - tr.getFirstIndex() + 1);
mhtParameterSet.meanTrackLength = sumTrackLength/trueTracks.size();
// compute number of tracks at first frame, and number of new tracks in other frames
int numTrackFirstFrame = 0;
int numNewTracks = 0;
for (Track tr:trueTracks)
{
if (tr.getFirstIndex() == 0)
numTrackFirstFrame ++;
else
numNewTracks ++;
}
mhtParameterSet.numberInitialObjects = numTrackFirstFrame;
mhtParameterSet.numberNewObjects = (numNewTracks/(1d + mhtParameterSet.detectionResults.getLastFrameTime() - mhtParameterSet.detectionResults.getFirstFrameTime()));
// compute number of false detections per frame
int numFalseDetections = 0;
for (Track tr:falseTracks)
{
for (int tt = tr.getFirstIndex(); tt < tr.getLastIndex() + 1; tt++)
{
if (tr.isPredictionAtFrame(tt))
numFalseDetections++;
}
}
mhtParameterSet.numberOfFalseDetections = (int) (Math.round(numFalseDetections/(1d + mhtParameterSet.detectionResults.getLastFrameTime() - mhtParameterSet.detectionResults.getFirstFrameTime())));
// re estimate diffusion //TODO: do this with MSDs to account for directed periods
// if (isSingleMotion && !isDirectedMotion)
{
// just use squared displacement
double sumDistSq = 0;
double sumDistSqZ = 0;
int numDist = 0;
for (Track tr:trueTracks)
{
for (int f = tr.getFirstIndex(); f < tr.getLastIndex(); f++)
{
if(!tr.isPredictionAtFrame(f) && !tr.isPredictionAtFrame(f +1))
{
Spot s1 = tr.getSpotAtFrame(f);
Spot s2 = tr.getSpotAtFrame(f + 1);
sumDistSq += (s1.mass_center.x - s2.mass_center.x)*(s1.mass_center.x - s2.mass_center.x) + (s1.mass_center.y - s2.mass_center.y)*(s1.mass_center.y - s2.mass_center.y);
sumDistSqZ += (s1.mass_center.z - s2.mass_center.z)*(s1.mass_center.z - s2.mass_center.z);
numDist++;
if (numDist > maxNumDetections)
break;
}
}
if (numDist > maxNumDetections)
break;
}
mhtParameterSet.displacementXY = Math.sqrt(sumDistSq/((double) numDist));
mhtParameterSet.displacementZ = Math.sqrt(sumDistSqZ/((double) numDist));
}
// estimate detection rate
int numTrueDetections = 0;
int numVirtualDetections = 0;
for (Track tr:trueTracks)
{
for (int f = tr.getFirstIndex(); f < tr.getLastIndex(); f++)
if (tr.isPredictionAtFrame(f))
numVirtualDetections++;
else
numTrueDetections++;
}
mhtParameterSet.detectionRate = numTrueDetections/((double)numTrueDetections + numVirtualDetections);
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
simpleGUIPanel.changeTrackingState(false);
advancedGUIPanel.changeTrackingState(false);
if (trackingAnnounce != null)
trackingAnnounce.close();
}});
// computationThread = null;
}
};
trackingAnnounce = new ProgressFrame("Parameter estimation in progress");
simpleGUIPanel.changeTrackingState(true);
advancedGUIPanel.changeTrackingState(true);
computationThread.start();
}
}
} |
Partager