Beispiel #1
0
            public void Will_Dispose_Of_Db_Context()
            {
                //Arrange
                var context   = new Mock <IDbContext>();
                var projector = new Projector <TestModel>(context.Object);

                //Act
                projector.Dispose();

                //Assert
                context.Verify(x => x.Dispose(), Times.Once);
            }
Beispiel #2
0
        public void GatherRefinementProgress(string[] folders)
        {
            foreach (var species in Species)
            {
                string SpeciesID = species.GUID.ToString().Substring(0, 8);

                Projector Rec1 = species.HalfMap1Reconstruction[0];
                Projector Rec2 = species.HalfMap2Reconstruction[0];

                Particle[] OriginalParticles = species.Particles.ToList().ToArray();
                Particle[] FinalParticles    = species.Particles;

                foreach (var folder in folders)
                {
                    foreach (var path in Directory.EnumerateFiles(folder, $"{SpeciesID}_half1_*.mrc"))
                    {
                        Projector Saved = Projector.FromFile(path);

                        Rec1.Data.Add(Saved.Data);
                        Rec1.Weights.Add(Saved.Weights);

                        Saved.Dispose();
                    }

                    foreach (var path in Directory.EnumerateFiles(folder, $"{SpeciesID}_half2_*.mrc"))
                    {
                        Projector Saved = Projector.FromFile(path);

                        Rec2.Data.Add(Saved.Data);
                        Rec2.Weights.Add(Saved.Weights);

                        Saved.Dispose();
                    }

                    Particle[] UpdatedParticles = species.ParticlesFromStar(new Star(System.IO.Path.Combine(folder, $"{SpeciesID}_particles.star")));

                    for (int p = 0; p < OriginalParticles.Length; p++)
                    {
                        if (OriginalParticles[p].Coordinates.Where((v, i) => v != UpdatedParticles[p].Coordinates[i]).Count() > 0 ||
                            OriginalParticles[p].Angles.Where((v, i) => v != UpdatedParticles[p].Angles[i]).Count() > 0)
                        {
                            FinalParticles[p] = UpdatedParticles[p];
                        }
                    }
                }

                Rec1.FreeDevice();
                Rec2.FreeDevice();
            }
        }
Beispiel #3
0
        public static Image AlignLocallyToTarget(Image map, Image target, int alignmentSize, int oversampling, out float3 shift, out float3 rotation)
        {
            float ScaleFactor = (float)alignmentSize / map.Dims.X;
            int3  DimsScaled  = new int3(alignmentSize, alignmentSize, alignmentSize);

            #region Prepare Scaled & FFTed maps

            Image MapScaled = map.AsScaled(DimsScaled);
            map.FreeDevice();

            Image TargetScaled = target.AsScaled(DimsScaled);
            target.FreeDevice();
            TargetScaled.RemapToFT(true);
            TargetScaled.WriteMRC("d_targetscaled.mrc", true);
            Image TargetScaledFT = TargetScaled.AsFFT(true);
            TargetScaled.Dispose();

            Projector ProjMapScaled = new Projector(MapScaled, oversampling);
            Image     TestFT        = ProjMapScaled.Project(DimsScaled, new float3[1]);
            Image     Test          = TestFT.AsIFFT(true);
            Test.RemapFromFT(true);
            Test.WriteMRC("d_projected.mrc", true);

            #endregion

            float3 CurShift = new float3(), CurRotation = new float3();

            Func <double[], double> GetDiff         = input =>
            {
                CurShift    = new float3((float)input[0], (float)input[1], (float)input[2]);
                CurRotation = new float3((float)input[3], (float)input[4], (float)input[5]) * Helper.ToRad;
                CurRotation = Matrix3.EulerFromMatrix(Matrix3.RotateX(CurRotation.X) * Matrix3.RotateY(CurRotation.Y) * Matrix3.RotateZ(CurRotation.Z)) * Helper.ToDeg;

                Image TargetFTShifted = TargetScaledFT.AsShiftedVolume(-CurShift * ScaleFactor);
                Image MapRotatedFT    = ProjMapScaled.Project(DimsScaled, new[] { CurRotation *Helper.ToRad });

                GPU.MultiplySlices(TargetFTShifted.GetDevice(Intent.Read),
                                   MapRotatedFT.GetDevice(Intent.Read),
                                   TargetFTShifted.GetDevice(Intent.Write),
                                   TargetFTShifted.ElementsReal,
                                   1);
                MapRotatedFT.Dispose();

                IntPtr d_Sum = GPU.MallocDevice(1);
                GPU.Sum(TargetFTShifted.GetDevice(Intent.Read), d_Sum, (uint)TargetFTShifted.ElementsReal, 1);
                TargetFTShifted.Dispose();

                float[] h_Sum = new float[1];
                GPU.CopyDeviceToHost(d_Sum, h_Sum, 1);

                GPU.FreeDevice(d_Sum);

                return(h_Sum[0]);
            };

            Func <double[], double[]> GetGradient   = input =>
            {
                float    Delta  = 0.05f;
                double[] Result = new double[input.Length];

                Console.WriteLine(GetDiff(input));

                for (int i = 0; i < input.Length; i++)
                {
                    double[] InputPlus = input.ToList().ToArray();
                    InputPlus[i] += Delta;
                    double ScorePlus = GetDiff(InputPlus);

                    double[] InputMinus = input.ToList().ToArray();
                    InputMinus[i] -= Delta;
                    double ScoreMinus = GetDiff(InputMinus);

                    Result[i] = (ScorePlus - ScoreMinus) / (Delta * 2);
                }

                return(Result);
            };

            double[] StartParams                    = new double[6];
            BroydenFletcherGoldfarbShanno Optimizer = new BroydenFletcherGoldfarbShanno(StartParams.Length, GetDiff, GetGradient);
            Optimizer.MaxIterations = 20;
            Optimizer.Maximize(StartParams);

            GetDiff(StartParams);
            shift    = CurShift;
            rotation = CurRotation;

            #region Shift and rotate input map by determined values for final output

            ProjMapScaled.Dispose();
            Image MapResult = map.AsShiftedVolume(shift);
            map.FreeDevice();

            Projector ProjMapResult = new Projector(MapResult, oversampling);
            MapResult.Dispose();

            Image MapResultFT = ProjMapResult.Project(map.Dims, new[] { rotation *Helper.ToRad });
            ProjMapResult.Dispose();

            MapResult = MapResultFT.AsIFFT(true);
            MapResultFT.Dispose();
            MapResult.RemapFromFT(true);

            #endregion

            return(MapResult);
        }