Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

FeatExtractorPanel.java

00001 /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 package com.meapsoft.gui;
00024 
00025 import java.awt.*;
00026 import java.awt.event.*;
00027 import java.io.*;
00028 import java.util.*;
00029 import java.text.*;
00030 import javax.swing.*; 
00031 import javax.swing.border.*; 
00032 
00033 import util.RTSI;
00034 import com.meapsoft.*;
00035 import com.meapsoft.featextractors.*;
00036 
00037 
00044 public class FeatExtractorPanel extends MEAPsoftGUIPanel
00045 {
00046         //feature extractor GUI
00047         JCheckBox enableBox;
00048         Vector featureCheckBoxes;
00049         Vector featureDescriptions;
00050     Vector featureWeightFields;
00051         JLabel inputSegmentsFileLabel;
00052         JLabel outputFeaturesFileLabel;
00053         JTextField inputSegmentsFileField;
00054         JTextField outputFeaturesFileField;
00055         JButton displayFeaturesButton;
00056     JCheckBox clearNonMetaFeatures;
00057 
00061     public FeatExtractorPanel(MEAPsoftGUI msg)
00062         {
00063         super(msg);
00064         BuildFeatureExtractorsGUI();
00065 
00066         title = "Feature Extractor";
00067         helpURL += "#" + title;
00068     }
00069 
00070         private void BuildFeatureExtractorsGUI()
00071         {
00072                 featureDescriptions = new Vector();
00073                 
00074                 Color c = new Color((int)(Math.random() * 127 + 127),
00075                                         (int)(Math.random() * 127 + 127),
00076                                         (int)(Math.random() * 127 + 127));
00077         color = c;
00078 
00079                 setBackground(c);
00080                 BoxLayout fbl = new BoxLayout(this, BoxLayout.Y_AXIS);
00081                 setLayout(fbl);
00082                 
00083                 Vector classNames = SniffFeatureExtractors();
00084 
00085                 featureCheckBoxes = new Vector(classNames.size());
00086         featureWeightFields = new Vector(classNames.size());
00087                 
00088                 JPanel enablePanel = new JPanel();
00089                 enablePanel.setBackground(c);
00090                 
00091                 enableBox = new JCheckBox("ENABLE FEATURE EXTRACTOR");
00092                 enableBox.setBackground(c);
00093                 enableBox.setSelected(true);
00094                 enablePanel.add(enableBox);
00095                 
00096                 enablePanel.add(helpButton);
00097                 
00098                 add(enablePanel);
00099                 
00100                 JPanel controlsPanel = new JPanel();
00101                 controlsPanel.setBackground(c);
00102                 BoxLayout fecp = new BoxLayout(controlsPanel, BoxLayout.Y_AXIS);
00103                 controlsPanel.setLayout(fecp);
00104                 
00105                 JPanel extractorInputFNPanel = new JPanel();
00106                 extractorInputFNPanel.setBackground(c);
00107                 JLabel fEINL = new JLabel("input segments file: ");
00108                 extractorInputFNPanel.add(fEINL);
00109                 inputSegmentsFileLabel = new JLabel(" " + dataBaseName + ".seg ");
00110                 inputSegmentsFileLabel.setOpaque(true);
00111                 inputSegmentsFileLabel.setBackground(c.darker());
00112                 extractorInputFNPanel.add(inputSegmentsFileLabel);
00113                 controlsPanel.add(extractorInputFNPanel);
00114 
00115                 int numFeatures = classNames.size();
00116 
00117                 JPanel selectFeaturesPanel = new JPanel();
00118                 selectFeaturesPanel.setBackground(c);
00119                 TitledBorder title = BorderFactory.createTitledBorder(
00120                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), 
00121                         "Feature Extractors");
00122                 title.setTitleJustification(TitledBorder.CENTER);
00123                 selectFeaturesPanel.setBorder(title);
00124                 controlsPanel.add(selectFeaturesPanel);
00125 
00126 
00127                 JPanel selectMetaFeaturesPanel = new JPanel();
00128                 selectMetaFeaturesPanel.setBackground(c);
00129                 TitledBorder metaTitle = BorderFactory.createTitledBorder(
00130                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), 
00131                         "Meta Feature Extractors");
00132         selectMetaFeaturesPanel.setBorder(metaTitle);
00133                 metaTitle.setTitleJustification(TitledBorder.CENTER);
00134         controlsPanel.add(selectMetaFeaturesPanel);
00135                 
00136         clearNonMetaFeatures = new JCheckBox("clear non-meta features");
00137         clearNonMetaFeatures.setBackground(c); 
00138         clearNonMetaFeatures.setSelected(true);
00139         clearNonMetaFeatures.setAlignmentX(Component.CENTER_ALIGNMENT);
00140         controlsPanel.add(clearNonMetaFeatures);
00141 
00142                 int fENum = 0;
00143                 int mFENum = 0;
00144                 JPanel fEPanel = null;
00145                 JPanel mFEPanel = null;
00146                 for (int i = 0; i < classNames.size(); i++)
00147                 {
00148                         String name = (String)classNames.elementAt(i);
00149                         
00150                         FeatureExtractor f = null;
00151                         try
00152                         {
00153                                 f = (FeatureExtractor)(Class.forName("com.meapsoft.featextractors." + name).newInstance());
00154                         }
00155                         catch (Exception e)
00156                         {
00157                                 //we should never get here!
00158                                 e.printStackTrace();
00159                         }
00160 
00161                         //not using actions, we'll just check their states 
00162                         //when the GO! button is hit...
00163                         JToggleButton cb = new JCheckBox(name);
00164             if(f instanceof MetaFeatureExtractor)
00165                 cb = new JRadioButton(name);
00166 
00167                         featureCheckBoxes.add(cb);
00168                         cb.setBackground(c);
00169                         cb.setToolTipText((String)featureDescriptions.elementAt(i));
00170                         
00171             cb.setActionCommand("enableFeatExt"+i);
00172             cb.addActionListener(this);
00173 
00174             JFormattedTextField tf = new JFormattedTextField(NumberFormat.getNumberInstance());
00175             tf.setValue(new Double(1.0));
00176             tf.setColumns(2);
00177             tf.setBackground(c);
00178             tf.setEnabled(false);
00179             featureWeightFields.add(tf);
00180 
00181             JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
00182             panel.setBackground(c);
00183             panel.add(cb);
00184                         panel.add(tf);
00185 
00186                         if(!(f instanceof MetaFeatureExtractor))
00187                         {
00188                                 fENum++;
00189                 if (name.equals("AvgPitchSimple"))
00190                 {
00191                     cb.setSelected(true);
00192                     tf.setEnabled(true);
00193                 }
00194 
00195                 selectFeaturesPanel.add(panel);
00196             }
00197                         else
00198                         {
00199                                 mFENum++;
00200                 // hack to keep the MFE panel symmetric when it
00201                 // contains only one row of checkboxes.
00202                 if(mFENum < 4)
00203                     panel.setLayout(new FlowLayout());
00204                 selectMetaFeaturesPanel.add(panel);
00205                         }
00206                 }
00207         selectFeaturesPanel.setLayout(new GridLayout((int)(fENum/4.0 + 1), 4));
00208         selectMetaFeaturesPanel.setLayout(new GridLayout((int)(mFENum/4.0 + 1), 4));
00209 
00210                 add(controlsPanel);
00211 
00212                 JPanel extractorOutputFNPanel = new JPanel();
00213                 extractorOutputFNPanel.setBackground(c);
00214                 JLabel fEONL = new JLabel("output features file: ");
00215                 extractorOutputFNPanel.add(fEONL);
00216                 outputFeaturesFileLabel = new JLabel(" " + dataBaseName + ".feat ");
00217                 outputFeaturesFileLabel.setOpaque(true);
00218                 outputFeaturesFileLabel.setBackground(c.darker());
00219                 extractorOutputFNPanel.add(outputFeaturesFileLabel);
00220         add(Box.createRigidArea(new Dimension(0, 10)));
00221                 add(extractorOutputFNPanel);
00222                 
00223                 JPanel displayFeaturesPanel = new JPanel();
00224                 displayFeaturesPanel.setBackground(c);
00225                 displayFeaturesButton = new JButton("display extracted features");
00226                 displayFeaturesButton.setEnabled(false);
00227                 displayFeaturesButton.addActionListener(this);
00228                 displayFeaturesButton.setActionCommand("displayFeatures");
00229                 displayFeaturesButton.setBackground(c);
00230                 displayFeaturesPanel.add(displayFeaturesButton);
00231                 add(displayFeaturesPanel);
00232         }
00233 
00234         private Vector SniffFeatureExtractors()
00235         {
00236         Vector v = null;
00237         try 
00238         {
00239             v = RTSI.findnames("com.meapsoft.featextractors", Class.forName("com.meapsoft.featextractors.FeatureExtractor"));
00240             // \todo{where else should we look for feature extractors?}
00241             // also check current directory:
00242             Vector v2 = RTSI.findnames(System.getProperty("user.dir"), Class.forName("com.meapsoft.featextractors.FeatureExtractor")); 
00243             if(v2 != null)
00244                 v.addAll(v2);
00245             
00246             for (int i = 0; i < v.size(); i++)
00247             {
00248                                 FeatureExtractor f = null;
00249                                 
00250                                 try
00251                                 {
00252                                         String name = "com.meapsoft.featextractors." + (String)v.elementAt(i);
00253                                         f = (FeatureExtractor)(Class.forName(name).newInstance());
00254                                         featureDescriptions.add(f.description());
00255                                 }
00256                                 catch (Exception e)
00257                                 {
00258                                         GUIUtils.ShowDialog("", GUIUtils.MESSAGE, meapsoftGUI.jframe);  
00259                     //ShowDialog(e, "", FATAL_ERROR);
00260                                 }
00261             }
00262         }
00263         catch(ClassNotFoundException e)
00264         {
00265                         GUIUtils.ShowDialog("", GUIUtils.FATAL_ERROR, meapsoftGUI.jframe);      
00266         }
00267 
00268         return v;
00269         }
00270 
00271         public void enableDisplayButton(boolean enable)
00272         {
00273                 displayFeaturesButton.setEnabled(enable);               
00274         }
00275 
00276         public void actionPerformed(ActionEvent arg0)
00277         {
00278                 String command = arg0.getActionCommand();
00279 
00280                 if (command.equals("displayFeatures"))
00281                 {
00282             DataDisplayPanel.spawnWindow(featFile.getFeatures(), featFile.filename);
00283         }
00284         else if(command.startsWith("enableFeatExt"))
00285         {
00286             int i = Integer.parseInt(command.substring("enableFeatExt".length()));
00287 
00288             ((JTextField)featureWeightFields.get(i)).setEnabled(
00289                 ((JToggleButton)featureCheckBoxes.get(i)).isSelected());
00290         }
00291     }
00292 
00293         public synchronized int run()
00294         {
00295         if(!enableBox.isSelected())
00296         {
00297             // not all composers need features, so by default set the
00298             // featFile to be the empty segmentFile
00299             if(featFile == null)
00300                 featFile = segmentFile;
00301 
00302             return 0;
00303         }
00304 
00305                 boolean regularFE = false;
00306                 boolean metaFE = false;
00307                 
00308         Vector featExts = new Vector();
00309         Vector featWeights = new Vector();
00310                 for(int i = 0; i < featureCheckBoxes.size(); i++)       
00311         {
00312                         JToggleButton cb = (JToggleButton)featureCheckBoxes.elementAt(i);
00313 
00314             try
00315             {
00316                 if (cb.isSelected())
00317                 {
00318                         FeatureExtractor fe = (FeatureExtractor)(Class.forName("com.meapsoft.featextractors." + cb.getText())
00319                                         .newInstance());
00320                                         if (fe instanceof MetaFeatureExtractor)
00321                                                 metaFE = true;
00322                                         else
00323                                                 regularFE = true;
00324                                                 
00325                                         featExts.add(fe);
00326                     featWeights.add(((JFormattedTextField)featureWeightFields.elementAt(i)).getValue());
00327                 }
00328             }
00329             catch(Exception e)
00330             {
00331                                 GUIUtils.ShowDialog(e, "Error loading featextractor", GUIUtils.FATAL_ERROR, meapsoftGUI.jframe);        
00332                 return -1;
00333             }
00334             
00335             if (metaFE && !regularFE)
00336             {
00337                                 GUIUtils.ShowDialog("You need at least one regular feature extractor in order to use a " +
00338                                         "meta feature extractor!", GUIUtils.MESSAGE, meapsoftGUI.jframe);
00339                                 return -1;      
00340             }
00341         }
00342 
00343         FeatExtractor featExtractor = 
00344             new FeatExtractor(segmentFile, featFile, featExts);
00345         featExtractor.writeMEAPFile = meapsoftGUI.writeMEAPFile;
00346         featExtractor.setFeatureExtractorWeights(featWeights);
00347         featExtractor.setClearNonMetaFeatures(
00348             clearNonMetaFeatures.isSelected());
00349 
00350         JPanel progressPanel = new JPanel();
00351         progressPanel.add(new JLabel("Extracting features: "));
00352         JProgressBar progressBar = new JProgressBar(featExtractor.getProgress());
00353         progressBar.setStringPainted(true);
00354         progressPanel.add(progressBar);
00355         meapsoftGUI.setProgressPanel(progressPanel);
00356 
00357         try
00358         {                               
00359             // clear whatever chunks may have already been calculated
00360             featFile.clearChunks();
00361             
00362             featExtractor.setup();
00363             featExtractor.processFeatFiles();
00364 
00365             if(featExtractor.writeMEAPFile)
00366                 featFile.writeFile();
00367         }
00368         catch (Exception e)
00369         {
00370                         GUIUtils.ShowDialog(e, "Error running Feature Extractor", GUIUtils.FATAL_ERROR, meapsoftGUI.jframe);    
00371             return -1;
00372                 }
00373 
00374         displayFeaturesButton.setEnabled(true);
00375 
00376         return 0;
00377         }
00378 }

Generated on Tue Feb 6 19:02:26 2007 for MEAPsoft by doxygen1.2.18