private static ReconstructionInfo ReconstructSimple(Data input, float[,] fullPsf, string folder, int cutFactor, int maxMajor, string dirtyPrefix, string xImagePrefix, StreamWriter writer, double objectiveCutoff, float epsilon, bool startWithFullPSF)
        {
            var info           = new ReconstructionInfo();
            var psfCut         = PSF.Cut(fullPsf, cutFactor);
            var maxSidelobe    = PSF.CalcMaxSidelobe(fullPsf, cutFactor);
            var totalSize      = new Rectangle(0, 0, input.c.GridSize, input.c.GridSize);
            var psfBMap        = startWithFullPSF ? fullPsf : psfCut;
            var bMapCalculator = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psfBMap, totalSize), new Rectangle(0, 0, psfBMap.GetLength(0), psfBMap.GetLength(1)));
            var fastCD         = new FastSerialCD(totalSize, psfCut);

            if (startWithFullPSF)
            {
                fastCD.ResetLipschitzMap(fullPsf);
            }
            var referenceCD = new FastSerialCD(totalSize, psfCut);

            referenceCD.ResetLipschitzMap(fullPsf);
            FitsIO.Write(psfCut, folder + cutFactor + "psf.fits");

            var lambda     = (float)(LAMBDA_GLOBAL * PSF.CalcMaxLipschitz(psfBMap));
            var lambdaTrue = (float)(LAMBDA_GLOBAL * PSF.CalcMaxLipschitz(fullPsf));

            var xImage      = new float[input.c.GridSize, input.c.GridSize];
            var residualVis = input.visibilities;
            DeconvolutionResult lastResult = null;

            for (int cycle = 0; cycle < maxMajor; cycle++)
            {
                Console.WriteLine("cycle " + cycle);
                var dirtyGrid  = IDG.GridW(input.c, input.metadata, residualVis, input.uvw, input.frequencies);
                var dirtyImage = FFT.WStackIFFTFloat(dirtyGrid, input.c.VisibilitiesCount);
                FFT.Shift(dirtyImage);
                FitsIO.Write(dirtyImage, folder + dirtyPrefix + cycle + ".fits");

                //calc data and reg penalty
                var dataPenalty       = Residuals.CalcPenalty(dirtyImage);
                var regPenalty        = ElasticNet.CalcPenalty(xImage, lambdaTrue, alpha);
                var regPenaltyCurrent = ElasticNet.CalcPenalty(xImage, lambda, alpha);
                info.lastDataPenalty = dataPenalty;
                info.lastRegPenalty  = regPenalty;

                var maxDirty = Residuals.GetMax(dirtyImage);
                bMapCalculator.ConvolveInPlace(dirtyImage);
                FitsIO.Write(dirtyImage, folder + dirtyPrefix + "bmap_" + cycle + ".fits");
                var maxB             = Residuals.GetMax(dirtyImage);
                var correctionFactor = Math.Max(maxB / (maxDirty * fastCD.MaxLipschitz), 1.0f);
                var currentSideLobe  = maxB * maxSidelobe * correctionFactor;
                var currentLambda    = Math.Max(currentSideLobe / alpha, lambda);

                writer.Write(cycle + ";" + currentLambda + ";" + currentSideLobe + ";" + referenceCD.GetAbsMaxDiff(xImage, dirtyImage, lambdaTrue, alpha) + ";" + dataPenalty + ";" + regPenalty + ";" + regPenaltyCurrent + ";");
                writer.Flush();

                //check wether we can minimize the objective further with the current psf
                var objectiveReached = (dataPenalty + regPenalty) < objectiveCutoff;
                var minimumReached   = (lastResult != null && referenceCD.GetAbsMaxDiff(xImage, dirtyImage, lambdaTrue, alpha) < MAJOR_EPSILON && lastResult.Converged);
                if (!objectiveReached & !minimumReached)
                {
                    info.totalDeconv.Start();
                    lastResult = fastCD.Deconvolve(xImage, dirtyImage, currentLambda, alpha, 30000, epsilon);
                    info.totalDeconv.Stop();

                    FitsIO.Write(xImage, folder + xImagePrefix + cycle + ".fits");
                    writer.Write(lastResult.Converged + ";" + lastResult.IterationCount + ";" + lastResult.ElapsedTime.TotalSeconds + "\n");
                    writer.Flush();

                    FFT.Shift(xImage);
                    var xGrid = FFT.Forward(xImage);
                    FFT.Shift(xImage);
                    var modelVis = IDG.DeGridW(input.c, input.metadata, xGrid, input.uvw, input.frequencies);
                    residualVis = Visibilities.Substract(input.visibilities, modelVis, input.flags);
                }
                else
                {
                    writer.Write(false + ";0;0");
                    writer.Flush();
                    break;
                }
            }

            bMapCalculator.Dispose();


            return(info);
        }
        public bool DeconvolveGreedy(float[,] xImage, float[,] residuals, float[,] psf, float lambda, float alpha, Random random, int blockSize, int threadCount, int maxIteration = 100, float epsilon = 1e-4f)
        {
            var xExplore    = Copy(xImage);
            var xCorrection = new float[xImage.GetLength(0), xImage.GetLength(1)];

            var PSFCorr = PSF.CalcPaddedFourierCorrelation(psf, new Rectangle(0, 0, residuals.GetLength(0), residuals.GetLength(1)));

            //calculate gradients for each pixel
            var gExplore    = Residuals.CalcGradientMap(residuals, PSFCorr, new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1)));
            var gCorrection = new float[residuals.GetLength(0), residuals.GetLength(1)];
            var psf2        = PSF.CalcPSFSquared(psf);

            FitsIO.Write(gExplore, "gExplore.fits");

            yBlockSize           = blockSize;
            xBlockSize           = blockSize;
            degreeOfSeperability = CountNonZero(psf);
            tau = threadCount; //number of processors
            var maxLipschitz = (float)PSF.CalcMaxLipschitz(psf);

            var theta = Greedy(xExplore, xCorrection, gExplore, gCorrection, psf2, maxLipschitz, lambda, alpha, random, maxIteration, epsilon);

            //decide which version should be taken#
            var CONVKernel          = PSF.CalcPaddedFourierConvolution(psf, new Rectangle(0, 0, residuals.GetLength(0), residuals.GetLength(1)));
            var residualsCalculator = new PaddedConvolver(CONVKernel, new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1)));
            var theta0   = tau / (float)(xExplore.Length / (yBlockSize * xBlockSize));
            var tmpTheta = theta < 1.0f ? ((theta * theta) / (1.0f - theta)) : theta0;

            //calculate residuals
            var residualsExplore     = Copy(xExplore);
            var residualsAccelerated = Copy(xExplore);

            for (int i = 0; i < xImage.GetLength(0); i++)
            {
                for (int j = 0; j < xImage.GetLength(1); j++)
                {
                    residualsExplore[i, j]     -= xImage[i, j];
                    residualsAccelerated[i, j] += tmpTheta * xCorrection[i, j] - xImage[i, j];
                    xCorrection[i, j]           = tmpTheta * xCorrection[i, j] + xExplore[i, j];
                }
            }

            FitsIO.Write(xExplore, "xExplore.fits");
            FitsIO.Write(xCorrection, "xAcc.fits");

            residualsCalculator.ConvolveInPlace(residualsExplore);
            residualsCalculator.ConvolveInPlace(residualsAccelerated);
            for (int i = 0; i < xImage.GetLength(0); i++)
            {
                for (int j = 0; j < xImage.GetLength(1); j++)
                {
                    residualsExplore[i, j]     -= residuals[i, j];
                    residualsAccelerated[i, j] -= residuals[i, j];
                }
            }

            var objectiveExplore = Residuals.CalcPenalty(residualsExplore) + ElasticNet.CalcPenalty(xExplore, lambda, alpha);
            var objectiveAcc     = Residuals.CalcPenalty(residualsAccelerated) + ElasticNet.CalcPenalty(xCorrection, lambda, alpha);

            if (objectiveAcc < objectiveExplore)
            {
                for (int i = 0; i < xImage.GetLength(0); i++)
                {
                    for (int j = 0; j < xImage.GetLength(1); j++)
                    {
                        xImage[i, j] = xCorrection[i, j];
                    }
                }
            }
            else
            {
                for (int i = 0; i < xImage.GetLength(0); i++)
                {
                    for (int j = 0; j < xImage.GetLength(1); j++)
                    {
                        xImage[i, j] = xExplore[i, j];
                    }
                }
            }

            return(objectiveAcc < objectiveExplore);
        }
        private static void ReconstructMinorCycle(MeasurementData input, GriddingConstants c, int cutFactor, float[,] fullPsf, string folder, string file, int minorCycles, float searchPercent, bool useAccelerated = true, int blockSize = 1, int maxCycle = 6)
        {
            var metadata = Partitioner.CreatePartition(c, input.UVW, input.Frequencies);

            var totalSize    = new Rectangle(0, 0, c.GridSize, c.GridSize);
            var psfCut       = PSF.Cut(fullPsf, cutFactor);
            var maxSidelobe  = PSF.CalcMaxSidelobe(fullPsf, cutFactor);
            var sidelobeHalf = PSF.CalcMaxSidelobe(fullPsf, 2);
            var random       = new Random(123);
            var approx       = new ApproxFast(totalSize, psfCut, 8, blockSize, 0.1f, searchPercent, false, useAccelerated);

            using (var bMapCalculator = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psfCut, totalSize), new Rectangle(0, 0, psfCut.GetLength(0), psfCut.GetLength(1))))
                using (var bMapCalculator2 = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(fullPsf, totalSize), new Rectangle(0, 0, fullPsf.GetLength(0), fullPsf.GetLength(1))))
                    using (var residualsConvolver = new PaddedConvolver(totalSize, fullPsf))
                    {
                        var currentBMapCalculator = bMapCalculator;

                        var maxLipschitz = PSF.CalcMaxLipschitz(psfCut);
                        var lambda       = (float)(LAMBDA * maxLipschitz);
                        var lambdaTrue   = (float)(LAMBDA * PSF.CalcMaxLipschitz(fullPsf));
                        var alpha        = ALPHA;
                        ApproxFast.LAMBDA_TEST = lambdaTrue;
                        ApproxFast.ALPHA_TEST  = alpha;

                        var switchedToOtherPsf = false;
                        var writer             = new StreamWriter(folder + "/" + file + "_lambda.txt");
                        var data        = new ApproxFast.TestingData(new StreamWriter(folder + "/" + file + ".txt"));
                        var xImage      = new float[c.GridSize, c.GridSize];
                        var residualVis = input.Visibilities;
                        for (int cycle = 0; cycle < maxCycle; cycle++)
                        {
                            Console.WriteLine("cycle " + cycle);
                            var dirtyGrid  = IDG.GridW(c, metadata, residualVis, input.UVW, input.Frequencies);
                            var dirtyImage = FFT.WStackIFFTFloat(dirtyGrid, c.VisibilitiesCount);
                            FFT.Shift(dirtyImage);
                            FitsIO.Write(dirtyImage, folder + "/dirty" + cycle + ".fits");

                            var minLambda     = 0.0f;
                            var dirtyCopy     = Copy(dirtyImage);
                            var xCopy         = Copy(xImage);
                            var currentLambda = 0f;
                            //var residualsConvolver = new PaddedConvolver(PSF.CalcPaddedFourierConvolution(fullPsf, totalSize), new Rectangle(0, 0, fullPsf.GetLength(0), fullPsf.GetLength(1)));
                            for (int minorCycle = 0; minorCycle < minorCycles; minorCycle++)
                            {
                                FitsIO.Write(dirtyImage, folder + "/dirtyMinor_" + minorCycle + ".fits");
                                var maxDirty         = Residuals.GetMax(dirtyImage);
                                var bMap             = currentBMapCalculator.Convolve(dirtyImage);
                                var maxB             = Residuals.GetMax(bMap);
                                var correctionFactor = Math.Max(maxB / (maxDirty * maxLipschitz), 1.0f);
                                var currentSideLobe  = maxB * maxSidelobe * correctionFactor;
                                currentLambda = (float)Math.Max(currentSideLobe / alpha, lambda);

                                if (minorCycle == 0)
                                {
                                    minLambda = (float)(maxB * sidelobeHalf * correctionFactor / alpha);
                                }

                                if (currentLambda < minLambda)
                                {
                                    currentLambda = minLambda;
                                }

                                writer.WriteLine(cycle + ";" + minorCycle + ";" + currentLambda + ";" + minLambda);
                                writer.Flush();
                                approx.DeconvolveTest(data, cycle, minorCycle, xImage, dirtyImage, psfCut, fullPsf, currentLambda, alpha, random, 15, 1e-5f);
                                FitsIO.Write(xImage, folder + "/xImageMinor_" + minorCycle + ".fits");

                                if (currentLambda == lambda | currentLambda == minLambda)
                                {
                                    break;
                                }

                                Console.WriteLine("resetting residuals!!");
                                //reset dirtyImage with full PSF
                                var residualsUpdate = new float[xImage.GetLength(0), xImage.GetLength(1)];
                                Parallel.For(0, xCopy.GetLength(0), (i) =>
                                {
                                    for (int j = 0; j < xCopy.GetLength(1); j++)
                                    {
                                        residualsUpdate[i, j] = xImage[i, j] - xCopy[i, j];
                                    }
                                });
                                residualsConvolver.ConvolveInPlace(residualsUpdate);

                                Parallel.For(0, xCopy.GetLength(0), (i) =>
                                {
                                    for (int j = 0; j < xCopy.GetLength(1); j++)
                                    {
                                        dirtyImage[i, j] = dirtyCopy[i, j] - residualsUpdate[i, j];
                                    }
                                });
                            }

                            if (currentLambda == lambda & !switchedToOtherPsf)
                            {
                                approx.ResetAMap(fullPsf);
                                currentBMapCalculator = bMapCalculator2;
                                lambda             = lambdaTrue;
                                switchedToOtherPsf = true;
                                writer.WriteLine("switched");
                                writer.Flush();
                            }

                            FitsIO.Write(xImage, folder + "/xImage_" + cycle + ".fits");

                            FFT.Shift(xImage);
                            var xGrid = FFT.Forward(xImage);
                            FFT.Shift(xImage);
                            var modelVis = IDG.DeGridW(c, metadata, xGrid, input.UVW, input.Frequencies);
                            residualVis = Visibilities.Substract(input.Visibilities, modelVis, input.Flags);
                        }

                        writer.Close();
                    }
        }
        public static bool Deconvolve2(double[,] xImage, double[,] residuals, double[,] psf, double lambda, double alpha, int blockSize, int maxIteration = 100, double epsilon = 1e-4)
        {
            var xImage2 = ToFloatImage(xImage);

            var PSFConvolution       = CommonDeprecated.PSF.CalcPaddedFourierConvolution(psf, residuals.GetLength(0), residuals.GetLength(1));
            var PSFCorrelation       = CommonDeprecated.PSF.CalculateFourierCorrelation(psf, residuals.GetLength(0), residuals.GetLength(1));
            var PSFSquared           = Fourier2D.Multiply(PSFConvolution, PSFCorrelation);
            var bMapCalculator       = new PaddedConvolver(PSFCorrelation, new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1)));
            var resUpdateCalculator  = new PaddedConvolver(PSFConvolution, new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1)));
            var bMapUpdateCalculator = new PaddedConvolver(PSFSquared, new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1)));

            var yBlockSize = blockSize;
            var xBlockSize = blockSize;

            var bMap = ToFloatImage(residuals);

            bMapCalculator.ConvolveInPlace(bMap);

            var xDiff   = new float[xImage.GetLength(0), xImage.GetLength(1)];
            var startL2 = NaiveGreedyCD.CalcDataObjective(residuals);

            var theta       = 2; //theta, also number of processors.
            var degreeOfSep = RandomCD.CountNonZero(psf);
            var blockCount  = xImage.Length / (yBlockSize * xBlockSize);
            var beta        = 1.0 + (degreeOfSep - 1.0) * (theta - 1.0) / (Math.Max(1.0, (blockCount - 1))); //arises from E.S.O of theta-nice sampling. Look at the original PCDM Paper for the explanation
            //Theta-nice sampling: take theta number of random pixels

            var lipschitz = RandomBlockCD2.ApproximateLipschitz(psf, yBlockSize, xBlockSize);

            lipschitz *= beta;
            lambda     = lambda / (yBlockSize * xBlockSize * beta);

            var iter = 0;

            while (iter < maxIteration)
            {
                bool containsNonZero = false;
                var  maxBlocks       = GetMaxBlocks(bMap, xImage2, lipschitz, (float)lambda, (float)alpha, yBlockSize, xBlockSize, theta);
                foreach (var b in maxBlocks)
                {
                    var yBlock = b.Item1;
                    var xBlock = b.Item2;
                    var block  = RandomBlockCD2.CopyFrom(bMap, yBlock, xBlock, yBlockSize, xBlockSize);

                    var update = block / lipschitz;

                    var xOld      = RandomBlockCD2.CopyFrom(xImage2, yBlock, xBlock, yBlockSize, xBlockSize);
                    var optimized = xOld + update;

                    //shrink
                    for (int j = 0; j < optimized.Count; j++)
                    {
                        optimized[j]     = CommonDeprecated.ShrinkElasticNet(optimized[j], lambda, alpha);
                        containsNonZero |= (optimized[j] - xOld[j]) != 0.0;
                    }

                    var optDiff = optimized - xOld;
                    RandomBlockCD2.AddInto(xDiff, optDiff, yBlock, xBlock, yBlockSize, xBlockSize);
                    RandomBlockCD2.AddInto(xImage2, optDiff, yBlock, xBlock, yBlockSize, xBlockSize);
                }

                if (containsNonZero)
                {
                    //FitsIO.Write(xImage2, "xImageBlock.fits");
                    //FitsIO.Write(xDiff, "xDiff.fits");

                    //update b-map
                    bMapUpdateCalculator.ConvolveInPlace(xDiff);
                    //FitsIO.Write(xDiff, "bMapUpdate.fits");
                    for (int i = 0; i < xDiff.GetLength(0); i++)
                    {
                        for (int j = 0; j < xDiff.GetLength(1); j++)
                        {
                            bMap[i, j] -= xDiff[i, j];
                            xDiff[i, j] = 0;
                        }
                    }
                    //FitsIO.Write(bMap, "bMap2.fits");

                    //calc residuals for debug purposes

                    /*if (maxBlock.Item3 < epsilon)
                     *  break;*/

                    //Console.WriteLine(maxBlock.Item3 + "\t yB = " + yB + "\t xB =" + xB);
                }
                iter++;
            }
            var elasticNet = 0.0;

            for (int i = 0; i < xImage.GetLength(0); i++)
            {
                for (int j = 0; j < xImage.GetLength(1); j++)
                {
                    xDiff[i, j]  = xImage2[i, j] - (float)xImage[i, j];
                    xImage[i, j] = xImage2[i, j];

                    elasticNet += lambda * 2 * lipschitz * GreedyBlockCD.ElasticNetPenalty(xImage2[i, j], (float)alpha);
                }
            }


            resUpdateCalculator.ConvolveInPlace(xDiff);
            //FitsIO.Write(xDiff, "residualsUpdate.fits");
            for (int i = 0; i < xDiff.GetLength(0); i++)
            {
                for (int j = 0; j < xDiff.GetLength(1); j++)
                {
                    residuals[i, j] -= xDiff[i, j];
                    xDiff[i, j]      = 0;
                }
            }
            var l2Penalty = NaiveGreedyCD.CalcDataObjective(residuals);

            Console.WriteLine("-------------------------");
            Console.WriteLine((l2Penalty + elasticNet));
            var io = System.IO.File.AppendText("penalty" + yBlockSize + ".txt");

            io.WriteLine("l2: " + l2Penalty + "\telastic: " + elasticNet + "\t " + (l2Penalty + elasticNet));
            io.Close();
            Console.WriteLine("-------------------------");

            return(false);
        }
