void RemoveConstantColumns(object sender, EventArgs e) { IDataset ds = App.ScriptApp.Dataset; List <string> columnIdList = new List <string>(ds.Columns); Dictionary <object, bool> vTable = new Dictionary <object, bool>(); IList <IColumnSpec> csList = ds.ColumnSpecList; // notice that ds.ColumnSpecList constructs the list on-fly. for (int col = 0; col < ds.Columns; col++) { vTable.Clear(); for (int row = 0; row < ds.Rows; row++) { object v = ds.GetValueAt(row, col); if (!vTable.ContainsKey(v)) { vTable.Add(v, true); } if (vTable.Count > 1) { break; } } if (vTable.Count == 1) { columnIdList.Add(csList[col].Id); } } int removed = 0; removed = ds.RemoveColumns(columnIdList); if (removed > 0) { ds.CommitChanges(); MessageBox.Show("Removed " + removed + " columns!\nNew table dimension: " + ds.Rows + " rows; " + ds.Columns + " columns."); } else { MessageBox.Show("No column have been removed"); } }
void RemoveCorruptRows(object sender, EventArgs e) { IDataset ds = App.ScriptApp.Dataset; List <string> rowIdList = new List <string>(); IList <IColumnSpec> csList = ds.ColumnSpecList; // notice that ds.ColumnSpecList constructs the list on-fly. var bodyList = ds.BodyList; for (int row = 0; row < ds.Rows; row++) { for (int col = 0; col < ds.Columns; col++) { if (csList[col].IsEnumerate) { continue; } double v = (double)ds.GetValueAt(row, col); if (double.IsNaN(v) || double.IsInfinity(v) || Math.Abs(v) > LargeValue) { rowIdList.Add(bodyList[row].Id); break; } } } if (rowIdList.Count > 0) { var ret = MessageBox.Show("Found " + rowIdList.Count + " rows with corrupt values!\nRemove them?", "Remove corrupt data", MessageBoxButtons.YesNo); if (ret == DialogResult.No) { return; } int removed = ds.RemoveRows(rowIdList); ds.CommitChanges(); MessageBox.Show("Removed " + removed + " rows!\nNew table dimension: " + ds.Rows + " rows; " + ds.Columns + " columns."); } else { MessageBox.Show("No corrupt rows have been found."); } }
public static void Synchronize3D() { IDataset ds = App.ScriptApp.Dataset; if ((ds.Columns != 3) && (ds.Columns != 2)) { MessageBox.Show("The dataset must have 2 or 3 numerical columns!"); return; } IList <IBody> bodies = ds.BodyList; for (int i = 0; i < bodies.Count; i++) { IBody b = bodies[i]; ds.SetDataAt(i, 0, b.X.ToString("g4")); ds.SetDataAt(i, 1, b.Y.ToString("g4")); if (ds.Columns == 3) { ds.SetDataAt(i, 2, b.Z.ToString("g4")); } } ds.CommitChanges(); }
void RemoveAllZeros(object sender, EventArgs e) { IDataset ds = App.ScriptApp.Dataset; List <string> zRows = new List <string>(); List <string> zColumns = new List <string>(); var csList = ds.ColumnSpecList; var rsList = ds.BodyList; for (int row = 0; row < ds.Rows; row++) { bool allZero = true; for (int col = 0; col < ds.Columns; col++) { if (!csList[col].IsNumber) { allZero = false; break; } else { double v = (double)ds.GetValueAt(row, col); if (v != 0) { allZero = false; break; } } } if (allZero) { zRows.Add(rsList[row].Id); } } for (int col = 0; col < ds.Columns; col++) { if (!csList[col].IsNumber) { continue; } bool allZero = true; for (int row = 0; row < ds.Rows; row++) { double v = (double)ds.GetValueAt(row, col); if (v != 0) { allZero = false; break; } } if (allZero) { zColumns.Add(csList[col].Id); } } if ((zRows.Count > 0) || (zColumns.Count > 0)) { var ret = MessageBox.Show("Found " + zRows.Count + " rows and " + zColumns.Count + " columns.\n" + "Remove all of them?", "Remove All-Zeros Rows and Columns", MessageBoxButtons.YesNo); if (ret == DialogResult.Yes) { ds.RemoveRows(zRows); ds.RemoveColumns(zColumns); ds.CommitChanges(); } } else { MessageBox.Show("No all-zero rows or columns have been found!"); } }