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