Skip to content

Commit 3d355d7

Browse files
authored
Quick-fix of segfaulting async (Python 3.12) (PolusAI#263)
* initial * dropped test_nonimq_wsi_scalability to please Python 3.12
1 parent 3140023 commit 3d355d7

File tree

7 files changed

+391
-413
lines changed

7 files changed

+391
-413
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ set(SOURCE
188188
src/nyx/features/texture_feature.cpp
189189
src/nyx/features/zernike.cpp
190190
src/nyx/features/zernike_nontriv.cpp
191+
src/nyx/helpers/diag.cpp
191192
src/nyx/helpers/timing.cpp
193+
src/nyx/arrow_helpers.cpp
192194
src/nyx/cli_fpimage_options.cpp
193195
src/nyx/cli_gabor_options.cpp
194196
src/nyx/cli_glcm_options.cpp
@@ -221,10 +223,10 @@ set(SOURCE
221223
src/nyx/roi_blacklist.cpp
222224
src/nyx/roi_cache.cpp
223225
src/nyx/roi_cache_basic.cpp
224-
src/nyx/scan_fastloader_way.cpp
225226
src/nyx/slideprops.cpp
226227
src/nyx/strpat.cpp
227228
src/nyx/workflow_3d.cpp
229+
src/nyx/workflow_pythonapi.cpp
228230
src/nyx/workflow_segmented.cpp
229231
src/nyx/workflow_wholeslide.cpp
230232
)

src/nyx/arrow_helpers.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <string>
2+
#include "environment.h"
3+
#include "helpers/fsystem.h"
4+
#include "helpers/helpers.h"
5+
6+
namespace Nyxus
7+
{
8+
9+
std::string get_arrow_filename(const std::string& output_path, const std::string& default_filename, const SaveOption& arrow_file_type) {
10+
11+
/*
12+
output_path condition verdict
13+
Case 1: /foo/bar exist in fs is a directory, append default filename with proper ext
14+
/foo/bar/ or ends with / or \
15+
\foo\bar\
16+
17+
Case 2: /foo/bar does not exist in fs assume the extension is missing, append proper ext
18+
but /foo exists
19+
20+
Case 3: /foo/bar neither /foo nor treat as directory, append default filename with proper ext
21+
/foo/bar exists in fs
22+
23+
Case 4: /foo/bar.ext exists in fs and is a append default filename with proper ext
24+
directory
25+
26+
Case 5: /foo/bar.ext does not exist in fs this is a file, check if ext is correct and modify if needed
27+
28+
Case 6: empty default filename with proper ext
29+
30+
31+
*/
32+
std::string valid_ext = [&arrow_file_type]() {
33+
if (arrow_file_type == Nyxus::SaveOption::saveArrowIPC) {
34+
return ".arrow";
35+
}
36+
else if (arrow_file_type == Nyxus::SaveOption::saveParquet) {
37+
return ".parquet";
38+
}
39+
else { return ""; }
40+
}();
41+
42+
if (output_path != "") {
43+
auto arrow_path = fs::path(output_path);
44+
if (fs::is_directory(arrow_path) // case 1, 4
45+
|| Nyxus::ends_with_substr(output_path, "/")
46+
|| Nyxus::ends_with_substr(output_path, "\\")) {
47+
arrow_path = arrow_path / default_filename;
48+
}
49+
else if (!arrow_path.has_extension()) {
50+
if (!fs::is_directory(arrow_path.parent_path())) { // case 3
51+
arrow_path = arrow_path / default_filename;
52+
}
53+
// else case 2, do nothing here
54+
}
55+
// case 5 here, but also for 1-4, update extenstion here
56+
arrow_path.replace_extension(valid_ext);
57+
return arrow_path.string();
58+
}
59+
else { // case 6
60+
return default_filename + valid_ext;
61+
}
62+
}
63+
64+
}
65+

src/nyx/helpers/diag.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include <fstream>
2+
#include <iostream>
3+
#include "../environment.h"
4+
#include "../globals.h"
5+
#include "../roi_cache.h"
6+
#include "../helpers/fsystem.h"
7+
8+
namespace Nyxus
9+
{
10+
11+
void dump_roi_metrics(const std::string& label_fpath)
12+
{
13+
// are we amidst a 3D scenario ?
14+
bool dim3 = theEnvironment.dim();
15+
16+
// prepare the file name
17+
fs::path pseg(label_fpath);
18+
std::string fpath = theEnvironment.output_dir + "/roi_metrics_" + pseg.stem().string() + ".csv";
19+
20+
// fix the special 3D file name character if needed
21+
if (dim3)
22+
for (auto& ch : fpath)
23+
if (ch == '*')
24+
ch = '~';
25+
26+
std::cout << "Dumping ROI metrics to " << fpath << '\n';
27+
28+
std::ofstream f(fpath);
29+
if (f.fail())
30+
{
31+
std::cerr << "Error: cannot create file " << fpath << '\n';
32+
return;
33+
}
34+
35+
// header
36+
f << "label, area, minx, miny, maxx, maxy, width, height, min_intens, max_intens, size_bytes, size_class \n";
37+
// sort labels
38+
std::vector<int> sortedLabs{ uniqueLabels.begin(), uniqueLabels.end() };
39+
std::sort(sortedLabs.begin(), sortedLabs.end());
40+
// body
41+
for (auto lab : sortedLabs)
42+
{
43+
LR& r = roiData[lab];
44+
auto szb = r.get_ram_footprint_estimate();
45+
std::string ovsz = szb < theEnvironment.get_ram_limit() ? "TRIVIAL" : "OVERSIZE";
46+
f << lab << ", "
47+
<< r.aux_area << ", "
48+
<< r.aabb.get_xmin() << ", "
49+
<< r.aabb.get_ymin() << ", "
50+
<< r.aabb.get_xmax() << ", "
51+
<< r.aabb.get_ymax() << ", "
52+
<< r.aabb.get_width() << ", "
53+
<< r.aabb.get_height() << ", "
54+
<< r.aux_min << ", "
55+
<< r.aux_max << ", "
56+
<< szb << ", "
57+
<< ovsz << ", ";
58+
f << "\n";
59+
}
60+
61+
f.flush();
62+
}
63+
64+
void dump_roi_pixels(const std::vector<int>& batch_labels, const std::string& label_fpath)
65+
{
66+
// no data ?
67+
if (batch_labels.size() == 0)
68+
{
69+
std::cerr << "Error: no ROI pixel data for file " << label_fpath << '\n';
70+
return;
71+
}
72+
73+
// sort labels for reader's comfort
74+
std::vector<int> srt_L{ batch_labels.begin(), batch_labels.end() };
75+
std::sort(srt_L.begin(), srt_L.end());
76+
77+
// are we amidst a 3D scenario ?
78+
bool dim3 = theEnvironment.dim();
79+
80+
// prepare the file name
81+
fs::path pseg(label_fpath);
82+
std::string fpath = theEnvironment.output_dir + "/roi_pixels_" + pseg.stem().string() + "_batch" + std::to_string(srt_L[0]) + '-' + std::to_string(srt_L[srt_L.size() - 1]) + ".csv";
83+
84+
// fix the special 3D file name character if needed
85+
if (dim3)
86+
for (auto& ch : fpath)
87+
if (ch == '*')
88+
ch = '~';
89+
90+
std::cout << "Dumping ROI pixels to " << fpath << '\n';
91+
92+
std::ofstream f(fpath);
93+
if (f.fail())
94+
{
95+
std::cerr << "Error: cannot create file " << fpath << '\n';
96+
return;
97+
}
98+
99+
// header
100+
f << "label,x,y,z,intensity, \n";
101+
102+
// body
103+
for (auto lab : srt_L)
104+
{
105+
LR& r = roiData[lab];
106+
if (dim3)
107+
for (auto& plane : r.zplanes)
108+
for (auto idx : plane.second)
109+
{
110+
auto& pxl = r.raw_pixels_3D[idx];
111+
f << lab << "," << pxl.x << ',' << pxl.y << ',' << pxl.z << ',' << pxl.inten << ',' << '\n';
112+
113+
}
114+
else
115+
for (auto pxl : r.raw_pixels)
116+
f << lab << "," << pxl.x << ',' << pxl.y << ',' << pxl.inten << ',' << '\n';
117+
}
118+
119+
f.flush();
120+
}
121+
122+
}

0 commit comments

Comments
 (0)