Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions WarpLib/Movie/Movie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,14 +1674,14 @@ public virtual Image GetImagesForOneParticle(ProcessingOptionsBase options, Imag

#region GetCTFs methods

public virtual Image GetCTFsForOneParticle(ProcessingOptionsBase options, float3 coords, Image ctfCoords, Image gammaCorrection, bool weighted = true, bool weightsonly = false, bool useglobalweights = false, Image result = null)
public virtual Image GetCTFsForOneParticle(ProcessingOptionsBase options, float3 coords, Image ctfCoords, Image gammaCorrection, bool weighted = true, bool weightsonly = false, bool useglobalweights = false, Image result = null, string outputpath = "")
{
float3[] PerFrameCoords = Helper.ArrayOfConstant(coords, NFrames);

return GetCTFsForOneParticle(options, PerFrameCoords, ctfCoords, gammaCorrection, weighted, weightsonly, useglobalweights, result);
return GetCTFsForOneParticle(options, PerFrameCoords, ctfCoords, gammaCorrection, weighted, weightsonly, useglobalweights, result, outputpath);
}

public virtual Image GetCTFsForOneParticle(ProcessingOptionsBase options, float3[] coordsMoving, Image ctfCoords, Image gammaCorrection, bool weighted = true, bool weightsonly = false, bool useglobalweights = false, Image result = null)
public virtual Image GetCTFsForOneParticle(ProcessingOptionsBase options, float3[] coordsMoving, Image ctfCoords, Image gammaCorrection, bool weighted = true, bool weightsonly = false, bool useglobalweights = false, Image result = null, string outputpath = "")
{
float3[] ImagePositions = GetPositionInAllFrames(coordsMoving);

Expand Down
21 changes: 20 additions & 1 deletion WarpLib/OptionsWarp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ public ProcessingOptionsTomoSubReconstruction GetProcessingTomoSubReconstruction

MakeSparse = Tasks.TomoSubReconstructMakeSparse,

OutputCTFCSV = Tasks.TomoSubReconstructCTFCSV,
CorrectCTF = Tasks.TomoSubReconstructCorrectCTF,

UseCPU = Tasks.UseCPU
});

Expand Down Expand Up @@ -2080,6 +2083,22 @@ public bool TomoSubReconstructMakeSparse
get { return _TomoSubReconstructMakeSparse; }
set { if (value != _TomoSubReconstructMakeSparse) { _TomoSubReconstructMakeSparse = value; OnPropertyChanged(); } }
}

private bool _TomoSubReconstructCTFCSV = true;
[WarpSerializable]
public bool TomoSubReconstructCTFCSV
{
get { return _TomoSubReconstructCTFCSV; }
set { if (value != _TomoSubReconstructCTFCSV) { _TomoSubReconstructCTFCSV = value; OnPropertyChanged(); } }
}

private bool _TomoSubReconstructCorrectCTF = true;
[WarpSerializable]
public bool TomoSubReconstructCorrectCTF
{
get { return _TomoSubReconstructCorrectCTF; }
set { if (value != _TomoSubReconstructCorrectCTF) { _TomoSubReconstructCorrectCTF = value; OnPropertyChanged(); } }
}

#endregion

