Skip to content

Commit

Permalink
Merge pull request #192 from friskluft/POL3837_new_3d_layout
Browse files Browse the repository at this point in the history
POL3837 new 3D layout
  • Loading branch information
sameeul authored Jan 19, 2024
2 parents e57f3c1 + 29f2d6b commit effc33c
Show file tree
Hide file tree
Showing 121 changed files with 6,093 additions and 3,248 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ set(SOURCE
src/nyx/features/image_moments.cpp
src/nyx/features/image_moments_nontriv.cpp
src/nyx/features/intensity.cpp
src/nyx/features/intensity_3d.cpp
src/nyx/features/neighbors.cpp
src/nyx/features/ngldm.cpp
src/nyx/features/ngtdm.cpp
Expand All @@ -185,6 +186,7 @@ set(SOURCE
src/nyx/dirs_and_files.cpp
src/nyx/environment.cpp
src/nyx/environment_basic.cpp
src/nyx/env_features.cpp
src/nyx/feature_method.cpp
src/nyx/feature_mgr.cpp
src/nyx/feature_mgr_init.cpp
Expand All @@ -206,6 +208,7 @@ set(SOURCE
src/nyx/roi_cache.cpp
src/nyx/roi_cache_basic.cpp
src/nyx/scan_fastloader_way.cpp
src/nyx/strpat.cpp
)


Expand Down
2 changes: 2 additions & 0 deletions src/nyx/cli_gabor_options.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "cli_gabor_options.h"
#include "features/gabor.h"

using namespace Nyxus;

bool GaborOptions::parse_input()
{
// OK to have both angles and frequencies specified but not one of them
Expand Down
2 changes: 1 addition & 1 deletion src/nyx/cli_nested_roi_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ namespace Nyxus
ofile << s << ",";

// User feature selection
std::vector<std::tuple<std::string, AvailableFeatures>> F = theFeatureSet.getEnabledFeatures();
std::vector<std::tuple<std::string, int>> F = theFeatureSet.getEnabledFeatures();
for (auto& f : F)
{
auto fn = std::get<0>(f); // feature name
Expand Down
177 changes: 148 additions & 29 deletions src/nyx/dirs_and_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
#else
error "Missing the <filesystem> header."
#endif

#include <vector>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <tiffio.h>
#include "dirs_and_files.h"
#include "strpat.h"
#include "helpers/helpers.h"

namespace Nyxus
{
Expand All @@ -27,7 +32,7 @@ namespace Nyxus
return fs::exists(p);
}

void readDirectoryFiles(const std::string& dir, const std::string& file_pattern, std::vector<std::string>& files)
void readDirectoryFiles_2D (const std::string& dir, const std::string& file_pattern, std::vector<std::string>& files)
{
std::regex re(file_pattern);

Expand All @@ -48,29 +53,7 @@ namespace Nyxus
}
}

int datasetDirsOK(const std::string& dirIntens, const std::string& dirLabels, const std::string& dirOut, bool mustCheckDirOut)
{
if (!existsOnFilesystem(dirIntens))
{
std::cout << "Error: " << dirIntens << " is not a directory" << std::endl;
return 1;
}

if (!existsOnFilesystem(dirLabels))
{
std::cout << "Error: " << dirLabels << " is not a directory" << std::endl;
return 2;
}

if (mustCheckDirOut && !existsOnFilesystem(dirOut))
{
std::cout << "Error: " << dirOut << " is not a directory" << std::endl;
return 3;
}
return 0; // success
}

int read_dataset (
int read_2D_dataset (
// input
const std::string& dirIntens,
const std::string& dirLabels,
Expand All @@ -83,9 +66,7 @@ namespace Nyxus
std::vector <std::string>& intensFiles,
std::vector <std::string>& labelFiles)
{
// Check the directories
if (datasetDirsOK(dirIntens, dirLabels, dirOut, mustCheckDirOut) != 0)
return 1; // No need to issue console messages here, datasetDirsOK() does that
// Check directories

if (!existsOnFilesystem(dirIntens))
{
Expand All @@ -106,8 +87,8 @@ namespace Nyxus
if (intLabMappingFile.empty())
{
// Common case - no ad hoc intensity-label file mapping, 1-to-1 correspondence instead
readDirectoryFiles(dirIntens, filePatt, intensFiles);
readDirectoryFiles(dirLabels, filePatt, labelFiles);
readDirectoryFiles_2D (dirIntens, filePatt, intensFiles);
readDirectoryFiles_2D (dirLabels, filePatt, labelFiles);

// Check if the dataset is meaningful
if (intensFiles.size() == 0 || labelFiles.size() == 0)
Expand Down Expand Up @@ -219,4 +200,142 @@ namespace Nyxus
else { throw (std::runtime_error("Tile Loader ERROR: The file can not be opened.")); }
}



bool readDirectoryFiles_3D (const std::string & dir, const StringPattern & filePatt, std::vector <Imgfile3D_layoutA> & files)
{
// grammar is OK to read data
std::map<std::string, std::vector<std::string>> imgDirs;

for (auto& fpath : fs::directory_iterator(dir))
{
// Skip hidden objects, e.g. directories '.DS_store' in OSX
if (fpath.path().filename().string()[0] == '.')
continue;

std::string fullPath = fpath.path().string(),
pureFname = fpath.path().filename().string();

std::string ermsg;
if (!filePatt.match(pureFname, imgDirs, ermsg))
{
std::cerr << "Error parsing file name " << pureFname << ": " << ermsg << '\n';
break;
}

} //- directory scan loop

// copy the file info to the external container
files.clear();
size_t i = 0;
for (const auto & x : imgDirs)
{
Nyxus::Imgfile3D_layoutA img3;
img3.fname = x.first; // image name
img3.z_indices = x.second; // z-indices as they are in corresponding file names
files.push_back(img3);
}

return true;
}

int read_3D_dataset(
// input:
const std::string& dirIntens,
const std::string& dirLabels,
const StringPattern& filePatt,
const std::string& dirOut,
const std::string& intLabMappingDir,
const std::string& intLabMappingFile,
bool mustCheckDirOut,
// output:
std::vector <Imgfile3D_layoutA>& intensFiles,
std::vector <Imgfile3D_layoutA>& labelFiles)
{
if (!existsOnFilesystem(dirIntens))
{
std::cout << "Error: nonexisting directory " << dirIntens << std::endl;
return 1;
}
if (!existsOnFilesystem(dirLabels))
{
std::cout << "Error: nonexisting directory " << dirLabels << std::endl;
return 1;
}
if (!existsOnFilesystem(dirOut))
{
std::cout << "Error: nonexisting directory " << dirOut << std::endl;
return 1;
}

// Common case - no ad hoc intensity-label file mapping, 1-to-1 correspondence instead
if (!readDirectoryFiles_3D(dirIntens, filePatt, intensFiles))
{
std::cerr << "Error reading directory " << dirIntens << '\n';
return 1;
}
if (!readDirectoryFiles_3D (dirLabels, filePatt, labelFiles))
{
std::cerr << "Error reading directory " << dirLabels << '\n';
return 1;
}

// Check if the dataset isn't blank
if (intensFiles.size() == 0 || labelFiles.size() == 0)
{
std::cout << "No intensity and/or label file pairs to process, probably due to file pattern " << filePatt.get_cached_pattern_string() << std::endl;
return 2;
}

// Shallow consistency check
if (intensFiles.size() != labelFiles.size())
{
std::cout << "Mismatch: " << intensFiles.size() << " intensity images vs " << labelFiles.size() << " mask images\n";
return 3;
}

// Deep consistency check
auto nf = intensFiles.size();
for (auto i = 0; i < nf; i++)
{
auto& file_i = intensFiles[i],
& file_m = labelFiles[i];

// name mismatch ?
if (file_i.fname != file_m.fname)
{
std::cerr << "Mismatch: intensity " << file_i.fname << " mask " << file_m.fname << '\n';
return 3;
}

// z-stack size mismatch ?
if (file_i.z_indices.size() != file_m.z_indices.size())
{
std::cerr << "Z-stack size mismatch: intensity " << file_i.z_indices.size() << " mask " << file_m.z_indices.size() << '\n';
return 3;
}

// z-stack indices mismatch ?
std::sort (file_i.z_indices.begin(), file_i.z_indices.end());
std::sort (file_m.z_indices.begin(), file_m.z_indices.end());
for (auto j=0; j< file_i.z_indices.size(); j++)
if (file_i.z_indices[j] != file_m.z_indices[j])
{
std::cerr << "Mismatch in z-stack indices: " << file_i.fname << "[" << j << "] != " << file_m.fname << "[" << j << "]\n";
return 3;
}
}

// let each file know its directory
for (auto i = 0; i < nf; i++)
{
auto& file_i = intensFiles[i],
& file_m = labelFiles[i];
file_i.fdir = dirIntens + '/';
file_m.fdir = dirLabels + '/';
}

return 0;
}

} // namespace Nyxus
35 changes: 24 additions & 11 deletions src/nyx/dirs_and_files.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
// Helper functions for manipulating directories and files
//

#include <map>
#include <string>
#include <vector>
#include "strpat.h"

namespace Nyxus
{
Expand All @@ -23,17 +25,8 @@ namespace Nyxus
/// @param dir
/// @param file_pattern
/// @param files
void readDirectoryFiles(const std::string& dir, const std::string& file_pattern, std::vector<std::string>& files);

/// @brief Nyxus specific: checks if the intensity, mask, and output directories exist
/// @param dirIntens
/// @param dirLabels
/// @param dirOut
/// @param mustCheckDirOut
/// @return
int datasetDirsOK(const std::string& dirIntens, const std::string& dirLabels, const std::string& dirOut, bool mustCheckDirOut);

int read_dataset(

int read_2D_dataset(
// input:
const std::string& dirIntens,
const std::string& dirLabels,
Expand All @@ -46,6 +39,26 @@ namespace Nyxus
std::vector <std::string>& intensFiles,
std::vector <std::string>& labelFiles);

struct Imgfile3D_layoutA
{
public:
std::string fname, fdir;
std::vector<std::string> z_indices;
};

int read_3D_dataset(
// input:
const std::string& dirIntens,
const std::string& dirLabels,
const StringPattern& filePatt,
const std::string& dirOut,
const std::string& intLabMappingDir,
const std::string& intLabMappingFile,
bool mustCheckDirOut,
// output:
std::vector <Imgfile3D_layoutA>& intensFiles,
std::vector <Imgfile3D_layoutA>& labelFiles);

/// @brief checks if the Tiff file is tiled or not
/// @param filePath File name with complete path
bool check_tile_status(const std::string& filePath);
Expand Down
Loading

0 comments on commit effc33c

Please sign in to comment.