private void CalculateElements(IDataset dataset, IDependencyCalculator calc, string partition, string variable = null, int frames = 0, double[,] alreadyCalculated = null) { var indices = GetRelevantIndices(problemData, partition); bwInfo = new BackgroundWorkerInfo { Dataset = dataset, Calculator = calc, Partition = partition, Indices = indices, Variable = variable, Frames = frames, AlreadyCalculated = alreadyCalculated }; if (bw == null) { bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.WorkerSupportsCancellation = true; bw.DoWork += new DoWorkEventHandler(BwDoWork); bw.ProgressChanged += new ProgressChangedEventHandler(BwProgressChanged); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BwRunWorkerCompleted); } if (bw.IsBusy) { bw.CancelAsync(); } else { bw.RunWorkerAsync(bwInfo); } }
protected override void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument; var dataset = bwInfo.Dataset; var indices = bwInfo.Indices.ToArray(); IDependencyCalculator calc = bwInfo.Calculator; string variable = bwInfo.Variable; int frames = bwInfo.Frames; double[,] alreadyCalculated = bwInfo.AlreadyCalculated; IList <string> doubleVariableNames = dataset.DoubleVariables.ToList(); int length = doubleVariableNames.Count; double[,] elements = new double[length, frames + 1]; double calculations = (frames + 1) * length; worker.ReportProgress(0); int start = 0; if (alreadyCalculated != null) { for (int i = 0; i < alreadyCalculated.GetLength(0); i++) { Array.Copy(alreadyCalculated, i * alreadyCalculated.GetLength(1), elements, i * elements.GetLength(1), alreadyCalculated.GetLength(1)); } start = alreadyCalculated.GetLength(1); } var var1 = dataset.GetDoubleValues(variable, indices).ToArray(); for (int i = 0; i < length; i++) { for (int j = start; j <= frames; j++) { if (worker.CancellationPending) { worker.ReportProgress(100); e.Cancel = true; return; } IEnumerable <double> var2 = dataset.GetDoubleValues(doubleVariableNames[i], indices); var error = OnlineCalculatorError.None; elements[i, j] = calc.Calculate(var1.Skip(j), var2.Take(var1.Length - j), out error); if (!error.Equals(OnlineCalculatorError.None)) { elements[i, j] = double.NaN; } worker.ReportProgress((int)((100.0 / calculations) * (i * (frames + 1) + j + 1))); } } e.Result = elements; worker.ReportProgress(100); }
public void CalculateElements(IDataAnalysisProblemData problemData, IDependencyCalculator calc, string partition, bool ignoreMissingValues) { var indices = GetRelevantIndices(problemData, partition); var info = new BackgroundWorkerInfo { Dataset = problemData.Dataset, Calculator = calc, Partition = partition, Indices = indices, IgnoreMissingValues = ignoreMissingValues }; StartCalculation(info); }
public void TryCancelCalculation() { if (bw != null && bw.IsBusy) { bwInfo = null; bw.CancelAsync(); } }
protected void StartCalculation(BackgroundWorkerInfo info) { bwInfo = info; if (backgroundWorker.IsBusy) { backgroundWorker.CancelAsync(); return; } backgroundWorker.RunWorkerAsync(bwInfo); }
public void CalculateElements(IDataAnalysisProblemData problemData, IDependencyCalculator calc, string partition, bool ignoreMissingValues) { var indices = GetRelevantIndices(problemData, partition); var info = new BackgroundWorkerInfo { Dataset = problemData.Dataset, Calculator = calc, Partition = partition, Indices = indices, IgnoreMissingValues = ignoreMissingValues }; StartCalculation(info); }
public void TryCancelCalculation() { if (!backgroundWorker.IsBusy) { return; } bwInfo = null; backgroundWorker.CancelAsync(); }
// returns true if any calculation takes place public bool CalculateTimeframeElements(IDataAnalysisProblemData problemData, IDependencyCalculator calc, string partition, string variable, int frames, double[,] correlation = null) { if (correlation != null && correlation.GetLength(1) > frames) return false; var indices = GetRelevantIndices(problemData, partition); var info = new BackgroundWorkerInfo { Dataset = problemData.Dataset, Calculator = calc, Partition = partition, Indices = indices, Variable = variable, Frames = frames, AlreadyCalculated = correlation }; StartCalculation(info); return true; }
protected void StartCalculation(BackgroundWorkerInfo info) { bwInfo = info; if (backgroundWorker.IsBusy) { backgroundWorker.CancelAsync(); return; } backgroundWorker.RunWorkerAsync(bwInfo); }
protected override void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument; var dataset = bwInfo.Dataset; var indices = bwInfo.Indices.ToArray(); IDependencyCalculator calc = bwInfo.Calculator; IList <string> doubleVariableNames = dataset.DoubleVariables.ToList(); int length = doubleVariableNames.Count; double[,] elements = new double[length, length]; worker.ReportProgress(0); for (int counter = 0; counter < length; counter++) { if (worker.CancellationPending) { worker.ReportProgress(100); e.Cancel = true; return; } var i = counter; Parallel.ForEach(Enumerable.Range(i, length - i), j => { var var1 = dataset.GetDoubleValues(doubleVariableNames[i], indices); var var2 = dataset.GetDoubleValues(doubleVariableNames[j], indices); OnlineCalculatorError error = OnlineCalculatorError.None; if (bwInfo.IgnoreMissingValues) { var filtered = FilterNaNValues(var1, var2); elements[i, j] = calc.Calculate(filtered, out error); } else { elements[i, j] = calc.Calculate(var1, var2, out error); } if (!error.Equals(OnlineCalculatorError.None)) { elements[i, j] = double.NaN; } elements[j, i] = elements[i, j]; }); worker.ReportProgress((int)(((double)counter) / length * 100)); } e.Result = elements; worker.ReportProgress(100); }
private void BwDoWork(object sender, DoWorkEventArgs e) { BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument; if (bwInfo.Variable == null) { BwCalculateCorrelation(sender, e); } else { BwCalculateTimeframeCorrelation(sender, e); } }
// returns true if any calculation takes place public bool CalculateTimeframeElements(IDataAnalysisProblemData problemData, IDependencyCalculator calc, string partition, string variable, int frames, double[,] correlation = null) { if (correlation != null && correlation.GetLength(1) > frames) { return(false); } var indices = GetRelevantIndices(problemData, partition); var info = new BackgroundWorkerInfo { Dataset = problemData.Dataset, Calculator = calc, Partition = partition, Indices = indices, Variable = variable, Frames = frames, AlreadyCalculated = correlation }; StartCalculation(info); return(true); }
private void BwCalculateCorrelation(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; BackgroundWorkerInfo bwInfo = (BackgroundWorkerInfo)e.Argument; var dataset = bwInfo.Dataset; IEnumerable <int> indices = bwInfo.Indices; IDependencyCalculator calc = bwInfo.Calculator; IList <string> doubleVariableNames = dataset.DoubleVariables.ToList(); OnlineCalculatorError error = OnlineCalculatorError.None; int length = doubleVariableNames.Count; double[,] elements = new double[length, length]; double calculations = (Math.Pow(length, 2) + length) / 2; worker.ReportProgress(0); for (int i = 0; i < length; i++) { for (int j = 0; j < i + 1; j++) { if (worker.CancellationPending) { worker.ReportProgress(100); e.Cancel = true; return; } IEnumerable <double> var1 = problemData.Dataset.GetDoubleValues(doubleVariableNames[i], indices); IEnumerable <double> var2 = problemData.Dataset.GetDoubleValues(doubleVariableNames[j], indices); elements[i, j] = calc.Calculate(var1, var2, out error); if (!error.Equals(OnlineCalculatorError.None)) { elements[i, j] = double.NaN; } elements[j, i] = elements[i, j]; worker.ReportProgress((int)Math.Round((((Math.Pow(i, 2) + i) / 2 + j + 1.0) / calculations) * 100)); } } e.Result = elements; worker.ReportProgress(100); }
private void CalculateElements(IDataset dataset, IDependencyCalculator calc, string partition, string variable = null, int frames = 0, double[,] alreadyCalculated = null) { var indices = GetRelevantIndices(problemData, partition); bwInfo = new BackgroundWorkerInfo { Dataset = dataset, Calculator = calc, Partition = partition, Indices = indices, Variable = variable, Frames = frames, AlreadyCalculated = alreadyCalculated }; if (bw == null) { bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.WorkerSupportsCancellation = true; bw.DoWork += new DoWorkEventHandler(BwDoWork); bw.ProgressChanged += new ProgressChangedEventHandler(BwProgressChanged); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BwRunWorkerCompleted); } if (bw.IsBusy) { bw.CancelAsync(); } else { bw.RunWorkerAsync(bwInfo); } }
public void TryCancelCalculation() { if (!backgroundWorker.IsBusy) return; bwInfo = null; backgroundWorker.CancelAsync(); }
public void TryCancelCalculation() { if (bw != null && bw.IsBusy) { bwInfo = null; bw.CancelAsync(); } }