File: fitinfo.cpp

package info (click to toggle)
fityk 1.3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,772 kB
  • sloc: cpp: 34,595; ansic: 4,676; python: 963; makefile: 384; sh: 119; xml: 91; java: 31; ruby: 27; perl: 25
file content (279 lines) | stat: -rw-r--r-- 10,037 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// This file is part of fityk program. Copyright 2001-2013 Marcin Wojdyr
// Licence: GNU General Public License ver. 2+

#include <wx/wx.h>
#include <wx/spinctrl.h>
#include <wx/statline.h>
#include <wx/tooltip.h>
#include <wx/clipbrd.h>

#include "fitinfo.h"
#include "frame.h" //frame
#include "cmn.h" // make_wxspinctrl, ProportionalSplitter
#include "fityk/logic.h"
#include "fityk/fit.h"
#include "fityk/data.h"
#include "fityk/var.h"

using namespace std;
using fityk::Variable;

NumericFormatPanel::NumericFormatPanel(wxWindow* parent)
    : wxPanel(parent, -1)
{
    wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
    sizer->Add(new wxStaticText(this, -1, wxT("precision:")),
               wxSizerFlags().Center());
    prec_sc = make_wxspinctrl(this, -1, 6, 0, 30);
    sizer->Add(prec_sc, wxSizerFlags().Center());
    wxArrayString fmt_choices;
    fmt_choices.Add(wxT("g"));
    fmt_choices.Add(wxT("e"));
    fmt_choices.Add(wxT("E"));
    fmt_choices.Add(wxT("f"));
    fmt_c = new wxChoice(this, -1, wxDefaultPosition, wxDefaultSize,
                         fmt_choices);
    fmt_c->SetSelection(0);
    fmt_c->SetToolTip(wxT("g: mixed format\n")
                      wxT("e: 1.234e+02\n")
                      wxT("E: 1.234E+02\n")
                      wxT("f: 123.400"));
    sizer->Add(fmt_c, wxSizerFlags(1).Center());
    SetSizerAndFit(sizer);

    update_format();
    Connect(fmt_c->GetId(), wxEVT_COMMAND_CHOICE_SELECTED,
            wxCommandEventHandler(NumericFormatPanel::OnFormatChanged));
    Connect(prec_sc->GetId(), wxEVT_COMMAND_SPINCTRL_UPDATED,
            wxSpinEventHandler(NumericFormatPanel::OnPrecisionSpin));
}

void NumericFormatPanel::update_format()
{
    format_= "%." + S(prec_sc->GetValue()) + REALT_LENGTH_MOD
             + wx2s(fmt_c->GetStringSelection());

    wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, GetId());
    AddPendingEvent(event);
}