Expand Down Expand Up @@ -2320,4 +2339,4 @@ public enum ProcessingStatus
FilteredOut = 4,
LeaveOut = 5
}
}
}
39 changes: 29 additions & 10 deletions WarpLib/TiltSeries/TiltSeries.ReconstructSubtomos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public void ReconstructSubtomos(ProcessingOptionsTomoSubReconstruction options,
ProjectorsMultiplicity[threadID].Weights.Fill(0);
CTFsComplex[threadID].Fill(new float2(1, 0));
CTFs[threadID].Fill(1);
CTFsAbs[threadID].Fill(1); //if dont correct ctf, voxel weight is 1

ProjectorsMultiplicity[threadID].BackProject(CTFsComplex[threadID], CTFs[threadID], !options.PrerotateParticles ? GetAngleInAllTilts(ParticlePositions) : GetParticleAngleInAllTilts(ParticlePositions, ParticleAngles), MagnificationCorrection);
ProjectorsMultiplicity[threadID].Weights.Min(1);
Expand All @@ -146,7 +147,10 @@ public void ReconstructSubtomos(ProcessingOptionsTomoSubReconstruction options,
Timing.Finish("ExtractImageData");

Timing.Start("CreateRawCTF");
GetCTFsForOneParticle(options, ParticlePositions, CTFCoords, null, true, false, false, CTFs[threadID]);
if (options.OutputCTFCSV)
GetCTFsForOneParticle(options, ParticlePositions, CTFCoords, null, true, false, false, CTFs[threadID], System.IO.Path.Combine(SubtomoDir, $"{RootName}{options.Suffix}_{p:D7}_ctf_{options.BinnedPixelSizeMean:F2}A.csv"));
else
GetCTFsForOneParticle(options, ParticlePositions, CTFCoords, null, true, false, false, CTFs[threadID]);
GetCTFsForOneParticle(options, ParticlePositions, CTFCoords, null, false, false, false, CTFsUnweighted[threadID]);
Timing.Finish("CreateRawCTF");

Expand All @@ -156,24 +160,34 @@ public void ReconstructSubtomos(ProcessingOptionsTomoSubReconstruction options,
// Subtomo is (Image * CTFweighted) / abs(CTFunweighted)
// 3D CTF is (CTFweighted * CTFweighted) / abs(CTFweighted)

ImagesFT[threadID].Multiply(CTFs[threadID]);
//GPU.Abs(CTFs[threadID].GetDevice(Intent.Read),
// CTFsAbs[threadID].GetDevice(Intent.Write),
// CTFs[threadID].ElementsReal);
CTFsAbs[threadID].Abs();
//MOD: if correct subtomo
if (options.CorrectCTF)
{
ImagesFT[threadID].Multiply(CTFs[threadID]);
//GPU.Abs(CTFs[threadID].GetDevice(Intent.Read),
// CTFsAbs[threadID].GetDevice(Intent.Write),
// CTFs[threadID].ElementsReal);
}

CTFsComplex[threadID].Fill(new float2(1, 0));
CTFsComplex[threadID].Multiply(CTFsUnweighted[threadID]); // What the raw image is like: unweighted, unflipped
if (options.CorrectCTF)
CTFsComplex[threadID].Multiply(CTFsUnweighted[threadID]); // What the raw image is like: unweighted, unflipped
CTFsComplex[threadID].Multiply(CTFs[threadID]); // Weight by the same CTF as raw image: weighted, unflipped

CTFsUnweighted[threadID].Abs();
if (options.CorrectCTF)
CTFsUnweighted[threadID].Abs();

#region Sub-tomo

Projectors[threadID].Data.Fill(0);
Projectors[threadID].Weights.Fill(0);

Timing.Start("ProjectImageData");
Projectors[threadID].BackProject(ImagesFT[threadID], CTFsUnweighted[threadID], !options.PrerotateParticles ? GetAngleInAllTilts(ParticlePositions) : GetParticleAngleInAllTilts(ParticlePositions, ParticleAngles), MagnificationCorrection);
if (options.CorrectCTF)
Projectors[threadID].BackProject(ImagesFT[threadID], CTFsUnweighted[threadID], !options.PrerotateParticles ? GetAngleInAllTilts(ParticlePositions) : GetParticleAngleInAllTilts(ParticlePositions, ParticleAngles), MagnificationCorrection);
else
Projectors[threadID].BackProject(ImagesFT[threadID], CTFsAbs[threadID], !options.PrerotateParticles ? GetAngleInAllTilts(ParticlePositions) : GetParticleAngleInAllTilts(ParticlePositions, ParticleAngles), MagnificationCorrection);
Timing.Finish("ProjectImageData");

//Projectors[threadID].Weights.Fill(1);
Expand Down Expand Up @@ -212,7 +226,10 @@ public void ReconstructSubtomos(ProcessingOptionsTomoSubReconstruction options,
Projectors[threadID].Data.Fill(0);
Projectors[threadID].Weights.Fill(0);

Projectors[threadID].BackProject(CTFsComplex[threadID], CTFsUnweighted[threadID], !options.PrerotateParticles ? GetAngleInAllTilts(ParticlePositions) : GetParticleAngleInAllTilts(ParticlePositions, ParticleAngles), MagnificationCorrection);
if (options.CorrectCTF)
Projectors[threadID].BackProject(CTFsComplex[threadID], CTFsUnweighted[threadID], !options.PrerotateParticles ? GetAngleInAllTilts(ParticlePositions) : GetParticleAngleInAllTilts(ParticlePositions, ParticleAngles), MagnificationCorrection);
else
Projectors[threadID].BackProject(CTFsComplex[threadID], CTFsAbs[threadID], !options.PrerotateParticles ? GetAngleInAllTilts(ParticlePositions) : GetParticleAngleInAllTilts(ParticlePositions, ParticleAngles), MagnificationCorrection);

//Projectors[threadID].Weights.Fill(1);
Projectors[threadID].Data.Multiply(ProjectorsMultiplicity[threadID].Weights);
Expand Down Expand Up @@ -343,4 +360,6 @@ public class ProcessingOptionsTomoSubReconstruction : TomoProcessingOptionsBase
[WarpSerializable] public int NTilts { get; set; }
[WarpSerializable] public bool MakeSparse { get; set; }
[WarpSerializable] public bool UseCPU { get; set; }
}
[WarpSerializable] public bool CorrectCTF { get; set; }
[WarpSerializable] public bool OutputCTFCSV { get; set; }
}
25 changes: 22 additions & 3 deletions WarpLib/TiltSeries/TiltSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -945,16 +945,16 @@ public override Image GetImagesForOneParticle(ProcessingOptionsBase options, Ima

#region GetCTFs methods

public override Image GetCTFsForOneParticle(ProcessingOptionsBase options, float3 coords, Image ctfCoords, Image gammaCorrection, bool weighted = true, bool weightsonly = false, bool useglobalweights = false, Image result = null)
public override Image GetCTFsForOneParticle(ProcessingOptionsBase options, float3 coords, Image ctfCoords, Image gammaCorrection, bool weighted = true, bool weightsonly = false, bool useglobalweights = false, Image result = null, string outputpath = "")
{
float3[] PerTiltCoords = new float3[NTilts];
for (int i = 0; i < NTilts; i++)
PerTiltCoords[i] = coords;

return GetCTFsForOneParticle(options, PerTiltCoords, ctfCoords, gammaCorrection, weighted, weightsonly, useglobalweights, result);
return GetCTFsForOneParticle(options, PerTiltCoords, ctfCoords, gammaCorrection, weighted, weightsonly, useglobalweights, result, outputpath);
}

public override Image GetCTFsForOneParticle(ProcessingOptionsBase options, float3[] coordsMoving, Image ctfCoords, Image gammaCorrection, bool weighted = true, bool weightsonly = false, bool useglobalweights = false, Image result = null)
public override Image GetCTFsForOneParticle(ProcessingOptionsBase options, float3[] coordsMoving, Image ctfCoords, Image gammaCorrection, bool weighted = true, bool weightsonly = false, bool useglobalweights = false, Image result = null, string outputpath = "")
{
float3[] ImagePositions = GetPositionInAllTilts(coordsMoving);

Expand Down Expand Up @@ -1017,6 +1017,25 @@ public override Image GetCTFsForOneParticle(ProcessingOptionsBase options, float
Params[t] = CurrCTF.ToStruct();
}

if (outputpath != "")
{
using (StreamWriter writer = new StreamWriter(outputpath))
{
string output = string.Join(", ", typeof(CTFStruct)
.GetFields()
.Select(field => $"{field.Name}"));
writer.WriteLine($"TiltAngle, X, Y, Z, {output}");

for (int t = 0; t < NTilts; t++)
{
string output_t = string.Join(", ", typeof(CTFStruct)
.GetFields()
.Select(field => $"{field.GetValue(Params[t])}"));
writer.WriteLine($"{Angles[t]}, {coordsMoving[t].X}, {coordsMoving[t].Y}, {coordsMoving[t].Z}, {output_t}");
}
}
}

Image Result = result == null ? new Image(IntPtr.Zero, new int3(ctfCoords.Dims.X, ctfCoords.Dims.Y, NTilts), true) : result;
GPU.CreateCTF(Result.GetDevice(Intent.Write),
ctfCoords.GetDevice(Intent.Read),
Expand Down
12 changes: 10 additions & 2 deletions WarpTools/Commands/Tiltseries/ExportParticlesTiltseries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ExportParticlesTiltseriesOptions : DistributedOptions

[Option("3d", HelpText = "Output particles as 3d images (subtomograms)")]
public bool Output3DParticles { get; set; }

[OptionGroup("Expert options")]
[Option("dont_normalize_input",
HelpText =
Expand All @@ -87,6 +87,12 @@ class ExportParticlesTiltseriesOptions : DistributedOptions
"Don't normalize output particle volumes (only works with --3d)")]
public bool DontNormalizeSubtomos { get; set; }

[Option("output_ctf_csv", HelpText = "Export CTF parameters as CSV files")]
public bool OutputCTFCSV { get; set; }

[Option("dont_correct_ctf_3d", HelpText = "Dont apply ctf correction to 3d subtomogram")]
public bool DontCorrectCTFSubtomos { get; set; }

[Option("n_tilts",
HelpText =
"Number of tilt images to include in the output, images with the lowest overall exposure will be included first",
Expand Down Expand Up @@ -457,6 +463,8 @@ private ProcessingOptionsTomoSubReconstruction PrepareWarpWorkerExportOptions(
options.Tasks.Export2DBoxSize = cli.OutputBoxSize;
options.Tasks.Export2DParticleDiameter = cli.OutputBoxSize;
options.Tasks.InputNormalize = !cli.DontNormalizeInputImages;
options.Tasks.TomoSubReconstructCTFCSV = cli.OutputCTFCSV;
options.Tasks.TomoSubReconstructCorrectCTF = !cli.DontCorrectCTFSubtomos;

options.Tasks.OutputNormalize =
cli.Output3DParticles && !cli.DontNormalizeSubtomos;
Expand Down Expand Up @@ -1168,4 +1176,4 @@ int maxMissingTilts


}
}
}