Example #5
0
        /// <summary>
        /// Major cycle implemnentation for the parallel coordinate descent algorithm
        /// </summary>
        /// <param name="data"></param>
        /// <param name="c"></param>
        /// <param name="psfCutFactor"></param>
        /// <param name="maxMajorCycle"></param>
        /// <param name="maxMinorCycle"></param>
        /// <param name="lambda"></param>
        /// <param name="alpha"></param>
        /// <param name="deconvIterations"></param>
        /// <param name="deconvEpsilon"></param>
        public static void ReconstructPCDM(string obsName, MeasurementData data, GriddingConstants c, int psfCutFactor, int maxMajorCycle, int maxMinorCycle, float lambda, float alpha, int deconvIterations, float deconvEpsilon)
        {
            var metadata = Partitioner.CreatePartition(c, data.UVW, data.Frequencies);
            var psfVis   = new Complex[data.UVW.GetLength(0), data.UVW.GetLength(1), data.Frequencies.Length];

            for (int i = 0; i < data.Visibilities.GetLength(0); i++)
            {
                for (int j = 0; j < data.Visibilities.GetLength(1); j++)
                {
                    for (int k = 0; k < data.Visibilities.GetLength(2); k++)
                    {
                        if (!data.Flags[i, j, k])
                        {
                            psfVis[i, j, k] = new Complex(1.0, 0);
                        }
                        else
                        {
                            psfVis[i, j, k] = new Complex(0, 0);
                        }
                    }
                }
            }

            Console.WriteLine("gridding psf");
            var psfGrid = IDG.Grid(c, metadata, psfVis, data.UVW, data.Frequencies);
            var psf     = FFT.BackwardFloat(psfGrid, c.VisibilitiesCount);

            FFT.Shift(psf);

            var totalWatch   = new Stopwatch();
            var currentWatch = new Stopwatch();

            var totalSize    = new Rectangle(0, 0, c.GridSize, c.GridSize);
            var psfCut       = PSF.Cut(psf, psfCutFactor);
            var maxSidelobe  = PSF.CalcMaxSidelobe(psf, psfCutFactor);
            var sidelobeHalf = PSF.CalcMaxSidelobe(psf, 2);

            var pcdm = new ParallelCoordinateDescent(totalSize, psfCut, Environment.ProcessorCount, 1000);

            using (var gCalculator = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psfCut, totalSize), new Rectangle(0, 0, psfCut.GetLength(0), psfCut.GetLength(1))))
                using (var gCalculator2 = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psf, totalSize), new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1))))
                    using (var residualsConvolver = new PaddedConvolver(totalSize, psf))
                    {
                        var currentGCalculator = gCalculator;

                        var maxLipschitz    = PSF.CalcMaxLipschitz(psfCut);
                        var lambdaLipschitz = (float)(lambda * maxLipschitz);
                        var lambdaTrue      = (float)(lambda * PSF.CalcMaxLipschitz(psf));

                        var switchedToOtherPsf = false;
                        var xImage             = new float[c.GridSize, c.GridSize];
                        var residualVis        = data.Visibilities;
                        ParallelCoordinateDescent.PCDMStatistics lastResult = null;
                        for (int cycle = 0; cycle < maxMajorCycle; cycle++)
                        {
                            Console.WriteLine("Beginning Major cycle " + cycle);
                            var dirtyGrid  = IDG.GridW(c, metadata, residualVis, data.UVW, data.Frequencies);
                            var dirtyImage = FFT.WStackIFFTFloat(dirtyGrid, c.VisibilitiesCount);
                            FFT.Shift(dirtyImage);
                            FitsIO.Write(dirtyImage, obsName + "_dirty_pcdm_majorCycle" + cycle + ".fits");

                            currentWatch.Restart();
                            totalWatch.Start();

                            var breakMajor       = false;
                            var minLambda        = 0.0f;
                            var dirtyCopy        = Copy(dirtyImage);
                            var xCopy            = Copy(xImage);
                            var currentLambda    = 0f;
                            var currentObjective = 0.0;
                            var absMax           = 0.0f;
                            for (int minorCycle = 0; minorCycle < maxMinorCycle; minorCycle++)
                            {
                                Console.WriteLine("Beginning Minor Cycle " + minorCycle);
                                var maxDirty         = Residuals.GetMax(dirtyImage);
                                var bMap             = currentGCalculator.Convolve(dirtyImage);
                                var maxB             = Residuals.GetMax(bMap);
                                var correctionFactor = Math.Max(maxB / (maxDirty * maxLipschitz), 1.0f);
                                var currentSideLobe  = maxB * maxSidelobe * correctionFactor;
                                currentLambda = (float)Math.Max(currentSideLobe / alpha, lambdaLipschitz);

                                if (minorCycle == 0)
                                {
                                    minLambda = (float)(maxB * sidelobeHalf * correctionFactor / alpha);
                                }
                                if (currentLambda < minLambda)
                                {
                                    currentLambda = minLambda;
                                }
                                currentObjective = Residuals.CalcPenalty(dirtyImage) + ElasticNet.CalcPenalty(xImage, lambdaTrue, alpha);
                                absMax           = pcdm.GetAbsMax(xImage, bMap, lambdaTrue, alpha);
                                if (absMax < MAJOR_EPSILON)
                                {
                                    breakMajor = true;
                                    break;
                                }

                                lastResult = pcdm.Deconvolve(xImage, bMap, currentLambda, alpha, 40, deconvEpsilon);

                                if (currentLambda == lambda | currentLambda == minLambda)
                                {
                                    break;
                                }

                                var residualsUpdate = new float[xImage.GetLength(0), xImage.GetLength(1)];
                                Parallel.For(0, xCopy.GetLength(0), (i) =>
                                {
                                    for (int j = 0; j < xCopy.GetLength(1); j++)
                                    {
                                        residualsUpdate[i, j] = xImage[i, j] - xCopy[i, j];
                                    }
                                });
                                residualsConvolver.ConvolveInPlace(residualsUpdate);
                                Parallel.For(0, xCopy.GetLength(0), (i) =>
                                {
                                    for (int j = 0; j < xCopy.GetLength(1); j++)
                                    {
                                        dirtyImage[i, j] = dirtyCopy[i, j] - residualsUpdate[i, j];
                                    }
                                });
                            }

                            currentWatch.Stop();
                            totalWatch.Stop();

                            if (breakMajor)
                            {
                                break;
                            }
                            if (currentLambda == lambda & !switchedToOtherPsf)
                            {
                                pcdm.ResetAMap(psf);
                                currentGCalculator = gCalculator2;
                                lambda             = lambdaTrue;
                                switchedToOtherPsf = true;
                            }

                            FitsIO.Write(xImage, obsName + "_model_pcdm_majorCycle" + cycle + ".fits");

                            FFT.Shift(xImage);
                            var xGrid = FFT.Forward(xImage);
                            FFT.Shift(xImage);
                            var modelVis = IDG.DeGridW(c, metadata, xGrid, data.UVW, data.Frequencies);
                            residualVis = Visibilities.Substract(data.Visibilities, modelVis, data.Flags);
                        }

                        Console.WriteLine("Reconstruction finished in (seconds): " + totalWatch.Elapsed.TotalSeconds);
                    }
        }
