-
Notifications
You must be signed in to change notification settings - Fork 3
/
csvscansample.py
348 lines (314 loc) · 13.5 KB
/
csvscansample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import abc, os, pathlib, re
from ...hpfs.flatfield.config import CONST as FF_CONST
from ...utilities.config import CONST as UNIV_CONST
from ...shared.argumentparser import ArgumentParserWithVersionRequirement, InitAndRunFromArgumentParserBase, RunFromArgumentParserBase
from ...shared.csvclasses import Annotation, AnnotationInfo, Batch, Constant, ExposureTime, PhenotypedCell, QPTiffCsv, Region, ROIGlobals
from ...shared.rectangle import GeomLoadRectangle, PhenotypedRectangle, Rectangle
from ...shared.overlap import Overlap
from ...shared.sample import CellPhenotypeSampleBase, GeomSampleBase, ReadRectanglesDbload, TissueSampleBase, WorkflowSample
from ...shared.workflowdependency import ThingWithRoots, ThingWithWorkflowKwargs
from ...utilities.dataclasses import MyDataClass
from ...utilities.tableio import pathfield, TableReader
from ..align.alignsample import AlignSample
from ..align.field import Field, FieldBoundary, FieldOverlap
from ..align.imagestats import ImageStats
from ..align.overlap import AlignmentResult
from ..align.stitch import AffineEntry
from ..annotationinfo.annotationinfo import CopyAnnotationInfoSampleBase
from ..annowarp.annowarpsample import AnnoWarpAlignmentResult, AnnoWarpSampleInformTissueMask, WarpedVertex
from ..annowarp.stitch import AnnoWarpStitchResultEntry
from ..geomcell.geomcellsample import CellGeomLoad, GeomCellSampleDeepCell, GeomCellSampleInform, GeomCellSampleMesmer
class CsvScanRectangle(GeomLoadRectangle, PhenotypedRectangle):
pass
class CsvScanBase(TableReader, ThingWithWorkflowKwargs):
def __init__(self, *args, skipannotations=False, skipcells=False, segmentationalgorithms=None, **kwargs):
self.__skipannotations = skipannotations
self.__skipcells = skipcells
if not segmentationalgorithms: segmentationalgorithms = ["inform"]
if "inform" not in segmentationalgorithms:
raise ValueError("Have to include inform segmentation")
#this is so that the hasanycells() function below works
#can modify it to open up the segmented component tiff if there's
#no inform and then this restriction wouldn't be necessary
self.__segmentationalgorithms = segmentationalgorithms
super().__init__(*args, **kwargs)
@property
def segmentationalgorithms(self): return self.__segmentationalgorithms
@property
def skipcells(self): return self.__skipcells
@property
def skipannotations(self): return self.__skipannotations
@property
@abc.abstractmethod
def logger(self): return super().logger
def processcsv(self, csv, csvclass, tablename, extrakwargs={}, *, SlideID, checkcsv=True, fieldsizelimit=None):
self.logger.debug(f"Processing {csv}")
#read the csv, to check that it's valid
if checkcsv:
rows = self.readtable(csv, csvclass, extrakwargs=extrakwargs, fieldsizelimit=fieldsizelimit, checknewlines=True, checkorder=True)
nrows = len(rows)
else:
with open(csv) as f:
for nrows, line in enumerate(f):
pass
return LoadFile(
fileid="",
SlideID=SlideID,
filename=csv,
tablename=tablename,
nrows=nrows,
nrowsloaded=0,
)
@classmethod
def logmodule(cls): return "csvscan"
@property
def workflowkwargs(self) :
return {
**super().workflowkwargs,
"segmentationalgorithms": self.segmentationalgorithms,
"skipcells": self.skipcells,
"skipannotations": self.skipannotations,
}
class RunCsvScanBase(CsvScanBase, ArgumentParserWithVersionRequirement, InitAndRunFromArgumentParserBase, RunFromArgumentParserBase, ThingWithRoots, ThingWithWorkflowKwargs):
possiblesegmentationalgorithms = "inform", "deepcell", "mesmer"
@classmethod
def makeargumentparser(cls, **kwargs):
p = super().makeargumentparser(**kwargs)
p.add_argument("--skip-check", action="store_false", dest="checkcsvs", help="do not check the validity of the csvs")
p.add_argument("--skip-cells", action="store_true", help="skip cells csvs (segmentation and phenotype)")
p.add_argument("--skip-annotations", action="store_true", help="skip annotation csvs")
p.add_argument("--ignore-csvs", action="append", type=re.compile, help="ignore extraneous csv files that match this regex", default=[])
p.add_argument("--segmentation-algorithm", action="append", choices=cls.possiblesegmentationalgorithms, help="load cell geometry csvs from these segmentation algorithms", metavar="algorithm", dest="segmentation_algorithms")
return p
@classmethod
def initkwargsfromargumentparser(cls, parsed_args_dict):
kwargs = {
**super().initkwargsfromargumentparser(parsed_args_dict),
"segmentationalgorithms": parsed_args_dict.pop("segmentation_algorithms"),
"skipcells": parsed_args_dict.pop("skip_cells"),
"skipannotations": parsed_args_dict.pop("skip_annotations"),
}
return kwargs
@classmethod
def runkwargsfromargumentparser(cls, parsed_args_dict):
kwargs = {
**super().runkwargsfromargumentparser(parsed_args_dict),
"checkcsvs": parsed_args_dict.pop("checkcsvs"),
"ignorecsvs": parsed_args_dict.pop("ignore_csvs"),
}
return kwargs
class CsvScanSample(WorkflowSample, ReadRectanglesDbload, GeomSampleBase, CellPhenotypeSampleBase, TissueSampleBase, RunCsvScanBase):
rectangletype = CsvScanRectangle
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@property
def logger(self): return super().logger
@property
def rectangleextrakwargs(self):
return {
**super().rectangleextrakwargs,
"geomfolder": self.geomfolder,
"phenotypefolder": self.phenotypefolder,
}
def processcsv(self, *args, **kwargs):
return super().processcsv(*args, SlideID=self.SlideID, **kwargs)
def runcsvscan(self, *, checkcsvs=True, ignorecsvs=[]):
toload = []
expectcsvs = {
self.csv(_) for _ in (
"affine",
"align",
"batch",
"constants",
"exposures",
"fieldGeometry",
"fieldoverlaps",
"fields",
"imstat",
"overlap",
"qptiff",
"rect",
)
}
if not self.skipannotations:
expectcsvs |= {
self.csv(_) for _ in (
"annotationinfo",
"annotations",
"annowarp",
"annowarp-stitch",
"regions",
"vertices",
)
}
if not self.skipcells:
expectcsvs |= {
r.geomloadcsv(algo) for r in self.rectangles for algo in self.segmentationalgorithms
}
expectcsvs |= {
r.phenotypecsv for r in self.rectangles if self.hasanycells(r, "inform")
}
meanimagecsvs = {
self.im3folder/f"{self.SlideID}-mean.csv",
self.im3folder/UNIV_CONST.MEANIMAGE_DIRNAME/FF_CONST.FIELDS_USED_CSV_FILENAME,
self.im3folder/UNIV_CONST.MEANIMAGE_DIRNAME/f"{self.SlideID}-{FF_CONST.BACKGROUND_THRESHOLD_CSV_FILE_NAME_STEM}",
self.im3folder/UNIV_CONST.MEANIMAGE_DIRNAME/f"{self.SlideID}-{FF_CONST.METADATA_SUMMARY_STACKED_IMAGES_CSV_FILENAME}",
self.im3folder/UNIV_CONST.MEANIMAGE_DIRNAME/f"{self.SlideID}-{FF_CONST.METADATA_SUMMARY_THRESHOLDING_IMAGES_CSV_FILENAME}",
self.im3folder/UNIV_CONST.MEANIMAGE_DIRNAME/f"{self.SlideID}-{FF_CONST.THRESHOLDING_DATA_TABLE_CSV_FILENAME}",
self.im3folder/UNIV_CONST.MEANIMAGE_DIRNAME/FF_CONST.IMAGE_MASKING_SUBDIR_NAME/FF_CONST.LABELLED_MASK_REGIONS_CSV_FILENAME,
}
annotationinfocsvs = {xml.with_suffix(".annotationinfo.csv") for xml in self.scanfolder.glob("*annotations*polygons*.xml")}
optionalcsvs = {
self.csv(_) for _ in (
"globals",
"tumorGeometry",
)
} | {
r.phenotypecsv for r in self.rectangles if not self.hasanycells(r, "inform")
} | {
r.phenotypeQAQCcsv for r in self.rectangles
} | annotationinfocsvs | meanimagecsvs
goodcsvs = set()
unknowncsvs = set()
folders = {self.mainfolder, self.dbload.parent, self.geomfolder.parent, self.phenotypefolder.parent.parent}
for folder, csv in ((folder, csv) for folder in folders for csv in folder.rglob("*.csv")):
if csv == self.csv("loadfiles"):
continue
try:
expectcsvs.remove(csv)
except KeyError:
try:
optionalcsvs.remove(csv)
except KeyError:
if any(otherfolder/csv.relative_to(folder) in expectcsvs|optionalcsvs|goodcsvs for otherfolder in folders): continue
if any(regex.match(os.fspath(csv.relative_to(folder))) for regex in ignorecsvs): continue
unknowncsvs.add(csv)
continue
goodcsvs.add(csv)
if csv.parent == self.dbload:
match = re.match(f"{self.SlideID}_(.*)[.]csv$", csv.name)
csvclass, tablename = {
"affine": (AffineEntry, "Affine"),
"align": (AlignmentResult, "Align"),
"annotationinfo": (AnnotationInfo, "AnnotationInfo"),
"annotations": (Annotation, "Annotations"),
"annowarp": (AnnoWarpAlignmentResult, "AnnoWarp"),
"annowarp-stitch": (AnnoWarpStitchResultEntry, "AnnoWarpStitch"),
"batch": (Batch, "Batch"),
"constants": (Constant, "Constants"),
"exposures": (ExposureTime, "Exposures"),
"fieldoverlaps": (FieldOverlap, "FieldOverlaps"),
"fieldGeometry": (FieldBoundary, "FieldGeometry"),
"fields": (Field, "Fields"),
"globals": (ROIGlobals, "Globals"),
"imstat": (ImageStats, "Imstat"),
"overlap": (Overlap, "Overlap"),
"qptiff": (QPTiffCsv, "Qptiff"),
"rect": (Rectangle, "Rect"),
"regions": (Region, "Regions"),
"tumorGeometry": (FieldBoundary, "TumorGeometry"),
"vertices": (WarpedVertex, "Vertices"),
}[match.group(1)]
if self.skipannotations:
allannotationinfos = allannotations = None
else:
allannotationinfos = self.readcsv("annotationinfo", AnnotationInfo, extrakwargs={"scanfolder": self.scanfolder})
allannotations = self.readcsv("annotations", Annotation, extrakwargs={"annotationinfos": allannotationinfos})
allrectangles = self.readcsv("rect", Rectangle)
extrakwargs = {
"annotationinfo": {"scanfolder": self.scanfolder},
"annotations": {"annotationinfos": allannotationinfos},
"annowarp": {"tilesize": 0, "bigtilesize": 0, "bigtileoffset": 0},
"fieldoverlaps": {"nclip": 8, "rectangles": allrectangles},
"overlap": {"nclip": 8, "rectangles": allrectangles},
"regions": {"annotations": allannotations},
"vertices": {"annotations": allannotations},
}.get(match.group(1), {})
fieldsizelimit = {
"regions": 500000,
"tumorGeometry": 500000,
}.get(match.group(1), None)
elif csv.parent.parent == self.geomfolder:
csvclass = CellGeomLoad
tablename = "CellGeom"
extrakwargs = {}
fieldsizelimit = None
elif csv.parent == self.phenotypetablesfolder:
csvclass = PhenotypedCell
tablename = "Cell"
extrakwargs = {}
fieldsizelimit = None
elif csv.parent == self.phenotypeQAQCtablesfolder:
continue
elif csv in meanimagecsvs | annotationinfocsvs:
continue
else:
assert False, csv
toload.append({"csv": csv, "csvclass": csvclass, "tablename": tablename, "extrakwargs": extrakwargs, "fieldsizelimit": fieldsizelimit})
toload.sort(key=lambda x: ((x["csvclass"]==CellGeomLoad), (x["csvclass"]==PhenotypedCell), x["csv"]))
if expectcsvs or unknowncsvs:
errors = []
if expectcsvs:
errors.append("Some csvs are missing: "+", ".join(str(_) for _ in sorted(expectcsvs)))
if unknowncsvs:
errors.append("Unknown csvs: "+", ".join(str(_) for _ in sorted(unknowncsvs)))
raise ValueError("\n".join(errors))
loadfiles = [self.processcsv(checkcsv=checkcsvs, **kwargs) for kwargs in toload]
self.writecsv("loadfiles", loadfiles, header=False)
@classmethod
def getoutputfiles(cls, SlideID, *, dbloadroot, **otherworkflowkwargs):
dbload = dbloadroot/SlideID/UNIV_CONST.DBLOAD_DIR_NAME
return [dbload/f"{SlideID}_loadfiles.csv"]
def inputfiles(self, **kwargs):
yield from super().inputfiles(**kwargs)
if not self.skipcells:
for r in self.rectangles:
for algo in self.segmentationalgorithms:
yield r.geomloadcsv(algo)
if self.hasanycells(r, "inform"):
yield r.phenotypecsv
@classmethod
def workflowdependencyclasses(cls, **kwargs):
segmentationalgorithms = kwargs["segmentationalgorithms"]
skipcells = kwargs["skipcells"]
skipannotations = kwargs["skipannotations"]
result = super().workflowdependencyclasses(**kwargs)
result += [
AlignSample,
]
if not skipannotations:
result += [
AnnoWarpSampleInformTissueMask,
CopyAnnotationInfoSampleBase,
]
if not skipcells:
result += [
{
"inform": GeomCellSampleInform,
"deepcell": GeomCellSampleDeepCell,
"mesmer": GeomCellSampleMesmer,
}[algo] for algo in segmentationalgorithms
]
return result
def run(self, *args, **kwargs): return self.runcsvscan(*args, **kwargs)
@staticmethod
def hasanycells(rectangle, algorithm):
try:
with open(rectangle.geomloadcsv(algorithm)) as f:
next(f)
next(f)
except (FileNotFoundError, StopIteration):
return False
else:
return True
class LoadFile(MyDataClass):
fileid: str
SlideID: str
filename: pathlib.Path = pathfield()
tablename: str
nrows: int
nrowsloaded: int
def main(args=None):
CsvScanSample.runfromargumentparser(args)
if __name__ == "__main__":
main()