Esempio n. 1
0
void FieldNewCalcLagDlg::OnLagOperandUpdated( wxCommandEvent& event )
{
	int sel = m_var->GetSelection();
	m_var_tm->Enable(sel != wxNOT_FOUND &&
						IsTimeVariant(col_id_map[sel]));	
    Display();
}
Esempio n. 2
0
void FieldNewCalcUniDlg::OnUnaryResultUpdated( wxCommandEvent& event )
{
	int sel = m_result->GetSelection();
	m_result_tm->Enable(sel != wxNOT_FOUND &&
						IsTimeVariant(col_id_map[sel]));
    Display();
}
Esempio n. 3
0
void FieldNewCalcLagDlg::Apply()
{
	if (m_result->GetSelection() == wxNOT_FOUND) {
		wxString msg("Please select a Result field.");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}
	
	if (m_weight->GetSelection() == wxNOT_FOUND) {
		wxString msg("Please specify a Weights matrix.");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}
	
	if (m_var->GetSelection() == wxNOT_FOUND) {
		wxString msg("Please select an Variable field.");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}
	
	int result_col = col_id_map[m_result->GetSelection()];
	int var_col = col_id_map[m_var->GetSelection()];
	
	TableState* ts = project->GetTableState();
	wxString grp_nm = table_int->GetColName(result_col);
	if (!GenUtils::CanModifyGrpAndShowMsgIfNot(ts, grp_nm)) return;
	
	if (is_space_time &&
		!IsAllTime(result_col, m_result_tm->GetSelection()) &&
		IsAllTime(var_col, m_var_tm->GetSelection())) {
		wxString msg("When \"all times\" selected for variable, result "
					 "field must also be \"all times.\"");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}
	
	std::vector<int> time_list;
	if (IsAllTime(result_col, m_result_tm->GetSelection())) {
		int ts = project->GetTableInt()->GetTimeSteps();
		time_list.resize(ts);
		for (int i=0; i<ts; i++) time_list[i] = i;
	} else {
		int tm = IsTimeVariant(result_col) ? m_result_tm->GetSelection() : 0;
		time_list.resize(1);
		time_list[0] = tm;
	}
	
	std::vector<double> data(table_int->GetNumberRows(), 0);
	std::vector<bool> undefined(table_int->GetNumberRows(), false);
	if (!IsAllTime(var_col, m_var_tm->GetSelection())) {
		int tm = IsTimeVariant(var_col) ? m_var_tm->GetSelection() : 0;
		table_int->GetColData(var_col, tm, data);
		table_int->GetColUndefined(var_col, tm, undefined);
	}
	
	int rows = table_int->GetNumberRows();
	std::vector<double> r_data(table_int->GetNumberRows(), 0);
	std::vector<bool> r_undefined(table_int->GetNumberRows(), false);
	
	GalWeight* gal_w = w_manager->GetGalWeight(m_weight->GetSelection());
	if (gal_w == NULL) return;
	GalElement* W = gal_w->gal;
	
	for (int t=0; t<time_list.size(); t++) {
		for (int i=0; i<rows; i++) {
			r_data[i] = 0;
			r_undefined[i] = false;
		}
		if (IsAllTime(var_col, m_var_tm->GetSelection())) {
			table_int->GetColData(var_col, time_list[t], data);
			table_int->GetColUndefined(var_col, time_list[t], undefined);
		}
		// Row-standardized lag calculation.
		for (int i=0, iend=table_int->GetNumberRows(); i<iend; i++) {
			double lag = 0;
			if (W[i].size == 0) r_undefined[i] = true;
			for (int j=0; j<W[i].size && !r_undefined[i]; j++) {
				if (undefined[W[i].data[j]]) {
					r_undefined[i] = true;
				} else {
					lag += data[W[i].data[j]];
				}
			}
			r_data[i] = r_undefined[i] ? 0 : lag /= W[i].size;
		}
		table_int->SetColData(result_col, time_list[t], r_data);
		table_int->SetColUndefined(result_col, time_list[t], r_undefined);

	}
}
Esempio n. 4
0
void FieldNewCalcUniDlg::Apply()
{
	if (m_result->GetSelection() == wxNOT_FOUND) {
		wxString msg("Please choose a Result field.");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}
	int result_col = col_id_map[m_result->GetSelection()];

	int var_col = wxNOT_FOUND;
	if (m_var_sel != wxNOT_FOUND) {
		var_col = col_id_map[m_var_sel];
	}	
	if (var_col == wxNOT_FOUND && !m_valid_const) {
		wxString msg("Operation requires a valid field name or constant.");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}
	
	if (is_space_time && var_col != wxNOT_FOUND &&
		!IsAllTime(result_col, m_result_tm->GetSelection()) &&
		IsAllTime(var_col, m_var_tm->GetSelection())) {
		wxString msg("When \"all times\" selected for variable, result "
					 "field must also be \"all times.\"");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}
	
	TableState* ts = project->GetTableState();
	wxString grp_nm = table_int->GetColName(result_col);
	if (!GenUtils::CanModifyGrpAndShowMsgIfNot(ts, grp_nm)) return;

	
	// Mersenne Twister random number generator, randomly seeded
	// with current time in seconds since Jan 1 1970.
	static boost::mt19937 rng(std::time(0));
	
	std::vector<int> time_list;
	if (IsAllTime(result_col, m_result_tm->GetSelection())) {
		int ts = project->GetTableInt()->GetTimeSteps();
		time_list.resize(ts);
		for (int i=0; i<ts; i++) time_list[i] = i;
	} else {
		int tm = IsTimeVariant(result_col) ? m_result_tm->GetSelection() : 0;
		time_list.resize(1);
		time_list[0] = tm;
	}
	
	int rows = table_int->GetNumberRows();
	std::vector<double> data(rows, 0);
	std::vector<bool> undefined(rows, false);
	if (var_col != wxNOT_FOUND &&
		!IsAllTime(var_col, m_var_tm->GetSelection())) {
		int tm = IsTimeVariant(var_col) ? m_var_tm->GetSelection() : 0;
		table_int->GetColData(var_col,tm, data);
		table_int->GetColUndefined(var_col, tm, undefined);
	} else {
		for (int i=0; i<rows; i++) data[i] = m_const;
	}
	std::vector<double> r_data(table_int->GetNumberRows(), 0);
	std::vector<bool> r_undefined(table_int->GetNumberRows(), false);
	
	for (int t=0; t<time_list.size(); t++) {
		if (var_col != wxNOT_FOUND &&
			IsAllTime(var_col, m_var_tm->GetSelection()))
		{
			table_int->GetColData(var_col, time_list[t], data);
			table_int->GetColUndefined(var_col, time_list[t], undefined);
		}
		for (int i=0; i<rows; i++) {
			r_data[i] = data[i];
			r_undefined[i] = undefined[i];
		}
		switch (m_op->GetSelection()) {
			case assign_op:
			{
			}
				break;
			case negate_op:
			{
				for (int i=0; i<rows; i++) {
					if (!undefined[i]) r_data[i] = -data[i];
				}
			}
				break;
			case invert_op:
			{
				for (int i=0; i<rows; i++) {
					if (!undefined[i] && data[i] != 0) {
						r_data[i] = 1.0 / data[i];
					} else {
						r_data[i] = 0;
						r_undefined[i] = true;
					}
				}
			}
				break;
			case sqrt_op:
			{
				for (int i=0; i<rows; i++) {
					if (!undefined[i] && data[i] >= 0) {
						r_data[i] = sqrt(data[i]);
					} else {
						r_data[i] = 0;
						r_undefined[i] = true;
					}
				}
			}
				break;
			case log_10_op:
			{
				for (int i=0; i<rows; i++) {
					if (!undefined[i] && data[i] > 0) {
						r_data[i] = log10(data[i]);
					} else {
						r_data[i] = 0;
						r_undefined[i] = true;
					}
				}
			}
				break;			
			case log_e_op:
			{
				for (int i=0; i<rows; i++) {
					if (!undefined[i] && data[i] > 0) {
						r_data[i] = log(data[i]);
					} else {
						r_data[i] = 0;
						r_undefined[i] = true;
					}
				}
			}
				break;
			case dev_from_mean_op:
			{
				for (int i=0; i<rows; i++) {
					if (undefined[i]) {
						wxString msg;
						msg << "Observation " << i;
						msg << " is undefined. ";
						msg << "Operation aborted.";
						wxMessageDialog dlg (this, msg, "Error",
											 wxOK | wxICON_ERROR);
						dlg.ShowModal();
						return;
					}
					r_data[i] = data[i];
				}
				GenUtils::DeviationFromMean(r_data);
			}
				break;
			case standardize_op:
			{
				for (int i=0; i<rows; i++) {
					if (undefined[i]) {
						wxString msg;
						msg << "Observation ";
						msg << i << " is undefined. ";
						msg << "Operation aborted.";
						wxMessageDialog dlg (this, msg, "Error",
											 wxOK | wxICON_ERROR);
						dlg.ShowModal();
						return;
					}
					r_data[i] = data[i];
				}
				double ssum = 0.0;
				for (int i=0; i<rows; i++) ssum += r_data[i] * r_data[i];
				if (ssum == 0) {
					wxString msg("Standard deviation is 0, operation aborted.");
					wxMessageDialog dlg (this, msg, "Error", wxOK|wxICON_ERROR);
					dlg.ShowModal();
					return;
				}
				GenUtils::StandardizeData(r_data);
			}
				break;
			case shuffle_op:
			{
				for (int i=0; i<rows; i++) {
					r_data[i] = data[i];
					r_undefined[i] = undefined[i];
				}
				static boost::random::uniform_int_distribution<> X(0, rows-1);
				// X(rng) -> returns a uniform random number from 0 to rows-1;
				for (int i=0; i<rows; i++) {
					// swap each item in data with a random position in data.
					// This will produce a random permutation
					int r = X(rng);
					double d_t = r_data[r];
					bool u_t = r_undefined[r];
					r_data[r] = r_data[i];
					r_undefined[r] = r_undefined[i];
					r_data[i] = d_t;
					r_undefined[i] = u_t;
					if (undefined[i]) r_data[i] = 0;
					if (undefined[r]) r_data[r] = 0;
				}
			}
				break;
			default:
				return;
				break;
		}
		table_int->SetColData(result_col, time_list[t], r_data);
		table_int->SetColUndefined(result_col, time_list[t], r_undefined);

	}
}
Esempio n. 5
0
void FieldNewCalcRateDlg::OnRateOperand2Updated( wxCommandEvent& event )
{
	int sel = m_base->GetSelection();
	m_base_tm->Enable(sel != wxNOT_FOUND &&
					  IsTimeVariant(col_id_map[sel]));
}
Esempio n. 6
0
void FieldNewCalcRateDlg::Apply()
{
	if (m_result->GetSelection() == wxNOT_FOUND) {
		wxString msg("Please select a results field.");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}
	
	const int op = m_method->GetSelection();
	if ((op == 3 || op == 4) && GetWeightsId().is_nil()) {
		wxString msg("Weight matrix required for chosen spatial "
					 "rate method.");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}
	
	if (m_event->GetSelection() == wxNOT_FOUND) {
		wxString msg("Please select an Event field.");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}

	if (m_base->GetSelection() == wxNOT_FOUND) {
		wxString msg("Please select an Base field.");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}
	
	const int result_col = col_id_map[m_result->GetSelection()];
	const int cop1 = col_id_map[m_event->GetSelection()];
	const int cop2 = col_id_map[m_base->GetSelection()];
	
	TableState* ts = project->GetTableState();
	wxString grp_nm = table_int->GetColName(result_col);
	if (!Project::CanModifyGrpAndShowMsgIfNot(ts, grp_nm)) return;
	
	if (is_space_time && !IsAllTime(result_col, m_result_tm->GetSelection()) &&
		(IsAllTime(cop1, m_event_tm->GetSelection()) ||
		 IsAllTime(cop2, m_base_tm->GetSelection())))
	{
		wxString msg("When \"all times\" selected for either variable, result "
					 "field must also be \"all times.\"");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}

	boost::uuids::uuid weights_id = GetWeightsId();
	if (op == 3 || op == 4)	{
		if (!w_man_int->IsValid(weights_id)) {
			wxString msg("Was not able to load weights matrix.");
			wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
			dlg.ShowModal();
			return;
		}
	}

	std::vector<int> time_list;
	if (IsAllTime(result_col, m_result_tm->GetSelection())) {
		int ts = project->GetTableInt()->GetTimeSteps();
		time_list.resize(ts);
		for (int i=0; i<ts; i++) time_list[i] = i;
	} else {
		int tm = IsTimeVariant(result_col) ? m_result_tm->GetSelection() : 0;
		time_list.resize(1);
		time_list[0] = tm;
	}
	
	const int obs = table_int->GetNumberRows();
	
	bool Event_undefined = false;
	if (IsAllTime(cop1, m_event_tm->GetSelection())) {
		b_array_type undefined;
		table_int->GetColUndefined(cop1, undefined);
		int ts = project->GetTableInt()->GetTimeSteps();
		for (int t=0; t<ts && !Event_undefined; t++) {
			for (int i=0; i<obs && !Event_undefined; i++) {
				if (undefined[t][i]) Event_undefined = true;
			}
		}
	} else {
		std::vector<bool> undefined(obs);
		int tm = IsTimeVariant(cop1) ? m_event_tm->GetSelection() : 0;
		table_int->GetColUndefined(cop1, tm, undefined);
		for (int i=0; i<obs && !Event_undefined; i++) {
			if (undefined[i]) Event_undefined = true;
		}		
	}
	if (Event_undefined) {
		wxString msg("Event field has undefined values.  Please define "
					 "missing values or choose a different field.");
		wxMessageDialog dlg (this, msg, "Error", wxOK | wxICON_ERROR);
		dlg.ShowModal();
		return;
	}

	// for 
	HighlightState* highlight_state = project->GetHighlightState();
	std::vector<bool>& hs = highlight_state->GetHighlight();
	std::vector<bool> hs_backup = hs;

	for (int i=0; i<obs; i++) {
		hs[i] = true;
	}

	bool Base_undefined = false;
	if (IsAllTime(cop2, m_base_tm->GetSelection())) {
		b_array_type undefined;
		table_int->GetColUndefined(cop2, undefined);
		int ts = project->GetTableInt()->GetTimeSteps();
		for (int t=0; t<ts && !Base_undefined; t++) {
			for (int i=0; i<obs && !Base_undefined; i++) {
				if (undefined[t][i]) {
					Base_undefined = true;
					hs[i] = false;
				}
			}
		}
	} else {
		std::vector<bool> undefined(obs);
		int tm = IsTimeVariant(cop2) ? m_base_tm->GetSelection() : 0;
		table_int->GetColUndefined(cop2, tm, undefined);
		for (int i=0; i<obs && !Base_undefined; i++) {
			if (undefined[i]) {
				Base_undefined = true;
				hs[i] = false;
			}
		}
	}
	if (Base_undefined) {
		wxString msg("Base field has undefined values. Do you want to "
                     "save a subset without undefined values as a new "
                     "shape file? or please define "
					 "missing values or choose a different field.");
		wxMessageDialog dlg (this, msg, "Error", 
                             wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
        if (dlg.ShowModal() == wxID_YES) {
			ExportDataDlg dlg(this, project, true);
			dlg.ShowModal();
        }
		hs = hs_backup;
		return;
	}

	for (int i=0; i<obs; i++) {
		hs[i] = true;
	}

	bool Base_non_positive = false;
	if (IsAllTime(cop2, m_base_tm->GetSelection())) {
		d_array_type data;
		table_int->GetColData(cop2, data);
		int ts = project->GetTableInt()->GetTimeSteps();
		for (int t=0; t<ts && !Base_non_positive; t++) {
			for (int i=0; i<obs && !Base_non_positive; i++) {
				if (data[t][i] <= 0) {
					Base_non_positive = true;
					hs[i] = false;
				}
			}
		}
	} else {
		std::vector<double> data(obs);
		int tm = IsTimeVariant(cop2) ? m_base_tm->GetSelection() : 0;
		table_int->GetColData(cop2, tm, data);
		for (int i=0; i<obs && !Base_non_positive; i++) {
			if (data[i] <= 0) {
				Base_non_positive = true;
				hs[i] = false;
			}
		}
	}
	if (Base_non_positive) {
		wxString msg("Base field has zero or negative values, but all base "
					 "values must be strictly greater than zero. Do you want "
                     "to save a subset of non-zeros as a new shape file? ");
		wxMessageDialog dlg (this, msg, "Error", 
                             wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
		if (dlg.ShowModal() == wxID_YES) {
			ExportDataDlg exp_dlg(this, project, true);
			exp_dlg.ShowModal();
        }
		hs = hs_backup;
		return;
	}
	hs = hs_backup;

	bool has_undefined = false;
	double* B = new double[obs]; // Base variable vector == cop2
	double* E = new double[obs]; // Event variable vector == cop1
	double* r = new double[obs]; // result vector
	std::vector<double> data(obs);

	if (!IsAllTime(cop2, m_base_tm->GetSelection())) {
		int tm = IsTimeVariant(cop2) ? m_base_tm->GetSelection() : 0;
		table_int->GetColData(cop2, tm, data);
		for (int i=0; i<obs; i++) B[i] = data[i];
	}
	if (!IsAllTime(cop1, m_event_tm->GetSelection())) {
		int tm = IsTimeVariant(cop1) ? m_event_tm->GetSelection() : 0;
		table_int->GetColData(cop1, tm, data);
		for (int i=0; i<obs; i++) E[i] = data[i];
	}
	
	for (int t=0; t<time_list.size(); t++) {
		if (IsAllTime(cop2, m_base_tm->GetSelection())) {
			table_int->GetColData(cop2, time_list[t], data);
			for (int i=0; i<obs; i++) B[i] = data[i];
		}
		if (IsAllTime(cop1, m_event_tm->GetSelection())) {
			table_int->GetColData(cop1, time_list[t], data);
			for (int i=0; i<obs; i++) E[i] = data[i];
		}
		for (int i=0; i<obs; i++) r[i] = -9999;
	
		std::vector<bool> undef_r;
		switch (op) {
			case 0:
				GdaAlgs::RateSmoother_RawRate(obs, B, E, r, undef_r);
				break;
			case 1:
				GdaAlgs::RateSmoother_ExcessRisk(obs, B, E, r, undef_r);
				break;
			case 2:
				GdaAlgs::RateSmoother_EBS(obs, B, E, r, undef_r);
				break;
			case 3:
				has_undefined = GdaAlgs::RateSmoother_SRS(obs, w_man_int,
														  weights_id, B, E, r,
														  undef_r);
				break;
			case 4:
				has_undefined = GdaAlgs::RateSmoother_SEBS(obs, w_man_int,
														   weights_id, B, E, r,
														   undef_r);
				break;
			case 5:
				GdaAlgs::RateStandardizeEB(obs, B, E, r, undef_r);
				break;
			default:
				break;
		}
	
		for (int i=0; i<obs; i++) data[i] = r[i];
		table_int->SetColData(result_col, time_list[t], data);
		table_int->SetColUndefined(result_col, time_list[t], undef_r);

	}
	
	if (B) delete [] B; B = NULL;
	if (E) delete [] E; E = NULL;
	if (r) delete [] r; r = NULL;
	
	if (has_undefined) {
		wxString msg("Some calculated values were undefined and this is "
					 "most likely due to neighborless observations in the "
					 "weight matrix. Rate calculation successful for "
					 "observations with neighbors.");
		wxMessageDialog dlg (this, msg, "Success / Warning",
							 wxOK | wxICON_INFORMATION);
		dlg.ShowModal();
	} else {
		wxString msg("Rate calculation successful.");
		wxMessageDialog dlg (this, msg, "Success", wxOK | wxICON_INFORMATION);
		dlg.ShowModal();
	}
}