Example #6
0
        public static bool Deconvolve2(double[,] xImage, double[,] residuals, double[,] psf, double lambda, double alpha, int blockSize, int maxIteration = 100, double epsilon = 1e-4)
        {
            var xImage2 = ToFloatImage(xImage);

            var PSFConvolution       = CommonDeprecated.PSF.CalcPaddedFourierConvolution(psf, residuals.GetLength(0), residuals.GetLength(1));
            var PSFCorrelation       = CommonDeprecated.PSF.CalculateFourierCorrelation(psf, residuals.GetLength(0), residuals.GetLength(1));
            var PSFSquared           = Fourier2D.Multiply(PSFConvolution, PSFCorrelation);
            var bMapCalculator       = new PaddedConvolver(PSFCorrelation, new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1)));
            var resUpdateCalculator  = new PaddedConvolver(PSFConvolution, new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1)));
            var bMapUpdateCalculator = new PaddedConvolver(PSFSquared, new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1)));

            var yBlockSize = blockSize;
            var xBlockSize = blockSize;

            lambda = lambda / (yBlockSize * xBlockSize);
            var bMap = ToFloatImage(residuals);

            bMapCalculator.ConvolveInPlace(bMap);
            FitsIO.Write(bMap, "bmapFirst.fits");

            var xDiff     = new float[xImage.GetLength(0), xImage.GetLength(1)];
            var lipschitz = ApproximateLipschitz(psf, yBlockSize, xBlockSize);
            var startL2   = NaiveGreedyCD.CalcDataObjective(residuals);

            var iter = 0;

            while (iter < maxIteration)
            {
                var maxBlock = GetMaxBlock(bMap, xImage2, lipschitz, (float)lambda, (float)alpha, yBlockSize, xBlockSize);
                var yB       = maxBlock.Item1;
                var xB       = maxBlock.Item2;
                //yB = 64 / yBlockSize;
                //xB = 64 / xBlockSize;
                var block = CopyFrom(bMap, yB, xB, yBlockSize, xBlockSize);

                //var optimized = block * blockInversion;

                var update    = block / lipschitz;
                var xOld      = CopyFrom(xImage2, yB, xB, yBlockSize, xBlockSize);
                var optimized = xOld + update;

                //shrink
                bool containsNonZero = false;
                for (int i = 0; i < optimized.Count; i++)
                {
                    optimized[i]     = CommonDeprecated.ShrinkElasticNet(optimized[i], lambda, alpha);
                    containsNonZero |= (optimized[i] - xOld[i]) != 0.0;
                }

                var optDiff = optimized - xOld;
                if (containsNonZero)
                {
                    AddInto(xDiff, optDiff, yB, xB, yBlockSize, xBlockSize);
                    AddInto(xImage2, optDiff, yB, xB, yBlockSize, xBlockSize);
                    //FitsIO.Write(xImage2, "xImageBlock.fits");
                    //FitsIO.Write(xDiff, "xDiff.fits");

                    //update b-map
                    bMapUpdateCalculator.ConvolveInPlace(xDiff);
                    //FitsIO.Write(xDiff, "bMapUpdate.fits");
                    for (int i = 0; i < xDiff.GetLength(0); i++)
                    {
                        for (int j = 0; j < xDiff.GetLength(1); j++)
                        {
                            bMap[i, j] -= xDiff[i, j];
                            xDiff[i, j] = 0;
                        }
                    }
                    //FitsIO.Write(bMap, "bMap2.fits");

                    //calc residuals for debug purposes

                    /*if (maxBlock.Item3 < epsilon)
                     *  break;*/

                    Console.WriteLine(maxBlock.Item3 + "\t yB = " + yB + "\t xB =" + xB);
                }
                iter++;
            }
            var elasticNet = 0.0;

            for (int i = 0; i < xImage.GetLength(0); i++)
            {
                for (int j = 0; j < xImage.GetLength(1); j++)
                {
                    xDiff[i, j]  = xImage2[i, j] - (float)xImage[i, j];
                    xImage[i, j] = xImage2[i, j];

                    elasticNet += lambda * 2 * lipschitz * ElasticNetPenalty(xImage2[i, j], (float)alpha);
                }
            }


            resUpdateCalculator.ConvolveInPlace(xDiff);
            //FitsIO.Write(xDiff, "residualsUpdate.fits");
            for (int i = 0; i < xDiff.GetLength(0); i++)
            {
                for (int j = 0; j < xDiff.GetLength(1); j++)
                {
                    residuals[i, j] -= xDiff[i, j];
                    xDiff[i, j]      = 0;
                }
            }
            var l2Penalty = NaiveGreedyCD.CalcDataObjective(residuals);

            Console.WriteLine("-------------------------");
            Console.WriteLine((l2Penalty + elasticNet));
            var io = System.IO.File.AppendText("penalty" + yBlockSize + ".txt");

            io.WriteLine("l2: " + l2Penalty + "\telastic: " + elasticNet + "\t " + (l2Penalty + elasticNet));
            io.Close();
            Console.WriteLine("-------------------------");

            return(false);
        }