FitInfoDlg::FitInfoDlg(wxWindow* parent, wxWindowID id)
  : wxDialog(parent, id, wxString(wxT("Fit Info")),
             wxDefaultPosition, wxDefaultSize,
             wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
{
}

bool FitInfoDlg::Initialize()
{
    wxBoxSizer *top_sizer = new wxBoxSizer(wxVERTICAL);
    wxSplitterWindow *hsplit = new ProportionalSplitter(this, -1, 0.25);
    wxPanel *left_panel = new wxPanel(hsplit);
    wxSizer *lsizer = new wxBoxSizer(wxVERTICAL);
    nf = new NumericFormatPanel(left_panel);
    lsizer->Add(nf, wxSizerFlags().Expand().Border(wxTOP|wxBOTTOM));
    left_tc = new wxTextCtrl(left_panel, -1, wxEmptyString,
                             wxDefaultPosition, wxDefaultSize,
                             wxTE_MULTILINE|wxTE_RICH|wxTE_READONLY);
    lsizer->Add(left_tc, wxSizerFlags(1).Expand());
    left_panel->SetSizerAndFit(lsizer);
    wxPanel *right_panel = new wxPanel(hsplit);
    wxSizer *rsizer = new wxBoxSizer(wxVERTICAL);
    wxArrayString choices;
    // \u00B1 == +/-
    choices.Add(wxT("\u00B1 standard errors: sqrt(WSSR/DoF COV_kk)"));
    choices.Add(wxT("\u00B1 sqrt(COV_kk)"));
    choices.Add(wxT("\u00B1 50% confidence intervals"));
    choices.Add(wxT("\u00B1 90% confidence intervals"));
    choices.Add(wxT("\u00B1 95% confidence intervals"));
    choices.Add(wxT("\u00B1 99% confidence intervals"));
    choices.Add(wxT("covariance matrix"));
    right_c = new wxChoice(right_panel, -1, wxDefaultPosition, wxDefaultSize,
                           choices);
    right_c->SetSelection(0);
    rsizer->Add(right_c, wxSizerFlags().Expand().Border());
    right_tc = new wxTextCtrl(right_panel, -1, wxT(""),
                              wxDefaultPosition, wxDefaultSize,
                              wxTE_MULTILINE|wxTE_RICH|wxTE_READONLY|
                              wxTE_DONTWRAP);
    int font_size = right_tc->GetFont().GetPointSize();
#ifndef __WXMSW__
    font_size -= 1;
#endif
    wxFont font(font_size, wxFONTFAMILY_TELETYPE,
                wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
    wxTextAttr attr;
    attr.SetFont(font);
    right_tc->SetDefaultStyle(attr);
    rsizer->Add(right_tc, wxSizerFlags(1).Expand());
    right_panel->SetSizerAndFit(rsizer);
    hsplit->SplitVertically(left_panel, right_panel);
    top_sizer->Add(hsplit, wxSizerFlags(1).Expand().Border());
    wxBoxSizer *btn_sizer = new wxBoxSizer(wxHORIZONTAL);
    btn_sizer->Add(new wxButton(this, wxID_COPY),
                   wxSizerFlags().Border());
    btn_sizer->AddStretchSpacer();
    btn_sizer->Add(new wxButton(this, wxID_CLOSE),
                   wxSizerFlags().Border());
    top_sizer->Add(btn_sizer, wxSizerFlags().Expand());
    SetSizerAndFit(top_sizer);
    SetSize(wxSize(640, 440));

    SetEscapeId(wxID_CLOSE);

    try {
        update_left_tc();
        update_right_tc();
    } catch (fityk::ExecuteError &e) {
        ftk->ui()->warn(string("Error: ") + e.what());
        return false;
    }

    // connect both right_c and nf (NumericFormatPanel)
    Connect(-1, wxEVT_COMMAND_CHOICE_SELECTED,
            wxCommandEventHandler(FitInfoDlg::OnChoice));
    Connect(wxID_COPY, wxEVT_COMMAND_BUTTON_CLICKED,
            wxCommandEventHandler(FitInfoDlg::OnCopy));

    return true;
}

void FitInfoDlg::update_left_tc()
{
    string s;
    vector<Data*> datas = frame->get_selected_datas();
    const vector<realt> &pp = ftk->mgr.parameters();
    fityk::Fit *fit = ftk->get_fit();
    int dof = fit->get_dof(datas);
    double wssr = fit->compute_wssr(pp, datas, true);
    wssr_over_dof = wssr / dof;
    double ssr = fit->compute_wssr(pp, datas, false);
    double r2 = fit->compute_r_squared(pp, datas);
    int points = 0;
    for (vector<Data*>::const_iterator i = datas.begin(); i != datas.end(); ++i)
        points += (*i)->get_n();

    if (datas.size() == 1)
        s = "dataset " + S(frame->get_selected_data_indices()[0])
            + ": " + datas[0]->get_title() + "\n";
    else
        s = S(datas.size()) + " datasets\n";
    s += "points: " + S(points) +
         "\n\nDoF: " + S(dof) +
         "\nWSSR: " + nf->fmt(wssr) +
         "\nSSR: " + nf->fmt(ssr) +
         "\nWSSR/DoF: " + nf->fmt(wssr_over_dof) +
         "\nRes.St.Dev.: " + nf->fmt(sqrt(wssr_over_dof)) +
         "\nR-squared: " + nf->fmt(r2) + "\n";
    left_tc->SetValue(s2wx(s));
}


void FitInfoDlg::update_right_tc()
{
    fityk::Fit *fit = ftk->get_fit();
    int choice = right_c->GetSelection();
    vector<Data*> datas = frame->get_selected_datas();
    vector<realt> const &pp = ftk->mgr.parameters();
    int na = pp.size();
    wxString s;
    if (choice <= 5) {
        vector<double> errors;
        if (choice == 0 || choice == 1) {
            try {
                errors = fit->get_standard_errors(datas);
            }
            catch (fityk::ExecuteError&) {
                errors.resize(na, 0.);
            }
            if (choice == 1)
                vm_foreach (double, i, errors)
                    *i *= 1. / sqrt(wssr_over_dof);
        } else {
            int level;
            if (choice == 2)
                level = 50;
            else if (choice == 3)
                level = 90;
            else if (choice == 4)
                level = 95;
            else //if (choice == 5)
                level = 99;
            try {
                errors = fit->get_confidence_limits(datas, level);
            }
            catch (fityk::ExecuteError&) {
                errors.resize(na, 0.);
            }
        }
        for (int i = 0; i < na; ++i) {
            if (fit->is_param_used(i)) {
                const Variable *var = ftk->mgr.gpos_to_var(i);
                vector<string> in = ftk->mgr.get_variable_references(var->name);
                wxString name = wxT("$") + s2wx(var->name);
                if (in.size() == 1 && in[0][0] == '%')
                    name += wxT(" = ") + s2wx(in[0]);
                else if (in.size() == 1)
                    name += wxT(" (in ") + s2wx(in[0]) + wxT(")");
                else
                    name += wxT(" (") + s2wx(S(in.size())) + wxT(" refs)");
                wxString val = s2wx(nf->fmt(pp[i]));
                // \u00B1 == +/-
                s += wxString::Format(wxT("\n%20s = %10s \u00B1 "),
                                      name.c_str(), val.c_str());
                if (errors[i] == 0.)
                    s += wxT("??");
                else
                    s += s2wx(nf->fmt(errors[i]));
            }
        }
    } else {
        s = wxT("          ");
        vector<double> alpha;
        try {
            alpha = fit->get_covariance_matrix(datas);
        }
        catch (fityk::ExecuteError&) {
            alpha.resize(na*na, 0.);
        }
        for (int i = 0; i < na; ++i)
            if (fit->is_param_used(i)) {
                string name = ftk->mgr.gpos_to_var(i)->name;
                s += wxString::Format("%10s", s2wx("$"+name).c_str());
            }
        for (int i = 0; i < na; ++i) {
            if (fit->is_param_used(i)) {
                string name = ftk->mgr.gpos_to_var(i)->name;
                s += wxString::Format("\n%10s", s2wx("$"+name).c_str());
                for (int j = 0; j < na; ++j) {
                    if (fit->is_param_used(j)) {
                        double val = alpha[na*i + j];
                        if (fabs(val) < 1e-99)
                            val = 0.;
                        s += wxString::Format(wxT(" %9s"),
                                              s2wx(nf->fmt(val)).c_str());
                    }
                }
            }
        }
    }
    // On wxMSW 2.9.0 wxTextCtrl::ChangeValue() ignores default styles
    //right_tc->ChangeValue(s);
    right_tc->Clear();
    right_tc->AppendText(s);
}

void FitInfoDlg::OnCopy(wxCommandEvent&)
{
    wxString sel = right_tc->GetStringSelection();
    if (sel.empty())
        sel = left_tc->GetStringSelection();
    if (sel.empty())
        sel = right_tc->GetValue();
    if (wxTheClipboard->Open()) {
        wxTheClipboard->SetData(new wxTextDataObject(sel));
        wxTheClipboard->Close();
    }
}