IMP logo
IMP Reference Guide  2.6.1
The Integrative Modeling Platform
ProfileFitter.h
Go to the documentation of this file.
1 /**
2  * \file IMP/saxs/ProfileFitter.h \brief a class for fitting two profiles
3  *
4  * Copyright 2007-2016 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPSAXS_PROFILE_FITTER_H
9 #define IMPSAXS_PROFILE_FITTER_H
10 
11 #include "ChiScore.h"
12 #include "FitParameters.h"
13 #include "Profile.h"
14 #include <IMP/Object.h>
15 
16 #include <fstream>
17 
18 IMPSAXS_BEGIN_NAMESPACE
19 
20 //! Fit two profiles with user-defined scoring function as a template parameter.
21 /** By default chi score is used.
22  The scoring function template parameter class has to implement three
23  basic functions: compute_score, compute_scale_factor and compute_offset.
24  see ChiScore for example.
25  Currently four scoring functions are implemented:
26  ChiScore, ChiScoreLog, ChiFreeScore, and RatioVolatilityScore
27  */
28 template <typename ScoringFunctionT = ChiScore>
29 class ProfileFitter : public Object {
30  public:
31  //! Constructor
32  /**
33  \param[in] exp_profile Experimental profile we want to fit
34  */
35  ProfileFitter(const Profile* exp_profile)
36  : Object("ProfileFitter%1%"), exp_profile_(exp_profile) {
37  set_was_used(true);
38  scoring_function_ = new ScoringFunctionT();
39  }
40 
41  ProfileFitter(const Profile* exp_profile, ScoringFunctionT* sf)
42  : Object("ProfileFitter%1%"), exp_profile_(exp_profile) {
43  set_was_used(true);
44  scoring_function_ = sf;
45  }
46 
47  //! compute fit score
48  double compute_score(const Profile* model_profile, bool use_offset = false,
49  const std::string fit_file_name = "") const;
50 
51  //! fit experimental profile through optimization of c1 and c2 parameters
52  /**
53  c1 - adjusts the excluded volume, valid range [0.95 - 1.05]
54  c2 - adjusts the density of hydration layer, valid range [-2.0 - 4.0]
55  \param[in] partial_profile partial profiles computed
56  \param[in] min_c1 minimal c1 value
57  \param[in] max_c1 maximal c1 value
58  \param[in] min_c2 minimal c2 value
59  \param[in] max_c2 maximal c2 value
60  \param[in] use_offset use offset in fitting
61  \param[in] fit_file_name write fit in the given filename,
62  nothing is written if empty
63  \return FitParameters (score, c1, c2)
64  */
65  FitParameters fit_profile(Profile* partial_profile, double min_c1 = 0.95,
66  double max_c1 = 1.05, double min_c2 = -2.0,
67  double max_c2 = 4.0, bool use_offset = false,
68  const std::string fit_file_name = "") const;
69 
70  //! computes the scaling factor needed for fitting the modeled profile
71  // onto the experimental one
72  /** resampling of the modeled profile is required prior to calling
73  to this function, in order to match the q values of the exp. profile.
74  this is not done by default to minimize the number of calls to resample.
75  */
76  double compute_scale_factor(const Profile* model_profile,
77  double offset = 0.0) const {
78  return scoring_function_->compute_scale_factor(exp_profile_, model_profile,
79  offset);
80  }
81 
82  //! computes offset for fitting for fitting the modeled profile
83  // onto the experimental one
84  /** resampling of the modeled profile is required prior to calling
85  to this function, in order to match the q values of the exp. profile.
86  this is not done by default to minimize the number of calls to resample.
87  */
88  double compute_offset(const Profile* model_profile) const {
89  return scoring_function_->compute_offset(exp_profile_, model_profile);
90  }
91 
92  //! resampling of the modeled profile is required to fit the q
93  // values of the computational profile to the experimental one
94  void resample(const Profile* model_profile, Profile* resampled_profile) const;
95 
96  // writes 3 column fit file, given scale factor c, offset and chi square
97  void write_SAXS_fit_file(const std::string& file_name,
98  const Profile* model_profile, const double chi_square,
99  const double c = 1, const double offset = 0) const;
100 
101  private:
102  FitParameters search_fit_parameters(Profile* partial_profile, double min_c1,
103  double max_c1, double min_c2, double max_c2,
104  bool use_offset, double old_chi) const;
105 
107  friend class DerivativeCalculator;
108 
109  protected:
110  // experimental saxs profile
111  PointerMember<const Profile> exp_profile_;
112  PointerMember<ScoringFunctionT> scoring_function_;
113 };
114 
115 template <typename ScoringFunctionT>
117  const Profile* model_profile, Profile* resampled_profile) const {
118  model_profile->resample(exp_profile_, resampled_profile);
119 }
120 
121 template <typename ScoringFunctionT>
123  Profile* partial_profile, double min_c1, double max_c1, double min_c2,
124  double max_c2, bool use_offset, double old_chi) const {
125  int c1_cells = 10;
126  int c2_cells = 10;
127  if (old_chi < (std::numeric_limits<double>::max() - 1)) { // second iteration
128  c1_cells = 5;
129  c2_cells = 5;
130  }
131 
132  double delta_c1 = (max_c1 - min_c1) / c1_cells;
133  double delta_c2 = (max_c2 - min_c2) / c2_cells;
134 
135  bool last_c1 = false;
136  bool last_c2 = false;
137  if (delta_c1 < 0.0001) {
138  c1_cells = 1;
139  delta_c1 = max_c1 - min_c1;
140  last_c1 = true;
141  }
142  if (delta_c2 < 0.001) {
143  c2_cells = 1;
144  delta_c2 = max_c2 - min_c2;
145  last_c2 = true;
146  }
147  double best_c1(1.0), best_c2(0.0), best_chi(old_chi);
148  bool best_set = false;
149 
150  double c1(min_c1);
151  for (int i = 0; i <= c1_cells; i++, c1 += delta_c1) {
152  double c2 = min_c2;
153  for (int j = 0; j <= c2_cells; j++, c2 += delta_c2) {
154  partial_profile->sum_partial_profiles(c1, c2);
155  double curr_chi = compute_score(partial_profile, use_offset);
156  if (!best_set || curr_chi < best_chi) {
157  best_set = true;
158  best_chi = curr_chi;
159  best_c1 = c1;
160  best_c2 = c2;
161  }
162  }
163  }
164 
165  if (std::fabs(best_chi - old_chi) > 0.0001 &&
166  (!(last_c1 && last_c2))) { // refine more
167  min_c1 = std::max(best_c1 - delta_c1, min_c1);
168  max_c1 = std::min(best_c1 + delta_c1, max_c1);
169  min_c2 = std::max(best_c2 - delta_c2, min_c2);
170  max_c2 = std::min(best_c2 + delta_c2, max_c2);
171  return search_fit_parameters(partial_profile, min_c1, max_c1, min_c2,
172  max_c2, use_offset, best_chi);
173  }
174  return FitParameters(best_chi, best_c1, best_c2);
175 }
176 
177 template <typename ScoringFunctionT>
179  Profile* partial_profile, double min_c1, double max_c1, double min_c2,
180  double max_c2, bool use_offset, const std::string fit_file_name) const {
181 
182  // compute chi value for default c1/c1 (remove?)
183  double default_c1 = 1.0, default_c2 = 0.0;
184  partial_profile->sum_partial_profiles(default_c1, default_c2);
185  double default_chi = compute_score(partial_profile, use_offset);
186 
187  FitParameters fp =
188  search_fit_parameters(partial_profile, min_c1, max_c1, min_c2, max_c2,
189  use_offset, std::numeric_limits<double>::max());
190  double best_c1 = fp.get_c1();
191  double best_c2 = fp.get_c2();
192  fp.set_default_chi(default_chi);
193 
194  // compute a profile for best c1/c2 combination
195  partial_profile->sum_partial_profiles(best_c1, best_c2);
196  compute_score(partial_profile, use_offset, fit_file_name);
197 
198  return fp;
199 }
200 
201 template <typename ScoringFunctionT>
203  const Profile* model_profile, bool use_offset,
204  const std::string fit_file_name) const {
205  IMP_NEW(Profile, resampled_profile,
206  (exp_profile_->get_min_q(), exp_profile_->get_max_q(),
207  exp_profile_->get_delta_q()));
208  model_profile->resample(exp_profile_, resampled_profile);
209 
210  double score = scoring_function_->compute_score(exp_profile_,
211  resampled_profile,
212  use_offset);
213  if (fit_file_name.length() > 0) {
214  double offset = 0.0;
215  if (use_offset)
216  offset =
217  scoring_function_->compute_offset(exp_profile_, resampled_profile);
218  double c = scoring_function_->compute_scale_factor(exp_profile_,
219  resampled_profile,
220  offset);
221  write_SAXS_fit_file(fit_file_name, resampled_profile, score, c, offset);
222  }
223  return score;
224 }
225 
226 template <typename ScoringFunctionT>
228  const std::string& file_name, const Profile* model_profile,
229  const double score, const double c, const double offset) const {
230  std::ofstream out_file(file_name.c_str());
231  if (!out_file) {
232  IMP_THROW("Can't open file " << file_name, IOException);
233  }
234 
235  unsigned int profile_size =
236  std::min(model_profile->size(), exp_profile_->size());
237  // header line
238  out_file.precision(15);
239  out_file << "# SAXS profile: number of points = " << profile_size
240  << ", q_min = " << exp_profile_->get_min_q()
241  << ", q_max = " << exp_profile_->get_max_q();
242  out_file << ", delta_q = " << exp_profile_->get_delta_q() << std::endl;
243 
244  out_file.setf(std::ios::showpoint);
245  out_file << "# offset = " << offset << ", scaling c = " << c
246  << ", Chi = " << score << std::endl;
247  out_file << "# q exp_intensity model_intensity" << std::endl;
248 
249  out_file.setf(std::ios::fixed, std::ios::floatfield);
250  // Main data
251  for (unsigned int i = 0; i < profile_size; i++) {
252  out_file.setf(std::ios::left);
253  out_file.width(10);
254  out_file.precision(5);
255  out_file << exp_profile_->get_q(i) << " ";
256 
257  out_file.setf(std::ios::left);
258  out_file.width(15);
259  out_file.precision(8);
260  out_file << exp_profile_->get_intensity(i) << " ";
261 
262  out_file.setf(std::ios::left);
263  out_file.width(15);
264  out_file.precision(8);
265  out_file << model_profile->get_intensity(i) * c - offset << " ";
266 
267  out_file.setf(std::ios::left);
268  out_file.width(10);
269  out_file.precision(5);
270  out_file << exp_profile_->get_error(i) << std::endl;
271  }
272  out_file.close();
273 }
274 
275 IMPSAXS_END_NAMESPACE
276 
277 #endif /* IMPSAXS_PROFILE_FITTER_H */
unsigned int size() const
return number of entries in SAXS profile
Definition: Profile.h:171
ProfileFitter(const Profile *exp_profile)
Constructor.
Definition: ProfileFitter.h:35
Basic chi score implementation.
Parameters of a fit, from ProfileFitter.
Definition: FitParameters.h:16
#define IMP_REF_COUNTED_DESTRUCTOR(Name)
Ref counted objects should have private destructors.
#define IMP_NEW(Typename, varname, args)
Declare a ref counted pointer to a new object.
Definition: object_macros.h:62
An input/output exception.
Definition: exception.h:174
Common base class for heavy weight IMP objects.
Definition: Object.h:106
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:146
Copyright 2007-2016 IMP Inventors. All rights reserved.
double compute_offset(const Profile *model_profile) const
computes offset for fitting for fitting the modeled profile
Definition: ProfileFitter.h:88
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
A shared base class to help in debugging and things.
void resample(const Profile *exp_profile, Profile *resampled_profile) const
return a profile that is sampled on the q values of the exp_profile
Fit two profiles with user-defined scoring function as a template parameter.
Definition: ProfileFitter.h:29
void sum_partial_profiles(double c1, double c2, bool check_cashed=true)
computes full profile for given fitting parameters
double compute_scale_factor(const Profile *model_profile, double offset=0.0) const
computes the scaling factor needed for fitting the modeled profile
Definition: ProfileFitter.h:76
A class for profile storing and computation.