BioLib  4.3.0
A GUI-less version of Bio .NET library for editing & annotating various microscopy image formats.
Loading...
Searching...
No Matches
BioLib.Napari Class Reference

Parser for Napari CSV format ROI files. Handles both Points.csv and Shapes.csv formats. More...

Static Public Member Functions

static void Initialize (double physicalX, double physicalY, bool hasTime)
 
static List< ROI > ParsePointsFile (string filePath)
 Parse a Points CSV file and return ROIs.
 
static List< ROI > ParseShapesFile (string filePath)
 Parse a Shapes CSV file and return ROIs.
 
static List< ROI > ParseNapariFiles (string pointsPath, string shapesPath)
 Parse both Points and Shapes files and combine results.
 
static void WritePointsFile (string filePath, List< ROI > rois)
 Write Point ROIs to a Napari Points.csv file.
 
static void WriteShapesFile (string filePath, List< ROI > rois)
 Write Shape ROIs to a Napari Shapes.csv file.
 
static void WriteNapariFiles (string pointsPath, string shapesPath, List< ROI > rois)
 Write both Points and Shapes files from a list of ROIs.
 

Static Public Attributes

static bool HasTime = false
 
static double PhysicalX
 
static double PhysicalY
 

Properties

static bool Initialized [get]
 

Detailed Description

Parser for Napari CSV format ROI files. Handles both Points.csv and Shapes.csv formats.

Member Function Documentation

◆ Initialize()

static void BioLib.Napari.Initialize ( double physicalX,
double physicalY,
bool hasTime )
static
20 {
21 PhysicalX = physicalX;
22 PhysicalY = physicalY;
23 HasTime = hasTime;
24 }
static double PhysicalY
Definition Napari.cs:26
static double PhysicalX
Definition Napari.cs:25
static bool HasTime
Definition Napari.cs:18

◆ ParseNapariFiles()

static List< ROI > BioLib.Napari.ParseNapariFiles ( string pointsPath,
string shapesPath )
static

Parse both Points and Shapes files and combine results.

139 {
140 List<ROI> allRois = new List<ROI>();
141
142 // Stage: Parse points file if it exists
143 if (File.Exists(pointsPath))
144 {
145 List<ROI> pointRois = ParsePointsFile(pointsPath);
146 allRois.AddRange(pointRois);
147 }
148
149 // Stage: Parse shapes file if it exists
150 if (File.Exists(shapesPath))
151 {
152 List<ROI> shapeRois = ParseShapesFile(shapesPath);
153 allRois.AddRange(shapeRois);
154 }
155
156 Recorder.Record($"Napari.ParseNapariFiles(\"{pointsPath}\", \"{shapesPath}\");");
157 return allRois;
158 }
static List< ROI > ParseShapesFile(string filePath)
Parse a Shapes CSV file and return ROIs.
Definition Napari.cs:95
static List< ROI > ParsePointsFile(string filePath)
Parse a Points CSV file and return ROIs.
Definition Napari.cs:61

◆ ParsePointsFile()

static List< ROI > BioLib.Napari.ParsePointsFile ( string filePath)
static

Parse a Points CSV file and return ROIs.

62 {
63 List<ROI> rois = new List<ROI>();
64
65 // Stage: Read and validate file
66 if (!File.Exists(filePath))
67 throw new FileNotFoundException($"Points file not found: {filePath}");
68
69 string[] lines = File.ReadAllLines(filePath);
70 if (lines.Length < 2)
71 return rois; // Empty or header-only file
72
73 // Stage: Parse each point row
74 for (int i = 1; i < lines.Length; i++) // Skip header
75 {
76 string line = lines[i].Trim();
77 if (string.IsNullOrEmpty(line))
78 continue;
79
80 CsvRow row = ParsePointRow(line);
81 if (row != null)
82 {
83 ROI roi = CreatePointRoi(row);
84 rois.Add(roi);
85 }
86 }
87
88 Recorder.Record($"Napari.ParsePointsFile(\"{filePath}\");");
89 return rois;
90 }

◆ ParseShapesFile()

static List< ROI > BioLib.Napari.ParseShapesFile ( string filePath)
static

Parse a Shapes CSV file and return ROIs.

96 {
97 List<ROI> rois = new List<ROI>();
98
99 // Stage: Read and validate file
100 if (!File.Exists(filePath))
101 throw new FileNotFoundException($"Shapes file not found: {filePath}");
102
103 string[] lines = File.ReadAllLines(filePath);
104 if (lines.Length < 2)
105 return rois; // Empty or header-only file
106
107 // Stage: Parse all rows
108 List<CsvRow> allRows = new List<CsvRow>();
109 for (int i = 1; i < lines.Length; i++) // Skip header
110 {
111 string line = lines[i].Trim();
112 if (string.IsNullOrEmpty(line))
113 continue;
114
115 CsvRow row = ParseShapeRow(line);
116 if (row != null)
117 allRows.Add(row);
118 }
119
120 // Stage: Group vertices by shape index
121 Dictionary<double, ShapeGroup> shapeGroups = GroupShapesByIndex(allRows);
122
123 // Stage: Create ROIs from grouped shapes
124 foreach (ShapeGroup group in shapeGroups.Values.OrderBy(g => g.Index))
125 {
126 ROI roi = CreateShapeRoi(group);
127 if (roi != null)
128 rois.Add(roi);
129 }
130
131 Recorder.Record($"Napari.ParseShapesFile(\"{filePath}\");");
132 return rois;
133 }

