Skip to content

Commit

Permalink
Fix mask source, minor changes (#123)
Browse files Browse the repository at this point in the history
* fix: mask source was always acting on all slices

* if no file is selected, skip BeginDataexport/EndDataexport in SaveAs/SaveCopyAs

* morpho: limit number of threads for running all slices (2d)

Co-authored-by: Bryn Lloyd <[email protected]>
  • Loading branch information
dyollb and dyollb authored Oct 12, 2021
1 parent 9faaf54 commit 5a2bd1d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 30 deletions.
10 changes: 10 additions & 0 deletions Core/Morpho.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "Data/ItkProgressObserver.h"
#include "Data/ItkUtils.h"
#include "Data/ScopeExit.h"
#include "Data/SlicesHandlerITKInterface.h"

#include <itkBinaryDilateImageFilter.h>
Expand All @@ -21,6 +22,10 @@
#include <itkImageRegionIteratorWithIndex.h>
#include <itkPasteImageFilter.h>

#ifndef NO_OPENMP_SUPPORT
# include <omp.h>
#endif

#include <boost/variant.hpp>

namespace iseg {
Expand Down Expand Up @@ -177,6 +182,11 @@ void MorphologicalOperation(iseg::SlicesHandlerInterface* handler, boost::varian
progress->SetNumberOfSteps(endslice - startslice);
}

#ifndef NO_OPENMP_SUPPORT
const auto guard = MakeScopeExit([nthreads = omp_get_num_threads()]() { omp_set_num_threads(nthreads); });
omp_set_num_threads(std::max<int>(2, std::thread::hardware_concurrency() / 2));
#endif

#pragma omp parallel for
for (std::int64_t slice = startslice; slice < endslice; ++slice)
{
Expand Down
54 changes: 54 additions & 0 deletions Data/ScopeExit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2021 The Foundation for Research on Information Technologies in Society (IT'IS).
*
* This file is part of iSEG
* (see https://github.com/ITISFoundation/osparc-iseg).
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

#pragma once

namespace iseg {

/** This simple template class allows to execute arbitrary code when leaving a scope.
\note Instances can only be moved, not copied.
*/
template <typename F>
struct ScopeExit
{
explicit ScopeExit(F f)
: f(std::move(f))
, m_Empty(false)
{}
~ScopeExit()
{
if (!m_Empty)
f();
}

F f;

ScopeExit(ScopeExit && rhs) noexcept
: f(std::move(rhs.f))
{
rhs.m_Empty = true;
m_Empty = false;
}

private:
ScopeExit(const ScopeExit & rhs) = delete;
void operator=(const ScopeExit & rhs) = delete;
bool m_Empty;
};

/// Creates
template <typename F>
ScopeExit<F> MakeScopeExit(F f)
{
return std::move(ScopeExit<F>(std::move(f)));
};

} // namespace iseg
46 changes: 23 additions & 23 deletions iSeg/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2559,18 +2559,17 @@ void MainWindow::ExecuteSaveimg()

void MainWindow::ExecuteSaveprojas()
{
DataSelection data_selection;
data_selection.bmp = true;
data_selection.work = true;
data_selection.tissues = true;
data_selection.tissueHierarchy = true;
emit BeginDataexport(data_selection, this);

QString savefilename = RecentPlaces::GetSaveFileName(this, "Save as", QString::null, "Projects (*.prj)");

if (!savefilename.isEmpty())
if (savefilename.length() > 4)
{
if (savefilename.length() <= 4 || !savefilename.endsWith(QString(".prj")))
DataSelection data_selection;
data_selection.bmp = true;
data_selection.work = true;
data_selection.tissues = true;
data_selection.tissueHierarchy = true;
emit BeginDataexport(data_selection, this);

if (!savefilename.endsWith(QString(".prj")))
savefilename.append(".prj");

m_MSaveprojfilename = savefilename;
Expand Down Expand Up @@ -2655,24 +2654,24 @@ void MainWindow::ExecuteSaveprojas()
QFile::rename(temp_file_name_without_extension + ".h5", source_file_name_without_extension + ".h5");

progress.setValue(num_tasks);

emit EndDataexport(this);
}
emit EndDataexport(this);
}

void MainWindow::ExecuteSavecopyas()
{
DataSelection data_selection;
data_selection.bmp = true;
data_selection.work = true;
data_selection.tissues = true;
data_selection.tissueHierarchy = true;
emit BeginDataexport(data_selection, this);

QString savefilename = RecentPlaces::GetSaveFileName(this, "Save as", QString::null, "Projects (*.prj)");

if (!savefilename.isEmpty())
QString savefilename = RecentPlaces::GetSaveFileName(this, "Save copy as", QString::null, "Projects (*.prj)");
if (savefilename.length() > 4)
{
if (savefilename.length() <= 4 || !savefilename.endsWith(QString(".prj")))
DataSelection data_selection;
data_selection.bmp = true;
data_selection.work = true;
data_selection.tissues = true;
data_selection.tissueHierarchy = true;
emit BeginDataexport(data_selection, this);

if (!savefilename.endsWith(QString(".prj")))
savefilename.append(".prj");

FILE* fp = m_Handler3D->SaveProject(savefilename.ascii(), "xmf");
Expand Down Expand Up @@ -2705,8 +2704,9 @@ void MainWindow::ExecuteSavecopyas()
fp = SaveNotes(fp, save_proj_version);

fclose(fp);

emit EndDataexport(this);
}
emit EndDataexport(this);
}

void MainWindow::SaveSettings()
Expand Down
27 changes: 20 additions & 7 deletions iSeg/SlicesHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7839,14 +7839,12 @@ void SlicesHandler::MaskSource(bool all_slices, float maskvalue)
mask.at(label) = true;
}

const int i_n = m_Endslice - m_Startslice;
auto sources = SourceSlices();
auto tissues = TissueSlices(0);

#pragma omp parallel for
for (int i = 0; i < i_n; ++i)
{
const int slice = m_Startslice + i;
const auto tissue = TissueSlices(0)[slice];
auto source = SourceSlices()[slice];
const auto mask_slice = [this, &sources, &tissues, &mask](int slice, float maskvalue) {
auto source = sources[slice];
auto tissue = tissues[slice];

for (unsigned int k = 0; k < m_Area; ++k)
{
Expand All @@ -7855,6 +7853,21 @@ void SlicesHandler::MaskSource(bool all_slices, float maskvalue)
source[k] = maskvalue;
}
}
};

if (all_slices)
{
const int i_n = m_Endslice - m_Startslice;

#pragma omp parallel for
for (int i = 0; i < i_n; ++i)
{
mask_slice(m_Startslice + i, maskvalue);
}
}
else
{
mask_slice(m_Activeslice, maskvalue);
}
}

Expand Down

0 comments on commit 5a2bd1d

Please sign in to comment.