-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCSBFile70.py
189 lines (159 loc) · 7.74 KB
/
CSBFile70.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
__copyright__ = "Copyright (c) 2022-2025, Intelligent Imaging Innovations, Inc. All rights reserved. All rights reserved."
__license__ = "This source code is licensed under the BSD-style license found in the LICENSE file in the root directory of this source tree."
import re
import os
class CSBFile70(object):
""" generated source for class CSBFile70 """
kSlideSuffix = ".sldy"
kZSlideSuffix = ".sldyz"
kRootDirSuffix = ".dir"
kImageDirSuffix = ".imgdir"
kBinaryFileSuffix = ".npy"
kZBinaryFileSuffix = ".npyz";
kImageRecordFilename = "ImageRecord.yaml"
kChannelRecordFilename = "ChannelRecord.yaml"
kAnnotationRecordFilename = "AnnotationRecord.yaml"
kMaskRecordFilename = "MaskRecord.yaml"
kAuxDataFilename = "AuxData.yaml"
kElapsedTimesFilename = "ElapsedTimes.yaml"
kSAPositionDataFilename = "SAPositionData.yaml"
kStagePositionDataFilename = "StagePositionData.yaml"
kNumDigitsInTimepoint = 7
mSlidePath = str()
mIsCompressed = False
def __init__(self, inSlidePath):
""" generated source for method __init__ """
self.mSlidePath = inSlidePath
self.mDebugPrint = False
if(self.mSlidePath.endswith(self.kZSlideSuffix)):
self.mIsCompressed = True
def GetSlideRootDirectory(self):
""" generated source for method GetSlideRootDirectory """
if(not self.mIsCompressed):
theRootDirectory = re.sub(self.kSlideSuffix + "$", self.kRootDirSuffix,self.mSlidePath)
else:
theRootDirectory = re.sub(self.kZSlideSuffix + "$", self.kRootDirSuffix,self.mSlidePath)
return theRootDirectory
def GetListOfImageGroupTitles(self):
""" generated source for method GetListOfImageGroupTitles """
theRootDirectory = self.GetSlideRootDirectory()
theTitles = []
theMap = {}
for entry in os.scandir(theRootDirectory):
if not entry.is_dir():
continue
if entry.name.endswith(self.kImageDirSuffix) == False:
continue
#check the directory is not empty
theImageRecordPath = entry.path + os.sep + self.kImageRecordFilename
if os.path.isfile(theImageRecordPath) == False:
continue
#scan this directory for .npy files
found = False
for subentry in os.scandir(entry.path):
if subentry.name.endswith(self.kBinaryFileSuffix):
found = True
break
if subentry.name.endswith(self.kZBinaryFileSuffix):
found = True
break
if found == False:
continue
theTitle = re.sub(self.kImageDirSuffix,"",entry.name)
statinfo = os.stat(entry.path)
theModTimNS = statinfo.st_mtime_ns
theInserted = False
while theInserted == False:
theKey = "{:025d}".format(theModTimNS)
if theKey in theMap:
theModTimNS = theModTimNS + 1
else:
theInserted = True
theMap[theKey] = theTitle
for theModTim, theTitle in sorted(theMap.items()):
theTitles.append(theTitle)
return theTitles
def GetImageGroupDirectory(self, inTitle):
""" generated source for method GetImageGroupDirectory """
if inTitle == None:
return None
theRootDirectory = self.GetSlideRootDirectory()
theImageGroupDirectory = theRootDirectory + os.sep + inTitle + self.kImageDirSuffix + os.sep
return theImageGroupDirectory
def GetImageDataFile(self, inTitle, inChannel, inTimepoint):
""" generated source for method GetImageDataFile """
if inTitle == None:
return None
theImageGroupDirectory = self.GetImageGroupDirectory(inTitle)
# buf = "A = %d\n , B = %s\n" % (a, b)
theSuffix = self.kBinaryFileSuffix
if(self.mIsCompressed):
theSuffix = self.kZBinaryFileSuffix
thePath = "%s%s%s_Ch%1d_TP%07d%s" %( theImageGroupDirectory, os.sep, "ImageData", inChannel, inTimepoint, theSuffix)
return thePath
def GetMaskDataFile(self, inTitle, inTimepoint):
""" generated source for method GetMaskDataFile """
if inTitle == None:
return None
theImageGroupDirectory = self.GetImageGroupDirectory(inTitle)
thePath = "%s%s%s_TP%07d%s" % ( theImageGroupDirectory, os.sep, "MaskData", inTimepoint, self.kBinaryFileSuffix)
return thePath
def GetHistogramDataFile(self, inTitle, inChannel, inTimepoint):
""" generated source for method GetHistogramDataFile """
theImageGroupDirectory = self.GetImageGroupDirectory(inTitle)
thePath = str()
if inTimepoint >= 0:
thePath = "%s%s%s_Ch%1d_TP%07d%s" % ( theImageGroupDirectory, os.sep, "HistogramData", inChannel, inTimepoint, self.kBinaryFileSuffix)
else:
thePath = "%s%s%s_Ch%1d%s" % ( theImageGroupDirectory, os.sep, "HistogramSummary", inChannel, self.kBinaryFileSuffix)
return thePath
def GetChannelIndexOfPath(self, inPath):
""" generated source for method GetChannelIndexOfPath """
thePos = inPath.rindex("_Ch")
if thePos == -1:
return -1
theDigit = inPath[thePos + 3: thePos+4]
theChannel = int(theDigit)
return theChannel
def GetTimepointOfPath(self, inPath):
""" generated source for method GetTimepointOfPath """
thePos = inPath.rindex("_TP")
if thePos == -1:
return -1
theDigit = inPath[thePos + 3: thePos + 3 + self.kNumDigitsInTimepoint]
theTimepoint = int(theDigit)
return theTimepoint
def RenamePathToTimepoint0(self,inPath):
ouPath = inPath
thePos = inPath.rfind("_TP")
if thePos == -1:
return False,ouPath
ouPath = "{f1}{f2}{f3}".format(f1=inPath[:thePos],f2='_TP0000000',f3=inPath[thePos + 3 + self.kNumDigitsInTimepoint:])
return True,ouPath
def getListOfNpyDataFiles(self, inTitle, inStartWith):
""" generated source for method getListOfNpyDataFiles """
theImageGroupDirectory = self.GetImageGroupDirectory(inTitle)
if self.mDebugPrint:
print ("getListOfNpyDataFiles: theImageGroupDirectory " + theImageGroupDirectory)
theFilePaths = []
for entry in os.scandir(theImageGroupDirectory):
if not entry.name.endswith(self.kBinaryFileSuffix) and not entry.name.endswith(self.kZBinaryFileSuffix):
continue
if not entry.name.startswith(inStartWith):
continue
theFilePaths.append(entry.path)
if self.mDebugPrint:
print ("getListOfNpyDataFiles: found: " + entry.path)
return theFilePaths
def GetListOfImageDataFiles(self, inTitle):
""" generated source for method GetListOfImageDataFiles """
return self.getListOfNpyDataFiles(inTitle, "ImageData")
def GetListOfMaskDataFiles(self, inTitle):
""" generated source for method GetListOfMaskDataFiles """
return self.getListOfNpyDataFiles(inTitle, "MaskData")
def GetListOfHistogramDataFiles(self, inTitle):
""" generated source for method GetListOfHistogramDataFiles """
return self.getListOfNpyDataFiles(inTitle, "HistogramData")
def GetListOfHistogramSummaryFiles(self, inTitle):
""" generated source for method GetListOfHistogramSummaryFiles """
return self.getListOfNpyDataFiles(inTitle, "HistogramSummary")