◆ WriteNapariFiles()

static void BioLib.Napari.WriteNapariFiles ( string pointsPath,
string shapesPath,
List< ROI > rois )
static

Write both Points and Shapes files from a list of ROIs.

238 {
239 // Stage: Write points if any exist
240 List<ROI> pointRois = rois.Where(r => r.type == ROI.Type.Point).ToList();
241 if (pointRois.Count > 0)
242 {
243 WritePointsFile(pointsPath, rois);
244 }
245
246 // Stage: Write shapes if any exist
247 List<ROI> shapeRois = rois.Where(r =>
248 r.type != ROI.Type.Point &&
249 r.type != ROI.Type.Label &&
250 r.type != ROI.Type.Mask).ToList();
251 if (shapeRois.Count > 0)
252 {
253 WriteShapesFile(shapesPath, rois);
254 }
255
256 Recorder.Record(BioLib.Recorder.GetCurrentMethodInfo(), false, pointsPath, shapesPath, rois.Count);
257 }
static void WritePointsFile(string filePath, List< ROI > rois)
Write Point ROIs to a Napari Points.csv file.
Definition Napari.cs:167
static void WriteShapesFile(string filePath, List< ROI > rois)
Write Shape ROIs to a Napari Shapes.csv file.
Definition Napari.cs:200
Definition Recorder.cs:12
static MethodInfo GetCurrentMethodInfo()
Definition Recorder.cs:111
Definition Bio.cs:65

◆ WritePointsFile()

static void BioLib.Napari.WritePointsFile ( string filePath,
List< ROI > rois )
static

Write Point ROIs to a Napari Points.csv file.

168 {
169 // Stage: Filter to only Point type ROIs
170 List<ROI> pointRois = rois.Where(r => r.type == ROI.Type.Point).ToList();
171
172 if (pointRois.Count == 0)
173 return; // No points to write
174
175 // Stage: Determine format (4-axis or 5-axis)
176 bool hasTimeData = HasTime;
177 // Stage: Build CSV content
178 List<string> lines = new List<string>();
179
180 // Header
181 if (hasTimeData)
182 lines.Add("index,axis-0,axis-1,axis-2,axis-3,axis-4");
183 else
184 lines.Add("index,axis-0,axis-1,axis-2,axis-3");
185
186 // Data rows
187 for (int i = 0; i < pointRois.Count; i++)
188 {
189 string line = FormatPointRow(pointRois[i], i, hasTimeData);
190 lines.Add(line);
191 }
192
193 // Stage: Write to file
194 File.WriteAllLines(filePath, lines);
195 }

◆ WriteShapesFile()

static void BioLib.Napari.WriteShapesFile ( string filePath,
List< ROI > rois )
static

Write Shape ROIs to a Napari Shapes.csv file.

201 {
202 // Stage: Filter to only shape type ROIs (exclude Point and Label)
203 List<ROI> shapeRois = rois.Where(r =>
204 r.type != ROI.Type.Point &&
205 r.type != ROI.Type.Label &&
206 r.type != ROI.Type.Mask).ToList();
207
208 if (shapeRois.Count == 0)
209 return; // No shapes to write
210
211 // Stage: Determine format (4-axis or 5-axis)
212 bool hasTimeData = HasTime;
213
214 // Stage: Build CSV content
215 List<string> lines = new List<string>();
216
217 // Header
218 if (hasTimeData)
219 lines.Add("index,shape-type,vertex-index,axis-0,axis-1,axis-2,axis-3,axis-4");
220 else
221 lines.Add("index,shape-type,vertex-index,axis-0,axis-1,axis-2,axis-3");
222
223 // Data rows
224 for (int i = 0; i < shapeRois.Count; i++)
225 {
226 List<string> shapeLines = FormatShapeRows(shapeRois[i], i, hasTimeData);
227 lines.AddRange(shapeLines);
228 }
229
230 // Stage: Write to file
231 File.WriteAllLines(filePath, lines);
232 }

Member Data Documentation

◆ HasTime

bool BioLib.Napari.HasTime = false
static

◆ PhysicalX

double BioLib.Napari.PhysicalX
static

◆ PhysicalY

double BioLib.Napari.PhysicalY
static

Property Documentation

◆ Initialized

bool BioLib.Napari.Initialized
staticget

The documentation for this class was generated from the following file: