EMAN2
analyzer.h
Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #ifndef eman_analyzer_h__
00037 #define eman_analyzer_h__
00038 
00039 #include "emobject.h"
00040 #include <gsl/gsl_linalg.h>
00041 using std::vector;
00042 
00043 namespace EMAN
00044 {
00045         class EMData;
00046 
00060         class Analyzer
00061         {
00062           public:
00063                 Analyzer() {}
00064 
00065                 virtual ~Analyzer()
00066                 {}
00067 
00072                 virtual int insert_image(EMData * image) = 0;
00073 
00078                 virtual int insert_images_list(vector<EMData *> image_list);
00079 
00083                 virtual vector<EMData*> analyze() = 0;
00084 
00088                 virtual string get_name() const = 0;
00089 
00093                 virtual string get_desc() const = 0;
00094 
00098                 virtual void set_params(const Dict & new_params)
00099                 {
00100                         params = new_params;
00101                 }
00102 
00106                 virtual Dict get_params() const
00107                 {
00108                         return params;
00109                 }
00110 
00117                 virtual TypeDict get_param_types() const = 0;
00118 
00119           protected:
00120                 mutable Dict params;
00121                 vector<EMData *> images;
00122         };
00123 
00131         class InertiaMatrixAnalyzer:public Analyzer
00132         {
00133           public:
00134                 InertiaMatrixAnalyzer() : verbose(0) {}
00135 
00136                 virtual int insert_image(EMData *image) {
00137                         images.push_back(image);
00138                         if (images.size()>1) { printf("InertiaMatrixAnalyzer takes only a single image\n"); return 1; }
00139                         return 0;
00140                 }
00141 
00142                 virtual vector<EMData*> analyze();
00143 
00144                 string get_name() const
00145                 {
00146                         return NAME;
00147                 }
00148 
00149                 string get_desc() const
00150                 {
00151                         return "Compute Inertia matrix for a volume";
00152                 }
00153 
00154                 static Analyzer * NEW()
00155                 {
00156                         return new InertiaMatrixAnalyzer();
00157                 }
00158 
00159                 TypeDict get_param_types() const
00160                 {
00161                         TypeDict d;
00162                         d.put("verbose", EMObject::INT, "Display progress if set, more detail with larger numbers");
00163                         return d;
00164                 }
00165 
00166                 static const string NAME;
00167 
00168           protected:
00169                 int verbose;
00170                 vector<EMData *> ret;           // This will contain only a single image
00171         };
00172 
00181         class ShapeAnalyzer:public Analyzer
00182         {
00183           public:
00184                 ShapeAnalyzer() : verbose(0) {}
00185 
00186                 virtual int insert_image(EMData *image) {
00187                         images.push_back(image);
00188                         if (images.size()>1) { printf("ShapeAnalyzer takes only a single image\n"); return 1; }
00189                         return 0;
00190                 }
00191 
00192                 virtual vector<EMData*> analyze();
00193 
00194                 string get_name() const
00195                 {
00196                         return NAME;
00197                 }
00198 
00199                 string get_desc() const
00200                 {
00201                         return "Experimental. Computes a set of values characterizing a 3-D volume. Returns a 3x2x1 image containing X, Y and Z axial distributions using axis squared and axis linear weighting.";
00202                 }
00203 
00204                 static Analyzer * NEW()
00205                 {
00206                         return new ShapeAnalyzer();
00207                 }
00208 
00209                 TypeDict get_param_types() const
00210                 {
00211                         TypeDict d;
00212                         d.put("verbose", EMObject::INT, "Display progress if set, more detail with larger numbers");
00213                         return d;
00214                 }
00215 
00216                 static const string NAME;
00217 
00218           protected:
00219                 int verbose;
00220                 vector<EMData *> ret;           // This will contain only a single image
00221         };
00222 
00223 
00224         
00239         class KMeansAnalyzer:public Analyzer
00240         {
00241           public:
00242                 KMeansAnalyzer() : ncls(0),verbose(0),minchange(0),maxiter(100),mininclass(2),slowseed(0) {}
00243 
00244                 virtual int insert_image(EMData *image) {
00245                         images.push_back(image);
00246                         return 0;
00247                 }
00248 
00249                 virtual vector<EMData*> analyze();
00250 
00251                 string get_name() const
00252                 {
00253                         return NAME;
00254                 }
00255 
00256                 string get_desc() const
00257                 {
00258                         return "k-means classification";
00259                 }
00260 
00261                 static Analyzer * NEW()
00262                 {
00263                         return new KMeansAnalyzer();
00264                 }
00265 
00266                 void set_params(const Dict & new_params);
00267 
00268                 TypeDict get_param_types() const
00269                 {
00270                         TypeDict d;
00271                         d.put("verbose", EMObject::INT, "Display progress if set, more detail with larger numbers (9 max)");
00272                         d.put("ncls", EMObject::INT, "number of desired classes");
00273                         d.put("maxiter", EMObject::INT, "maximum number of iterations");
00274                         d.put("minchange", EMObject::INT, "Terminate if fewer than minchange members move in an iteration");
00275                         d.put("mininclass", EMObject::INT, "Minumum number of particles to keep a class as good (not enforced at termination");
00276                         d.put("slowseed",EMObject::INT, "Instead of seeding all classes at once, it will gradually increase the number of classes by adding new seeds in groups with large standard deviations");
00277                         d.put("calcsigmamean",EMObject::INT, "Computes standard deviation of the mean image for each class-average (center), and returns them at the end of the list of centers");
00278                         return d;
00279                 }
00280 
00281                 static const string NAME;
00282 
00283           protected:
00284                 void update_centers(int sigmas=0);
00285                 void reclassify();
00286                 void reseed();
00287 
00288                 vector<EMData *> centers;
00289                 int ncls;       //number of desired classes
00290                 int verbose;
00291                 int minchange;
00292                 int maxiter;
00293                 int mininclass;
00294                 int nchanged;
00295                 int slowseed;
00296                 int calcsigmamean;
00297 
00298         };
00299 
00305         class SVDAnalyzer : public Analyzer
00306         {
00307           public:
00308                 SVDAnalyzer() : mask(0), nvec(0), nimg(0), A(NULL) {}
00309 
00310                 virtual int insert_image(EMData * image);
00311 
00312                 virtual int insert_images_list(vector<EMData *> image_list) {
00313                         vector<EMData*>::const_iterator iter;
00314                         for(iter=image_list.begin(); iter!=image_list.end(); ++iter) {
00315                                 images.push_back(*iter);
00316                         }
00317                         return 0;
00318                 }
00319 
00320                 virtual vector<EMData*> analyze();
00321 
00322                 string get_name() const
00323                 {
00324                         return NAME;
00325                 }
00326 
00327                 string get_desc() const
00328                 {
00329                         return "Singular Value Decomposition from GSL. Comparable to pca";
00330                 }
00331 
00332                 static Analyzer * NEW()
00333                 {
00334                         return new SVDAnalyzer();
00335                 }
00336 
00337                 void set_params(const Dict & new_params);
00338 
00339                 TypeDict get_param_types() const
00340                 {
00341                         TypeDict d;
00342                         d.put("mask", EMObject::EMDATA, "mask image");
00343                         d.put("nvec", EMObject::INT, "number of desired basis vectors");
00344                         d.put("nimg", EMObject::INT, "total number of input images, required even with insert_image()");
00345                         return d;
00346                 }
00347 
00348                 static const string NAME;
00349 
00350           protected:
00351                 EMData * mask;
00352                 int nvec;       //number of desired principal components
00353                 int pixels;     // pixels under the mask
00354                 int nimg; // number of input images
00355 
00356                 private:
00357                 int nsofar;
00358                 gsl_matrix *A;
00359         };
00360 
00361         template <> Factory < Analyzer >::Factory();
00362 
00363         void dump_analyzers();
00364         map<string, vector<string> > dump_analyzers_list();
00365 }
00366 
00367 #endif  //eman_analyzer_h__