Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
robomics committed Nov 7, 2024
1 parent 3d6b49e commit 666135a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/libnchg/expected_matrix_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ inline ExpectedMatrixStats::ExpectedMatrixStats(const Pixels &pixels, hictk::Chr
_min_delta(min_delta_),
_max_delta(max_delta_) {
if (scaling_factor != 1) {
std::ranges::transform(_weights, _weights.begin(),
[&](const auto n) { return n / scaling_factor; });
std::ranges::transform(_weights, _weights.begin(), [&](const auto n) {
const auto scaled_n = n / scaling_factor;
return std::isfinite(scaled_n) ? scaled_n : 0.0;
});
}

MatrixStats<double> stats(_chrom1, _chrom2, bin_mask1, bin_mask2, bins.resolution(), min_delta_,
Expand Down
4 changes: 4 additions & 0 deletions src/libnchg/matrix_stats_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ template <typename N>
return true;
}

if (_bin_mask1->empty() || _bin_mask2->empty()) {
return false;
}

const auto bin1_id = bin1.rel_id();
const auto bin2_id = bin2.rel_id();

Expand Down
2 changes: 1 addition & 1 deletion src/nchg/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void Cli::make_compute_subcommand() {
"-v,--verbosity",
c.verbosity,
"Set verbosity of output to the console.")
->check(CLI::Range(1, 5))
->check(CLI::Range(1, 4))
->capture_default_str();
// clang-format on

Expand Down
15 changes: 9 additions & 6 deletions src/nchg/compute/compute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ using ChromosomePairs = std::vector<std::pair<hictk::Chromosome, hictk::Chromoso
assert(c.output_prefix.empty());
assert(!c.output_path.empty());

SPDLOG_INFO("begin processing {}:{}", chrom1.name(), chrom2.name());
SPDLOG_INFO("[{}:{}]: begin processing...", chrom1.name(), chrom2.name());

std::vector<std::string> args{
"compute",
Expand All @@ -349,7 +349,7 @@ using ChromosomePairs = std::vector<std::pair<hictk::Chromosome, hictk::Chromoso
"--compression-level",
fmt::to_string(c.compression_lvl),
"--compression-method",
fmt::to_string(c.compression_method),
c.compression_method,
"--verbosity",
"2",
c.path_to_hic.string(),
Expand Down Expand Up @@ -398,11 +398,13 @@ using ChromosomePairs = std::vector<std::pair<hictk::Chromosome, hictk::Chromoso
boost::process::child proc(
c.exec.string(), args,
boost::process::std_in<boost::process::null, boost::process::std_out> boost::process::null);
if (proc.running()) {
SPDLOG_DEBUG("spawned worker process {}...", proc.id());
if (proc.running() || proc.exit_code() == 0) {
SPDLOG_DEBUG("[{}:{}]: spawned worker process {}...", chrom1.name(), chrom2.name(),
proc.id());
return proc;
}
SPDLOG_DEBUG("spawning worker process failed (attempt {}/10)...", proc.id(), attempt + 1);
SPDLOG_WARN("[{}:{}]: spawning worker process {} failed (attempt {}/10)...", chrom1.name(),
chrom2.name(), proc.id(), attempt + 1);
proc.terminate();
}

Expand Down Expand Up @@ -514,8 +516,9 @@ static std::size_t process_queries_mt(
BS::multi_future<std::size_t> workers(chrom_pairs.size());
for (std::size_t i = 0; i < workers.size(); ++i) {
workers[i] = tpool.submit_task([&, i] {
SPDLOG_DEBUG("submitting task {}/{}...", i + 1, workers.size());
const auto &[chrom1, chrom2] = chrom_pairs[i];
SPDLOG_DEBUG("submitting task {}/{} ({}:{})...", i + 1, workers.size(), chrom1.name(),
chrom2.name());
return worker_fx(chrom1, chrom2, base_config, user_provided_expected_values, early_return);
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/nchg/filter/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ using RecordQueue = moodycamel::BlockingReaderWriterQueue<NCHGFilterResult>;
ParquetStatsFile f{c.input_path, ParquetStatsFile::RecordType::NCHGCompute};
auto first = f.begin<NCHGResult>();
const auto last = f.end<NCHGResult>();
for (; first != last; ++first) {
for (std::size_t i = 0; first != last; ++first, ++i) {
if (early_return) [[unlikely]] {
return records_enqueued;
}

const auto pvalue_corrected = corrected_pvalues.at(records_enqueued++);
const auto pvalue_corrected = corrected_pvalues.at(i);
assert(first->pval <= pvalue_corrected);

if (!c.drop_non_significant || (pvalue_corrected <= c.fdr && first->log_ratio >= c.log_ratio))
Expand All @@ -299,6 +299,7 @@ using RecordQueue = moodycamel::BlockingReaderWriterQueue<NCHGFilterResult>;
return records_enqueued;
}
}
++records_enqueued;
}
}

Expand Down
26 changes: 21 additions & 5 deletions src/nchg/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ class GlobalLogger {
_msg_buffer.clear();
}

static void reset_logger() noexcept {
try {
spdlog::set_default_logger(
std::make_shared<spdlog::logger>("main_logger", init_stderr_sink()));
} catch (...) { // NOLINT
}
}

public:
GlobalLogger() noexcept {
try {
Expand All @@ -141,6 +149,7 @@ class GlobalLogger {

~GlobalLogger() noexcept {
if (!_ok) {
reset_logger();
return;
}

Expand All @@ -151,6 +160,7 @@ class GlobalLogger {
} catch (...) {
print_noexcept("FAILURE! Failed to replay NCHG warnings: unknown error");
}
reset_logger();
}

static void set_level(int lvl) {
Expand Down Expand Up @@ -183,18 +193,20 @@ static auto global_logger = std::make_unique<GlobalLogger<256>>();

static auto acquire_global_logger() noexcept { return std::move(global_logger); }

static std::tuple<int, Cli::subcommand, Config> parse_cli_and_setup_logger(Cli &cli) {
template <typename Logger>
static std::tuple<int, Cli::subcommand, Config> parse_cli_and_setup_logger(Cli &cli,
Logger &logger) {
try {
auto config = cli.parse_arguments();
const auto subcmd = cli.get_subcommand();
const auto ec = cli.exit();
std::visit(
[&]<typename T>(const T &config_) {
if constexpr (!std::is_same_v<T, std::monostate>) {
if (global_logger && global_logger->ok()) {
global_logger->set_level(config_.verbosity); // NOLINT
if (logger.ok()) {
logger.set_level(config_.verbosity); // NOLINT
if (subcmd != Cli::subcommand::help) {
global_logger->print_welcome_msg();
logger.print_welcome_msg();
}
}
}
Expand Down Expand Up @@ -227,7 +239,7 @@ int main(int argc, char **argv) noexcept {

auto local_logger = acquire_global_logger();
cli = std::make_unique<Cli>(argc, argv);
const auto [ec, subcmd, config] = parse_cli_and_setup_logger(*cli);
const auto [ec, subcmd, config] = parse_cli_and_setup_logger(*cli, *local_logger);
if (ec != 0 || subcmd == Cli::subcommand::help) {
local_logger->clear();
return ec;
Expand Down Expand Up @@ -267,6 +279,10 @@ int main(int argc, char **argv) noexcept {
} catch (const std::bad_alloc &err) {
SPDLOG_CRITICAL("FAILURE! Unable to allocate enough memory: {}\n", err.what());
return 1;
} catch (const spdlog::spdlog_ex &e) {
fmt::print(stderr, "FAILURE! NCHG encountered the following error while logging: {}\n",
e.what());
return 1;
} catch (const std::exception &e) {
if (cli) {
SPDLOG_CRITICAL("FAILURE! NCHG {} encountered the following error: {}\n",
Expand Down

0 comments on commit 666135a

Please sign in to comment.