Skip to content

Commit

Permalink
#501 scatter plot table deal with undefs
Browse files Browse the repository at this point in the history
  • Loading branch information
lixun910 committed Oct 19, 2016
1 parent fcb0537 commit 6d7cb69
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 37 deletions.
43 changes: 40 additions & 3 deletions Explore/BoxNewPlotView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,10 @@ void BoxPlotCanvas::DetermineMouseHoverObjects()

for (int t=0; t<cur_num_plots; t++) {
for (int i=0; i<num_obs; i++) {
wxPoint& pt0 = selectable_shps[t*num_obs + i]->center;
wxPoint& pt1 = sel1;
sel_scratch[i] = sel_scratch[i] ||
GenUtils::distance_sqrd(selectable_shps[t*num_obs + i]->center,
sel1) <= 16.5;
GenUtils::distance_sqrd(pt0, pt1) <= 16.5;
}
}
for (int i=0; i<num_obs && total_hover_obs<max_hover_obs; i++) {
Expand Down Expand Up @@ -910,10 +911,46 @@ void BoxPlotCanvas::UpdateStatusBar()
{
wxStatusBar* sb = template_frame->GetStatusBar();
if (!sb) return;

TableInterface* table_int = project->GetTableInt();

const std::vector<bool>& hl = highlight_state->GetHighlight();

wxString s;
if (highlight_state->GetTotalHighlighted()> 0) {
s << "#selected=" << highlight_state->GetTotalHighlighted() << " ";
int n_total_hl = highlight_state->GetTotalHighlighted();
s << "#selected=" << n_total_hl << " ";

if (num_time_vals == 1) {
int t = 0;
int n_undefs = 0;
for (int i=0; i<num_obs; i++) {
if (data_undef[0][t][i] && hl[i]) {
n_undefs += 1;
}
}
if (n_undefs> 0) {
s << "(undefined:" << n_undefs << ") ";
}
} else {
wxString str;
for (int t=0; t<num_time_vals; t++) {
int n_undefs = 0;
for (int i=0; i<num_obs; i++) {
if (data_undef[0][t][i] && hl[i])
n_undefs += 1;
}
if (n_undefs > 0) {
wxString t_str = table_int->GetTimeString(t);
str << n_undefs << "@" << t_str <<" ";
}
}
if (!str.IsEmpty()) {
s << "(undefined:" << str << ")";
}
}
}

if (mousemode == select && selectstate == start) {
if (total_hover_obs >= 1) {
s << "hover obs " << hover_obs[0]+1;
Expand Down
39 changes: 32 additions & 7 deletions Explore/ScatterNewPlotView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,11 +1049,11 @@ void ScatterNewPlotCanvas::PopulateCanvas()

// create axes
x_baseline = new GdaAxis(GetNameWithTime(0), axis_scale_x,
wxRealPoint(0,0), wxRealPoint(100, 0));
wxRealPoint(0,0), wxRealPoint(100, 0));
x_baseline->setPen(*GdaConst::scatterplot_scale_pen);
background_shps.push_back(x_baseline);
y_baseline = new GdaAxis(GetNameWithTime(1), axis_scale_y,
wxRealPoint(0,0), wxRealPoint(0, 100));
wxRealPoint(0,0), wxRealPoint(0, 100));
y_baseline->setPen(*GdaConst::scatterplot_scale_pen);
background_shps.push_back(y_baseline);

Expand All @@ -1080,15 +1080,15 @@ void ScatterNewPlotCanvas::PopulateCanvas()
foreground_shps.push_back(reg_line_excluded);
foreground_shps.push_back(reg_line);


if (IsShowLinearSmoother() && !is_bubble_plot) {
double cc_degs_of_rot;
double reg_line_slope;
bool reg_line_infinite_slope;
bool reg_line_defined;
wxRealPoint a, b;
SmoothingUtils::CalcRegressionLine(*reg_line,
reg_line_slope, reg_line_infinite_slope,
reg_line_slope,
reg_line_infinite_slope,
reg_line_defined, a, b, cc_degs_of_rot,
axis_scale_x, axis_scale_y,
regressionXY, *pens.GetRegPen());
Expand Down Expand Up @@ -1170,6 +1170,8 @@ void ScatterNewPlotCanvas::TimeChange()
cat_data.SetCurrentCanvasTmStep(ref_time - ref_time_min);
invalidateBms();
PopulateCanvas();
UpdateStatusBar();

Refresh();
LOG_MSG("Exiting ScatterNewPlotCanvas::TimeChange");
}
Expand Down Expand Up @@ -1735,14 +1737,22 @@ void ScatterNewPlotCanvas::UpdateDisplayStats()
for (int k=i*cols, kend=i*cols+cols; k<kend; k++) {
attributes[k].color = *wxBLACK;
}

const std::vector<bool>& hl = highlight_state->GetHighlight();
int tot_obs = 0;
int tot_sel_obs = 0;
int tot_unsel_obs = 0;

for (size_t i=0; i<XYZ_undef.size(); i++) {
if (!XYZ_undef[i]) {
tot_obs += 1;
if (hl[i])
tot_sel_obs += 1;
else
tot_unsel_obs += 1;
}
}
int tot_sel_obs = highlight_state->GetTotalHighlighted();
int tot_unsel_obs = tot_obs - tot_sel_obs;

vals[i*cols+j++] = "#obs";
vals[i*cols+j++] = "R^2";
vals[i*cols+j++] = "const a";
Expand Down Expand Up @@ -1910,8 +1920,23 @@ void ScatterNewPlotCanvas::UpdateStatusBar()
wxStatusBar* sb = template_frame->GetStatusBar();
if (!sb) return;
wxString s;
TableInterface* table_int = project->GetTableInt();

const std::vector<bool>& hl = highlight_state->GetHighlight();

if (highlight_state->GetTotalHighlighted()> 0) {
s << "#selected=" << highlight_state->GetTotalHighlighted();
int n_total_hl = highlight_state->GetTotalHighlighted();
s << "#selected=" << n_total_hl << " ";

int n_undefs = 0;
for (int i=0; i<num_obs; i++) {
if (XYZ_undef[i] && hl[i]) {
n_undefs += 1;
}
}
if (n_undefs> 0) {
s << "(undefined:" << n_undefs << ") ";
}

if (brushtype == rectangle) {
wxRealPoint pt1 = MousePntToObsPnt(sel1);
Expand Down
18 changes: 5 additions & 13 deletions GenUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ SimpleLinearRegression::SimpleLinearRegression(const std::vector<double>& X,
const std::vector<double>& Y,
double meanX, double meanY,
double varX, double varY)
: covariance(0), correlation(0), alpha(0), beta(0), r_squared(0),
: n(0), covariance(0), correlation(0), alpha(0), beta(0), r_squared(0),
std_err_of_estimate(0), std_err_of_beta(0), std_err_of_alpha(0),
t_score_alpha(0), t_score_beta(0), p_value_alpha(0), p_value_beta(0),
valid(false), valid_correlation(false), valid_std_err(false),
Expand All @@ -486,7 +486,7 @@ SimpleLinearRegression::SimpleLinearRegression(const std::vector<double>& X,
const std::vector<bool>& Y_undef,
double meanX, double meanY,
double varX, double varY)
: covariance(0), correlation(0), alpha(0), beta(0), r_squared(0),
: n(0), covariance(0), correlation(0), alpha(0), beta(0), r_squared(0),
std_err_of_estimate(0), std_err_of_beta(0), std_err_of_alpha(0),
t_score_alpha(0), t_score_beta(0), p_value_alpha(0), p_value_beta(0),
valid(false), valid_correlation(false), valid_std_err(false),
Expand All @@ -512,25 +512,19 @@ void SimpleLinearRegression::CalculateRegression(const std::vector<double>& X,
double varX, double varY)
{
LOG_MSG("Entering SimpleLinearRegression::CalculateRegression");
LOG(meanX);
LOG(meanY);
LOG(varX);
LOG(varY);
if (X.size() != Y.size() || X.size() < 2 ) return;
n = X.size();
if (X.size() != Y.size() || X.size() < 2 )
return;
double expectXY = 0;
for (int i=0, iend=X.size(); i<iend; i++) {
expectXY += X[i]*Y[i];
}
expectXY /= (double) X.size();
covariance = expectXY - meanX * meanY;
LOG(expectXY);
LOG(covariance);
if (varX > 4*DBL_MIN) {
beta = covariance / varX;
alpha = meanY - beta * meanX;
valid = true;
LOG(alpha);
LOG(beta);
}
double SS_tot = varY*Y.size();
error_sum_squares = 0; // error_sum_squares = SS_err
Expand All @@ -539,13 +533,11 @@ void SimpleLinearRegression::CalculateRegression(const std::vector<double>& X,
err = Y[i] - (alpha + beta * X[i]);
error_sum_squares += err * err;
}
LOG(error_sum_squares);
if (error_sum_squares < 16*DBL_MIN) {
r_squared = 1;
} else {
r_squared = 1 - error_sum_squares / SS_tot;
}
LOG(r_squared);

if (Y.size()>2 && varX > 4*DBL_MIN) {
// error_sum_squares/(n-k-1), k=1
Expand Down
3 changes: 2 additions & 1 deletion GenUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ struct SimpleLinearRegression {
static double TScoreTo2SidedPValue(double tscore, int df);

std::string ToString();


int n;
double covariance;
double correlation;
double alpha;
Expand Down
31 changes: 18 additions & 13 deletions ShapeOperations/PolysToContigWeights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,13 +1157,17 @@ GalElement* MakeContiguity(Shapefile::Main& main, bool is_queen,
for (int step= 0; step < gMinX.Cells(); ++step) {
// include all elements from xmin[step]
for (curr= gMinX.first(step); curr != GdaConst::EMPTY;
curr= gMinX.tail(curr)) gY->include(curr);
curr= gMinX.tail(curr))
{
gY->include(curr);
}

// test each element in xmax[step]
for (curr= gMaxX.first(step); curr != GdaConst::EMPTY;
curr= gMaxX.tail(curr)) {
PolygonContents* ply = dynamic_cast<PolygonContents*> (
main.records[curr].contents_p);
curr= gMaxX.tail(curr))
{
RecordContents* rec = main.records[curr].contents_p;
PolygonContents* ply = dynamic_cast<PolygonContents*> (rec);
PolygonPartition testPoly(ply);
testPoly.MakePartition();

Expand All @@ -1179,7 +1183,8 @@ GalElement* MakeContiguity(Shapefile::Main& main, bool is_queen,
// test each potential neighbor
for (int nbr = Neighbors.Pop(); nbr != GdaConst::EMPTY;
nbr = Neighbors.Pop()) {
PolygonContents* nbr_ply = dynamic_cast<PolygonContents*> (main.records[nbr].contents_p);
RecordContents* nbr_rec = main.records[nbr].contents_p;
PolygonContents* nbr_ply = dynamic_cast<PolygonContents*>(nbr_rec);

if (ply->intersect(nbr_ply)) {

Expand All @@ -1190,18 +1195,18 @@ GalElement* MakeContiguity(Shapefile::Main& main, bool is_queen,
if (curr == 0 && nbr == 0) {

}

// run sweep with testPoly as a host and nbrPoly as a guest
int related = testPoly.sweep(nbrPoly, is_queen,
precision_threshold);
int related = testPoly.sweep(nbrPoly, is_queen, precision_threshold);
if (related) Related.Push(nbr);
}
}


if (size_t sz = Related.Size()) {
gl[curr].SetSizeNbrs(sz);
for (size_t i=0; i<sz; ++i) gl[curr].SetNbr(i, Related.Pop());
for (size_t i=0; i<sz; ++i) {
gl[curr].SetNbr(i, Related.Pop());
}
}

gY->remove(curr); // remove from the partition
Expand Down Expand Up @@ -1236,7 +1241,7 @@ void MakeFull(GalElement* W, size_t obs)


GalElement* PolysToContigWeights(Shapefile::Main& main, bool is_queen,
double precision_threshold)
double precision_threshold)
{
using namespace Shapefile;

Expand All @@ -1255,9 +1260,9 @@ GalElement* PolysToContigWeights(Shapefile::Main& main, bool is_queen,
gMaxX.alloc(gRecords, gx, shp_x_len );

for (cnt= 0; cnt < gRecords; ++cnt) {
PolygonContents* ply = dynamic_cast<PolygonContents*> (
main.records[cnt].contents_p);
RecordContents* rec = main.records[cnt].contents_p;
PolygonContents* ply = dynamic_cast<PolygonContents*>(rec);

gMinX.include( cnt, ply->box[0] - shp_min_x );
gMaxX.include( cnt, ply->box[2] - shp_min_x );
}
Expand Down

0 comments on commit 6d7cb69

Please sign in to comment.