public static float[,] CalcGradientMap(float[,] residuals, Complex[,] psfCorrelation, Rectangle psfSize) { var yPadding = psfSize.YEnd; var xPadding = psfSize.XEnd; var resPadded = Pad(residuals, yPadding, xPadding); var ResPAdded = FFT.Forward(resPadded, 1.0); var B = Fourier2D.Multiply(ResPAdded, psfCorrelation); var bPadded = FFT.BackwardFloat(B, (double)(B.GetLength(0) * B.GetLength(1))); var bMap = RemovePadding(bPadded, yPadding, xPadding); return(bMap); }
public static void DebugILGPU() { var frequencies = FitsIO.ReadFrequencies(@"C:\dev\GitHub\p9-data\small\fits\simulation_point\freq.fits"); var uvw = FitsIO.ReadUVW(@"C:\dev\GitHub\p9-data\small\fits\simulation_point\uvw.fits"); var flags = new bool[uvw.GetLength(0), uvw.GetLength(1), frequencies.Length]; //completely unflagged dataset double norm = 2.0; var visibilities = FitsIO.ReadVisibilities(@"C:\dev\GitHub\p9-data\small\fits\simulation_point\vis.fits", uvw.GetLength(0), uvw.GetLength(1), frequencies.Length, norm); var visibilitiesCount = visibilities.Length; int gridSize = 256; int subgridsize = 8; int kernelSize = 4; int max_nr_timesteps = 1024; double cellSize = 1.0 / 3600.0 * PI / 180.0; var c = new GriddingConstants(visibilitiesCount, gridSize, subgridsize, kernelSize, max_nr_timesteps, (float)cellSize, 1, 0.0f); var watchTotal = new Stopwatch(); var watchForward = new Stopwatch(); var watchBackwards = new Stopwatch(); var watchDeconv = new Stopwatch(); watchTotal.Start(); var metadata = Partitioner.CreatePartition(c, uvw, frequencies); var psfGrid = IDG.GridPSF(c, metadata, uvw, flags, frequencies); var psf = FFT.Backward(psfGrid, c.VisibilitiesCount); FFT.Shift(psf); var psfCutDouble = CutImg(psf); var psfCut = ToFloatImage(psfCutDouble); FitsIO.Write(psfCut, "psfCut.fits"); var totalSize = new Rectangle(0, 0, gridSize, gridSize); var imageSection = new Rectangle(0, 128, gridSize, gridSize); var bMapCalculator = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psfCut, totalSize), new Rectangle(0, 0, psfCut.GetLength(0), psfCut.GetLength(1))); var fastCD = new FastSerialCD(totalSize, psfCut); fastCD.ResetLipschitzMap(ToFloatImage(psf)); var gpuCD = new GPUSerialCD(totalSize, psfCut, 100); var lambda = 0.5f * fastCD.MaxLipschitz; var alpha = 0.8f; var xImage = new float[gridSize, gridSize]; var residualVis = visibilities; /*var truth = new double[gridSize, gridSize]; * truth[30, 30] = 1.0; * truth[35, 36] = 1.5; * var truthVis = IDG.ToVisibilities(c, metadata, truth, uvw, frequencies); * visibilities = truthVis; * var residualVis = truthVis;*/ for (int cycle = 0; cycle < 4; cycle++) { //FORWARD watchForward.Start(); var dirtyGrid = IDG.Grid(c, metadata, residualVis, uvw, frequencies); var dirtyImage = FFT.BackwardFloat(dirtyGrid, c.VisibilitiesCount); FFT.Shift(dirtyImage); FitsIO.Write(dirtyImage, "dirty_" + cycle + ".fits"); watchForward.Stop(); //DECONVOLVE watchDeconv.Start(); bMapCalculator.ConvolveInPlace(dirtyImage); FitsIO.Write(dirtyImage, "bMap_" + cycle + ".fits"); //var result = fastCD.Deconvolve(xImage, dirtyImage, lambda, alpha, 1000, 1e-4f); var result = gpuCD.Deconvolve(xImage, dirtyImage, lambda, alpha, 1000, 1e-4f); if (result.Converged) { Console.WriteLine("-----------------------------CONVERGED!!!!------------------------"); } else { Console.WriteLine("-------------------------------not converged----------------------"); } FitsIO.Write(xImage, "xImageGreedy" + cycle + ".fits"); FitsIO.Write(dirtyImage, "residualDebug_" + cycle + ".fits"); watchDeconv.Stop(); //BACKWARDS watchBackwards.Start(); FFT.Shift(xImage); var xGrid = FFT.Forward(xImage); FFT.Shift(xImage); var modelVis = IDG.DeGrid(c, metadata, xGrid, uvw, frequencies); residualVis = Visibilities.Substract(visibilities, modelVis, flags); watchBackwards.Stop(); var hello = FFT.Forward(xImage, 1.0); hello = Common.Fourier2D.Multiply(hello, psfGrid); var hImg = FFT.Backward(hello, (double)(128 * 128)); //FFT.Shift(hImg); FitsIO.Write(hImg, "modelDirty_FFT.fits"); var imgRec = IDG.ToImage(c, metadata, modelVis, uvw, frequencies); FitsIO.Write(imgRec, "modelDirty" + cycle + ".fits"); } }
public static void DebugSimulatedApprox() { var frequencies = FitsIO.ReadFrequencies(@"C:\dev\GitHub\p9-data\small\fits\simulation_point\freq.fits"); var uvw = FitsIO.ReadUVW(@"C:\dev\GitHub\p9-data\small\fits\simulation_point\uvw.fits"); var flags = new bool[uvw.GetLength(0), uvw.GetLength(1), frequencies.Length]; //completely unflagged dataset double norm = 2.0; var visibilities = FitsIO.ReadVisibilities(@"C:\dev\GitHub\p9-data\small\fits\simulation_point\vis.fits", uvw.GetLength(0), uvw.GetLength(1), frequencies.Length, norm); var visibilitiesCount = visibilities.Length; int gridSize = 256; int subgridsize = 8; int kernelSize = 4; int max_nr_timesteps = 1024; double cellSize = 1.0 / 3600.0 * PI / 180.0; var c = new GriddingConstants(visibilitiesCount, gridSize, subgridsize, kernelSize, max_nr_timesteps, (float)cellSize, 1, 0.0f); var watchTotal = new Stopwatch(); var watchForward = new Stopwatch(); var watchBackwards = new Stopwatch(); var watchDeconv = new Stopwatch(); watchTotal.Start(); var metadata = Partitioner.CreatePartition(c, uvw, frequencies); var psfGrid = IDG.GridPSF(c, metadata, uvw, flags, frequencies); var psf = FFT.BackwardFloat(psfGrid, c.VisibilitiesCount); FFT.Shift(psf); var psfCut = PSF.Cut(psf); FitsIO.Write(psfCut, "psfCut.fits"); var random = new Random(123); var totalSize = new Rectangle(0, 0, gridSize, gridSize); var bMapCalculator = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psfCut, totalSize), new Rectangle(0, 0, psfCut.GetLength(0), psfCut.GetLength(1))); var fastCD = new FastSerialCD(totalSize, psfCut); //fastCD.ResetAMap(psf); var lambda = 0.5f * fastCD.MaxLipschitz; var alpha = 0.8f; var approx = new ApproxParallel(); var approx2 = new ApproxFast(totalSize, psfCut, 4, 8, 0f, 0.25f, false, true); var xImage = new float[gridSize, gridSize]; var residualVis = visibilities; /*var truth = new double[gridSize, gridSize]; * truth[30, 30] = 1.0; * truth[35, 36] = 1.5; * var truthVis = IDG.ToVisibilities(c, metadata, truth, uvw, frequencies); * visibilities = truthVis; * var residualVis = truthVis;*/ var data = new ApproxFast.TestingData(new StreamWriter("approxConvergence.txt")); for (int cycle = 0; cycle < 4; cycle++) { //FORWARD watchForward.Start(); var dirtyGrid = IDG.Grid(c, metadata, residualVis, uvw, frequencies); var dirtyImage = FFT.BackwardFloat(dirtyGrid, c.VisibilitiesCount); FFT.Shift(dirtyImage); FitsIO.Write(dirtyImage, "dirty_" + cycle + ".fits"); watchForward.Stop(); //DECONVOLVE watchDeconv.Start(); //approx.ISTAStep(xImage, dirtyImage, psf, lambda, alpha); //FitsIO.Write(xImage, "xIsta.fits"); //FitsIO.Write(dirtyImage, "dirtyFista.fits"); //bMapCalculator.ConvolveInPlace(dirtyImage); //FitsIO.Write(dirtyImage, "bMap_" + cycle + ".fits"); //var result = fastCD.Deconvolve(xImage, dirtyImage, 0.5f * fastCD.MaxLipschitz, 0.8f, 1000, 1e-4f); //var converged = approx.DeconvolveActiveSet(xImage, dirtyImage, psfCut, lambda, alpha, random, 8, 1, 1); //var converged = approx.DeconvolveGreedy(xImage, dirtyImage, psfCut, lambda, alpha, random, 4, 4, 500); //var converged = approx.DeconvolveApprox(xImage, dirtyImage, psfCut, lambda, alpha, random, 1, threads, 500, 1e-4f, cycle == 0); approx2.DeconvolveTest(data, cycle, 0, xImage, dirtyImage, psfCut, psf, lambda, alpha, random, 10, 1e-4f); if (data.converged) { Console.WriteLine("-----------------------------CONVERGED!!!!------------------------"); } else { Console.WriteLine("-------------------------------not converged----------------------"); } FitsIO.Write(xImage, "xImageApprox_" + cycle + ".fits"); watchDeconv.Stop(); //BACKWARDS watchBackwards.Start(); FFT.Shift(xImage); var xGrid = FFT.Forward(xImage); FFT.Shift(xImage); var modelVis = IDG.DeGrid(c, metadata, xGrid, uvw, frequencies); residualVis = Visibilities.Substract(visibilities, modelVis, flags); watchBackwards.Stop(); } var dirtyGridCheck = IDG.Grid(c, metadata, residualVis, uvw, frequencies); var dirtyCheck = FFT.Backward(dirtyGridCheck, c.VisibilitiesCount); FFT.Shift(dirtyCheck); var l2Penalty = Residuals.CalcPenalty(ToFloatImage(dirtyCheck)); var elasticPenalty = ElasticNet.CalcPenalty(xImage, (float)lambda, (float)alpha); var sum = l2Penalty + elasticPenalty; data.writer.Close(); }