public static Matrix3 Euler(float rot, float tilt, float psi) { float ca, sa, cb, sb, cg, sg; float cc, cs, sc, ss; ca = (float)Math.Cos(rot); cb = (float)Math.Cos(tilt); cg = (float)Math.Cos(psi); sa = (float)Math.Sin(rot); sb = (float)Math.Sin(tilt); sg = (float)Math.Sin(psi); cc = cb * ca; cs = cb * sa; sc = sb * ca; ss = sb * sa; Matrix3 A = new Matrix3(); A.M11 = cg * cc - sg * sa; A.M12 = cg * cs + sg * ca; A.M13 = -cg * sb; A.M21 = -sg * cc - cg * sa; A.M22 = -sg * cs + cg * ca; A.M23 = sg * sb; A.M31 = sc; A.M32 = ss; A.M33 = cb; return A; }
public MainWindow() { try { Options.DeviceCount = GPU.GetDeviceCount(); if (Options.DeviceCount <= 0) throw new Exception(); } catch (Exception) { MessageBox.Show("No CUDA devices found, shutting down."); Close(); } GPU.MemoryChanged += () => Options.UpdateGPUStats(); DataContext = Options; Options.PropertyChanged += Options_PropertyChanged; Closing += MainWindow_Closing; InitializeComponent(); DisableWhenRunning = new List<UIElement> { GridOptionsIO, GridOptionsPreprocessing, GridOptionsParticles, GridOptionsCTF, GridOptionsMovement, GridOptionsGrids, GridOptionsPostprocessing }; if (File.Exists("Previous.settings")) Options.Load("Previous.settings"); for (int i = 0; i < GPU.GetDeviceCount(); i++) { GPU.SetDevice(i); Console.WriteLine($"Device {i}:"); Console.WriteLine($"Free memory: {GPU.GetFreeMemory(i)} MB"); Console.WriteLine($"Total memory: {GPU.GetTotalMemory(i)} MB"); } GPU.SetDevice(0); Options.UpdateGPUStats(); // Create mockup { float2[] SplinePoints = { new float2(0f, 0f), new float2(1f / 3f, 1f)};//, new float2(2f / 3f, 0f)};//, new float2(1f, 1f) }; Cubic1D ReferenceSpline = new Cubic1D(SplinePoints); Cubic1DShort ShortSpline = Cubic1DShort.GetInterpolator(SplinePoints); for (float i = -1f; i < 2f; i += 0.01f) { float Reference = ReferenceSpline.Interp(i); float Test = ShortSpline.Interp(i); if (Math.Abs(Reference - Test) > 1e-6f) throw new Exception(); } Random Rnd = new Random(123); int3 GridDim = new int3(1, 1, 1); float[] GridValues = new float[GridDim.Elements()]; for (int i = 0; i < GridValues.Length; i++) GridValues[i] = (float)Rnd.NextDouble(); CubicGrid CGrid = new CubicGrid(GridDim, GridValues); float[] Managed = CGrid.GetInterpolated(new int3(16, 16, 16), new float3(0, 0, 0)); float[] Native = CGrid.GetInterpolatedNative(new int3(16, 16, 16), new float3(0, 0, 0)); for (int i = 0; i < Managed.Length; i++) if (Math.Abs(Managed[i] - Native[i]) > 1e-6f) throw new Exception(); Matrix3 A = new Matrix3(1, 2, 3, 4, 5, 6, 7, 8, 9); Matrix3 B = new Matrix3(11, 12, 13, 14, 15, 16, 17, 18, 19); Matrix3 C = A * B; // Euler matrix { Matrix3 E = Matrix3.Euler(0 * Helper.ToRad, 20 * Helper.ToRad, 0 * Helper.ToRad); float3 EE = Matrix3.EulerFromMatrix(E.Transposed()) * Helper.ToDeg; float3 Transformed = E * new float3(1, 0, 0); Transformed.Y = 0; } //float3[] HealpixAngles = Helper.GetHealpixAngles(3, "D4"); // Deconvolve reconstructions using a separate CTF //{ // for (int i = 1; i <= 24; i++) // { // Image Map = StageDataLoad.LoadMap($"F:\\stefanribo\\vlion\\warped_{i}.mrc", new int2(1, 1), 0, typeof(float)); // Image MapFT = Map.AsFFT(true); // Map.Dispose(); // Image CTF = StageDataLoad.LoadMap($"F:\\stefanribo\\vlion\\warped_ctf_{i}.mrc", new int2(1, 1), 0, typeof(float)); // foreach (var slice in CTF.GetHost(Intent.ReadWrite)) // for (int s = 0; s < slice.Length; s++) // slice[s] = Math.Max(1e-3f, slice[s]); // MapFT.Divide(CTF); // Map = MapFT.AsIFFT(true); // MapFT.Dispose(); // Map.WriteMRC($"F:\\stefanribo\\vlion\\warped_deconv_{i}.mrc"); // Map.Dispose(); // } //} //{ // Image SumFT = new Image(new int3(220, 220, 220), true, true); // Image SumWeights = new Image(new int3(220, 220, 220), true); // int read = 0; // foreach (var tomoPath in Directory.EnumerateFiles("F:\\stefanribo\\oridata\\particles", "tomo*.mrc")) // { // FileInfo Info = new FileInfo(tomoPath); // Image Tomo = StageDataLoad.LoadMap(tomoPath, new int2(1, 1), 0, typeof(float)); // Image TomoFT = Tomo.AsFFT(true); // Tomo.Dispose(); // Image TomoWeights = StageDataLoad.LoadMap("F:\\stefanribo\\oridata\\particlectf\\" + Info.Name, new int2(1, 1), 0, typeof(float)); // TomoFT.Multiply(TomoWeights); // TomoWeights.Multiply(TomoWeights); // SumFT.Add(TomoFT); // SumWeights.Add(TomoWeights); // TomoFT.Dispose(); // TomoWeights.Dispose(); // Debug.WriteLine(read++); // } // foreach (var slice in SumWeights.GetHost(Intent.ReadWrite)) // { // for (int i = 0; i < slice.Length; i++) // { // slice[i] = Math.Max(1e-3f, slice[i]); // } // } // SumFT.Divide(SumWeights); // Image Sum = SumFT.AsIFFT(true); // Sum.WriteMRC("F:\\stefanribo\\oridata\\particles\\weightedaverage.mrc"); // SumFT.Dispose(); // SumWeights.Dispose(); // Sum.Dispose(); //} //{ // Image Subtrahend = StageDataLoad.LoadMap("E:\\martinsried\\stefan\\membranebound\\vlion\\relion_subtrahend.mrc", new int2(1, 1), 0, typeof(float)); // Image SubtrahendFT = Subtrahend.AsFFT(true); // int read = 0; // foreach (var tomoPath in Directory.EnumerateFiles("E:\\martinsried\\stefan\\membranebound\\oridata\\particles", "tomo*.mrc")) // { // FileInfo Info = new FileInfo(tomoPath); // Image Tomo = StageDataLoad.LoadMap(tomoPath, new int2(1, 1), 0, typeof(float)); // Image TomoFT = Tomo.AsFFT(true); // Tomo.Dispose(); // Image TomoWeights = StageDataLoad.LoadMap("E:\\martinsried\\stefan\\membranebound\\oridata\\particlectf\\" + Info.Name, new int2(1, 1), 0, typeof(float)); // Image SubtrahendFTMult = new Image(SubtrahendFT.GetDevice(Intent.Read), SubtrahendFT.Dims, true, true); // SubtrahendFTMult.Multiply(TomoWeights); // TomoFT.Subtract(SubtrahendFTMult); // Tomo = TomoFT.AsIFFT(true); // Tomo.WriteMRC("D:\\stefanribo\\particles\\" + Info.Name); // Tomo.Dispose(); // TomoFT.Dispose(); // SubtrahendFTMult.Dispose(); // TomoWeights.Dispose(); // Debug.WriteLine(read++); // } //} //{ // Image SubtRef1 = StageDataLoad.LoadMap("E:\\martinsried\\stefan\\membranebound\\vlion\\warp_subtrahend.mrc", new int2(1, 1), 0, typeof(float)); // Projector Subt = new Projector(SubtRef1, 2); // SubtRef1.Dispose(); // Image ProjFT = Subt.Project(new int2(220, 220), new[] { new float3(0, 0, 0) }, 110); // Image Proj = ProjFT.AsIFFT(); // Proj.RemapFromFT(); // Proj.WriteMRC("d_testproj.mrc"); //} // Projector /*{ Image MapForProjector = StageDataLoad.LoadMap("E:\\youwei\\run36_half1_class001_unfil.mrc", new int2(1, 1), 0, typeof (float)); Projector Proj = new Projector(MapForProjector, 2); Image Projected = Proj.Project(new int2(240, 240), new[] { new float3(0, 0, 0) }, 120); Projected = Projected.AsIFFT(); Projected.RemapFromFT(); Projected.WriteMRC("d_projected.mrc"); }*/ // Backprojector /*{ Image Dot = new Image(new int3(32, 32, 360)); for (int a = 0; a < 360; a++) Dot.GetHost(Intent.Write)[a][0] = 1; Dot = Dot.AsFFT(); Dot.AsAmplitudes().WriteMRC("d_dot.mrc"); Image DotWeights = new Image(new int3(32, 32, 360), true); for (int a = 0; a < 360; a++) for (int i = 0; i < DotWeights.ElementsSliceReal; i++) DotWeights.GetHost(Intent.Write)[a][i] = 1; float3[] Angles = new float3[360]; for (int a = 0; a < 360; a++) Angles[a] = new float3(0, a * Helper.ToRad * 0.05f, 0); Projector Proj = new Projector(new int3(32, 32, 32), 2); Proj.BackProject(Dot, DotWeights, Angles); Proj.Weights.WriteMRC("d_weights.mrc"); //Image Re = Proj.Data.AsImaginary(); //Re.WriteMRC("d_projdata.mrc"); Image Rec = Proj.Reconstruct(true); Rec.WriteMRC("d_rec.mrc"); }*/ //Star Models = new Star("D:\\rado27\\Refine3D\\run1_ct5_it005_half1_model.star", "data_model_group_2"); //Debug.WriteLine(Models.GetRow(0)[0]); /*Image Volume = StageDataLoad.LoadMap("F:\\carragher20s\\ref256.mrc", new int2(1, 1), 0, typeof (float)); Image VolumePadded = Volume.AsPadded(new int3(512, 512, 512)); VolumePadded.WriteMRC("d_padded.mrc"); Volume.Dispose(); VolumePadded.RemapToFT(true); Image VolumeFT = VolumePadded.AsFFT(true); VolumePadded.Dispose(); Image VolumeProjFT = VolumeFT.AsProjections(new[] { new float3(Helper.ToRad * 0, Helper.ToRad * 0, Helper.ToRad * 0) }, new int2(256, 256), 2f); Image VolumeProj = VolumeProjFT.AsIFFT(); VolumeProjFT.Dispose(); VolumeProj.RemapFromFT(); VolumeProj.WriteMRC("d_proj.mrc"); VolumeProj.Dispose();*/ /*Options.Movies.Add(new Movie(@"D:\Dev\warp\May19_21.44.54.mrc")); Options.Movies.Add(new Movie(@"D:\Dev\warp\May19_21.49.06.mrc")); Options.Movies.Add(new Movie(@"D:\Dev\warp\May19_21.50.48.mrc")); Options.Movies.Add(new Movie(@"D:\Dev\warp\May19_21.52.16.mrc")); Options.Movies.Add(new Movie(@"D:\Dev\warp\May19_21.53.43.mrc")); CTFDisplay.PS2D = new BitmapImage();*/ /*float2[] SimCoords = new float2[512 * 512]; for (int y = 0; y < 512; y++) for (int x = 0; x < 512; x++) { int xcoord = x - 512, ycoord = y - 512; SimCoords[y * 512 + x] = new float2((float) Math.Sqrt(xcoord * xcoord + ycoord * ycoord), (float) Math.Atan2(ycoord, xcoord)); } float[] Sim2D = new CTF {Defocus = -2M}.Get2D(SimCoords, 512, true); byte[] Sim2DBytes = new byte[Sim2D.Length]; for (int i = 0; i < 512 * 512; i++) Sim2DBytes[i] = (byte) (Sim2D[i] * 255f); BitmapSource Sim2DSource = BitmapSource.Create(512, 512, 96, 96, PixelFormats.Indexed8, BitmapPalettes.Gray256, Sim2DBytes, 512); CTFDisplay.Simulated2D = Sim2DSource;*/ /*float2[] PointsPS1D = new float2[512]; for (int i = 0; i < PointsPS1D.Length; i++) PointsPS1D[i] = new float2(i, (float) Math.Exp(-i / 300f)); CTFDisplay.PS1D = PointsPS1D; float[] SimCTF = new CTF { Defocus = -2M }.Get1D(512, true); float2[] PointsSim1D = new float2[SimCTF.Length]; for (int i = 0; i < SimCTF.Length; i++) PointsSim1D[i] = new float2(i, SimCTF[i] * (float)Math.Exp(-i / 100f) + (float)Math.Exp(-i / 300f)); CTFDisplay.Simulated1D = PointsSim1D;*/ /*CubicGrid Grid = new CubicGrid(new int3(5, 5, 5), 0, 0, Dimension.X); Grid.Values[2, 2, 2] = 1f; float[] Data = new float[11 * 11 * 11]; int i = 0; for (float z = 0f; z < 1.05f; z += 0.1f) for (float y = 0f; y < 1.05f; y += 0.1f) for (float x = 0f; x < 1.05f; x += 0.1f) Data[i++] = Grid.GetInterpolated(new float3(x, y, z)); Image DataImage = new Image(Data, new int3(11, 11, 11)); DataImage.WriteMRC("bla.mrc"); Image GPUImage = new Image(DataImage.GetDevice(Intent.Read), new int3(11, 11, 11)); GPUImage.WriteMRC("gpu.mrc");*/ /*CubicGrid WiggleGrid = new CubicGrid(new int3(2, 2, 1)); float[][] WiggleWeights = WiggleGrid.GetWiggleWeights(new int3(3, 3, 1));*/ } }
public static float3 EulerFromMatrix(Matrix3 a) { float alpha, beta, gamma; float abs_sb, sign_sb; abs_sb = (float)Math.Sqrt(a.M13 * a.M13 + a.M23 * a.M23); if (abs_sb > 16 * 1.192092896e-07f) { gamma = (float)Math.Atan2(a.M23, -a.M13); alpha = (float)Math.Atan2(a.M32, a.M31); if (Math.Abs((float)Math.Sin(gamma)) < 1.192092896e-07f) sign_sb = Math.Sign(-a.M13 / Math.Cos(gamma)); else sign_sb = (Math.Sin(gamma) > 0) ? Math.Sign(a.M23) : -Math.Sign(a.M23); beta = (float)Math.Atan2(sign_sb * abs_sb, a.M33); } else { if (Math.Sign(a.M33) > 0) { // Let's consider the matrix as a rotation around Z alpha = 0; beta = 0; gamma = (float)Math.Atan2(-a.M21, a.M11); } else { alpha = 0; beta = (float)Math.PI; gamma = (float)Math.Atan2(a.M21, -a.M11); } } return new float3(alpha, beta, gamma); }