Skip to content

Commit

Permalink
Merge pull request #138 from friskluft/POL3369_fixed_featurize_files
Browse files Browse the repository at this point in the history
POL3369 Python API changes: [1] fixed featurize_files(); [2] control of verbosity level
  • Loading branch information
sameeul authored Aug 24, 2023
2 parents f779aa1 + 86f354c commit 813b7aa
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ features = nyx.featurize_files(
"/path/to/images/labels/m1.ome.tif",
"/path/to/images/labels/m2.ome.tif",
"/path/to/images/labels/m2.ome.tif"
])
],
False)
```

The `features` variable is a Pandas dataframe similar to what is shown below.
Expand Down
6 changes: 3 additions & 3 deletions src/nyx/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ bool Environment::parse_cmdline(int argc, char **argv)
find_string_argument(i, GLCMANGLES, rawGlcmAngles) ||
find_string_argument(i, PXLDIST, pixel_distance) ||
find_string_argument(i, COARSEGRAYDEPTH, raw_coarse_grayscale_depth) ||
find_string_argument(i, VERBOSITY, verbosity) ||
find_string_argument(i, VERBOSITY, rawVerbosity) ||
find_string_argument(i, IBSICOMPLIANCE, raw_ibsi_compliance) ||
find_string_argument(i, RAMLIMIT, rawRamLimit) ||
find_string_argument(i, TEMPDIR, rawTempDir) ||
Expand Down Expand Up @@ -918,10 +918,10 @@ bool Environment::parse_cmdline(int argc, char **argv)
}
}

if (!verbosity.empty())
if (!rawVerbosity.empty())
{
// string -> integer
if (sscanf(verbosity.c_str(), "%d", &verbosity_level) != 1 || verbosity_level < 0)
if (sscanf(rawVerbosity.c_str(), "%d", &verbosity_level) != 1 || verbosity_level < 0)
{
std::cout << "Error: " << VERBOSITY << "=" << reduce_threads << ": expecting a positive integer constant\n";
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/nyx/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Environment: public BasicEnvironment
std::string rawGlcmAngles = "";
std::vector<int> glcmAngles = {0, 45, 90, 135};

std::string verbosity = ""; // 'verbosity_level' is inherited from BasicEnvironment
std::string rawVerbosity = ""; // 'verbosity_level' is inherited from BasicEnvironment

std::string rawOnlineStatsThresh = "";
int onlineStatsTreshold = 0;
Expand Down
13 changes: 7 additions & 6 deletions src/nyx/python/new_bindings_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void set_environment_params_imp (
uint32_t n_reduce_threads = 0,
uint32_t n_loader_threads = 0,
int using_gpu = -2,
int verbosity_level = 0
int verb_level = 0
) {
if (features.size() > 0) {
theEnvironment.recognizedFeatureNames = features;
Expand All @@ -132,8 +132,10 @@ void set_environment_params_imp (
theEnvironment.n_loader_threads = n_loader_threads;
}

if (verbosity_level != 0)
theEnvironment.verbosity = verbosity_level;
if (verb_level >= 0)
theEnvironment.set_verbosity_level (verb_level);
else
throw std::runtime_error("Error: verbosity (" + std::to_string(verb_level) + ") should be a non-negative value");
}

py::tuple featurize_directory_imp (
Expand Down Expand Up @@ -289,10 +291,10 @@ py::tuple featurize_montage_imp (
return py::make_tuple(error_message);
}

py::tuple featurize_fname_lists_imp (const py::list& int_fnames, const py::list & seg_fnames, bool pandas_output=true)
py::tuple featurize_fname_lists_imp (const py::list& int_fnames, const py::list & seg_fnames, bool single_roi, bool pandas_output=true)
{
// Set the whole-slide/multi-ROI flag
theEnvironment.singleROI = false;
theEnvironment.singleROI = single_roi;

std::vector<std::string> intensFiles, labelFiles;
for (auto it = int_fnames.begin(); it != int_fnames.end(); ++it)
Expand Down Expand Up @@ -363,7 +365,6 @@ py::tuple featurize_fname_lists_imp (const py::list& int_fnames, const py::list
pyNumData = pyNumData.reshape({ nRows, pyNumData.size() / nRows });

return py::make_tuple(pyHeader, pyStrData, pyNumData);

}

// Return "nothing" when output will be an Arrow format
Expand Down
12 changes: 8 additions & 4 deletions src/nyx/python/nyxus/nyxus.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,18 +388,20 @@ def featurize_files (
self,
intensity_files: list,
mask_files: list,
single_roi: bool,
output_type: Optional[str] = "pandas",
output_path: Optional[str] = ""):
"""Extract features from image file pairs passed as lists
Extracts all the requested features _at the image level_ from the intensity images
present in list `intensity_files` with respect to region of interest masks presented in
list `mask_files`. Multiple
list `mask_files`. Single-ROI feature extraction is enforced via passing 'True' as parameter 'single_roi'
Parameters
----------
intensity_files : list of intensity image file paths
mask_files : list of mask image file paths
single_roi : 'True' to treat items of 'intensity_files' as single-ROI ('mask_files' will be ignored), 'False' to treat items of 'intensity_files' and 'mask_files' as intensity/segmentation image pairs
Returns
-------
Expand All @@ -415,11 +417,11 @@ def featurize_files (
raise IOError ("The list of segment file paths is empty")

if (output_type not in self._valid_output_types):
raise ValueError(f'Invalid output type {output_type}. Valid output types are {self._valid_output_types}.')
raise ValueError(f'Invalid output type {output_type}. Valid output types are {self._valid_output_types}')

if (output_type == 'pandas'):

header, string_data, numeric_data = featurize_fname_lists_imp (intensity_files, mask_files, True)
header, string_data, numeric_data = featurize_fname_lists_imp (intensity_files, mask_files, single_roi, True)

df = pd.concat(
[
Expand All @@ -437,7 +439,7 @@ def featurize_files (

else:

featurize_fname_lists_imp (intensity_files, mask_files, False)
featurize_fname_lists_imp (intensity_files, mask_files, single_roi, False)

output_type = output_type.lower() # ignore case of output type

Expand Down Expand Up @@ -579,6 +581,8 @@ def set_environment_params (self, **params):
Id of the gpu to use. To find available gpus along with ids, using nyxus.get_gpu_properties().
The default value of -1 uses cpu calculations. Note that the gpu features only support a single
thread for feature calculation.
* verbose: int (optional, non-negative, default 0)
Level of diagnostic output shown in the console. Set '0' to disable any diagnostic output.
"""

Expand Down

0 comments on commit 813b7aa

Please sign in to comment.