8 #ifndef IMPSAXS_PROFILE_FITTER_H
9 #define IMPSAXS_PROFILE_FITTER_H
18 IMPSAXS_BEGIN_NAMESPACE
29 template <
class ScoringFunctionT = ChiScore>
37 : base::Object(
"ProfileFitter%1%"), exp_profile_(exp_profile) {
39 scoring_function_ =
new ScoringFunctionT();
43 : base::Object(
"ProfileFitter%1%"), exp_profile_(exp_profile) {
45 scoring_function_ = sf;
49 Float compute_score(
const Profile* model_profile,
bool use_offset =
false,
50 const std::string fit_file_name =
"")
const;
53 Float compute_score(
const Profile* model_profile,
Float min_q,
70 FitParameters fit_profile(Profile* partial_profile,
float min_c1 = 0.95,
71 float max_c1 = 1.05,
float min_c2 = -2.0,
72 float max_c2 = 4.0,
bool use_offset =
false,
73 const std::string fit_file_name =
"")
const;
82 Float offset = 0.0)
const {
83 return scoring_function_->compute_scale_factor(exp_profile_, model_profile,
94 return scoring_function_->compute_offset(exp_profile_, model_profile);
99 void resample(
const Profile* model_profile,
Profile* resampled_profile)
const;
102 void write_SAXS_fit_file(
const std::string& file_name,
104 const Float c = 1,
const Float offset = 0)
const;
107 FitParameters search_fit_parameters(
Profile* partial_profile,
float min_c1,
108 float max_c1,
float min_c2,
float max_c2,
109 bool use_offset,
float old_chi)
const;
117 ScoringFunctionT* scoring_function_;
120 template <
class ScoringFunctionT>
123 model_profile->
resample(exp_profile_, resampled_profile);
126 template <
class ScoringFunctionT>
128 Profile* partial_profile,
float min_c1,
float max_c1,
float min_c2,
129 float max_c2,
bool use_offset,
float old_chi)
const {
132 if (old_chi < (std::numeric_limits<float>::max() - 1)) {
137 float delta_c1 = (max_c1 - min_c1) / c1_cells;
138 float delta_c2 = (max_c2 - min_c2) / c2_cells;
140 bool last_c1 =
false;
141 bool last_c2 =
false;
142 if (delta_c1 < 0.0001) {
144 delta_c1 = max_c1 - min_c1;
147 if (delta_c2 < 0.001) {
149 delta_c2 = max_c2 - min_c2;
152 float best_c1(1.0), best_c2(0.0), best_chi(old_chi);
153 bool best_set =
false;
156 for (
int i = 0; i <= c1_cells; i++, c1 += delta_c1) {
158 for (
int j = 0; j <= c2_cells; j++, c2 += delta_c2) {
160 float curr_chi = compute_score(partial_profile, use_offset);
161 if (!best_set || curr_chi < best_chi) {
170 if (std::fabs(best_chi - old_chi) > 0.0001 &&
171 (!(last_c1 && last_c2))) {
172 min_c1 = std::max(best_c1 - delta_c1, min_c1);
173 max_c1 = std::min(best_c1 + delta_c1, max_c1);
174 min_c2 = std::max(best_c2 - delta_c2, min_c2);
175 max_c2 = std::min(best_c2 + delta_c2, max_c2);
176 return search_fit_parameters(partial_profile, min_c1, max_c1, min_c2,
177 max_c2, use_offset, best_chi);
179 return FitParameters(best_chi, best_c1, best_c2);
182 template <
class ScoringFunctionT>
184 Profile* partial_profile,
float min_c1,
float max_c1,
float min_c2,
185 float max_c2,
bool use_offset,
const std::string fit_file_name)
const {
188 float default_c1 = 1.0, default_c2 = 0.0;
190 float default_chi = compute_score(partial_profile, use_offset);
193 search_fit_parameters(partial_profile, min_c1, max_c1, min_c2, max_c2,
194 use_offset, std::numeric_limits<float>::max());
195 float best_c1 = fp.get_c1();
196 float best_c2 = fp.get_c2();
197 fp.set_default_chi(default_chi);
201 compute_score(partial_profile, use_offset, fit_file_name);
208 template <
class ScoringFunctionT>
212 (exp_profile_->get_min_q(), exp_profile_->get_max_q(),
213 exp_profile_->get_delta_q()));
214 model_profile->
resample(exp_profile_, resampled_profile);
216 Float score = scoring_function_->compute_score(
217 exp_profile_, resampled_profile, min_q, max_q);
221 template <
class ScoringFunctionT>
223 const Profile* model_profile,
bool use_offset,
224 const std::string fit_file_name)
const {
226 (exp_profile_->get_min_q(), exp_profile_->get_max_q(),
227 exp_profile_->get_delta_q()));
228 model_profile->
resample(exp_profile_, resampled_profile);
230 Float score = scoring_function_->compute_score(exp_profile_,
231 resampled_profile, use_offset);
233 if (fit_file_name.length() > 0) {
237 scoring_function_->compute_offset(exp_profile_, resampled_profile);
238 Float c = scoring_function_->compute_scale_factor(
239 exp_profile_, resampled_profile, offset);
240 write_SAXS_fit_file(fit_file_name, resampled_profile, score, c, offset);
245 template <
class ScoringFunctionT>
247 const std::string& file_name,
const Profile* model_profile,
249 std::ofstream out_file(file_name.c_str());
251 IMP_THROW(
"Can't open file " << file_name, IOException);
254 unsigned int profile_size =
255 std::min(model_profile->
size(), exp_profile_->size());
257 out_file.precision(15);
258 out_file <<
"# SAXS profile: number of points = " << profile_size
259 <<
", q_min = " << exp_profile_->get_min_q()
260 <<
", q_max = " << exp_profile_->get_max_q();
261 out_file <<
", delta_q = " << exp_profile_->get_delta_q() << std::endl;
263 out_file.setf(std::ios::showpoint);
264 out_file <<
"# offset = " << offset <<
", scaling c = " << c
265 <<
", Chi = " << score << std::endl;
266 out_file <<
"# q exp_intensity model_intensity" << std::endl;
268 out_file.setf(std::ios::fixed, std::ios::floatfield);
270 for (
unsigned int i = 0; i < profile_size; i++) {
271 out_file.setf(std::ios::left);
273 out_file.precision(5);
274 out_file << exp_profile_->get_q(i) <<
" ";
276 out_file.setf(std::ios::left);
278 out_file.precision(8);
279 out_file << exp_profile_->get_intensity(i) <<
" ";
281 out_file.setf(std::ios::left);
283 out_file.precision(8);
284 out_file << model_profile->get_intensity(i) * c - offset << std::endl;
289 IMPSAXS_END_NAMESPACE
unsigned int size() const
return number of entries in SAXS profile
ProfileFitter(const Profile *exp_profile)
Constructor.
Basic chi score implementation.
void resample(const Profile *exp_profile, Profile *resampled_profile, bool partial_profiles=false) const
return a profile that is sampled on the q values of the exp_profile
A smart pointer to a ref-counted Object that is a class memeber.
#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.
Float compute_scale_factor(const Profile *model_profile, Float offset=0.0) const
computes the scaling factor needed for fitting the modeled profile
void sum_partial_profiles(Float c1, Float c2)
computes full profile for given fitting parameters
Copyright 2007-2014 IMP Inventors. All rights reserved.
Common base class for heavy weight IMP objects.
Float compute_offset(const Profile *model_profile) const
computes offset for fitting for fitting the modeled profile
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
double Float
Basic floating-point value (could be float, double...)
A shared base class to help in debugging and things.
A class for profile storing and computation.