Skip to content

Commit

Permalink
Simplify down-sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
spinicist committed Apr 16, 2024
1 parent 47a0a24 commit 0e28cfa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
3 changes: 1 addition & 2 deletions cxx/riesling/util/downsamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ void main_downsamp(args::Subparser &parser)
args::ValueFlag<std::string> oname(parser, "OUTPUT", "Override output name", {'o', "out"});
args::ValueFlag<Eigen::Array3f, Array3fReader> res(parser, "R", "Target resolution (4 mm)", {"res"},
Eigen::Array3f::Constant(4.f));
args::ValueFlag<Index> lores(parser, "L", "First N traces are lo-res", {"lores"}, 0);
args::ValueFlag<float> filterStart(parser, "T", "Tukey filter start", {"filter-start"}, 0.5f);
args::ValueFlag<float> filterEnd(parser, "T", "Tukey filter end", {"filter-end"}, 1.0f);
args::Flag noShrink(parser, "S", "Do not shrink matrix", {"no-shrink"});
Expand All @@ -25,7 +24,7 @@ void main_downsamp(args::Subparser &parser)
Info info = reader.readInfo();
Trajectory traj(reader, info.voxel_size);
auto const ks1 = reader.readTensor<Cx5>();
auto [dsTraj, ks2] = traj.downsample(ks1, res.Get(), lores.Get(), !noShrink, corners);
auto [dsTraj, ks2] = traj.downsample(ks1, res.Get(), 0, !noShrink, corners);

if (filterStart || filterEnd) {
NoncartesianTukey(filterStart.Get() * 0.5, filterEnd.Get() * 0.5, 0.f, dsTraj.points(), ks2);
Expand Down
27 changes: 14 additions & 13 deletions cxx/vineyard/trajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,18 @@ Re1 Trajectory::point(int16_t const read, int32_t const spoke) const
return p;
}

auto Trajectory::downsample(Eigen::Array3f const tgtSize, Index const lores, bool const shrink, bool const corners) const
-> std::tuple<Trajectory, Index, Index>
auto Trajectory::downsample(Eigen::Array3f const tgtSize,
Index const fullResTraces,
bool const shrink,
bool const corners) const -> std::tuple<Trajectory, Index, Index>
{
Eigen::Array3f ratios = voxel_size_ / tgtSize;
if ((ratios > 1.f).any()) {
Log::Fail("Downsample voxel-size {} is larger than current voxel-size {}", tgtSize, voxel_size_);
}
auto dsVox = voxel_size_;
auto dsMatrix = matrix_;
Re1 thresh(nDims());
auto dsVox = voxel_size_;
auto dsMatrix = matrix_;
Re1 thresh(nDims());
for (Index ii = 0; ii < nDims(); ii++) {
if (shrink) {
// Account for rounding
Expand All @@ -184,7 +186,7 @@ auto Trajectory::downsample(Eigen::Array3f const tgtSize, Index const lores, boo
for (int ii = 0; ii < nDims(); ii++) {
p(ii) /= ratios(ii);
}
if (it >= lores) { // Ignore lo-res traces for this calculation
if (it < fullResTraces) { // Ignore lo-res traces for this calculation
minSamp = std::min(minSamp, is);
maxSamp = std::max(maxSamp, is);
}
Expand All @@ -195,32 +197,31 @@ auto Trajectory::downsample(Eigen::Array3f const tgtSize, Index const lores, boo
}
if (minSamp > maxSamp) { Log::Fail("No valid trajectory points remain after downsampling"); }
Index const dsSamples = maxSamp + 1 - minSamp;
Log::Print("Target res {} mm, ratios {}, matrix {}, voxel-size {} mm, read-points {}-{}{}", tgtSize,
fmt::streamed(ratios.transpose()), dsMatrix, dsVox.transpose(), minSamp, maxSamp,
lores > 0 ? fmt::format(", ignoring {} lo-res traces", lores) : "");
Log::Print("Target res {} mm, ratios {}, matrix {}, voxel-size {} mm, read-points {}-{}", tgtSize,
fmt::streamed(ratios.transpose()), dsMatrix, dsVox.transpose(), minSamp, maxSamp);
dsPoints = Re3(dsPoints.slice(Sz3{0, minSamp, 0}, Sz3{nDims(), dsSamples, nTraces()}));
Log::Print("Downsampled trajectory dims {}", dsPoints.dimensions());
return std::make_tuple(Trajectory(dsPoints, dsMatrix, dsVox), minSamp, dsSamples);
}

auto Trajectory::downsample(Cx5 const &ks,
Eigen::Array3f const tgt,
Index const lores,
Index const fullResTraces,
bool const shrink,
bool const corners) const -> std::tuple<Trajectory, Cx5>
{
auto const [dsTraj, minSamp, nSamp] = downsample(tgt, lores, shrink, corners);
auto const [dsTraj, minSamp, nSamp] = downsample(tgt, fullResTraces, shrink, corners);
Cx5 dsKs = ks.slice(Sz5{0, minSamp, 0, 0, 0}, Sz5{ks.dimension(0), nSamp, ks.dimension(2), ks.dimension(3), ks.dimension(4)});
return std::make_tuple(dsTraj, dsKs);
}

auto Trajectory::downsample(Cx4 const &ks,
Eigen::Array3f const tgt,
Index const lores,
Index const fullResTraces,
bool const shrink,
bool const corners) const -> std::tuple<Trajectory, Cx4>
{
auto const [dsTraj, minSamp, nSamp] = downsample(tgt, lores, shrink, corners);
auto const [dsTraj, minSamp, nSamp] = downsample(tgt, fullResTraces, shrink, corners);
Cx4 dsKs = ks.slice(Sz4{0, minSamp, 0, 0}, Sz4{ks.dimension(0), nSamp, ks.dimension(2), ks.dimension(3)});
return std::make_tuple(dsTraj, dsKs);
}
Expand Down
10 changes: 6 additions & 4 deletions cxx/vineyard/trajectory.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "info.hpp"
#include "types.hpp"
#include "io/reader.hpp"
#include "io/writer.hpp"
#include "types.hpp"

namespace rl {

Expand All @@ -25,11 +25,13 @@ struct Trajectory
void shiftFOV(Eigen::Vector3f const, Cx5 &data);
auto point(int16_t const sample, int32_t const trace) const -> Re1;
auto points() const -> Re3 const &;
auto downsample(Eigen::Array3f const tgtSize, Index const lores, bool const shrink, bool const corners) const
auto downsample(Eigen::Array3f const tgtSize, Index const fullResTraces, bool const shrink, bool const corners) const
-> std::tuple<Trajectory, Index, Index>;
auto downsample(Cx4 const &ks, Eigen::Array3f const tgtSize, Index const lores, bool const shrink, bool const corners) const
auto
downsample(Cx4 const &ks, Eigen::Array3f const tgtSize, Index const fullResTraces, bool const shrink, bool const corners) const
-> std::tuple<Trajectory, Cx4>;
auto downsample(Cx5 const &ks, Eigen::Array3f const tgtSize, Index const lores, bool const shrink, bool const corners) const
auto
downsample(Cx5 const &ks, Eigen::Array3f const tgtSize, Index const fullResTraces, bool const shrink, bool const corners) const
-> std::tuple<Trajectory, Cx5>;

private:
Expand Down

0 comments on commit 0e28cfa

Please sign in to comment.