"Fossies" - the Fresh Open Source Software Archive

Member "fityk-1.3.1/wxgui/fitinfo.cpp" (13 May 2016, 10024 Bytes) of package /linux/misc/fityk-1.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "fitinfo.cpp" see the Fossies "Dox" file reference documentation.

    1 // This file is part of fityk program. Copyright 2001-2013 Marcin Wojdyr
    2 // Licence: GNU General Public License ver. 2+
    3 
    4 #include <wx/wx.h>
    5 #include <wx/spinctrl.h>
    6 #include <wx/statline.h>
    7 #include <wx/tooltip.h>
    8 #include <wx/clipbrd.h>
    9 
   10 #include "fitinfo.h"
   11 #include "frame.h" //frame
   12 #include "cmn.h" //ProportionalSplitter
   13 #include "fityk/logic.h"
   14 #include "fityk/fit.h"
   15 #include "fityk/data.h"
   16 #include "fityk/var.h"
   17 
   18 using namespace std;
   19 using fityk::Variable;
   20 
   21 NumericFormatPanel::NumericFormatPanel(wxWindow* parent)
   22     : wxPanel(parent, -1)
   23 {
   24     wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
   25     sizer->Add(new wxStaticText(this, -1, wxT("precision:")),
   26                wxSizerFlags().Center());
   27     prec_sc = new SpinCtrl(this, -1, 6, 0, 30);
   28     sizer->Add(prec_sc, wxSizerFlags().Center());
   29     wxArrayString fmt_choices;
   30     fmt_choices.Add(wxT("g"));
   31     fmt_choices.Add(wxT("e"));
   32     fmt_choices.Add(wxT("E"));
   33     fmt_choices.Add(wxT("f"));
   34     fmt_c = new wxChoice(this, -1, wxDefaultPosition, wxDefaultSize,
   35                          fmt_choices);
   36     fmt_c->SetSelection(0);
   37     fmt_c->SetToolTip(wxT("g: mixed format\n")
   38                       wxT("e: 1.234e+02\n")
   39                       wxT("E: 1.234E+02\n")
   40                       wxT("f: 123.400"));
   41     sizer->Add(fmt_c, wxSizerFlags(1).Center());
   42     SetSizerAndFit(sizer);
   43 
   44     update_format();
   45     Connect(fmt_c->GetId(), wxEVT_COMMAND_CHOICE_SELECTED,
   46             wxCommandEventHandler(NumericFormatPanel::OnFormatChanged));
   47     Connect(prec_sc->GetId(), wxEVT_COMMAND_SPINCTRL_UPDATED,
   48             wxSpinEventHandler(NumericFormatPanel::OnPrecisionSpin));
   49 }
   50 
   51 void NumericFormatPanel::update_format()
   52 {
   53     format_= "%." + S(prec_sc->GetValue()) + REALT_LENGTH_MOD
   54              + wx2s(fmt_c->GetStringSelection());
   55 
   56     wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, GetId());
   57     AddPendingEvent(event);
   58 }
   59 
   60 
   61 FitInfoDlg::FitInfoDlg(wxWindow* parent, wxWindowID id)
   62   : wxDialog(parent, id, wxString(wxT("Fit Info")),
   63              wxDefaultPosition, wxDefaultSize,
   64              wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
   65 {
   66 }
   67 
   68 bool FitInfoDlg::Initialize()
   69 {
   70     wxBoxSizer *top_sizer = new wxBoxSizer(wxVERTICAL);
   71     wxSplitterWindow *hsplit = new ProportionalSplitter(this, -1, 0.25);
   72     wxPanel *left_panel = new wxPanel(hsplit);
   73     wxSizer *lsizer = new wxBoxSizer(wxVERTICAL);
   74     nf = new NumericFormatPanel(left_panel);
   75     lsizer->Add(nf, wxSizerFlags().Expand().Border(wxTOP|wxBOTTOM));
   76     left_tc = new wxTextCtrl(left_panel, -1, wxEmptyString,
   77                              wxDefaultPosition, wxDefaultSize,
   78                              wxTE_MULTILINE|wxTE_RICH|wxTE_READONLY);
   79     lsizer->Add(left_tc, wxSizerFlags(1).Expand());
   80     left_panel->SetSizerAndFit(lsizer);
   81     wxPanel *right_panel = new wxPanel(hsplit);
   82     wxSizer *rsizer = new wxBoxSizer(wxVERTICAL);
   83     wxArrayString choices;
   84     // \u00B1 == +/-
   85     choices.Add(wxT("\u00B1 standard errors: sqrt(WSSR/DoF COV_kk)"));
   86     choices.Add(wxT("\u00B1 sqrt(COV_kk)"));
   87     choices.Add(wxT("\u00B1 50% confidence intervals"));
   88     choices.Add(wxT("\u00B1 90% confidence intervals"));
   89     choices.Add(wxT("\u00B1 95% confidence intervals"));
   90     choices.Add(wxT("\u00B1 99% confidence intervals"));
   91     choices.Add(wxT("covariance matrix"));
   92     right_c = new wxChoice(right_panel, -1, wxDefaultPosition, wxDefaultSize,
   93                            choices);
   94     right_c->SetSelection(0);
   95     rsizer->Add(right_c, wxSizerFlags().Expand().Border());
   96     right_tc = new wxTextCtrl(right_panel, -1, wxT(""),
   97                               wxDefaultPosition, wxDefaultSize,
   98                               wxTE_MULTILINE|wxTE_RICH|wxTE_READONLY|
   99                               wxTE_DONTWRAP);
  100     int font_size = right_tc->GetFont().GetPointSize();
  101 #ifndef __WXMSW__
  102     font_size -= 1;
  103 #endif
  104     wxFont font(font_size, wxFONTFAMILY_TELETYPE,
  105                 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
  106     wxTextAttr attr;
  107     attr.SetFont(font);
  108     right_tc->SetDefaultStyle(attr);
  109     rsizer->Add(right_tc, wxSizerFlags(1).Expand());
  110     right_panel->SetSizerAndFit(rsizer);
  111     hsplit->SplitVertically(left_panel, right_panel);
  112     top_sizer->Add(hsplit, wxSizerFlags(1).Expand().Border());
  113     wxBoxSizer *btn_sizer = new wxBoxSizer(wxHORIZONTAL);
  114     btn_sizer->Add(new wxButton(this, wxID_COPY),
  115                    wxSizerFlags().Border());
  116     btn_sizer->AddStretchSpacer();
  117     btn_sizer->Add(new wxButton(this, wxID_CLOSE),
  118                    wxSizerFlags().Right().Border());
  119     top_sizer->Add(btn_sizer, wxSizerFlags().Expand());
  120     SetSizerAndFit(top_sizer);
  121     SetSize(wxSize(640, 440));
  122 
  123     SetEscapeId(wxID_CLOSE);
  124 
  125     try {
  126         update_left_tc();
  127         update_right_tc();
  128     } catch (fityk::ExecuteError &e) {
  129         ftk->ui()->warn(string("Error: ") + e.what());
  130         return false;
  131     }
  132 
  133     // connect both right_c and nf (NumericFormatPanel)
  134     Connect(-1, wxEVT_COMMAND_CHOICE_SELECTED,
  135             wxCommandEventHandler(FitInfoDlg::OnChoice));
  136     Connect(wxID_COPY, wxEVT_COMMAND_BUTTON_CLICKED,
  137             wxCommandEventHandler(FitInfoDlg::OnCopy));
  138 
  139     return true;
  140 }
  141 
  142 void FitInfoDlg::update_left_tc()
  143 {
  144     string s;
  145     vector<Data*> datas = frame->get_selected_datas();
  146     const vector<realt> &pp = ftk->mgr.parameters();
  147     fityk::Fit *fit = ftk->get_fit();
  148     int dof = fit->get_dof(datas);
  149     double wssr = fit->compute_wssr(pp, datas, true);
  150     wssr_over_dof = wssr / dof;
  151     double ssr = fit->compute_wssr(pp, datas, false);
  152     double r2 = fit->compute_r_squared(pp, datas);
  153     int points = 0;
  154     for (vector<Data*>::const_iterator i = datas.begin(); i != datas.end(); ++i)
  155         points += (*i)->get_n();
  156 
  157     if (datas.size() == 1)
  158         s = "dataset " + S(frame->get_selected_data_indices()[0])
  159             + ": " + datas[0]->get_title() + "\n";
  160     else
  161         s = S(datas.size()) + " datasets\n";
  162     s += "points: " + S(points) +
  163          "\n\nDoF: " + S(dof) +
  164          "\nWSSR: " + nf->fmt(wssr) +
  165          "\nSSR: " + nf->fmt(ssr) +
  166          "\nWSSR/DoF: " + nf->fmt(wssr_over_dof) +
  167          "\nRes.St.Dev.: " + nf->fmt(sqrt(wssr_over_dof)) +
  168          "\nR-squared: " + nf->fmt(r2) + "\n";
  169     left_tc->SetValue(s2wx(s));
  170 }
  171 
  172 
  173 void FitInfoDlg::update_right_tc()
  174 {
  175     fityk::Fit *fit = ftk->get_fit();
  176     int choice = right_c->GetSelection();
  177     vector<Data*> datas = frame->get_selected_datas();
  178     vector<realt> const &pp = ftk->mgr.parameters();
  179     int na = pp.size();
  180     wxString s;
  181     if (choice <= 5) {
  182         vector<double> errors;
  183         if (choice == 0 || choice == 1) {
  184             try {
  185                 errors = fit->get_standard_errors(datas);
  186             }
  187             catch (fityk::ExecuteError&) {
  188                 errors.resize(na, 0.);
  189             }
  190             if (choice == 1)
  191                 vm_foreach (double, i, errors)
  192                     *i *= 1. / sqrt(wssr_over_dof);
  193         } else {
  194             int level;
  195             if (choice == 2)
  196                 level = 50;
  197             else if (choice == 3)
  198                 level = 90;
  199             else if (choice == 4)
  200                 level = 95;
  201             else //if (choice == 5)
  202                 level = 99;
  203             try {
  204                 errors = fit->get_confidence_limits(datas, level);
  205             }
  206             catch (fityk::ExecuteError&) {
  207                 errors.resize(na, 0.);
  208             }
  209         }
  210         for (int i = 0; i < na; ++i) {
  211             if (fit->is_param_used(i)) {
  212                 const Variable *var = ftk->mgr.gpos_to_var(i);
  213                 vector<string> in = ftk->mgr.get_variable_references(var->name);
  214                 wxString name = wxT("$") + s2wx(var->name);
  215                 if (in.size() == 1 && in[0][0] == '%')
  216                     name += wxT(" = ") + s2wx(in[0]);
  217                 else if (in.size() == 1)
  218                     name += wxT(" (in ") + s2wx(in[0]) + wxT(")");
  219                 else
  220                     name += wxT(" (") + s2wx(S(in.size())) + wxT(" refs)");
  221                 wxString val = s2wx(nf->fmt(pp[i]));
  222                 // \u00B1 == +/-
  223                 s += wxString::Format(wxT("\n%20s = %10s \u00B1 "),
  224                                       name.c_str(), val.c_str());
  225                 if (errors[i] == 0.)
  226                     s += wxT("??");
  227                 else
  228                     s += s2wx(nf->fmt(errors[i]));
  229             }
  230         }
  231     } else {
  232         s = wxT("          ");
  233         vector<double> alpha;
  234         try {
  235             alpha = fit->get_covariance_matrix(datas);
  236         }
  237         catch (fityk::ExecuteError&) {
  238             alpha.resize(na*na, 0.);
  239         }
  240         for (int i = 0; i < na; ++i)
  241             if (fit->is_param_used(i)) {
  242                 string name = ftk->mgr.gpos_to_var(i)->name;
  243                 s += wxString::Format("%10s", s2wx("$"+name).c_str());
  244             }
  245         for (int i = 0; i < na; ++i) {
  246             if (fit->is_param_used(i)) {
  247                 string name = ftk->mgr.gpos_to_var(i)->name;
  248                 s += wxString::Format("\n%10s", s2wx("$"+name).c_str());
  249                 for (int j = 0; j < na; ++j) {
  250                     if (fit->is_param_used(j)) {
  251                         double val = alpha[na*i + j];
  252                         if (fabs(val) < 1e-99)
  253                             val = 0.;
  254                         s += wxString::Format(wxT(" %9s"),
  255                                               s2wx(nf->fmt(val)).c_str());
  256                     }
  257                 }
  258             }
  259         }
  260     }
  261     // On wxMSW 2.9.0 wxTextCtrl::ChangeValue() ignores default styles
  262     //right_tc->ChangeValue(s);
  263     right_tc->Clear();
  264     right_tc->AppendText(s);
  265 }
  266 
  267 void FitInfoDlg::OnCopy(wxCommandEvent&)
  268 {
  269     wxString sel = right_tc->GetStringSelection();
  270     if (sel.empty())
  271         sel = left_tc->GetStringSelection();
  272     if (sel.empty())
  273         sel = right_tc->GetValue();
  274     if (wxTheClipboard->Open()) {
  275         wxTheClipboard->SetData(new wxTextDataObject(sel));
  276         wxTheClipboard->Close();
  277     }
  278 }
  279