forked from GeoDaCenter/geoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatClassifManager.cpp
182 lines (171 loc) · 5.77 KB
/
CatClassifManager.cpp
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
/**
* GeoDa TM, Copyright (C) 2011-2015 by Luc Anselin - all rights reserved
*
* This file is part of GeoDa.
*
* GeoDa is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeoDa is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/foreach.hpp>
#include "../logger.h"
#include "../DataViewer/TableInterface.h"
#include "../DataViewer/TableState.h"
#include "CatClassifManager.h"
CatClassifManager::CatClassifManager(TableInterface* _table_int,
TableState* _table_state,
CustomClassifPtree* cc_ptree)
: table_state(_table_state), table_int(_table_int)
{
BOOST_FOREACH(const CatClassifDef& cc, cc_ptree->GetCatClassifList()) {
CreateNewClassifState(cc);
}
table_state->registerObserver(this);
}
CatClassifManager::~CatClassifManager()
{
for (std::list<CatClassifState*>::iterator it=classif_states.begin();
it != classif_states.end(); it++) {
(*it)->closeAndDeleteWhenEmpty();
}
table_state->removeObserver(this);
}
void CatClassifManager::GetTitles(std::vector<wxString>& titles)
{
titles.resize(classif_states.size());
int i=0;
for (std::list<CatClassifState*>::iterator it=classif_states.begin();
it != classif_states.end(); it++) {
titles[i++] = (*it)->GetTitle();
}
}
CatClassifState* CatClassifManager::FindClassifState(const wxString& title)
{
CatClassifState* ccs=0;
for (std::list<CatClassifState*>::iterator it=classif_states.begin();
it != classif_states.end() && !ccs; it++) {
if (title == (*it)->GetTitle()) ccs = (*it);
}
return ccs;
}
CatClassifState* CatClassifManager::CreateNewClassifState(
const CatClassifDef& cc_data)
{
CatClassifState* ccs = new CatClassifState;
ccs->SetCatClassif(cc_data);
classif_states.push_back(ccs);
return ccs;
}
void CatClassifManager::RemoveClassifState(CatClassifState* ccs)
{
ccs->closeAndDeleteWhenEmpty();
classif_states.remove(ccs);
//delete ccs;
}
bool CatClassifManager::VerifyAgainstTable()
{
if (!table_int) return false;
bool any_changed = false;
for (std::list<CatClassifState*>::iterator it=classif_states.begin();
it != classif_states.end(); it++) {
CatClassifDef& cc = (*it)->GetCatClassif();
if (CatClassification::CorrectCatClassifFromTable(cc, table_int)) {
any_changed = true;
}
}
return any_changed;
}
void CatClassifManager::update(TableState* o)
{
std::list<CatClassifState*>::iterator i;
if (o->GetEventType() == TableState::col_rename) {
if (!o->IsSimpleGroupRename()) return;
for (i = classif_states.begin(); i != classif_states.end(); ++i) {
wxString asso_fld = (*i)->GetCatClassif().assoc_db_fld_name;
if (asso_fld == o->GetOldColName()) {
(*i)->GetCatClassif().assoc_db_fld_name = o->GetNewColName();
}
}
} else if (o->GetEventType() == TableState::cols_delta) {
const TableDeltaList_type& tdl = o->GetTableDeltaListRef();
BOOST_FOREACH(const TableDeltaEntry& e, tdl) {
// remove association for every deleted field
if (!e.insert) {
for (i = classif_states.begin(); i!=classif_states.end(); ++i) {
wxString asso_fld = (*i)->GetCatClassif().assoc_db_fld_name;
if (asso_fld == e.group_name) {
(*i)->GetCatClassif().assoc_db_fld_name = "";
(*i)->notifyObservers();
}
}
}
}
} else if (o->GetEventType() == TableState::col_data_change) {
for (i = classif_states.begin(); i != classif_states.end(); ++i) {
CatClassifDef& cc = (*i)->GetCatClassif();
if (cc.assoc_db_fld_name == o->GetModifiedColName() &&
cc.cat_classif_type != CatClassification::custom)
{
// reset breaks and notify observers
int col = -1, tm = 0;
bool found = table_int->DbColNmToColAndTm(cc.assoc_db_fld_name,
col, tm);
if (!found) continue;
std::vector<double> v;
std::vector<bool> v_undef;
table_int->GetColData(col, tm, v);
table_int->GetColUndefined(col, tm, v_undef);
int num_obs = table_int->GetNumberRows();
Gda::dbl_int_pair_vec_type data(num_obs);
for (int ii=0; ii<num_obs; ++ii) {
data[ii].first = v[ii];
data[ii].second = ii;
}
std::sort(data.begin(), data.end(), Gda::dbl_int_pair_cmp_less);
CatClassifDef _cc = cc;
CatClassification::SetBreakPoints(_cc.breaks, _cc.names, data,
v_undef, _cc.cat_classif_type,
_cc.num_cats);
if (_cc != cc) {
cc = cc;
(*i)->notifyObservers();
}
} else if (cc.assoc_db_fld_name == o->GetModifiedColName() &&
cc.cat_classif_type == CatClassification::custom) {
int col = -1, tm = 0;
bool found = table_int->DbColNmToColAndTm(cc.assoc_db_fld_name,
col, tm);
if (!found) continue;
CatClassifDef _cc = cc;
// ensure that breaks and min/max are within new
// min/max bounds
double new_min = cc.uniform_dist_min;
double new_max = cc.uniform_dist_max;
table_int->GetMinMaxVals(col, tm, new_min, new_max);
if (cc.uniform_dist_min < new_min) {
cc.uniform_dist_min = new_min;
}
if (cc.uniform_dist_max > new_max) {
cc.uniform_dist_max = new_max;
}
for (int ii=0, sz=cc.breaks.size(); ii<sz; ++ii) {
if (cc.breaks[ii] < new_min) cc.breaks[ii] = new_min;
if (cc.breaks[ii] > new_max) cc.breaks[ii] = new_max;
}
if (_cc != cc) {
cc = cc;
(*i)->notifyObservers();
}
}
}
}
}