Example #7
0
        private static ReconstructionInfo Reconstruct(Data input, float fullLipschitz, float[,] maskedPsf, string folder, float maskFactor, int maxMajor, string dirtyPrefix, string xImagePrefix, StreamWriter writer, double objectiveCutoff, float epsilon, bool maskPsf2)
        {
            var info      = new ReconstructionInfo();
            var totalSize = new Rectangle(0, 0, input.c.GridSize, input.c.GridSize);

            var bMapCalculator = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(maskedPsf, totalSize), new Rectangle(0, 0, maskedPsf.GetLength(0), maskedPsf.GetLength(1)));
            var maskedPsf2     = PSF.CalcPSFSquared(maskedPsf);

            if (maskPsf2)
            {
                Mask(maskedPsf2, 1e-5f);
            }
            writer.WriteLine((CountNonZero(maskedPsf2) - maskedPsf2.Length) / (double)maskedPsf2.Length);
            var fastCD = new FastSerialCD(totalSize, totalSize, maskedPsf, maskedPsf2);

            FitsIO.Write(maskedPsf, folder + maskFactor + "psf.fits");



            var lambda     = 0.4f * fastCD.MaxLipschitz;
            var lambdaTrue = 0.4f * fullLipschitz;
            var alpha      = 0.1f;

            var xImage      = new float[input.c.GridSize, input.c.GridSize];
            var residualVis = input.visibilities;
            DeconvolutionResult lastResult = null;

            for (int cycle = 0; cycle < maxMajor; cycle++)
            {
                Console.WriteLine("cycle " + cycle);
                var dirtyGrid  = IDG.GridW(input.c, input.metadata, residualVis, input.uvw, input.frequencies);
                var dirtyImage = FFT.WStackIFFTFloat(dirtyGrid, input.c.VisibilitiesCount);
                FFT.Shift(dirtyImage);
                FitsIO.Write(dirtyImage, folder + dirtyPrefix + cycle + ".fits");

                //calc data and reg penalty
                var dataPenalty       = Residuals.CalcPenalty(dirtyImage);
                var regPenalty        = ElasticNet.CalcPenalty(xImage, lambdaTrue, alpha);
                var regPenaltyCurrent = ElasticNet.CalcPenalty(xImage, lambda, alpha);
                info.lastDataPenalty = dataPenalty;
                info.lastRegPenalty  = regPenalty;

                bMapCalculator.ConvolveInPlace(dirtyImage);
                //FitsIO.Write(dirtyImage, folder + dirtyPrefix + "bmap_" + cycle + ".fits");
                var currentLambda = lambda;

                writer.Write(cycle + ";" + currentLambda + ";" + Residuals.GetMax(dirtyImage) + ";" + dataPenalty + ";" + regPenalty + ";" + regPenaltyCurrent + ";");
                writer.Flush();

                //check wether we can minimize the objective further with the current psf
                var objectiveReached = (dataPenalty + regPenalty) < objectiveCutoff;
                var minimumReached   = (lastResult != null && lastResult.IterationCount < 100 && lastResult.Converged);
                if (!objectiveReached & !minimumReached)
                {
                    info.totalDeconv.Start();
                    lastResult = fastCD.Deconvolve(xImage, dirtyImage, currentLambda, alpha, 50000, epsilon);
                    info.totalDeconv.Stop();

                    FitsIO.Write(xImage, folder + xImagePrefix + cycle + ".fits");
                    writer.Write(lastResult.Converged + ";" + lastResult.IterationCount + ";" + lastResult.ElapsedTime.TotalSeconds + "\n");
                    writer.Flush();

                    FFT.Shift(xImage);
                    var xGrid = FFT.Forward(xImage);
                    FFT.Shift(xImage);
                    var modelVis = IDG.DeGridW(input.c, input.metadata, xGrid, input.uvw, input.frequencies);
                    residualVis = Visibilities.Substract(input.visibilities, modelVis, input.flags);
                }
                else
                {
                    writer.Write(false + ";0;0");
                    writer.Flush();
                    break;
                }
            }

            return(info);
        }
        private static void ReconstructPCDM(MeasurementData input, GriddingConstants c, float[,] fullPsf, string folder, string file, int minorCycles, float searchPercent, int processorCount)
        {
            var totalWatch   = new Stopwatch();
            var currentWatch = new Stopwatch();

            var totalSize    = new Rectangle(0, 0, c.GridSize, c.GridSize);
            var psfCut       = PSF.Cut(fullPsf, CUT_FACTOR_PCDM);
            var maxSidelobe  = PSF.CalcMaxSidelobe(fullPsf, CUT_FACTOR_PCDM);
            var sidelobeHalf = PSF.CalcMaxSidelobe(fullPsf, 2);
            var random       = new Random(123);
            var pcdm         = new ParallelCoordinateDescent(totalSize, psfCut, 1, 1000, searchPercent);

            var metadata = Partitioner.CreatePartition(c, input.UVW, input.Frequencies);

            using (var bMapCalculator = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psfCut, totalSize), new Rectangle(0, 0, psfCut.GetLength(0), psfCut.GetLength(1))))
                using (var bMapCalculator2 = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(fullPsf, totalSize), new Rectangle(0, 0, fullPsf.GetLength(0), fullPsf.GetLength(1))))
                    using (var residualsConvolver = new PaddedConvolver(totalSize, fullPsf))
                    {
                        var currentBMapCalculator = bMapCalculator;

                        var maxLipschitz = PSF.CalcMaxLipschitz(psfCut);
                        var lambda       = (float)(LAMBDA * maxLipschitz);
                        var lambdaTrue   = (float)(LAMBDA * PSF.CalcMaxLipschitz(fullPsf));
                        var alpha        = ALPHA;

                        var switchedToOtherPsf = false;
                        var writer             = new StreamWriter(folder + "/" + file + ".txt");
                        var xImage             = new float[c.GridSize, c.GridSize];
                        var residualVis        = input.Visibilities;
                        ParallelCoordinateDescent.PCDMStatistics lastResult = null;
                        for (int cycle = 0; cycle < 6; cycle++)
                        {
                            Console.WriteLine("Beginning Major cycle " + cycle);
                            var dirtyGrid  = IDG.GridW(c, metadata, residualVis, input.UVW, input.Frequencies);
                            var dirtyImage = FFT.WStackIFFTFloat(dirtyGrid, c.VisibilitiesCount);
                            FFT.Shift(dirtyImage);
                            FitsIO.Write(dirtyImage, folder + "/dirty" + cycle + ".fits");

                            currentWatch.Restart();
                            totalWatch.Start();

                            var breakMajor       = false;
                            var minLambda        = 0.0f;
                            var dirtyCopy        = Copy(dirtyImage);
                            var xCopy            = Copy(xImage);
                            var currentLambda    = 0f;
                            var currentObjective = 0.0;
                            var absMax           = 0.0f;
                            for (int minorCycle = 0; minorCycle < minorCycles; minorCycle++)
                            {
                                Console.WriteLine("Beginning Minor Cycle " + minorCycle);
                                var maxDirty         = Residuals.GetMax(dirtyImage);
                                var bMap             = currentBMapCalculator.Convolve(dirtyImage);
                                var maxB             = Residuals.GetMax(bMap);
                                var correctionFactor = Math.Max(maxB / (maxDirty * maxLipschitz), 1.0f);
                                var currentSideLobe  = maxB * maxSidelobe * correctionFactor;
                                currentLambda = (float)Math.Max(currentSideLobe / alpha, lambda);

                                if (minorCycle == 0)
                                {
                                    minLambda = (float)(maxB * sidelobeHalf * correctionFactor / alpha);
                                }
                                if (currentLambda < minLambda)
                                {
                                    currentLambda = minLambda;
                                }
                                currentObjective = Residuals.CalcPenalty(dirtyImage) + ElasticNet.CalcPenalty(xImage, lambdaTrue, alpha);
                                absMax           = pcdm.GetAbsMax(xImage, bMap, lambdaTrue, alpha);
                                if (absMax < MAJOR_STOP)
                                {
                                    breakMajor = true;
                                    break;
                                }

                                lastResult = pcdm.Deconvolve(xImage, bMap, currentLambda, alpha, 100, 1e-5f);

                                if (currentLambda == lambda | currentLambda == minLambda)
                                {
                                    break;
                                }

                                var residualsUpdate = new float[xImage.GetLength(0), xImage.GetLength(1)];
                                Parallel.For(0, xCopy.GetLength(0), (i) =>
                                {
                                    for (int j = 0; j < xCopy.GetLength(1); j++)
                                    {
                                        residualsUpdate[i, j] = xImage[i, j] - xCopy[i, j];
                                    }
                                });
                                residualsConvolver.ConvolveInPlace(residualsUpdate);
                                Parallel.For(0, xCopy.GetLength(0), (i) =>
                                {
                                    for (int j = 0; j < xCopy.GetLength(1); j++)
                                    {
                                        dirtyImage[i, j] = dirtyCopy[i, j] - residualsUpdate[i, j];
                                    }
                                });
                            }

                            currentWatch.Stop();
                            totalWatch.Stop();
                            writer.WriteLine(cycle + ";" + currentLambda + ";" + currentObjective + ";" + absMax + ";" + lastResult.IterationCount + ";" + totalWatch.Elapsed.TotalSeconds + ";" + currentWatch.Elapsed.TotalSeconds);
                            writer.Flush();

                            FitsIO.Write(xImage, folder + "/xImage_pcdm_" + cycle + ".fits");

                            if (breakMajor)
                            {
                                break;
                            }
                            if (currentLambda == lambda & !switchedToOtherPsf)
                            {
                                pcdm.ResetAMap(fullPsf);
                                currentBMapCalculator = bMapCalculator2;
                                lambda             = lambdaTrue;
                                switchedToOtherPsf = true;
                                writer.WriteLine("switched");
                                writer.Flush();
                            }

                            FFT.Shift(xImage);
                            var xGrid = FFT.Forward(xImage);
                            FFT.Shift(xImage);
                            var modelVis = IDG.DeGridW(c, metadata, xGrid, input.UVW, input.Frequencies);
                            residualVis = Visibilities.Substract(input.Visibilities, modelVis, input.Flags);
                        }

                        writer.Close();
                    }
        }